Tools.php 6.7 KB

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