Tools.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Dcat\Admin\Support\Helper;
  4. use Illuminate\Contracts\Support\Htmlable;
  5. use Illuminate\Contracts\Support\Renderable;
  6. use Illuminate\Support\Collection;
  7. class Tools implements Renderable
  8. {
  9. /**
  10. * @var Builder
  11. */
  12. protected $form;
  13. /**
  14. * Collection of tools.
  15. *
  16. * @var array
  17. */
  18. protected $tools = ['delete' => true, 'view' => true, 'list' => true];
  19. /**
  20. * Tools should be appends to default tools.
  21. *
  22. * @var Collection
  23. */
  24. protected $appends;
  25. /**
  26. * Tools should be prepends to default tools.
  27. *
  28. * @var Collection
  29. */
  30. protected $prepends;
  31. /**
  32. * Create a new Tools instance.
  33. *
  34. * @param Builder $builder
  35. */
  36. public function __construct(Builder $builder)
  37. {
  38. $this->form = $builder;
  39. $this->appends = new Collection();
  40. $this->prepends = new Collection();
  41. }
  42. /**
  43. * Append a tools.
  44. *
  45. * @param string|\Closure|Renderable|Htmlable|AbstractTool $tool
  46. * @return $this
  47. */
  48. public function append($tool)
  49. {
  50. $this->prepareTool($tool);
  51. $this->appends->push($tool);
  52. return $this;
  53. }
  54. /**
  55. * Prepend a tool.
  56. *
  57. * @param string|\Closure|Renderable|Htmlable|AbstractTool $tool
  58. * @return $this
  59. */
  60. public function prepend($tool)
  61. {
  62. $this->prepareTool($tool);
  63. $this->prepends->push($tool);
  64. return $this;
  65. }
  66. /**
  67. * @param mixed $tool
  68. * @return void
  69. */
  70. protected function prepareTool($tool)
  71. {
  72. if ($tool instanceof AbstractTool) {
  73. $tool->setForm($this->form->form());
  74. }
  75. }
  76. /**
  77. * Disable `list` tool.
  78. *
  79. * @return $this
  80. */
  81. public function disableList(bool $disable = true)
  82. {
  83. $this->tools['list'] = ! $disable;
  84. return $this;
  85. }
  86. /**
  87. * Disable `delete` tool.
  88. *
  89. * @return $this
  90. */
  91. public function disableDelete(bool $disable = true)
  92. {
  93. $this->tools['delete'] = ! $disable;
  94. return $this;
  95. }
  96. /**
  97. * Disable `view` tool.
  98. *
  99. * @return $this
  100. */
  101. public function disableView(bool $disable = true)
  102. {
  103. $this->tools['view'] = ! $disable;
  104. return $this;
  105. }
  106. /**
  107. * Get request path for resource list.
  108. *
  109. * @return string
  110. */
  111. protected function getListPath()
  112. {
  113. return $this->form->resource();
  114. }
  115. /**
  116. * Get request path for edit.
  117. *
  118. * @return string
  119. */
  120. protected function getDeletePath()
  121. {
  122. return $this->getViewPath();
  123. }
  124. /**
  125. * Get request path for delete.
  126. *
  127. * @return string
  128. */
  129. protected function getViewPath()
  130. {
  131. if ($key = $this->form->getResourceId()) {
  132. return $this->getListPath().'/'.$key;
  133. }
  134. return $this->getListPath();
  135. }
  136. /**
  137. * Get parent form of tool.
  138. *
  139. * @return Builder
  140. */
  141. public function form()
  142. {
  143. return $this->form;
  144. }
  145. /**
  146. * Render list button.
  147. *
  148. * @return string
  149. */
  150. protected function renderList()
  151. {
  152. $text = trans('admin.list');
  153. return <<<EOT
  154. <div class="btn-group pull-right" style="margin-right: 5px">
  155. <a href="{$this->getListPath()}" class="btn btn-sm btn-primary "><i class="feather icon-list"></i><span class="d-none d-sm-inline">&nbsp;$text</span></a>
  156. </div>
  157. EOT;
  158. }
  159. /**
  160. * Render list button.
  161. *
  162. * @return string
  163. */
  164. protected function renderView()
  165. {
  166. $view = trans('admin.view');
  167. return <<<HTML
  168. <div class="btn-group pull-right" style="margin-right: 5px">
  169. <a href="{$this->getViewPath()}" class="btn btn-sm btn-primary">
  170. <i class="feather icon-eye"></i><span class="d-none d-sm-inline"> {$view}</span>
  171. </a>
  172. </div>
  173. HTML;
  174. }
  175. /**
  176. * Render `delete` tool.
  177. *
  178. * @return string
  179. */
  180. protected function renderDelete()
  181. {
  182. $delete = trans('admin.delete');
  183. return <<<HTML
  184. <div class="btn-group pull-right" style="margin-right: 5px">
  185. <a class="btn btn-sm btn-white" data-action="delete" data-url="{$this->getDeletePath()}" data-redirect="{$this->getListPath()}">
  186. <i class="feather icon-trash"></i><span class="d-none d-sm-inline"> {$delete}</span>
  187. </a>
  188. </div>
  189. HTML;
  190. }
  191. /**
  192. * Render custom tools.
  193. *
  194. * @param Collection $tools
  195. * @return mixed
  196. */
  197. protected function renderCustomTools($tools)
  198. {
  199. if ($this->form->isCreating()) {
  200. $this->disableView();
  201. $this->disableDelete();
  202. }
  203. if (empty($tools)) {
  204. return '';
  205. }
  206. return $tools->map([Helper::class, 'render'])->implode(' ');
  207. }
  208. /**
  209. * Render tools.
  210. *
  211. * @return string
  212. */
  213. public function render()
  214. {
  215. $output = $this->renderCustomTools($this->prepends);
  216. foreach ($this->tools as $tool => $enable) {
  217. if ($enable) {
  218. $renderMethod = 'render'.ucfirst($tool);
  219. $output .= $this->$renderMethod();
  220. }
  221. }
  222. return $output.$this->renderCustomTools($this->appends);
  223. }
  224. }