123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- namespace Dcat\Admin\Grid;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Grid\Tools\AbstractTool;
- use Dcat\Admin\Grid\Tools\BatchActions;
- use Dcat\Admin\Grid\Tools\FilterButton;
- use Dcat\Admin\Grid\Tools\RefreshButton;
- use Dcat\Admin\Support\Helper;
- use Illuminate\Contracts\Support\Htmlable;
- use Illuminate\Contracts\Support\Renderable;
- use Illuminate\Support\Collection;
- class Tools implements Renderable
- {
- /**
- * Parent grid.
- *
- * @var Grid
- */
- protected $grid;
- /**
- * Collection of tools.
- *
- * @var Collection
- */
- protected $tools;
- /**
- * Create a new Tools instance.
- *
- * @param Grid $grid
- */
- public function __construct(Grid $grid)
- {
- $this->grid = $grid;
- $this->tools = new Collection();
- $this->appendDefaultTools();
- }
- /**
- * Append default tools.
- */
- protected function appendDefaultTools()
- {
- $this->append(new BatchActions())
- ->append(new RefreshButton())
- ->append(new FilterButton());
- }
- /**
- * Append tools.
- *
- * @param AbstractTool|string|\Closure|Renderable|Htmlable $tool
- *
- * @return $this
- */
- public function append($tool)
- {
- $this->prepareAction($tool);
- $this->tools->push($tool);
- return $this;
- }
- /**
- * Prepend a tool.
- *
- * @param AbstractTool|string|\Closure|Renderable|Htmlable $tool
- *
- * @return $this
- */
- public function prepend($tool)
- {
- $this->prepareAction($tool);
- $this->tools->prepend($tool);
- return $this;
- }
- /**
- * @param mixed $tool
- *
- * @return void
- */
- protected function prepareAction($tool)
- {
- if ($tool instanceof GridAction) {
- $tool->setGrid($this->grid);
- }
- }
- /**
- * @return bool
- */
- public function has()
- {
- return ! $this->tools->isEmpty();
- }
- /**
- * Disable filter button.
- *
- * @return void
- */
- public function disableFilterButton(bool $disable = true)
- {
- $this->tools = $this->tools->map(function ($tool) use ($disable) {
- if ($tool instanceof FilterButton) {
- return $tool->disable($disable);
- }
- return $tool;
- });
- }
- /**
- * Disable refresh button.
- *
- * @return void
- */
- public function disableRefreshButton(bool $disable = true)
- {
- $this->tools = $this->tools->map(function ($tool) use ($disable) {
- if ($tool instanceof RefreshButton) {
- return $tool->disable($disable);
- }
- return $tool;
- });
- }
- /**
- * Disable batch actions.
- *
- * @return void
- */
- public function disableBatchActions(bool $disable = true)
- {
- $this->tools = $this->tools->map(function ($tool) use ($disable) {
- if ($tool instanceof BatchActions) {
- return $tool->disable($disable);
- }
- return $tool;
- });
- }
- /**
- * @param \Closure|BatchAction|BatchAction[] $value
- */
- public function batch($value)
- {
- /* @var BatchActions $batchActions */
- $batchActions = $this->tools->first(function ($tool) {
- return $tool instanceof BatchActions;
- });
- if ($value instanceof \Closure) {
- $value($batchActions);
- return;
- }
- if (! is_array($value)) {
- $value = [$value];
- }
- foreach ($value as $action) {
- $batchActions->add($action);
- }
- }
- /**
- * Render header tools bar.
- *
- * @return string
- */
- public function render()
- {
- return $this->tools->map([Helper::class, 'render'])->implode(' ');
- }
- }
|