Tools.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. namespace Dcat\Admin\Show;
  3. use Dcat\Admin\Form;
  4. use Dcat\Admin\Support\Helper;
  5. use Illuminate\Contracts\Support\Htmlable;
  6. use Illuminate\Contracts\Support\Renderable;
  7. use Illuminate\Support\Collection;
  8. use Illuminate\Support\Str;
  9. class Tools implements Renderable
  10. {
  11. /**
  12. * The panel that holds this tool.
  13. *
  14. * @var Panel
  15. */
  16. protected $panel;
  17. /**
  18. * @var string
  19. */
  20. protected $resource;
  21. /**
  22. * Default tools.
  23. *
  24. * @var array
  25. */
  26. protected $tools = ['delete', 'edit', 'list'];
  27. /**
  28. * Tools should be appends to default tools.
  29. *
  30. * @var Collection
  31. */
  32. protected $appends;
  33. /**
  34. * Tools should be prepends to default tools.
  35. *
  36. * @var Collection
  37. */
  38. protected $prepends;
  39. /**
  40. * @var bool
  41. */
  42. protected $showList = true;
  43. /**
  44. * @var bool
  45. */
  46. protected $showDelete = true;
  47. /**
  48. * @var bool
  49. */
  50. protected $showEdit = true;
  51. /**
  52. * @var bool
  53. */
  54. protected $showQuickEdit = false;
  55. /**
  56. * @var array
  57. */
  58. protected $dialogFormDimensions = ['700px', '670px'];
  59. /**
  60. * Tools constructor.
  61. *
  62. * @param Panel $panel
  63. */
  64. public function __construct(Panel $panel)
  65. {
  66. $this->panel = $panel;
  67. $this->appends = new Collection();
  68. $this->prepends = new Collection();
  69. }
  70. /**
  71. * Append a tools.
  72. *
  73. * @param string|\Closure|AbstractTool|Renderable|Htmlable $tool
  74. *
  75. * @return $this
  76. */
  77. public function append($tool)
  78. {
  79. $this->prepareTool($tool);
  80. $this->appends->push($tool);
  81. return $this;
  82. }
  83. /**
  84. * Prepend a tool.
  85. *
  86. * @param string|\Closure|AbstractTool|Renderable|Htmlable $tool
  87. *
  88. * @return $this
  89. */
  90. public function prepend($tool)
  91. {
  92. $this->prepareTool($tool);
  93. $this->prepends->push($tool);
  94. return $this;
  95. }
  96. /**
  97. * @param $tool
  98. *
  99. * @return void
  100. */
  101. protected function prepareTool($tool)
  102. {
  103. if ($tool instanceof AbstractTool) {
  104. $tool->setParent($this->panel->getParent());
  105. }
  106. }
  107. /**
  108. * Get resource path.
  109. *
  110. * @return string
  111. */
  112. public function getResource()
  113. {
  114. if (is_null($this->resource)) {
  115. $this->resource = $this->panel->getParent()->getResource();
  116. }
  117. return $this->resource;
  118. }
  119. /**
  120. * Disable `list` tool.
  121. *
  122. * @return $this
  123. */
  124. public function disableList(bool $disable = true)
  125. {
  126. $this->showList = ! $disable;
  127. return $this;
  128. }
  129. /**
  130. * Disable `delete` tool.
  131. *
  132. * @return $this
  133. */
  134. public function disableDelete(bool $disable = true)
  135. {
  136. $this->showDelete = ! $disable;
  137. return $this;
  138. }
  139. /**
  140. * Disable `edit` tool.
  141. *
  142. * @return $this
  143. */
  144. public function disableEdit(bool $disable = true)
  145. {
  146. $this->showEdit = ! $disable;
  147. return $this;
  148. }
  149. /**
  150. * @param bool $disable
  151. *
  152. * @return $this
  153. */
  154. public function disableQuickEdit(bool $disable = true)
  155. {
  156. $this->showQuickEdit = ! $disable;
  157. return $this;
  158. }
  159. /**
  160. * @param string $width
  161. * @param string $height
  162. *
  163. * @return $this
  164. */
  165. public function showQuickEdit(?string $width = null, ?string $height = null)
  166. {
  167. $this->showQuickEdit = true;
  168. $this->showEdit = false;
  169. $width && ($this->dialogFormDimensions[0] = $width);
  170. $height && ($this->dialogFormDimensions[1] = $height);
  171. return $this;
  172. }
  173. /**
  174. * Get request path for resource list.
  175. *
  176. * @return string
  177. */
  178. protected function getListPath()
  179. {
  180. $url = $this->getResource();
  181. return url()->isValidUrl($url) ? $url : '/'.trim($url, '/');
  182. }
  183. /**
  184. * Get request path for edit.
  185. *
  186. * @return string
  187. */
  188. protected function getEditPath()
  189. {
  190. $key = $this->panel->getParent()->getKey();
  191. return $this->getListPath().'/'.$key.'/edit';
  192. }
  193. /**
  194. * Get request path for delete.
  195. *
  196. * @return string
  197. */
  198. protected function getDeletePath()
  199. {
  200. $key = $this->panel->getParent()->getKey();
  201. return $this->getListPath().'/'.$key;
  202. }
  203. /**
  204. * Render `list` tool.
  205. *
  206. * @return string
  207. */
  208. protected function renderList()
  209. {
  210. if (! $this->showList) {
  211. return;
  212. }
  213. $list = trans('admin.list');
  214. return <<<HTML
  215. <div class="btn-group pull-right btn-mini" style="margin-right: 5px">
  216. <a href="{$this->getListPath()}" class="btn btn-sm btn-white btn-shadow">
  217. <i class=" ti-view-list-alt"></i><span class="hidden-xs"> {$list}</span>
  218. </a>
  219. </div>
  220. HTML;
  221. }
  222. /**
  223. * Render `edit` tool.
  224. *
  225. * @return string
  226. */
  227. protected function renderEdit()
  228. {
  229. if (! $this->showQuickEdit && ! $this->showEdit) {
  230. return;
  231. }
  232. $edit = trans('admin.edit');
  233. $url = $this->getEditPath();
  234. $quickBtn = $btn = '';
  235. if ($this->showEdit) {
  236. $btn = <<<EOF
  237. <a href="{$url}" class="btn btn-sm btn-primary">
  238. <i class="ti-pencil-alt "></i><span class="hidden-xs"> {$edit}</span>
  239. </a>
  240. EOF;
  241. }
  242. if ($this->showQuickEdit) {
  243. $id = 'show-edit-'.Str::random(8);
  244. [$width, $height] = $this->dialogFormDimensions;
  245. Form::dialog($edit)
  246. ->click(".$id")
  247. ->dimensions($width, $height)
  248. ->success('Dcat.reload()');
  249. $text = $this->showEdit ? '' : "<span class='hidden-xs'> &nbsp; $edit</span>";
  250. $quickBtn = "<a data-url='$url' class='btn btn-sm btn-primary {$id}'><i class=' fa fa-clone'></i>$text</a>";
  251. }
  252. return <<<HTML
  253. <div class="btn-group pull-right btn-mini" style="margin-right: 5px">{$btn}{$quickBtn}</div>
  254. HTML;
  255. }
  256. /**
  257. * Render `delete` tool.
  258. *
  259. * @return string
  260. */
  261. protected function renderDelete()
  262. {
  263. if (! $this->showDelete) {
  264. return;
  265. }
  266. $delete = trans('admin.delete');
  267. return <<<HTML
  268. <div class="btn-group pull-right btn-mini" style="margin-right: 5px">
  269. <a class="btn btn-sm btn-danger " data-action="delete" data-url="{$this->getDeletePath()}" data-redirect="{$this->getListPath()}">
  270. <i class="ti-trash"></i><span class="hidden-xs"> {$delete}</span>
  271. </a>
  272. </div>
  273. HTML;
  274. }
  275. /**
  276. * Render custom tools.
  277. *
  278. * @param Collection $tools
  279. *
  280. * @return mixed
  281. */
  282. protected function renderCustomTools($tools)
  283. {
  284. return $tools->map([Helper::class, 'render'])->implode(' ');
  285. }
  286. /**
  287. * Render tools.
  288. *
  289. * @return string
  290. */
  291. public function render()
  292. {
  293. $output = $this->renderCustomTools($this->prepends);
  294. foreach ($this->tools as $tool) {
  295. $renderMethod = 'render'.ucfirst($tool);
  296. $output .= $this->$renderMethod();
  297. }
  298. return $output.$this->renderCustomTools($this->appends);
  299. }
  300. }