Tools.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace Dcat\Admin\Grid;
  3. use Dcat\Admin\Actions\Action;
  4. use Dcat\Admin\Grid;
  5. use Dcat\Admin\Grid\Tools\AbstractTool;
  6. use Dcat\Admin\Grid\Tools\BatchActions;
  7. use Dcat\Admin\Grid\Tools\FilterButton;
  8. use Dcat\Admin\Grid\Tools\RefreshButton;
  9. use Dcat\Admin\Support\Helper;
  10. use Illuminate\Contracts\Support\Htmlable;
  11. use Illuminate\Contracts\Support\Renderable;
  12. use Illuminate\Support\Collection;
  13. class Tools implements Renderable
  14. {
  15. /**
  16. * Parent grid.
  17. *
  18. * @var Grid
  19. */
  20. protected $grid;
  21. /**
  22. * Collection of tools.
  23. *
  24. * @var Collection
  25. */
  26. protected $tools;
  27. /**
  28. * Create a new Tools instance.
  29. *
  30. * @param Grid $grid
  31. */
  32. public function __construct(Grid $grid)
  33. {
  34. $this->grid = $grid;
  35. $this->tools = new Collection();
  36. $this->appendDefaultTools();
  37. }
  38. /**
  39. * Append default tools.
  40. */
  41. protected function appendDefaultTools()
  42. {
  43. $this->append(new BatchActions())
  44. ->append(new RefreshButton())
  45. ->append(new FilterButton());
  46. }
  47. /**
  48. * Append tools.
  49. *
  50. * @param AbstractTool|string|\Closure|Renderable|Htmlable $tool
  51. *
  52. * @return $this
  53. */
  54. public function append($tool)
  55. {
  56. $this->prepareAction($tool);
  57. $this->tools->push($tool);
  58. return $this;
  59. }
  60. /**
  61. * Prepend a tool.
  62. *
  63. * @param AbstractTool|string|\Closure|Renderable|Htmlable $tool
  64. *
  65. * @return $this
  66. */
  67. public function prepend($tool)
  68. {
  69. $this->prepareAction($tool);
  70. $this->tools->prepend($tool);
  71. return $this;
  72. }
  73. /**
  74. * @param mixed $tool
  75. *
  76. * @return void
  77. */
  78. protected function prepareAction($tool)
  79. {
  80. if ($tool instanceof GridAction) {
  81. $tool->setGrid($this->grid);
  82. }
  83. }
  84. /**
  85. * @return bool
  86. */
  87. public function has()
  88. {
  89. return ! $this->tools->isEmpty();
  90. }
  91. /**
  92. * Disable filter button.
  93. *
  94. * @return void
  95. */
  96. public function disableFilterButton(bool $disable = true)
  97. {
  98. $this->tools = $this->tools->map(function ($tool) use ($disable) {
  99. if ($tool instanceof FilterButton) {
  100. return $tool->disable($disable);
  101. }
  102. return $tool;
  103. });
  104. }
  105. /**
  106. * Disable refresh button.
  107. *
  108. * @return void
  109. */
  110. public function disableRefreshButton(bool $disable = true)
  111. {
  112. $this->tools = $this->tools->map(function ($tool) use ($disable) {
  113. if ($tool instanceof RefreshButton) {
  114. return $tool->disable($disable);
  115. }
  116. return $tool;
  117. });
  118. }
  119. /**
  120. * Disable batch actions.
  121. *
  122. * @return void
  123. */
  124. public function disableBatchActions(bool $disable = true)
  125. {
  126. $this->tools = $this->tools->map(function ($tool) use ($disable) {
  127. if ($tool instanceof BatchActions) {
  128. return $tool->disable($disable);
  129. }
  130. return $tool;
  131. });
  132. }
  133. /**
  134. * @param \Closure|BatchAction|BatchAction[] $value
  135. */
  136. public function batch($value)
  137. {
  138. /* @var BatchActions $batchActions */
  139. $batchActions = $this->tools->first(function ($tool) {
  140. return $tool instanceof BatchActions;
  141. });
  142. if ($value instanceof \Closure) {
  143. $value($batchActions);
  144. return;
  145. }
  146. if (! is_array($value)) {
  147. $value = [$value];
  148. }
  149. foreach ($value as $action) {
  150. $batchActions->add($action);
  151. }
  152. }
  153. /**
  154. * Render header tools bar.
  155. *
  156. * @return string
  157. */
  158. public function render()
  159. {
  160. return $this->tools->map(function ($tool) {
  161. if ($tool instanceof Action && ! $tool->allowed()) {
  162. return;
  163. }
  164. return Helper::render($tool);
  165. })->implode(' ');
  166. }
  167. }