HasTools.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace Dcat\Admin\Grid\Concerns;
  3. use Closure;
  4. use Dcat\Admin\Actions\Action;
  5. use Dcat\Admin\Grid\BatchAction;
  6. use Dcat\Admin\Grid\Tools;
  7. use Illuminate\Contracts\Support\Htmlable;
  8. use Illuminate\Contracts\Support\Renderable;
  9. trait HasTools
  10. {
  11. /**
  12. * Header tools.
  13. *
  14. * @var Tools
  15. */
  16. protected $tools;
  17. /**
  18. * Setup grid tools.
  19. */
  20. public function setUpTools()
  21. {
  22. $this->tools = new Tools($this);
  23. }
  24. /**
  25. * @param bool $value
  26. *
  27. * @return $this
  28. */
  29. public function toolsWithOutline(bool $value = true)
  30. {
  31. $this->tools->withOutline($value);
  32. return $this;
  33. }
  34. /**
  35. * Get or setup grid tools.
  36. *
  37. * @param Closure|array|Action|Tools\AbstractTool|Renderable|Htmlable|string $value
  38. *
  39. * @return $this|Tools
  40. */
  41. public function tools($value = null)
  42. {
  43. if ($value === null) {
  44. return $this->tools;
  45. }
  46. if ($value instanceof Closure) {
  47. $value($this->tools);
  48. return $this;
  49. }
  50. if (! is_array($value)) {
  51. $value = [$value];
  52. }
  53. foreach ($value as $tool) {
  54. $this->tools->append($tool);
  55. }
  56. return $this;
  57. }
  58. /**
  59. * Set grid batch-action callback.
  60. *
  61. * @param Closure|BatchAction|BatchAction[] $value
  62. *
  63. * @return $this
  64. */
  65. public function batchActions($value)
  66. {
  67. $this->tools(function (Tools $tools) use ($value) {
  68. $tools->batch($value);
  69. });
  70. return $this;
  71. }
  72. /**
  73. * Render custom tools.
  74. *
  75. * @return string
  76. */
  77. public function renderTools()
  78. {
  79. return $this->tools->render();
  80. }
  81. /**
  82. * @param bool $val
  83. *
  84. * @return mixed
  85. */
  86. public function disableToolbar(bool $val = true)
  87. {
  88. return $this->option('toolbar', ! $val);
  89. }
  90. /**
  91. * @param bool $val
  92. *
  93. * @return mixed
  94. */
  95. public function showToolbar(bool $val = true)
  96. {
  97. return $this->disableToolbar(! $val);
  98. }
  99. /**
  100. * Disable batch actions.
  101. *
  102. * @param bool $disable
  103. *
  104. * @return $this
  105. */
  106. public function disableBatchActions(bool $disable = true)
  107. {
  108. $this->tools->disableBatchActions($disable);
  109. return $this;
  110. }
  111. /**
  112. * Show batch actions.
  113. *
  114. * @param bool $val
  115. *
  116. * @return $this
  117. */
  118. public function showBatchActions(bool $val = true)
  119. {
  120. return $this->disableBatchActions(! $val);
  121. }
  122. /**
  123. * Disable batch delete.
  124. *
  125. * @param bool $disable
  126. *
  127. * @return $this
  128. */
  129. public function disableBatchDelete(bool $disable = true)
  130. {
  131. $this->tools->batch(function ($action) use ($disable) {
  132. $action->disableDelete($disable);
  133. });
  134. return $this;
  135. }
  136. /**
  137. * Show batch delete.
  138. *
  139. * @param bool $val
  140. *
  141. * @return $this
  142. */
  143. public function showBatchDelete(bool $val = true)
  144. {
  145. return $this->disableBatchDelete(! $val);
  146. }
  147. /**
  148. * Disable refresh button.
  149. *
  150. * @param bool $disable
  151. *
  152. * @return $this
  153. */
  154. public function disableRefreshButton(bool $disable = true)
  155. {
  156. $this->tools->disableRefreshButton($disable);
  157. return $this;
  158. }
  159. /**
  160. * Show refresh button.
  161. *
  162. * @param bool $val
  163. *
  164. * @return $this
  165. */
  166. public function showRefreshButton(bool $val = true)
  167. {
  168. return $this->disableRefreshButton(! $val);
  169. }
  170. /**
  171. * If grid show toolbar.
  172. *
  173. * @return bool
  174. */
  175. public function allowToolbar()
  176. {
  177. if (
  178. $this->option('toolbar')
  179. && (
  180. $this->tools()->has()
  181. || $this->allowExporter()
  182. || $this->allowCreateButton()
  183. || ! empty($this->variables['title'])
  184. )
  185. ) {
  186. return true;
  187. }
  188. return false;
  189. }
  190. }