Tree.php 3.7 KB

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