123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <?php
- namespace Dcat\Admin\Form\Field;
- use Dcat\Admin\Form\Field;
- use Dcat\Admin\Support\Helper;
- use Dcat\Admin\Widgets\Checkbox as WidgetCheckbox;
- use Illuminate\Contracts\Support\Arrayable;
- use Illuminate\Support\Arr;
- class Tree extends Field
- {
- protected $options = [
- 'plugins' => ['checkbox', 'types'],
- 'core' => [
- 'check_callback' => true,
- 'themes' => [
- 'name' => 'proton',
- 'responsive' => true,
- ],
- ],
- 'checkbox' => [
- 'keep_selected_style' => false,
- ],
- 'types' => [
- 'default' => [
- 'icon' => false,
- ],
- ],
- ];
- protected $nodes = [];
- protected $parents = [];
- protected $expand = true;
- protected $columnNames = [
- 'id' => 'id',
- 'text' => 'name',
- 'parent' => 'parent_id',
- ];
- protected $exceptParents = true;
- protected $readOnly = false;
- protected $rootParentId = 0;
- /**
- * @param array|Arrayable|\Closure $data exp:
- * {
- * "id": "1",
- * "parent": "#",
- * "text": "Dashboard",
- * // "state": {"selected": true}
- * }
- *
- * @return $this
- */
- public function nodes($data)
- {
- if ($data instanceof Arrayable) {
- $data = $data->toArray();
- }
- $this->nodes = &$data;
- return $this;
- }
- /**
- * 过滤父节点.
- *
- * @param bool $value
- *
- * @return $this
- */
- public function exceptParentNode(bool $value = true)
- {
- $this->exceptParents = $value;
- return $this;
- }
- public function rootParentId($id)
- {
- $this->rootParentId = $id;
- return $this;
- }
- /**
- * {@inheritDoc}
- */
- public function readOnly(bool $value = true)
- {
- $this->readOnly = true;
- return $this;
- }
- public function setIdColumn(string $name)
- {
- $this->columnNames['id'] = $name;
- return $this;
- }
- public function setTitleColumn(string $name)
- {
- $this->columnNames['text'] = $name;
- return $this;
- }
- public function setParentColumn(string $name)
- {
- $this->columnNames['parent'] = $name;
- return $this;
- }
- protected function formatNodes()
- {
- $value = Helper::array($this->value());
- $this->value = &$value;
- if ($this->nodes instanceof \Closure) {
- $this->nodes = Helper::array($this->nodes->call($this->values(), $value, $this));
- }
- if (! $this->nodes) {
- return;
- }
- $idColumn = $this->columnNames['id'];
- $textColumn = $this->columnNames['text'];
- $parentColumn = $this->columnNames['parent'];
- $parentIds = $nodes = [];
- foreach ($this->nodes as &$v) {
- if (empty($v[$idColumn])) {
- continue;
- }
- $parentId = $v[$parentColumn] ?? '#';
- if (empty($parentId) || $parentId == $this->rootParentId) {
- $parentId = '#';
- } else {
- $parentIds[] = $parentId;
- }
- $v['state'] = [];
- if ($value && in_array($v[$idColumn], $value)) {
- $v['state']['selected'] = true;
- }
- if ($this->readOnly) {
- $v['state']['disabled'] = true;
- }
- $nodes[] = [
- 'id' => $v[$idColumn],
- 'text' => $v[$textColumn] ?? null,
- 'parent' => $parentId,
- 'state' => $v['state'],
- ];
- }
- if ($this->exceptParents) {
- // 筛选出所有父节点,最终点击树节点时过滤掉父节点
- $this->parents = array_unique($parentIds);
- }
- $this->nodes = &$nodes;
- }
- /**
- * Set type.
- *
- * @param array $value
- *
- * @return $this
- */
- public function type(array $value)
- {
- $this->options['types'] = array_merge($this->options['types'], $value);
- return $this;
- }
- /**
- * Set plugins.
- *
- * @param array $value
- *
- * @return $this
- */
- public function plugins(array $value)
- {
- $this->options['plugins'] = $value;
- return $this;
- }
- /**
- * @param bool $value
- *
- * @return $this
- */
- public function expand(bool $value = true)
- {
- $this->expand = $value;
- return $this;
- }
- protected function formatFieldData($data)
- {
- return Helper::array($this->getValueFromData($data), true);
- }
- protected function prepareInputValue($value)
- {
- return Helper::array($value, true);
- }
- public function render()
- {
- $checkboxes = new WidgetCheckbox();
- $checkboxes->style('primary');
- $checkboxes->inline();
- $checkboxes->options([
- 1 => trans('admin.selectall'),
- 2 => trans('admin.expand'),
- ]);
- $this->readOnly && $checkboxes->disable(1);
- $this->expand && $checkboxes->check(2);
- $this->formatNodes();
- if ($v = $this->value()) {
- $this->attribute('value', implode(',', $v));
- }
- $this->addVariables([
- 'checkboxes' => $checkboxes,
- 'nodes' => $this->nodes,
- 'expand' => $this->expand,
- 'disabled' => empty($this->attributes['disabled']) ? '' : 'disabled',
- 'parents' => $this->parents,
- ]);
- return parent::render();
- }
- }
|