Tree.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Dcat\Admin\Admin;
  4. use Illuminate\Contracts\Support\Arrayable;
  5. use Illuminate\Support\Str;
  6. class Tree extends Widget
  7. {
  8. protected $view = 'admin::widgets.tree';
  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. public function __construct($nodes = [])
  28. {
  29. $this->nodes($nodes);
  30. }
  31. /**
  32. * @var array
  33. */
  34. protected $columnNames = [
  35. 'id' => 'id',
  36. 'text' => 'name',
  37. 'parent' => 'parent_id',
  38. ];
  39. protected $nodes = [];
  40. protected $value = [];
  41. protected $checkedAll;
  42. public function checkedAll()
  43. {
  44. $this->checkedAll = true;
  45. return $this;
  46. }
  47. public function checked($value = [])
  48. {
  49. if ($value instanceof Arrayable) {
  50. $value = $value->toArray();
  51. }
  52. $this->value = (array)$value;
  53. return $this;
  54. }
  55. /**
  56. * @param string $idColumn
  57. * @param string $textColumn
  58. * @param string $parentColumn
  59. * @return $this
  60. */
  61. public function columnNames(string $idColumn = 'id', string $textColumn = 'name', string $parentColumn = 'parent_id')
  62. {
  63. $this->columnNames['id'] = $idColumn;
  64. $this->columnNames['text'] = $textColumn;
  65. $this->columnNames['parent'] = $parentColumn;
  66. return $this;
  67. }
  68. /**
  69. * @param array $data exp:
  70. * {
  71. * "id": "1",
  72. * "parent": "#",
  73. * "text": "Dashboard",
  74. * // "state": {"selected": true}
  75. * }
  76. *
  77. * @param array $data
  78. * @return $this
  79. */
  80. public function nodes($data)
  81. {
  82. if ($data instanceof Arrayable) {
  83. $data = $data->toArray();
  84. }
  85. $this->nodes = &$data;
  86. return $this;
  87. }
  88. public function render()
  89. {
  90. $id = 'widget-tree-' . Str::random(8);
  91. $this->id($id);
  92. $this->class('jstree-wrapper');
  93. $this->defaultHtmlAttribute('style', 'border:0;padding:5px 0');
  94. $this->formatNodes();
  95. $this->variables = [
  96. 'id' => $id,
  97. 'nodes' => &$this->nodes,
  98. ];
  99. return parent::render(); // TODO: Change the autogenerated stub
  100. }
  101. protected function formatNodes()
  102. {
  103. $value = $this->value;
  104. if ($value && !is_array($value)) {
  105. $value = explode(',', $value);
  106. }
  107. $value = (array)$value;
  108. if (!$this->nodes) {
  109. return;
  110. }
  111. $idColumn = $this->columnNames['id'];
  112. $textColumn = $this->columnNames['text'];
  113. $parentColumn = $this->columnNames['parent'];
  114. $nodes = [];
  115. foreach ($this->nodes as &$v) {
  116. if (empty($v[$idColumn])) {
  117. continue;
  118. }
  119. $parentId = $v[$parentColumn] ?? '#';
  120. if (empty($parentId)) {
  121. $parentId = '#';
  122. }
  123. $v['state'] = [];
  124. if ($this->checkedAll || ($value && in_array($v[$idColumn], $value))) {
  125. $v['state']['selected'] = true;
  126. }
  127. $v['state']['disabled'] = true;
  128. $nodes[] = [
  129. 'id' => $v[$idColumn],
  130. 'text' => $v[$textColumn] ?? null,
  131. 'parent' => $parentId,
  132. 'state' => $v['state'],
  133. ];
  134. }
  135. $this->nodes = &$nodes;
  136. }
  137. protected function collectAssets()
  138. {
  139. Admin::css('vendor/dcat-admin/jstree-theme/themes/proton/style.min.css');
  140. Admin::collectComponentAssets('jstree');
  141. }
  142. }