123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- <?php
- namespace Dcat\Admin\Grid\Displayers;
- use Dcat\Admin\Actions\Action;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid\RowAction;
- use Dcat\Admin\Support\Helper;
- use Illuminate\Contracts\Support\Htmlable;
- use Illuminate\Contracts\Support\Renderable;
- class Actions extends AbstractDisplayer
- {
- protected static $resolvedWindow;
- /**
- * @var array
- */
- protected $appends = [];
- /**
- * @var array
- */
- protected $prepends = [];
- /**
- * Default actions.
- *
- * @var array
- */
- protected $actions = [
- 'view' => true,
- 'edit' => true,
- 'quickEdit' => false,
- 'delete' => true,
- ];
- /**
- * @var string
- */
- protected $resource;
- /**
- * Append a action.
- *
- * @param string|Renderable|Action|Htmlable $action
- *
- * @return $this
- */
- public function append($action)
- {
- $this->prepareAction($action);
- array_push($this->appends, $action);
- return $this;
- }
- /**
- * Prepend a action.
- *
- * @param string|Renderable|Action|Htmlable $action
- *
- * @return $this
- */
- public function prepend($action)
- {
- $this->prepareAction($action);
- array_unshift($this->prepends, $action);
- return $this;
- }
- /**
- * @param mixed $action
- *
- * @return void
- */
- protected function prepareAction(&$action)
- {
- if ($action instanceof RowAction) {
- $action->setGrid($this->grid)
- ->setColumn($this->column)
- ->setRow($this->row);
- }
- }
- /**
- * Disable view action.
- *
- * @param bool $disable
- *
- * @return $this
- */
- public function disableView(bool $disable = true)
- {
- return $this->disableDefaultAction('view', $disable);
- }
- /**
- * Disable delete.
- *
- * @param bool $disable
- *
- * @return $this.
- */
- public function disableDelete(bool $disable = true)
- {
- return $this->disableDefaultAction('delete', $disable);
- }
- /**
- * Disable edit.
- *
- * @param bool $disable
- *
- * @return $this.
- */
- public function disableEdit(bool $disable = true)
- {
- return $this->disableDefaultAction('edit', $disable);
- }
- /**
- * Disable quick edit.
- *
- * @param bool $disable
- *
- * @return $this.
- */
- public function disableQuickEdit(bool $disable = true)
- {
- return $this->disableDefaultAction('quickEdit', $disable);
- }
- /**
- * @param string $key
- * @param bool $disable
- *
- * @return $this
- */
- protected function disableDefaultAction(string $key, bool $disable)
- {
- $this->actions[$key] = ! $disable;
- return $this;
- }
- /**
- * Set resource of current resource.
- *
- * @param $resource
- *
- * @return $this
- */
- public function setResource($resource)
- {
- $this->resource = $resource;
- return $this;
- }
- /**
- * Get resource of current resource.
- *
- * @return string
- */
- public function resource()
- {
- return $this->resource ?: parent::resource();
- }
- /**
- * @return void
- */
- protected function resetDefaultActions()
- {
- $this->disableView(! $this->grid->option('show_view_button'));
- $this->disableEdit(! $this->grid->option('show_edit_button'));
- $this->disableQuickEdit(! $this->grid->option('show_quick_edit_button'));
- $this->disableDelete(! $this->grid->option('show_delete_button'));
- }
- /**
- * @param array $callbacks
- *
- * @return void
- */
- protected function call(array $callbacks = [])
- {
- foreach ($callbacks as $callback) {
- if ($callback instanceof \Closure) {
- $callback->call($this->row, $this);
- }
- }
- }
- /**
- * {@inheritdoc}
- */
- public function display(array $callbacks = [])
- {
- $this->resetDefaultActions();
- $this->call($callbacks);
- $toString = [Helper::class, 'render'];
- $prepends = array_map($toString, $this->prepends);
- $appends = array_map($toString, $this->appends);
- foreach ($this->actions as $action => $enable) {
- if ($enable) {
- $method = 'render'.ucfirst($action);
- array_push($prepends, $this->{$method}());
- }
- }
- return implode('', array_merge($prepends, $appends));
- }
- /**
- * Render view action.
- *
- * @return string
- */
- protected function renderView()
- {
- return <<<EOT
- <a href="{$this->resource()}/{$this->getKey()}">
- <i class="ti-eye grid-action-icon"></i>
- </a>
- EOT;
- }
- /**
- * Render edit action.
- *
- * @return string
- */
- protected function renderEdit()
- {
- return <<<EOT
- <a href="{$this->resource()}/{$this->getKey()}/edit">
- <i class="ti-pencil-alt grid-action-icon"></i>
- </a>
- EOT;
- }
- /**
- * @return string
- */
- protected function renderQuickEdit()
- {
- if (! static::$resolvedWindow) {
- static::$resolvedWindow = true;
- [$width, $height] = $this->grid->option('dialog_form_area');
- Form::dialog(trans('admin.edit'))
- ->click(".{$this->grid->getRowName()}-edit")
- ->dimensions($width, $height)
- ->success('Dcat.reload()');
- }
- return <<<EOF
- <a class="{$this->grid->getRowName()}-edit" data-url="{$this->resource()}/{$this->getKey()}/edit" href="javascript:void(0);">
- <i class=" fa fa-clone grid-action-icon"></i>
- </a>
- EOF;
- }
- /**
- * Render delete action.
- *
- * @return string
- */
- protected function renderDelete()
- {
- return <<<EOT
- <a href="javascript:void(0);" data-url="{$this->resource()}/{$this->getKey()}" data-action="delete">
- <i class="ti-trash grid-action-icon"></i>
- </a>
- EOT;
- }
- }
|