Action.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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|array $class
  136. *
  137. * @return $this
  138. */
  139. public function addHtmlClass($class)
  140. {
  141. $this->htmlClasses = array_merge($this->htmlClasses, (array) $class);
  142. return $this;
  143. }
  144. /**
  145. * 需要执行的JS代码.
  146. *
  147. * @return string|void
  148. */
  149. protected function script()
  150. {
  151. }
  152. /**
  153. * @return string
  154. */
  155. protected function html()
  156. {
  157. $this->defaultHtmlAttribute('href', 'javascript:void(0)');
  158. return <<<HTML
  159. <a {$this->formatHtmlAttributes()}>{$this->title()}</a>
  160. HTML;
  161. }
  162. /**
  163. * @return void
  164. */
  165. protected function prepareHandler()
  166. {
  167. if (
  168. ! $this->allowHandler
  169. || ! method_exists($this, 'handle')
  170. ) {
  171. return;
  172. }
  173. $this->addHandlerScript();
  174. }
  175. /**
  176. * @return string
  177. */
  178. public function render()
  179. {
  180. if (! $this->allowed()) {
  181. return '';
  182. }
  183. $this->prepareHandler();
  184. $this->setUpHtmlAttributes();
  185. if ($script = $this->script()) {
  186. Admin::script($script);
  187. }
  188. return $this->html();
  189. }
  190. /**
  191. * @return string
  192. */
  193. protected function formatHtmlClasses()
  194. {
  195. return implode(' ', array_unique($this->htmlClasses));
  196. }
  197. /**
  198. * @return void
  199. */
  200. protected function setUpHtmlAttributes()
  201. {
  202. $this->addHtmlClass($this->getElementClass());
  203. $attributes = [
  204. 'class' => $this->formatHtmlClasses(),
  205. ];
  206. if (method_exists($this, 'href') && ($href = $this->href())) {
  207. $this->allowHandler = false;
  208. $attributes['href'] = $href;
  209. }
  210. $this->defaultHtmlAttribute('style', 'cursor: pointer');
  211. $this->setHtmlAttribute($attributes);
  212. }
  213. /**
  214. * @return string
  215. */
  216. public function __toString()
  217. {
  218. return Helper::render($this->render());
  219. }
  220. /**
  221. * @param mixed ...$params
  222. *
  223. * @return $this
  224. */
  225. public static function make(...$params)
  226. {
  227. return new static(...$params);
  228. }
  229. }