HasActions.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace Dcat\Admin\Grid\Concerns;
  3. use Closure;
  4. use Dcat\Admin\Grid;
  5. use Dcat\Admin\Grid\Displayers;
  6. trait HasActions
  7. {
  8. /**
  9. * Callback for grid actions.
  10. *
  11. * @var Closure[]
  12. */
  13. protected $actionsCallback = [];
  14. /**
  15. * Actions column display class.
  16. *
  17. * @var string
  18. */
  19. protected $actionsClass;
  20. /**
  21. * @param string $actionClass
  22. *
  23. * @return $this
  24. */
  25. public function setActionClass(string $actionClass)
  26. {
  27. if (is_subclass_of($actionClass, Grid\Displayers\Actions::class)) {
  28. $this->actionsClass = $actionClass;
  29. }
  30. return $this;
  31. }
  32. /**
  33. * Get action display class.
  34. *
  35. * @return \Illuminate\Config\Repository|mixed|string
  36. */
  37. public function getActionClass()
  38. {
  39. if ($this->actionsClass) {
  40. return $this->actionsClass;
  41. }
  42. if ($class = config('admin.grid.grid_action_class')) {
  43. return $class;
  44. }
  45. return Grid\Displayers\Actions::class;
  46. }
  47. /**
  48. * Set grid action callback.
  49. *
  50. * @param Closure $callback
  51. *
  52. * @return $this
  53. */
  54. public function actions(Closure $callback)
  55. {
  56. $this->actionsCallback[] = $callback;
  57. return $this;
  58. }
  59. /**
  60. * Add `actions` column for grid.
  61. *
  62. * @return void
  63. */
  64. protected function appendActionsColumn()
  65. {
  66. if (!$this->options['show_actions']) {
  67. return;
  68. }
  69. $this->addColumn('__actions__', trans('admin.action'))
  70. ->displayUsing($this->getActionClass(), [$this->actionsCallback]);
  71. }
  72. /**
  73. * Disable all actions.
  74. *
  75. * @return $this
  76. */
  77. public function disableActions(bool $disable = true)
  78. {
  79. return $this->option('show_actions', !$disable);
  80. }
  81. /**
  82. * Show all actions.
  83. *
  84. * @return $this
  85. */
  86. public function showActions(bool $val = true)
  87. {
  88. return $this->disableActions(!$val);
  89. }
  90. /**
  91. * Disable edit.
  92. *
  93. * @param bool $disable
  94. * @return $this
  95. */
  96. public function disableEditButton(bool $disable = true)
  97. {
  98. $this->options['show_edit_button'] = !$disable;
  99. return $this;
  100. }
  101. /**
  102. * Show edit.
  103. *
  104. * @param bool $val
  105. * @return $this
  106. */
  107. public function showEditButton(bool $val = true)
  108. {
  109. return $this->disableEditButton(!$val);
  110. }
  111. /**
  112. * Disable quick edit.
  113. *
  114. * @return $this.
  115. */
  116. public function disableQuickEditButton(bool $disable = true)
  117. {
  118. $this->options['show_quick_edit_button'] = !$disable;
  119. return $this;
  120. }
  121. /**
  122. * Show quick edit button.
  123. *
  124. * @return $this.
  125. */
  126. public function showQuickEditButton(bool $val = true)
  127. {
  128. return $this->disableQuickEditButton(!$val);
  129. }
  130. /**
  131. * Disable view action.
  132. *
  133. * @param bool $disable
  134. * @return $this
  135. */
  136. public function disableViewButton(bool $disable = true)
  137. {
  138. $this->options['show_view_button'] = !$disable;
  139. return $this;
  140. }
  141. /**
  142. * Show view action.
  143. *
  144. * @param bool $disable
  145. * @return $this
  146. */
  147. public function showViewButton(bool $val = true)
  148. {
  149. return $this->disableViewButton(!$val);
  150. }
  151. /**
  152. * Disable delete.
  153. *
  154. * @param bool $disable
  155. * @return $this
  156. */
  157. public function disableDeleteButton(bool $disable = true)
  158. {
  159. $this->options['show_delete_button'] = !$disable;
  160. return $this;
  161. }
  162. /**
  163. * Show delete button.
  164. *
  165. * @param bool $disable
  166. * @return $this
  167. */
  168. public function showDeleteButton(bool $val = true)
  169. {
  170. return $this->disableDeleteButton(!$val);
  171. }
  172. }