Actions.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace Dcat\Admin\Grid\Displayers;
  3. use Dcat\Admin\Actions\Action;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Grid\RowAction;
  6. use Dcat\Admin\Support\Helper;
  7. use Illuminate\Contracts\Support\Htmlable;
  8. use Illuminate\Contracts\Support\Renderable;
  9. class Actions extends AbstractDisplayer
  10. {
  11. protected static $resolvedWindow;
  12. /**
  13. * @var array
  14. */
  15. protected $appends = [];
  16. /**
  17. * @var array
  18. */
  19. protected $prepends = [];
  20. /**
  21. * Default actions.
  22. *
  23. * @var array
  24. */
  25. protected $actions = ['view', 'edit', 'quickEdit', 'delete'];
  26. /**
  27. * @var string
  28. */
  29. protected $resource;
  30. /**
  31. * Append a action.
  32. *
  33. * @param string|Renderable|Action|Htmlable $action
  34. *
  35. * @return $this
  36. */
  37. public function append($action)
  38. {
  39. if ($action instanceof RowAction) {
  40. $this->prepareAction($action);
  41. }
  42. array_push($this->appends, $action);
  43. return $this;
  44. }
  45. /**
  46. * Prepend a action.
  47. *
  48. * @param string|Renderable|Action|Htmlable $action
  49. *
  50. * @return $this
  51. */
  52. public function prepend($action)
  53. {
  54. if ($action instanceof RowAction) {
  55. $this->prepareAction($action);
  56. }
  57. array_unshift($this->prepends, $action);
  58. return $this;
  59. }
  60. /**
  61. * @param RowAction $action
  62. */
  63. protected function prepareAction(RowAction $action)
  64. {
  65. $action->setGrid($this->grid)
  66. ->setColumn($this->column)
  67. ->setRow($this->row);
  68. }
  69. /**
  70. * Disable view action.
  71. *
  72. * @return $this
  73. */
  74. public function disableView(bool $disable = true)
  75. {
  76. if ($disable) {
  77. array_delete($this->actions, 'view');
  78. } elseif (! in_array('view', $this->actions)) {
  79. array_push($this->actions, 'view');
  80. }
  81. return $this;
  82. }
  83. /**
  84. * Disable delete.
  85. *
  86. * @return $this.
  87. */
  88. public function disableDelete(bool $disable = true)
  89. {
  90. if ($disable) {
  91. array_delete($this->actions, 'delete');
  92. } elseif (! in_array('delete', $this->actions)) {
  93. array_push($this->actions, 'delete');
  94. }
  95. return $this;
  96. }
  97. /**
  98. * Disable edit.
  99. *
  100. * @return $this.
  101. */
  102. public function disableEdit(bool $disable = true)
  103. {
  104. if ($disable) {
  105. array_delete($this->actions, 'edit');
  106. } elseif (! in_array('edit', $this->actions)) {
  107. array_push($this->actions, 'edit');
  108. }
  109. return $this;
  110. }
  111. /**
  112. * Disable quick edit.
  113. *
  114. * @return $this.
  115. */
  116. public function disableQuickEdit(bool $disable = true)
  117. {
  118. if ($disable) {
  119. array_delete($this->actions, 'quickEdit');
  120. } elseif (! in_array('quickEdit', $this->actions)) {
  121. array_push($this->actions, 'quickEdit');
  122. }
  123. return $this;
  124. }
  125. /**
  126. * Set resource of current resource.
  127. *
  128. * @param $resource
  129. *
  130. * @return $this
  131. */
  132. public function setResource($resource)
  133. {
  134. $this->resource = $resource;
  135. return $this;
  136. }
  137. /**
  138. * Get resource of current resource.
  139. *
  140. * @return string
  141. */
  142. public function resource()
  143. {
  144. return $this->resource ?: parent::resource();
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function display(array $callbacks = [])
  150. {
  151. $this->disableView(! $this->grid->option('show_view_button'));
  152. $this->disableEdit(! $this->grid->option('show_edit_button'));
  153. $this->disableQuickEdit(! $this->grid->option('show_quick_edit_button'));
  154. $this->disableDelete(! $this->grid->option('show_delete_button'));
  155. foreach ($callbacks as $callback) {
  156. if ($callback instanceof \Closure) {
  157. $callback->call($this->row, $this);
  158. }
  159. }
  160. $map = [Helper::class, 'render'];
  161. $prepends = array_map($map, $this->prepends);
  162. $appends = array_map($map, $this->appends);
  163. $actions = &$prepends;
  164. foreach ($this->actions as $action) {
  165. $method = 'render'.ucfirst($action);
  166. array_push($actions, $this->{$method}());
  167. }
  168. $actions = array_merge($actions, $appends);
  169. return implode('', $actions);
  170. }
  171. /**
  172. * Render view action.
  173. *
  174. * @return string
  175. */
  176. protected function renderView()
  177. {
  178. return <<<EOT
  179. <a href="{$this->resource()}/{$this->key()}">
  180. <i class="ti-eye grid-action-icon"></i>
  181. </a>&nbsp;
  182. EOT;
  183. }
  184. /**
  185. * Render edit action.
  186. *
  187. * @return string
  188. */
  189. protected function renderEdit()
  190. {
  191. return <<<EOT
  192. <a href="{$this->resource()}/{$this->key()}/edit">
  193. <i class="ti-pencil-alt grid-action-icon"></i>
  194. </a>&nbsp;
  195. EOT;
  196. }
  197. /**
  198. * @return string
  199. */
  200. protected function renderQuickEdit()
  201. {
  202. if (! static::$resolvedWindow) {
  203. static::$resolvedWindow = true;
  204. [$width, $height] = $this->grid->option('dialog_form_area');
  205. Form::modal(trans('admin.edit'))
  206. ->click(".{$this->grid->rowName()}-edit")
  207. ->dimensions($width, $height)
  208. ->success('LA.reload()')
  209. ->render();
  210. }
  211. return <<<EOF
  212. <a class="{$this->grid->rowName()}-edit" data-url="{$this->resource()}/{$this->key()}/edit" href="javascript:void(0);">
  213. <i class=" fa fa-clone grid-action-icon"></i>
  214. </a>&nbsp;
  215. EOF;
  216. }
  217. /**
  218. * Render delete action.
  219. *
  220. * @return string
  221. */
  222. protected function renderDelete()
  223. {
  224. return <<<EOT
  225. <a href="javascript:void(0);" data-url="{$this->resource()}/{$this->key()}" data-action="delete">
  226. <i class="ti-trash grid-action-icon"></i>
  227. </a>&nbsp;
  228. EOT;
  229. }
  230. }