Tree.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. *
  60. * @return $this
  61. */
  62. public function name(string $idColumn = 'id', string $textColumn = 'name', string $parentColumn = 'parent_id')
  63. {
  64. $this->columnNames['id'] = $idColumn;
  65. $this->columnNames['text'] = $textColumn;
  66. $this->columnNames['parent'] = $parentColumn;
  67. return $this;
  68. }
  69. /**
  70. * @param array $data exp:
  71. * {
  72. * "id": "1",
  73. * "parent": "#",
  74. * "text": "Dashboard",
  75. * // "state": {"selected": true}
  76. * }
  77. * @param array $data
  78. *
  79. * @return $this
  80. */
  81. public function nodes($data)
  82. {
  83. if ($data instanceof Arrayable) {
  84. $data = $data->toArray();
  85. }
  86. $this->nodes = &$data;
  87. return $this;
  88. }
  89. public function render()
  90. {
  91. $id = 'widget-tree-'.Str::random(8);
  92. $this->id($id);
  93. $this->class('jstree-wrapper');
  94. $this->defaultHtmlAttribute('style', 'border:0;padding:5px 0');
  95. $this->formatNodes();
  96. $this->variables = [
  97. 'id' => $id,
  98. 'nodes' => &$this->nodes,
  99. ];
  100. return parent::render(); // TODO: Change the autogenerated stub
  101. }
  102. protected function formatNodes()
  103. {
  104. $value = $this->value;
  105. if ($value && ! is_array($value)) {
  106. $value = explode(',', $value);
  107. }
  108. $value = (array) $value;
  109. if (! $this->nodes) {
  110. return;
  111. }
  112. $idColumn = $this->columnNames['id'];
  113. $textColumn = $this->columnNames['text'];
  114. $parentColumn = $this->columnNames['parent'];
  115. $nodes = [];
  116. foreach ($this->nodes as &$v) {
  117. if (empty($v[$idColumn])) {
  118. continue;
  119. }
  120. $parentId = $v[$parentColumn] ?? '#';
  121. if (empty($parentId)) {
  122. $parentId = '#';
  123. }
  124. $v['state'] = [];
  125. if ($this->checkedAll || ($value && in_array($v[$idColumn], $value))) {
  126. $v['state']['selected'] = true;
  127. }
  128. $v['state']['disabled'] = true;
  129. $nodes[] = [
  130. 'id' => $v[$idColumn],
  131. 'text' => $v[$textColumn] ?? null,
  132. 'parent' => $parentId,
  133. 'state' => $v['state'],
  134. ];
  135. }
  136. $this->nodes = &$nodes;
  137. }
  138. protected function collectAssets()
  139. {
  140. Admin::css('vendor/dcat-admin/jstree-theme/themes/proton/style.min.css');
  141. Admin::collectComponentAssets('jstree');
  142. }
  143. }