Tools.php 3.8 KB

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