Actions.php 5.9 KB

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