Tree.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. <?php
  2. namespace Dcat\Admin;
  3. use Closure;
  4. use Dcat\Admin\Traits\BuilderEvents;
  5. use Dcat\Admin\Tree\Tools;
  6. use Illuminate\Contracts\Support\Renderable;
  7. use Illuminate\Database\Eloquent\Model;
  8. class Tree implements Renderable
  9. {
  10. use BuilderEvents;
  11. /**
  12. * @var array
  13. */
  14. protected $items = [];
  15. /**
  16. * @var string
  17. */
  18. protected $elementId = 'tree-';
  19. /**
  20. * @var Model
  21. */
  22. protected $model;
  23. /**
  24. * @var \Closure
  25. */
  26. protected $queryCallback;
  27. /**
  28. * View of tree to render.
  29. *
  30. * @var string
  31. */
  32. protected $view = [
  33. 'tree' => 'admin::tree',
  34. 'branch' => 'admin::tree.branch',
  35. ];
  36. /**
  37. * @var \Closure
  38. */
  39. protected $callback;
  40. /**
  41. * @var null
  42. */
  43. protected $branchCallback = null;
  44. /**
  45. * @var string
  46. */
  47. public $path;
  48. /**
  49. * @var bool
  50. */
  51. public $useCreate = true;
  52. /**
  53. * @var bool
  54. */
  55. public $useQuickCreate = true;
  56. /**
  57. * @var array
  58. */
  59. public $dialogFormDimensions = ['700px', '620px'];
  60. /**
  61. * @var bool
  62. */
  63. public $useSave = true;
  64. /**
  65. * @var bool
  66. */
  67. public $useRefresh = true;
  68. /**
  69. * @var bool
  70. */
  71. public $useEdit = true;
  72. /**
  73. * @var bool
  74. */
  75. public $useQuickEdit = true;
  76. /**
  77. * @var bool
  78. */
  79. public $useDelete = true;
  80. /**
  81. * @var array
  82. */
  83. protected $nestableOptions = [];
  84. /**
  85. * Header tools.
  86. *
  87. * @var Tools
  88. */
  89. public $tools;
  90. /**
  91. * @var Closure
  92. */
  93. protected $wrapper;
  94. /**
  95. * Menu constructor.
  96. *
  97. * @param Model|null $model
  98. */
  99. public function __construct(Model $model = null, \Closure $callback = null)
  100. {
  101. $this->model = $model;
  102. $this->path = $this->path ?: request()->getPathInfo();
  103. $this->elementId .= uniqid();
  104. $this->setupTools();
  105. $this->collectAssets();
  106. if ($callback instanceof \Closure) {
  107. call_user_func($callback, $this);
  108. }
  109. $this->initBranchCallback();
  110. $this->callResolving();
  111. }
  112. /**
  113. * Setup tree tools.
  114. */
  115. public function setupTools()
  116. {
  117. $this->tools = new Tools($this);
  118. }
  119. protected function collectAssets()
  120. {
  121. Admin::collectComponentAssets('jquery.nestable');
  122. }
  123. /**
  124. * Initialize branch callback.
  125. *
  126. * @return void
  127. */
  128. protected function initBranchCallback()
  129. {
  130. if (is_null($this->branchCallback)) {
  131. $this->branchCallback = function ($branch) {
  132. $key = $branch[$this->model->getKeyName()];
  133. $title = $branch[$this->model->getTitleColumn()];
  134. return "$key - $title";
  135. };
  136. }
  137. }
  138. /**
  139. * Set branch callback.
  140. *
  141. * @param \Closure $branchCallback
  142. *
  143. * @return $this
  144. */
  145. public function branch(\Closure $branchCallback)
  146. {
  147. $this->branchCallback = $branchCallback;
  148. return $this;
  149. }
  150. /**
  151. * Set query callback this tree.
  152. *
  153. * @return Model
  154. */
  155. public function query(\Closure $callback)
  156. {
  157. $this->queryCallback = $callback;
  158. return $this;
  159. }
  160. /**
  161. * Set nestable options.
  162. *
  163. * @param array $options
  164. *
  165. * @return $this
  166. */
  167. public function nestable($options = [])
  168. {
  169. $this->nestableOptions = array_merge($this->nestableOptions, $options);
  170. return $this;
  171. }
  172. /**
  173. * Disable create.
  174. *
  175. * @return void
  176. */
  177. public function disableCreateButton()
  178. {
  179. $this->useCreate = false;
  180. }
  181. public function disableQuickCreateButton()
  182. {
  183. $this->useQuickCreate = false;
  184. }
  185. /**
  186. * @param string $width
  187. * @param string $height
  188. * @return $this
  189. */
  190. public function setdialogFormDimensions(string $width, string $height)
  191. {
  192. $this->dialogFormDimensions = [$width, $height];
  193. return $this;
  194. }
  195. /**
  196. * Disable save.
  197. *
  198. * @return void
  199. */
  200. public function disableSaveButton()
  201. {
  202. $this->useSave = false;
  203. }
  204. /**
  205. * Disable refresh.
  206. *
  207. * @return void
  208. */
  209. public function disableRefreshButton()
  210. {
  211. $this->useRefresh = false;
  212. }
  213. public function disableQuickEditButton()
  214. {
  215. $this->useQuickEdit = false;
  216. }
  217. public function disableEditButton()
  218. {
  219. $this->useEdit = false;
  220. }
  221. public function disableDeleteButton()
  222. {
  223. $this->useDelete = false;
  224. }
  225. /**
  226. * @param Closure $closure
  227. * @return $this;
  228. */
  229. public function wrap(\Closure $closure)
  230. {
  231. $this->wrapper = $closure;
  232. return $this;
  233. }
  234. /**
  235. * @return bool
  236. */
  237. public function hasWrapper()
  238. {
  239. return $this->wrapper ? true : false;
  240. }
  241. /**
  242. * Save tree order from a input.
  243. *
  244. * @param string $serialize
  245. *
  246. * @return bool
  247. */
  248. public function saveOrder($serialize)
  249. {
  250. $tree = json_decode($serialize, true);
  251. if (json_last_error() != JSON_ERROR_NONE) {
  252. throw new \InvalidArgumentException(json_last_error_msg());
  253. }
  254. $this->model->saveOrder($tree);
  255. return true;
  256. }
  257. /**
  258. * Build tree grid scripts.
  259. *
  260. * @return string
  261. */
  262. protected function script()
  263. {
  264. $deleteConfirm = trans('admin.delete_confirm');
  265. $saveSucceeded = trans('admin.save_succeeded');
  266. $deleteSucceeded = trans('admin.delete_succeeded');
  267. $confirm = trans('admin.confirm');
  268. $cancel = trans('admin.cancel');
  269. $nestableOptions = json_encode($this->nestableOptions);
  270. return <<<JS
  271. $('#{$this->elementId}').nestable($nestableOptions);
  272. $('.tree_branch_delete').click(function() {
  273. var id = $(this).data('id');
  274. LA.confirm("$deleteConfirm", function () {
  275. LA.NP.start();
  276. $.ajax({
  277. method: 'post',
  278. url: '{$this->path}/' + id,
  279. data: {
  280. _method:'delete',
  281. _token:LA.token,
  282. },
  283. success: function (data) {
  284. LA.NP.done();
  285. if (typeof data === 'object') {
  286. if (data.status) {
  287. LA.reload();
  288. LA.success("$deleteSucceeded");
  289. } else {
  290. LA.error(data.message || 'Delete failed.');
  291. }
  292. }
  293. }
  294. });
  295. }, "$confirm", "$cancel");
  296. });
  297. $('.{$this->elementId}-save').click(function () {
  298. var serialize = $('#{$this->elementId}').nestable('serialize');
  299. LA.NP.start();
  300. $.post('{$this->path}', {
  301. _token: LA.token,
  302. _order: JSON.stringify(serialize)
  303. },
  304. function(data){
  305. LA.NP.done();
  306. LA.reload();
  307. LA.success('{$saveSucceeded}');
  308. });
  309. });
  310. $('.{$this->elementId}-tree-tools').on('click', function(e){
  311. var action = $(this).data('action');
  312. if (action === 'expand') {
  313. $('.dd').nestable('expandAll');
  314. }
  315. if (action === 'collapse') {
  316. $('.dd').nestable('collapseAll');
  317. }
  318. });
  319. JS;
  320. }
  321. /**
  322. * Set view of tree.
  323. *
  324. * @param string $view
  325. */
  326. public function setView($view)
  327. {
  328. $this->view = $view;
  329. }
  330. /**
  331. * Return all items of the tree.
  332. *
  333. * @param array $items
  334. */
  335. public function getItems()
  336. {
  337. return $this->model->withQuery($this->queryCallback)->toTree();
  338. }
  339. /**
  340. * Variables in tree template.
  341. *
  342. * @return array
  343. */
  344. public function variables()
  345. {
  346. return [
  347. 'id' => $this->elementId,
  348. 'tools' => $this->tools->render(),
  349. 'items' => $this->getItems(),
  350. 'useCreate' => $this->useCreate,
  351. 'useQuickCreate' => $this->useQuickCreate,
  352. 'useSave' => $this->useSave,
  353. 'useRefresh' => $this->useRefresh,
  354. 'useEdit' => $this->useEdit,
  355. 'useQuickEdit' => $this->useQuickEdit,
  356. 'useDelete' => $this->useDelete,
  357. 'createButton' => $this->renderCreateButton(),
  358. ];
  359. }
  360. /**
  361. * Setup grid tools.
  362. *
  363. * @param Closure $callback
  364. *
  365. * @return void
  366. */
  367. public function tools(Closure $callback)
  368. {
  369. call_user_func($callback, $this->tools);
  370. }
  371. /**
  372. * @return string
  373. */
  374. protected function renderCreateButton()
  375. {
  376. if (!$this->useQuickCreate && !$this->useCreate) {
  377. return '';
  378. }
  379. $url = $this->path . '/create';
  380. $new = trans('admin.new');
  381. $quickBtn = $btn = '';
  382. if ($this->useCreate) {
  383. $btn = "<a href='{$url}' class='btn btn-sm btn-success'><i class='ti-plus'></i><span class='hidden-xs'>&nbsp;&nbsp;{$new}</span></a>";
  384. }
  385. if ($this->useQuickCreate) {
  386. $text = $this->useCreate ? '' : "<span class='hidden-xs'> &nbsp; $new</span>";
  387. $quickBtn = "<a data-url='$url' class='btn btn-sm btn-success tree-quick-create'><i class=' fa fa-clone'></i>$text</a>";
  388. }
  389. return "<div class='btn-group pull-right' style='margin-right:3px'>{$btn}{$quickBtn}</div>";
  390. }
  391. /**
  392. * Render a tree.
  393. *
  394. * @return \Illuminate\Http\JsonResponse|string
  395. */
  396. public function render()
  397. {
  398. try {
  399. $this->callResolving();
  400. if ($this->useQuickEdit) {
  401. list($width, $height) = $this->dialogFormDimensions;
  402. Form::popup(trans('admin.edit'))
  403. ->click('.tree-quick-edit')
  404. ->success('LA.reload()')
  405. ->dimensions($width, $height)
  406. ->render();
  407. }
  408. if ($this->useQuickCreate) {
  409. list($width, $height) = $this->dialogFormDimensions;
  410. Form::popup(trans('admin.new'))
  411. ->click('.tree-quick-create')
  412. ->success('LA.reload()')
  413. ->dimensions($width, $height)
  414. ->render();
  415. }
  416. Admin::script($this->script());
  417. view()->share([
  418. 'path' => url($this->path),
  419. 'keyName' => $this->model->getKeyName(),
  420. 'branchView' => $this->view['branch'],
  421. 'branchCallback' => $this->branchCallback,
  422. ]);
  423. return $this->doWrap();
  424. } catch (\Throwable $e) {
  425. return Admin::makeExceptionHandler()->renderException($e);
  426. }
  427. }
  428. /**
  429. * @return string
  430. */
  431. protected function doWrap()
  432. {
  433. $view = view($this->view['tree'], $this->variables());
  434. if (!$wrapper = $this->wrapper) {
  435. return "<div class='box box-default'>{$view->render()}</div>";
  436. }
  437. return $wrapper($view);
  438. }
  439. /**
  440. * Get the string contents of the grid view.
  441. *
  442. * @return string
  443. */
  444. public function __toString()
  445. {
  446. return $this->render();
  447. }
  448. /**
  449. * Create a tree instance.
  450. *
  451. * @param Model|null $model
  452. * @param Closure|null $callback
  453. * @return Tree
  454. */
  455. public static function make(Model $model = null, \Closure $callback = null)
  456. {
  457. return new static($model, $callback);
  458. }
  459. }