123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- namespace Dcat\Admin\Widgets;
- use Dcat\Admin\Admin;
- use Illuminate\Contracts\Support\Arrayable;
- use Illuminate\Support\Str;
- class Tree extends Widget
- {
- protected $view = 'admin::widgets.tree';
- protected $options = [
- 'plugins' => ['checkbox', 'types'],
- 'core' => [
- 'check_callback' => true,
- 'themes' => [
- 'name' => 'proton',
- 'responsive' => true,
- ],
- ],
- 'checkbox' => [
- 'keep_selected_style' => false,
- ],
- 'types' => [
- 'default' => [
- 'icon' => false,
- ],
- ],
- ];
- public function __construct($nodes = [])
- {
- $this->nodes($nodes);
- }
- /**
- * @var array
- */
- protected $columnNames = [
- 'id' => 'id',
- 'text' => 'name',
- 'parent' => 'parent_id',
- ];
- protected $nodes = [];
- protected $value = [];
- protected $checkedAll;
- public function checkedAll()
- {
- $this->checkedAll = true;
- return $this;
- }
- public function checked($value = [])
- {
- if ($value instanceof Arrayable) {
- $value = $value->toArray();
- }
- $this->value = (array) $value;
- return $this;
- }
- /**
- * @param string $idColumn
- * @param string $textColumn
- * @param string $parentColumn
- *
- * @return $this
- */
- public function name(string $idColumn = 'id', string $textColumn = 'name', string $parentColumn = 'parent_id')
- {
- $this->columnNames['id'] = $idColumn;
- $this->columnNames['text'] = $textColumn;
- $this->columnNames['parent'] = $parentColumn;
- return $this;
- }
- /**
- * @param array $data exp:
- * {
- * "id": "1",
- * "parent": "#",
- * "text": "Dashboard",
- * // "state": {"selected": true}
- * }
- * @param array $data
- *
- * @return $this
- */
- public function nodes($data)
- {
- if ($data instanceof Arrayable) {
- $data = $data->toArray();
- }
- $this->nodes = &$data;
- return $this;
- }
- public function render()
- {
- $id = 'widget-tree-'.Str::random(8);
- $this->id($id);
- $this->class('jstree-wrapper');
- $this->defaultHtmlAttribute('style', 'border:0;padding:5px 0');
- $this->formatNodes();
- $this->variables = [
- 'id' => $id,
- 'nodes' => &$this->nodes,
- ];
- return parent::render(); // TODO: Change the autogenerated stub
- }
- protected function formatNodes()
- {
- $value = $this->value;
- if ($value && ! is_array($value)) {
- $value = explode(',', $value);
- }
- $value = (array) $value;
- if (! $this->nodes) {
- return;
- }
- $idColumn = $this->columnNames['id'];
- $textColumn = $this->columnNames['text'];
- $parentColumn = $this->columnNames['parent'];
- $nodes = [];
- foreach ($this->nodes as &$v) {
- if (empty($v[$idColumn])) {
- continue;
- }
- $parentId = $v[$parentColumn] ?? '#';
- if (empty($parentId)) {
- $parentId = '#';
- }
- $v['state'] = [];
- if ($this->checkedAll || ($value && in_array($v[$idColumn], $value))) {
- $v['state']['selected'] = true;
- }
- $v['state']['disabled'] = true;
- $nodes[] = [
- 'id' => $v[$idColumn],
- 'text' => $v[$textColumn] ?? null,
- 'parent' => $parentId,
- 'state' => $v['state'],
- ];
- }
- $this->nodes = &$nodes;
- }
- protected function collectAssets()
- {
- Admin::css('vendor/dcat-admin/jstree-theme/themes/proton/style.min.css');
- Admin::collectComponentAssets('jstree');
- }
- }
|