Action.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. namespace Dcat\Admin\Actions;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Support\Helper;
  5. use Dcat\Admin\Traits\HasHtmlAttributes;
  6. use Illuminate\Contracts\Support\Renderable;
  7. use Illuminate\Support\Str;
  8. /**
  9. * Class Action.
  10. *
  11. * @method string href
  12. */
  13. abstract class Action implements Renderable
  14. {
  15. use HasHtmlAttributes;
  16. use HasActionHandler;
  17. /**
  18. * @var array
  19. */
  20. protected static $selectors = [];
  21. /**
  22. * @var array|string
  23. */
  24. protected $primaryKey;
  25. /**
  26. * @var string
  27. */
  28. protected $title;
  29. /**
  30. * @var string
  31. */
  32. protected $selector;
  33. /**
  34. * @var string
  35. */
  36. public $selectorPrefix = '.admin-action-';
  37. /**
  38. * @var string
  39. */
  40. protected $method = 'POST';
  41. /**
  42. * @var string
  43. */
  44. protected $event = 'click';
  45. /**
  46. * @var bool
  47. */
  48. protected $disabled = false;
  49. /**
  50. * @var bool
  51. */
  52. protected $allowHandler = true;
  53. /**
  54. * @var array
  55. */
  56. protected $htmlClasses = [];
  57. /**
  58. * Action constructor.
  59. *
  60. * @param string $title
  61. */
  62. public function __construct($title = null)
  63. {
  64. if ($title) {
  65. $this->title = $title;
  66. }
  67. }
  68. /**
  69. * 是否禁用动作.
  70. *
  71. * @param bool $disable
  72. *
  73. * @return $this
  74. */
  75. public function disable(bool $disable = true)
  76. {
  77. $this->disabled = $disable;
  78. return $this;
  79. }
  80. /**
  81. * @return bool
  82. */
  83. public function allowed()
  84. {
  85. return ! $this->disabled;
  86. }
  87. /**
  88. * Get primary key value of action.
  89. *
  90. * @return array|string
  91. */
  92. public function getKey()
  93. {
  94. return $this->primaryKey;
  95. }
  96. /**
  97. * 设置主键.
  98. *
  99. * @param mixed $key
  100. *
  101. * @return $this
  102. */
  103. public function setKey($key)
  104. {
  105. $this->primaryKey = $key;
  106. return $this;
  107. }
  108. /**
  109. * @return string
  110. */
  111. protected function getElementClass()
  112. {
  113. return ltrim($this->selector(), '.');
  114. }
  115. /**
  116. * 获取动作标题.
  117. *
  118. * @return string
  119. */
  120. public function title()
  121. {
  122. return $this->title;
  123. }
  124. /**
  125. * @return mixed|string
  126. */
  127. public function selector()
  128. {
  129. if (is_null($this->selector)) {
  130. return $this->makeSelector($this->selectorPrefix);
  131. }
  132. return $this->selector;
  133. }
  134. /**
  135. * 选择器.
  136. *
  137. * @param string $prefix
  138. * @param string $class
  139. *
  140. * @return string
  141. */
  142. public function makeSelector($prefix, $class = null)
  143. {
  144. $key = $this->getSelectorKey($prefix, $class);
  145. if (! isset(static::$selectors[$key])) {
  146. static::$selectors[$key] = $prefix.Str::random(8);
  147. }
  148. return static::$selectors[$key];
  149. }
  150. /**
  151. * @param string $prefix
  152. * @param string $class
  153. *
  154. * @return string
  155. */
  156. public function getSelectorKey($prefix, $class)
  157. {
  158. return $prefix.'-'.($class ?: static::class)
  159. .md5($this->normalizeConfirmData().$this->normalizeParameters());
  160. }
  161. /**
  162. * @param string|array $class
  163. *
  164. * @return $this
  165. */
  166. public function addHtmlClass($class)
  167. {
  168. $this->htmlClasses = array_merge($this->htmlClasses, (array) $class);
  169. return $this;
  170. }
  171. /**
  172. * 需要执行的JS代码.
  173. *
  174. * @return string|void
  175. */
  176. protected function script()
  177. {
  178. }
  179. /**
  180. * @return string
  181. */
  182. protected function html()
  183. {
  184. $this->defaultHtmlAttribute('href', 'javascript:void(0)');
  185. return <<<HTML
  186. <a {$this->formatHtmlAttributes()}>{$this->title()}</a>
  187. HTML;
  188. }
  189. /**
  190. * @return void
  191. */
  192. protected function prepareHandler()
  193. {
  194. if (
  195. ! $this->allowHandler
  196. || ! method_exists($this, 'handle')
  197. ) {
  198. return;
  199. }
  200. $this->addHandlerScript();
  201. }
  202. /**
  203. * @return string
  204. */
  205. public function render()
  206. {
  207. if (! $this->allowed()) {
  208. return '';
  209. }
  210. $this->prepareHandler();
  211. $this->setUpHtmlAttributes();
  212. if ($script = $this->script()) {
  213. Admin::script($script);
  214. }
  215. return $this->html();
  216. }
  217. /**
  218. * @return string
  219. */
  220. protected function formatHtmlClasses()
  221. {
  222. return implode(' ', array_unique($this->htmlClasses));
  223. }
  224. /**
  225. * @return void
  226. */
  227. protected function setUpHtmlAttributes()
  228. {
  229. $this->addHtmlClass($this->getElementClass());
  230. $attributes = [
  231. 'class' => $this->formatHtmlClasses(),
  232. ];
  233. if (method_exists($this, 'href') && ($href = $this->href())) {
  234. $this->allowHandler = false;
  235. $attributes['href'] = $href;
  236. }
  237. $this->defaultHtmlAttribute('style', 'cursor: pointer');
  238. $this->setHtmlAttribute($attributes);
  239. }
  240. /**
  241. * @return string
  242. */
  243. public function __toString()
  244. {
  245. return Helper::render($this->render());
  246. }
  247. /**
  248. * @param mixed ...$params
  249. *
  250. * @return $this
  251. */
  252. public static function make(...$params)
  253. {
  254. return new static(...$params);
  255. }
  256. }