Tree.php 13 KB

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