BatchActions.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace Dcat\Admin\Grid\Tools;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Grid\BatchAction;
  5. use Illuminate\Support\Collection;
  6. class BatchActions extends AbstractTool
  7. {
  8. /**
  9. * @var Collection
  10. */
  11. protected $actions;
  12. /**
  13. * @var bool
  14. */
  15. protected $enableDelete = true;
  16. /**
  17. * @var bool
  18. */
  19. protected $isHoldSelectAllCheckbox = false;
  20. /**
  21. * BatchActions constructor.
  22. */
  23. public function __construct()
  24. {
  25. $this->actions = new Collection();
  26. $this->appendDefaultAction();
  27. }
  28. /**
  29. * Append default action(batch delete action).
  30. *
  31. * return void
  32. */
  33. protected function appendDefaultAction()
  34. {
  35. $this->add(new BatchDelete(trans('admin.delete')));
  36. }
  37. /**
  38. * Disable delete.
  39. *
  40. * @return $this
  41. */
  42. public function disableDelete(bool $disable = true)
  43. {
  44. $this->enableDelete = ! $disable;
  45. return $this;
  46. }
  47. /**
  48. * Disable delete And Hode SelectAll Checkbox.
  49. *
  50. * @return $this
  51. */
  52. public function disableDeleteAndHodeSelectAll()
  53. {
  54. $this->enableDelete = false;
  55. $this->isHoldSelectAllCheckbox = true;
  56. return $this;
  57. }
  58. /**
  59. * Add a batch action.
  60. *
  61. * @param BatchAction $action
  62. *
  63. * @return $this
  64. */
  65. public function add(BatchAction $action)
  66. {
  67. $action->selectorPrefix = '.grid-batch-action-'.$this->actions->count();
  68. $this->actions->push($action);
  69. return $this;
  70. }
  71. /**
  72. * Prepare batch actions.
  73. *
  74. * @return void
  75. */
  76. protected function prepareActions()
  77. {
  78. foreach ($this->actions as $action) {
  79. $action->setGrid($this->parent);
  80. }
  81. }
  82. /**
  83. * Scripts of BatchActions button groups.
  84. */
  85. protected function setupScript()
  86. {
  87. $name = $this->parent->getName();
  88. $allName = $this->parent->selectAllName();
  89. $rowName = $this->parent->rowName();
  90. $selected = trans('admin.grid_items_selected');
  91. $script = <<<JS
  92. $('.{$rowName}-checkbox').on('change', function () {
  93. var btn = $('.{$allName}-btn');
  94. if (this.checked) {
  95. btn.show()
  96. } else {
  97. btn.hide()
  98. }
  99. btn.find('.selected').html("{$selected}".replace('{n}', LA.grid.selectedRows('$name').length));
  100. });
  101. JS;
  102. Admin::script($script);
  103. }
  104. /**
  105. * Render BatchActions button groups.
  106. *
  107. * @return string
  108. */
  109. public function render()
  110. {
  111. if (! $this->enableDelete) {
  112. $this->actions->shift();
  113. }
  114. if ($this->actions->isEmpty()) {
  115. return '';
  116. }
  117. $this->setupScript();
  118. $this->prepareActions();
  119. $data = [
  120. 'actions' => $this->actions,
  121. 'selectAllName' => $this->parent->selectAllName(),
  122. 'isHoldSelectAllCheckbox' => $this->isHoldSelectAllCheckbox,
  123. ];
  124. return view('admin::grid.batch-actions', $data)->render();
  125. }
  126. }