Actions.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. namespace Dcat\Admin\Grid\Displayers;
  3. use Dcat\Admin\Actions\Action;
  4. use Dcat\Admin\Grid\Actions\Delete;
  5. use Dcat\Admin\Grid\Actions\Edit;
  6. use Dcat\Admin\Grid\Actions\QuickEdit;
  7. use Dcat\Admin\Grid\Actions\Show;
  8. use Dcat\Admin\Grid\RowAction;
  9. use Dcat\Admin\Support\Helper;
  10. use Illuminate\Contracts\Support\Htmlable;
  11. use Illuminate\Contracts\Support\Renderable;
  12. class Actions extends AbstractDisplayer
  13. {
  14. /**
  15. * @var array
  16. */
  17. protected $appends = [];
  18. /**
  19. * @var array
  20. */
  21. protected $prepends = [];
  22. /**
  23. * Default actions.
  24. *
  25. * @var array
  26. */
  27. protected $actions = [
  28. 'view' => true,
  29. 'edit' => true,
  30. 'quickEdit' => false,
  31. 'delete' => true,
  32. ];
  33. /**
  34. * @var string
  35. */
  36. protected $resource;
  37. /**
  38. * Append a action.
  39. *
  40. * @param string|Renderable|Action|Htmlable $action
  41. *
  42. * @return $this
  43. */
  44. public function append($action)
  45. {
  46. $this->prepareAction($action);
  47. array_push($this->appends, $action);
  48. return $this;
  49. }
  50. /**
  51. * Prepend a action.
  52. *
  53. * @param string|Renderable|Action|Htmlable $action
  54. *
  55. * @return $this
  56. */
  57. public function prepend($action)
  58. {
  59. $this->prepareAction($action);
  60. array_unshift($this->prepends, $action);
  61. return $this;
  62. }
  63. /**
  64. * @param mixed $action
  65. *
  66. * @return void
  67. */
  68. protected function prepareAction(&$action)
  69. {
  70. if ($action instanceof RowAction) {
  71. $action->setGrid($this->grid)
  72. ->setColumn($this->column)
  73. ->setRow($this->row);
  74. }
  75. }
  76. public function view(bool $value = true)
  77. {
  78. return $this->setAction('view', $value);
  79. }
  80. /**
  81. * Disable view action.
  82. *
  83. * @param bool $disable
  84. *
  85. * @return $this
  86. */
  87. public function disableView(bool $disable = true)
  88. {
  89. return $this->setAction('view', ! $disable);
  90. }
  91. public function delete(bool $value = true)
  92. {
  93. return $this->setAction('delete', $value);
  94. }
  95. /**
  96. * Disable delete.
  97. *
  98. * @param bool $disable
  99. *
  100. * @return $this.
  101. */
  102. public function disableDelete(bool $disable = true)
  103. {
  104. return $this->setAction('delete', ! $disable);
  105. }
  106. public function edit(bool $value = true)
  107. {
  108. return $this->setAction('edit', $value);
  109. }
  110. /**
  111. * Disable edit.
  112. *
  113. * @param bool $disable
  114. *
  115. * @return $this.
  116. */
  117. public function disableEdit(bool $disable = true)
  118. {
  119. return $this->setAction('edit', ! $disable);
  120. }
  121. public function quickEdit(bool $value = true)
  122. {
  123. return $this->setAction('quickEdit', $value);
  124. }
  125. /**
  126. * Disable quick edit.
  127. *
  128. * @param bool $disable
  129. *
  130. * @return $this.
  131. */
  132. public function disableQuickEdit(bool $disable = true)
  133. {
  134. return $this->setAction('quickEdit', ! $disable);
  135. }
  136. /**
  137. * @param string $key
  138. * @param bool $disable
  139. *
  140. * @return $this
  141. */
  142. protected function setAction(string $key, bool $value)
  143. {
  144. $this->actions[$key] = $value;
  145. return $this;
  146. }
  147. /**
  148. * Set resource of current resource.
  149. *
  150. * @param $resource
  151. *
  152. * @return $this
  153. */
  154. public function setResource($resource)
  155. {
  156. $this->resource = $resource;
  157. return $this;
  158. }
  159. /**
  160. * Get resource of current resource.
  161. *
  162. * @return string
  163. */
  164. public function resource()
  165. {
  166. return $this->resource ?: parent::resource();
  167. }
  168. /**
  169. * @return void
  170. */
  171. protected function resetDefaultActions()
  172. {
  173. $this->view($this->grid->option('view_button'));
  174. $this->edit($this->grid->option('edit_button'));
  175. $this->quickEdit($this->grid->option('quick_edit_button'));
  176. $this->delete($this->grid->option('delete_button'));
  177. }
  178. /**
  179. * @param array $callbacks
  180. *
  181. * @return void
  182. */
  183. protected function call(array $callbacks = [])
  184. {
  185. foreach ($callbacks as $callback) {
  186. if ($callback instanceof \Closure) {
  187. $callback->call($this->row, $this);
  188. }
  189. }
  190. }
  191. /**
  192. * {@inheritdoc}
  193. */
  194. public function display(array $callbacks = [])
  195. {
  196. $this->resetDefaultActions();
  197. $this->call($callbacks);
  198. $toString = [Helper::class, 'render'];
  199. $prepends = array_map($toString, $this->prepends);
  200. $appends = array_map($toString, $this->appends);
  201. foreach ($this->actions as $action => $enable) {
  202. if ($enable) {
  203. $method = 'render'.ucfirst($action);
  204. array_push($prepends, $this->{$method}());
  205. }
  206. }
  207. return implode('', array_merge($prepends, $appends));
  208. }
  209. /**
  210. * Render view action.
  211. *
  212. * @return string
  213. */
  214. protected function renderView()
  215. {
  216. return Show::make($this->getViewLabel())
  217. ->setGrid($this->grid)
  218. ->setRow($this->row)
  219. ->render();
  220. }
  221. /**
  222. * @return string
  223. */
  224. protected function getViewLabel()
  225. {
  226. $label = trans('admin.show');
  227. return "<i title='{$label}' class=\"feather icon-eye grid-action-icon\"></i> &nbsp;";
  228. }
  229. /**
  230. * Render edit action.
  231. *
  232. * @return string
  233. */
  234. protected function renderEdit()
  235. {
  236. return Edit::make($this->getEditLabel())
  237. ->setGrid($this->grid)
  238. ->setRow($this->row)
  239. ->render();
  240. }
  241. /**
  242. * @return string
  243. */
  244. protected function getEditLabel()
  245. {
  246. $label = trans('admin.edit');
  247. return "<i title='{$label}' class=\"feather icon-edit-1 grid-action-icon\"></i> &nbsp;";
  248. }
  249. /**
  250. * @return string
  251. */
  252. protected function renderQuickEdit()
  253. {
  254. return QuickEdit::make($this->getQuickEditLabel())
  255. ->setGrid($this->grid)
  256. ->setRow($this->row)
  257. ->render();
  258. }
  259. /**
  260. * @return string
  261. */
  262. protected function getQuickEditLabel()
  263. {
  264. $label = trans('admin.quick_edit');
  265. return "<i title='{$label}' class=\"feather icon-edit grid-action-icon\"></i> &nbsp;";
  266. }
  267. /**
  268. * Render delete action.
  269. *
  270. * @return string
  271. */
  272. protected function renderDelete()
  273. {
  274. return Delete::make($this->getDeleteLabel())
  275. ->setGrid($this->grid)
  276. ->setRow($this->row)
  277. ->render();
  278. }
  279. /**
  280. * @return string
  281. */
  282. protected function getDeleteLabel()
  283. {
  284. $label = trans('admin.delete');
  285. return "<i class=\"feather icon-trash grid-action-icon\" title='{$label}'></i> &nbsp;";
  286. }
  287. }