DialogTree.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace Dcat\Admin\Grid\Displayers;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Support\Helper;
  5. use Illuminate\Contracts\Support\Arrayable;
  6. class DialogTree extends AbstractDisplayer
  7. {
  8. protected $url;
  9. protected $title;
  10. protected $area = ['580px', '600px'];
  11. protected $options = [
  12. 'plugins' => ['checkbox', 'types'],
  13. 'core' => [
  14. 'check_callback' => true,
  15. 'themes' => [
  16. 'name' => 'proton',
  17. 'responsive' => true,
  18. ],
  19. ],
  20. 'checkbox' => [
  21. 'keep_selected_style' => false,
  22. ],
  23. 'types' => [
  24. 'default' => [
  25. 'icon' => false,
  26. ],
  27. ],
  28. ];
  29. protected $columnNames = [
  30. 'id' => 'id',
  31. 'text' => 'name',
  32. 'parent' => 'parent_id',
  33. ];
  34. protected $nodes = [];
  35. protected $checkAll;
  36. /**
  37. * @param array $data exp:
  38. * {
  39. * "id": "1",
  40. * "parent": "#",
  41. * "text": "Dashboard",
  42. * // "state": {"selected": true}
  43. * }
  44. * @param array $data
  45. *
  46. * @return $this
  47. */
  48. public function nodes($data)
  49. {
  50. if ($data instanceof Arrayable) {
  51. $data = $data->toArray();
  52. }
  53. $this->nodes = &$data;
  54. return $this;
  55. }
  56. public function url(string $source)
  57. {
  58. $this->url = admin_url($source);
  59. return $this;
  60. }
  61. public function checkAll()
  62. {
  63. $this->checkAll = true;
  64. return $this;
  65. }
  66. /**
  67. * @param array $options
  68. *
  69. * @return $this
  70. */
  71. public function options($options = [])
  72. {
  73. if ($options instanceof Arrayable) {
  74. $options = $options->toArray();
  75. }
  76. $this->options = array_merge($this->options, $options);
  77. return $this;
  78. }
  79. public function title($title)
  80. {
  81. $this->title = $title;
  82. return $this;
  83. }
  84. /**
  85. * @param string $width
  86. * @param string $height
  87. *
  88. * @return $this
  89. */
  90. public function area(string $width, string $height)
  91. {
  92. $this->area = [$width, $height];
  93. return $this;
  94. }
  95. public function setIdColumn(string $name)
  96. {
  97. $this->columnNames['id'] = $name;
  98. return $this;
  99. }
  100. public function setTitleColumn(string $name)
  101. {
  102. $this->columnNames['text'] = $name;
  103. return $this;
  104. }
  105. public function setParentColumn(string $name)
  106. {
  107. $this->columnNames['parent'] = $name;
  108. return $this;
  109. }
  110. public function display($callbackOrNodes = null)
  111. {
  112. if (is_array($callbackOrNodes) || $callbackOrNodes instanceof Arrayable) {
  113. $this->nodes($callbackOrNodes);
  114. } elseif ($callbackOrNodes instanceof \Closure) {
  115. $callbackOrNodes->call($this->row, $this);
  116. }
  117. return Admin::view('admin::grid.displayer.dialogtree', [
  118. 'value' => $this->format($this->value),
  119. 'prefix' => $this->getSelectorPrefix(),
  120. 'nodes' => $this->nodes,
  121. 'title' => $this->title ?: $this->column->getLabel(),
  122. 'options' => $this->options,
  123. 'area' => $this->area,
  124. 'columnNames' => $this->columnNames,
  125. 'url' => $this->url,
  126. 'checkAll' => $this->checkAll,
  127. ]);
  128. }
  129. protected function format($val)
  130. {
  131. return implode(',', Helper::array($val, true));
  132. }
  133. protected function getSelectorPrefix()
  134. {
  135. return $this->grid->getName().'_'.$this->column->getName().'_'.$this->getKey();
  136. }
  137. }