RowSelector.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Dcat\Admin\Grid\Tools;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Grid;
  5. use Illuminate\Support\Arr;
  6. class RowSelector
  7. {
  8. protected $grid;
  9. protected $style = 'primary';
  10. protected $background;
  11. protected $rowClickable = false;
  12. protected $idColumn;
  13. protected $titleColumn;
  14. protected $checked = [];
  15. protected $disabled = [];
  16. public function __construct(Grid $grid)
  17. {
  18. $this->grid = $grid;
  19. }
  20. public function style(string $style)
  21. {
  22. $this->style = $style;
  23. return $this;
  24. }
  25. public function background(string $value)
  26. {
  27. $this->background = $value;
  28. return $this;
  29. }
  30. public function click(bool $value = true)
  31. {
  32. $this->rowClickable = $value;
  33. return $this;
  34. }
  35. public function check($data)
  36. {
  37. $this->checked = $data;
  38. return $this;
  39. }
  40. public function disable($data)
  41. {
  42. $this->disabled = $data;
  43. return $this;
  44. }
  45. public function idColumn(string $value)
  46. {
  47. $this->idColumn = $value;
  48. return $this;
  49. }
  50. public function titleColumn(string $value)
  51. {
  52. $this->titleColumn = $value;
  53. return $this;
  54. }
  55. public function renderHeader()
  56. {
  57. return <<<HTML
  58. <div class="vs-checkbox-con vs-checkbox-{$this->style} checkbox-grid checkbox-grid-header">
  59. <input type="checkbox" class="select-all {$this->grid->getSelectAllName()}">
  60. <span class="vs-checkbox"><span class="vs-checkbox--check"><i class="vs-icon feather icon-check"></i></span></span>
  61. </div>
  62. HTML;
  63. }
  64. public function renderColumn($row, $id)
  65. {
  66. $this->addScript();
  67. $title = $this->getTitle($row, $id);
  68. $title = e(is_array($title) ? json_encode($title) : $title);
  69. $id = $this->idColumn ? Arr::get($row->toArray(), $this->idColumn) : $id;
  70. $checked = $this->shouldChecked($row) ? 'checked="true"' : '';
  71. $disabled = $this->shouldDisable($row) ? 'disabled' : '';
  72. return <<<EOT
  73. <div class="vs-checkbox-con vs-checkbox-{$this->style} checkbox-grid checkbox-grid-column">
  74. <input type="checkbox" class="{$this->grid->getRowName()}-checkbox" data-id="{$id}" {$checked} {$disabled} data-label="{$title}">
  75. <span class="vs-checkbox"><span class="vs-checkbox--check"><i class="vs-icon feather icon-check"></i></span></span>
  76. </div>
  77. EOT;
  78. }
  79. protected function addScript()
  80. {
  81. $clickable = $this->rowClickable ? 'true' : 'false';
  82. $background = $this->background ?: Admin::color()->dark20();
  83. Admin::script(
  84. <<<JS
  85. var selector = Dcat.RowSelector({
  86. checkboxSelector: '.{$this->grid->getRowName()}-checkbox',
  87. selectAllSelector: '.{$this->grid->getSelectAllName()}',
  88. clickRow: {$clickable},
  89. background: '{$background}',
  90. });
  91. Dcat.grid.addSelector(selector, '{$this->grid->getName()}');
  92. JS
  93. );
  94. }
  95. protected function shouldChecked($row)
  96. {
  97. return $this->isSelectedRow($row, $this->checked);
  98. }
  99. protected function shouldDisable($row)
  100. {
  101. return $this->isSelectedRow($row, $this->disabled);
  102. }
  103. protected function isSelectedRow($row, $value)
  104. {
  105. if ($value instanceof \Closure) {
  106. return $value->call($row, $row);
  107. }
  108. if (is_array($value)) {
  109. foreach ($value as $v) {
  110. if (((int) $v) === $row->_index) {
  111. return true;
  112. }
  113. }
  114. }
  115. return false;
  116. }
  117. protected function getTitle($row, $id)
  118. {
  119. if ($key = $this->titleColumn) {
  120. $label = Arr::get($row->toArray(), $key);
  121. if ($label !== null && $label !== '') {
  122. return $label;
  123. }
  124. return $id;
  125. }
  126. $label = $row->name ?: $row->title;
  127. return $label ?: ($row->username ?: $id);
  128. }
  129. }