BatchActions.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Dcat\Admin\Grid\Tools;
  3. use Dcat\Admin\Grid\BatchAction;
  4. use Illuminate\Support\Collection;
  5. class BatchActions extends AbstractTool
  6. {
  7. /**
  8. * @var Collection
  9. */
  10. protected $actions;
  11. /**
  12. * @var bool
  13. */
  14. protected $enableDelete = true;
  15. /**
  16. * BatchActions constructor.
  17. */
  18. public function __construct()
  19. {
  20. $this->actions = new Collection();
  21. $this->appendDefaultAction();
  22. }
  23. /**
  24. * Append default action(batch delete action).
  25. *
  26. * return void
  27. */
  28. protected function appendDefaultAction()
  29. {
  30. $this->add(new BatchDelete(trans('admin.delete')));
  31. }
  32. /**
  33. * Disable delete.
  34. *
  35. * @return $this
  36. */
  37. public function disableDelete(bool $disable = true)
  38. {
  39. $this->enableDelete = ! $disable;
  40. return $this;
  41. }
  42. /**
  43. * Add a batch action.
  44. *
  45. * @param BatchAction $action
  46. *
  47. * @return $this
  48. */
  49. public function add(BatchAction $action)
  50. {
  51. $action->selectorPrefix = '.grid-batch-action-'.$this->actions->count();
  52. $this->actions->push($action);
  53. return $this;
  54. }
  55. /**
  56. * Prepare batch actions.
  57. *
  58. * @return void
  59. */
  60. protected function prepareActions()
  61. {
  62. foreach ($this->actions as $action) {
  63. $action->setGrid($this->parent);
  64. }
  65. }
  66. /**
  67. * Render BatchActions button groups.
  68. *
  69. * @return string
  70. */
  71. public function render()
  72. {
  73. if (! $this->enableDelete) {
  74. $this->actions->shift();
  75. }
  76. if ($this->actions->isEmpty()) {
  77. return '';
  78. }
  79. $this->prepareActions();
  80. $data = [
  81. 'actions' => $this->actions,
  82. ];
  83. return view('admin::grid.batch-actions', $data)->render();
  84. }
  85. }