['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 columnNames(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'); } }