Actions.php 6.7 KB

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