Tools.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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->tools->push($tool);
  56. return $this;
  57. }
  58. /**
  59. * Prepend a tool.
  60. *
  61. * @param AbstractTool|string|\Closure|Renderable|Htmlable $tool
  62. *
  63. * @return $this
  64. */
  65. public function prepend($tool)
  66. {
  67. $this->tools->prepend($tool);
  68. return $this;
  69. }
  70. public function has()
  71. {
  72. return ! $this->tools->isEmpty();
  73. }
  74. /**
  75. * Disable filter button.
  76. *
  77. * @return void
  78. */
  79. public function disableFilterButton(bool $disable = true)
  80. {
  81. $this->tools = $this->tools->map(function ($tool) use ($disable) {
  82. if ($tool instanceof FilterButton) {
  83. return $tool->disable($disable);
  84. }
  85. return $tool;
  86. });
  87. }
  88. /**
  89. * Disable refresh button.
  90. *
  91. * @return void
  92. */
  93. public function disableRefreshButton(bool $disable = true)
  94. {
  95. $this->tools = $this->tools->map(function ($tool) use ($disable) {
  96. if ($tool instanceof RefreshButton) {
  97. return $tool->disable($disable);
  98. }
  99. return $tool;
  100. });
  101. }
  102. /**
  103. * Disable batch actions.
  104. *
  105. * @return void
  106. */
  107. public function disableBatchActions(bool $disable = true)
  108. {
  109. $this->tools = $this->tools->map(function ($tool) use ($disable) {
  110. if ($tool instanceof BatchActions) {
  111. return $tool->disable($disable);
  112. }
  113. return $tool;
  114. });
  115. }
  116. /**
  117. * @param \Closure $closure
  118. */
  119. public function batch(\Closure $closure)
  120. {
  121. call_user_func($closure, $this->tools->first(function ($tool) {
  122. return $tool instanceof BatchActions;
  123. }));
  124. }
  125. /**
  126. * Render header tools bar.
  127. *
  128. * @return string
  129. */
  130. public function render()
  131. {
  132. return $this->tools->map(function ($tool) {
  133. if ($tool instanceof AbstractTool) {
  134. if (! $tool->allowed()) {
  135. return '';
  136. }
  137. return $tool->setGrid($this->grid)->render();
  138. }
  139. return Helper::render($tool);
  140. })->implode(' ');
  141. }
  142. }