DialogTree.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. protected $rootParentId = 0;
  37. /**
  38. * @param array $data exp:
  39. * {
  40. * "id": "1",
  41. * "parent": "#",
  42. * "text": "Dashboard",
  43. * // "state": {"selected": true}
  44. * }
  45. * @param array $data
  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 rootParentId($id)
  57. {
  58. $this->rootParentId = $id;
  59. return $this;
  60. }
  61. public function url(string $source)
  62. {
  63. $this->url = admin_url($source);
  64. return $this;
  65. }
  66. public function checkAll()
  67. {
  68. $this->checkAll = true;
  69. return $this;
  70. }
  71. /**
  72. * @param array $options
  73. * @return $this
  74. */
  75. public function options($options = [])
  76. {
  77. if ($options instanceof Arrayable) {
  78. $options = $options->toArray();
  79. }
  80. $this->options = array_merge($this->options, $options);
  81. return $this;
  82. }
  83. public function title($title)
  84. {
  85. $this->title = $title;
  86. return $this;
  87. }
  88. /**
  89. * @param string $width
  90. * @param string $height
  91. * @return $this
  92. */
  93. public function area(string $width, string $height)
  94. {
  95. $this->area = [$width, $height];
  96. return $this;
  97. }
  98. public function setIdColumn(string $name)
  99. {
  100. $this->columnNames['id'] = $name;
  101. return $this;
  102. }
  103. public function setTitleColumn(string $name)
  104. {
  105. $this->columnNames['text'] = $name;
  106. return $this;
  107. }
  108. public function setParentColumn(string $name)
  109. {
  110. $this->columnNames['parent'] = $name;
  111. return $this;
  112. }
  113. public function display($callbackOrNodes = null)
  114. {
  115. if (is_array($callbackOrNodes) || $callbackOrNodes instanceof Arrayable) {
  116. $this->nodes($callbackOrNodes);
  117. } elseif ($callbackOrNodes instanceof \Closure) {
  118. $callbackOrNodes->call($this->row, $this);
  119. }
  120. return Admin::view('admin::grid.displayer.dialogtree', [
  121. 'value' => $this->format($this->value),
  122. 'nodes' => $this->nodes,
  123. 'title' => $this->title ?: $this->column->getLabel(),
  124. 'options' => $this->options,
  125. 'area' => $this->area,
  126. 'columnNames' => $this->columnNames,
  127. 'url' => $this->url,
  128. 'checkAll' => $this->checkAll,
  129. 'rootParentId' => $this->rootParentId,
  130. ]);
  131. }
  132. protected function format($val)
  133. {
  134. return implode(',', Helper::array($val, true));
  135. }
  136. }