Tools.php 3.2 KB

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