HasTree.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace Dcat\Admin\Grid\Concerns;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Grid\Events\Fetched;
  5. use Dcat\Admin\Grid\Events\Fetching;
  6. use Dcat\Admin\Support\Helper;
  7. use Illuminate\Support\Collection;
  8. trait HasTree
  9. {
  10. /**
  11. * @var string
  12. */
  13. protected $parentIdQueryName = '_parent_id_';
  14. /**
  15. * @var string
  16. */
  17. protected $depthQueryName = '_depth_';
  18. /**
  19. * @var bool
  20. */
  21. protected $showAllChildrenNodes = false;
  22. /**
  23. * @var bool
  24. */
  25. protected $allowedTreeQuery = true;
  26. /**
  27. * @var array
  28. */
  29. protected $treeIgnoreQueryNames = [];
  30. /**
  31. * 开启树形表格功能.
  32. *
  33. * @param bool $showAll
  34. * @param bool $sortable
  35. *
  36. * @return void
  37. */
  38. public function enableTree(bool $showAll, bool $sortable)
  39. {
  40. $this->showAllChildrenNodes = $showAll;
  41. $this->grid()->listen(Fetching::class, function () use ($sortable) {
  42. $this->sortTree($sortable);
  43. $this->bindChildrenNodesQuery();
  44. if ($this->getParentIdFromRequest()) {
  45. $this->setPageName(
  46. $this->getChildrenPageName($this->getParentIdFromRequest())
  47. );
  48. }
  49. $this->addIgnoreQueries();
  50. });
  51. $this->grid()->listen(Fetched::class, function ($grid, Collection $collection) {
  52. if (! $this->getParentIdFromRequest()) {
  53. return;
  54. }
  55. if ($collection->isEmpty()) {
  56. return $grid->show(false);
  57. }
  58. $this->buildChildrenNodesPagination();
  59. });
  60. }
  61. /**
  62. * 设置保存为"前一个页面地址"时需要忽略的参数.
  63. */
  64. protected function addIgnoreQueries()
  65. {
  66. Admin::addIgnoreQueryName([
  67. $this->getParentIdQueryName(),
  68. $this->getDepthQueryName(),
  69. $this->getChildrenPageName($this->getParentIdFromRequest()),
  70. ]);
  71. }
  72. /**
  73. * 禁止树形表格查询.
  74. *
  75. * @return $this
  76. */
  77. public function disableBindTreeQuery()
  78. {
  79. $this->allowedTreeQuery = false;
  80. return $this->filterQueryBy(function ($query) {
  81. if (
  82. $query['method'] === 'where'
  83. && $query['arguments']
  84. && $query['arguments'][0] === optional($this->repository)->getParentColumn()
  85. ) {
  86. return false;
  87. }
  88. return true;
  89. });
  90. }
  91. /**
  92. * 设置子节点查询链接需要忽略的字段.
  93. *
  94. * @param string|array $keys
  95. *
  96. * @return $this
  97. */
  98. public function treeUrlWithoutQuery($keys)
  99. {
  100. $this->treeIgnoreQueryNames = array_merge(
  101. $this->treeIgnoreQueryNames,
  102. (array) $keys
  103. );
  104. return $this;
  105. }
  106. public function generateTreeUrl()
  107. {
  108. return Helper::urlWithoutQuery(
  109. $this->grid()->filter()->urlWithoutFilters(),
  110. $this->treeIgnoreQueryNames
  111. );
  112. }
  113. protected function buildChildrenNodesPagination()
  114. {
  115. if ($this->grid()->allowPagination()) {
  116. $nextPage = $this->getCurrentChildrenPage() + 1;
  117. Admin::html(
  118. <<<HTML
  119. <next-page class="hidden">{$nextPage}</next-page>
  120. <last-page class="hidden">{$this->paginator()->lastPage()}</last-page>
  121. HTML
  122. );
  123. }
  124. }
  125. protected function sortTree(bool $sortable)
  126. {
  127. if (
  128. $sortable
  129. && ! $this->findQueryByMethod('orderBy')
  130. && ! $this->findQueryByMethod('orderByDesc')
  131. && ($orderColumn = $this->repository->getOrderColumn())
  132. ) {
  133. $this->orderBy($orderColumn)
  134. ->orderBy($this->repository->getKeyName());
  135. }
  136. }
  137. protected function bindChildrenNodesQuery()
  138. {
  139. if (! $this->allowedTreeQuery) {
  140. return;
  141. }
  142. $this->where($this->repository->getParentColumn(), $this->getParentIdFromRequest());
  143. }
  144. /**
  145. * @return mixed
  146. */
  147. public function getChildrenQueryNamePrefix()
  148. {
  149. return $this->grid->getName();
  150. }
  151. /**
  152. * @param mixed $parentId
  153. *
  154. * @return string
  155. */
  156. public function getChildrenPageName($parentId)
  157. {
  158. return $this->getChildrenQueryNamePrefix().'_children_page_'.$parentId;
  159. }
  160. /**
  161. * @return int
  162. */
  163. public function getCurrentChildrenPage()
  164. {
  165. return $this->request->get(
  166. $this->getChildrenPageName(
  167. $this->getParentIdFromRequest()
  168. )
  169. ) ?: 1;
  170. }
  171. /**
  172. * @return string
  173. */
  174. public function getParentIdQueryName()
  175. {
  176. return $this->getChildrenQueryNamePrefix().$this->parentIdQueryName;
  177. }
  178. /**
  179. * @return int
  180. */
  181. public function getParentIdFromRequest()
  182. {
  183. return $this->request->get(
  184. $this->getParentIdQueryName()
  185. ) ?: 0;
  186. }
  187. /**
  188. * @return string
  189. */
  190. public function getDepthQueryName()
  191. {
  192. return $this->getChildrenQueryNamePrefix().$this->depthQueryName;
  193. }
  194. /**
  195. * @return int
  196. */
  197. public function getDepthFromRequest()
  198. {
  199. return $this->request->get(
  200. $this->getDepthQueryName()
  201. ) ?: 0;
  202. }
  203. /**
  204. * @return bool
  205. */
  206. public function showAllChildrenNodes()
  207. {
  208. return $this->showAllChildrenNodes;
  209. }
  210. }