123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- <?php
- namespace Dcat\Admin\Grid\Displayers;
- use Dcat\Admin\Actions\Action;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid\Actions\Delete;
- use Dcat\Admin\Grid\Actions\Edit;
- use Dcat\Admin\Grid\Actions\QuickEdit;
- use Dcat\Admin\Grid\Actions\Show;
- 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 $resolvedDialog;
- /**
- * @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);
- }
- }
- public function view(bool $value = true)
- {
- return $this->setAction('view', $value);
- }
- /**
- * Disable view action.
- *
- * @param bool $disable
- *
- * @return $this
- */
- public function disableView(bool $disable = true)
- {
- return $this->setAction('view', ! $disable);
- }
- public function delete(bool $value = true)
- {
- return $this->setAction('delete', $value);
- }
- /**
- * Disable delete.
- *
- * @param bool $disable
- *
- * @return $this.
- */
- public function disableDelete(bool $disable = true)
- {
- return $this->setAction('delete', ! $disable);
- }
- public function edit(bool $value = true)
- {
- return $this->setAction('edit', $value);
- }
- /**
- * Disable edit.
- *
- * @param bool $disable
- *
- * @return $this.
- */
- public function disableEdit(bool $disable = true)
- {
- return $this->setAction('edit', ! $disable);
- }
- public function quickEdit(bool $value = true)
- {
- return $this->setAction('quickEdit', $value);
- }
- /**
- * Disable quick edit.
- *
- * @param bool $disable
- *
- * @return $this.
- */
- public function disableQuickEdit(bool $disable = true)
- {
- return $this->setAction('quickEdit', ! $disable);
- }
- /**
- * @param string $key
- * @param bool $disable
- *
- * @return $this
- */
- protected function setAction(string $key, bool $value)
- {
- $this->actions[$key] = $value;
- 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 resetsetActions()
- {
- $this->view($this->grid->option('view_button'));
- $this->edit($this->grid->option('edit_button'));
- $this->quickEdit($this->grid->option('quick_edit_button'));
- $this->delete($this->grid->option('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->resetsetActions();
- $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()
- {
- $label = trans('admin.show');
- return Show::make(
- "<i title='{$label}' class=\"feather icon-eye grid-action-icon\"></i> "
- )
- ->setGrid($this->grid)
- ->setRow($this->row)
- ->render();
- }
- /**
- * Render edit action.
- *
- * @return string
- */
- protected function renderEdit()
- {
- $label = trans('admin.edit');
- return Edit::make(
- "<i title='{$label}' class=\"feather icon-edit-1 grid-action-icon\"></i> "
- )
- ->setGrid($this->grid)
- ->setRow($this->row)
- ->render();
- }
- /**
- * @return string
- */
- protected function renderQuickEdit()
- {
- if (! static::$resolvedDialog) {
- static::$resolvedDialog = 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()');
- }
- $label = trans('admin.quick_edit');
- return QuickEdit::make(
- "<i title='{$label}' class=\"feather icon-edit grid-action-icon\"></i> "
- )
- ->setGrid($this->grid)
- ->setRow($this->row)
- ->render();
- }
- /**
- * Render delete action.
- *
- * @return string
- */
- protected function renderDelete()
- {
- $label = trans('admin.delete');
- return Delete::make(
- "<i class=\"feather icon-trash grid-action-icon\" title='{$label}'></i> "
- )
- ->setGrid($this->grid)
- ->setRow($this->row)
- ->render();
- }
- }
|