Action.php 4.8 KB

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