Tree.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Form\Field;
  4. use Dcat\Admin\Support\Helper;
  5. use Dcat\Admin\Widgets\Checkbox as WidgetCheckbox;
  6. use Illuminate\Contracts\Support\Arrayable;
  7. class Tree extends Field
  8. {
  9. protected $options = [
  10. 'plugins' => ['checkbox', 'types'],
  11. 'core' => [
  12. 'check_callback' => true,
  13. 'themes' => [
  14. 'name' => 'proton',
  15. 'responsive' => true,
  16. ],
  17. ],
  18. 'checkbox' => [
  19. 'keep_selected_style' => false,
  20. ],
  21. 'types' => [
  22. 'default' => [
  23. 'icon' => false,
  24. ],
  25. ],
  26. ];
  27. protected $nodes = [];
  28. protected $parents = [];
  29. protected $expand = true;
  30. protected $columnNames = [
  31. 'id' => 'id',
  32. 'text' => 'name',
  33. 'parent' => 'parent_id',
  34. ];
  35. protected $exceptParents = true;
  36. protected $readOnly = false;
  37. protected $rootParentId = 0;
  38. /**
  39. * @param array|Arrayable|\Closure $data exp:
  40. * {
  41. * "id": "1",
  42. * "parent": "#",
  43. * "text": "Dashboard",
  44. * // "state": {"selected": true}
  45. * }
  46. *
  47. * @return $this
  48. */
  49. public function nodes($data)
  50. {
  51. if ($data instanceof Arrayable) {
  52. $data = $data->toArray();
  53. }
  54. $this->nodes = &$data;
  55. return $this;
  56. }
  57. /**
  58. * 过滤父节点.
  59. *
  60. * @param bool $value
  61. *
  62. * @return $this
  63. */
  64. public function exceptParentNode(bool $value = true)
  65. {
  66. $this->exceptParents = $value;
  67. return $this;
  68. }
  69. public function rootParentId($id)
  70. {
  71. $this->rootParentId = $id;
  72. return $this;
  73. }
  74. /**
  75. * {@inheritDoc}
  76. */
  77. public function readOnly(bool $value = true)
  78. {
  79. $this->readOnly = true;
  80. return $this;
  81. }
  82. public function setIdColumn(string $name)
  83. {
  84. $this->columnNames['id'] = $name;
  85. return $this;
  86. }
  87. public function setTitleColumn(string $name)
  88. {
  89. $this->columnNames['text'] = $name;
  90. return $this;
  91. }
  92. public function setParentColumn(string $name)
  93. {
  94. $this->columnNames['parent'] = $name;
  95. return $this;
  96. }
  97. protected function formatNodes()
  98. {
  99. $value = Helper::array($this->value());
  100. $this->value = &$value;
  101. if ($this->nodes instanceof \Closure) {
  102. $this->nodes = Helper::array($this->nodes->call($this->values(), $value, $this));
  103. }
  104. if (! $this->nodes) {
  105. return;
  106. }
  107. $idColumn = $this->columnNames['id'];
  108. $textColumn = $this->columnNames['text'];
  109. $parentColumn = $this->columnNames['parent'];
  110. $parentIds = $nodes = [];
  111. foreach ($this->nodes as &$v) {
  112. if (empty($v[$idColumn])) {
  113. continue;
  114. }
  115. $parentId = $v[$parentColumn] ?? '#';
  116. if (empty($parentId) || $parentId == $this->rootParentId) {
  117. $parentId = '#';
  118. } else {
  119. $parentIds[] = $parentId;
  120. }
  121. $v['state'] = [];
  122. if ($value && in_array($v[$idColumn], $value)) {
  123. $v['state']['selected'] = true;
  124. }
  125. if ($this->readOnly) {
  126. $v['state']['disabled'] = true;
  127. }
  128. $nodes[] = [
  129. 'id' => $v[$idColumn],
  130. 'text' => $v[$textColumn] ?? null,
  131. 'parent' => $parentId,
  132. 'state' => $v['state'],
  133. ];
  134. }
  135. if ($this->exceptParents) {
  136. // 筛选出所有父节点,最终点击树节点时过滤掉父节点
  137. $this->parents = array_unique($parentIds);
  138. }
  139. $this->nodes = &$nodes;
  140. }
  141. /**
  142. * Set type.
  143. *
  144. * @param array $value
  145. *
  146. * @return $this
  147. */
  148. public function type(array $value)
  149. {
  150. $this->options['types'] = array_merge($this->options['types'], $value);
  151. return $this;
  152. }
  153. /**
  154. * Set plugins.
  155. *
  156. * @param array $value
  157. *
  158. * @return $this
  159. */
  160. public function plugins(array $value)
  161. {
  162. $this->options['plugins'] = $value;
  163. return $this;
  164. }
  165. /**
  166. * @param bool $value
  167. *
  168. * @return $this
  169. */
  170. public function expand(bool $value = true)
  171. {
  172. $this->expand = $value;
  173. return $this;
  174. }
  175. protected function formatFieldData($data)
  176. {
  177. return Helper::array($this->getValueFromData($data), true);
  178. }
  179. protected function prepareInputValue($value)
  180. {
  181. return Helper::array($value, true);
  182. }
  183. public function render()
  184. {
  185. $checkboxes = new WidgetCheckbox();
  186. $checkboxes->style('primary');
  187. $checkboxes->inline();
  188. $checkboxes->options([
  189. 1 => trans('admin.selectall'),
  190. 2 => trans('admin.expand'),
  191. ]);
  192. $this->readOnly && $checkboxes->disable(1);
  193. $this->expand && $checkboxes->check(2);
  194. $this->formatNodes();
  195. if ($v = $this->value()) {
  196. $this->attribute('value', implode(',', $v));
  197. }
  198. $this->addVariables([
  199. 'checkboxes' => $checkboxes,
  200. 'nodes' => $this->nodes,
  201. 'expand' => $this->expand,
  202. 'disabled' => empty($this->attributes['disabled']) ? '' : 'disabled',
  203. 'parents' => $this->parents,
  204. ]);
  205. return parent::render();
  206. }
  207. }