Tree.php 13 KB

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