Tree.php 12 KB

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