123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- <?php
- namespace Dcat\Admin\Actions;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Support\Helper;
- use Dcat\Admin\Traits\HasHtmlAttributes;
- use Illuminate\Contracts\Support\Renderable;
- use Illuminate\Support\Str;
- /**
- * Class Action.
- *
- * @method string href
- */
- abstract class Action implements Renderable
- {
- use HasHtmlAttributes;
- use HasActionHandler;
- /**
- * @var array|string
- */
- protected $primaryKey;
- /**
- * @var string
- */
- protected $title;
- /**
- * @var string
- */
- protected $selector;
- /**
- * @var string
- */
- protected $method = 'POST';
- /**
- * @var string
- */
- protected $event = 'click';
- /**
- * @var bool
- */
- protected $disabled = false;
- /**
- * @var bool
- */
- protected $allowHandler = true;
- /**
- * @var array
- */
- protected $htmlClasses = [];
- /**
- * Action constructor.
- *
- * @param string $title
- */
- public function __construct($title = null)
- {
- if ($title) {
- $this->title = $title;
- }
- }
- /**
- * 是否禁用动作.
- *
- * @param bool $disable
- *
- * @return $this
- */
- public function disable(bool $disable = true)
- {
- $this->disabled = $disable;
- return $this;
- }
- /**
- * @return bool
- */
- public function allowed()
- {
- return ! $this->disabled;
- }
- /**
- * Get primary key value of action.
- *
- * @return array|string
- */
- public function getKey()
- {
- return $this->primaryKey;
- }
- /**
- * 设置主键.
- *
- * @param mixed $key
- *
- * @return $this
- */
- public function setKey($key)
- {
- $this->primaryKey = $key;
- return $this;
- }
- /**
- * @return string
- */
- protected function getElementClass()
- {
- return ltrim($this->selector(), '.');
- }
- /**
- * 获取动作标题.
- *
- * @return string
- */
- public function title()
- {
- return $this->title;
- }
- /**
- * @return mixed|string
- */
- public function selector()
- {
- return $this->selector ?: ($this->selector = $this->makeSelector());
- }
- /**
- * 生成选择器.
- *
- * @param string $prefix
- *
- * @return string
- */
- public function makeSelector()
- {
- return '.act-'.Str::random();
- }
- /**
- * @param string|array $class
- *
- * @return $this
- */
- public function addHtmlClass($class)
- {
- $this->htmlClasses = array_merge($this->htmlClasses, (array) $class);
- return $this;
- }
- /**
- * 需要执行的JS代码.
- *
- * @return string|void
- */
- protected function script()
- {
- }
- /**
- * @return string
- */
- protected function html()
- {
- $this->defaultHtmlAttribute('href', 'javascript:void(0)');
- return <<<HTML
- <a {$this->formatHtmlAttributes()}>{$this->title()}</a>
- HTML;
- }
- /**
- * @return void
- */
- protected function prepareHandler()
- {
- if (
- ! $this->allowHandler
- || ! method_exists($this, 'handle')
- ) {
- return;
- }
- $this->addHandlerScript();
- }
- /**
- * @return string
- */
- public function render()
- {
- if (! $this->allowed()) {
- return '';
- }
- $this->prepareHandler();
- $this->setUpHtmlAttributes();
- if ($script = $this->script()) {
- Admin::script($script);
- }
- return $this->html();
- }
- /**
- * @return string
- */
- protected function formatHtmlClasses()
- {
- return implode(' ', array_unique($this->htmlClasses));
- }
- /**
- * @return void
- */
- protected function setUpHtmlAttributes()
- {
- $this->addHtmlClass($this->getElementClass());
- $attributes = [
- 'class' => $this->formatHtmlClasses(),
- ];
- if (method_exists($this, 'href') && ($href = $this->href())) {
- $this->allowHandler = false;
- $attributes['href'] = $href;
- }
- $this->defaultHtmlAttribute('style', 'cursor: pointer');
- $this->setHtmlAttribute($attributes);
- }
- /**
- * @return string
- */
- public function __toString()
- {
- return Helper::render($this->render());
- }
- /**
- * @param mixed ...$params
- *
- * @return $this
- */
- public static function make(...$params)
- {
- return new static(...$params);
- }
- }
|