Action.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. /**
  8. * Class Action.
  9. *
  10. * @method string href
  11. */
  12. abstract class Action implements Renderable
  13. {
  14. use HasHtmlAttributes, HasActionHandler;
  15. /**
  16. * @var array
  17. */
  18. protected static $selectors = [];
  19. /**
  20. * @var array|string
  21. */
  22. protected $primaryKey;
  23. /**
  24. * @var string
  25. */
  26. protected $title;
  27. /**
  28. * @var string
  29. */
  30. protected $selector;
  31. /**
  32. * @var string
  33. */
  34. public $selectorPrefix = '.admin-action-';
  35. /**
  36. * @var string
  37. */
  38. protected $method = 'POST';
  39. /**
  40. * @var string
  41. */
  42. protected $event = 'click';
  43. /**
  44. * @var bool
  45. */
  46. protected $disabled = false;
  47. /**
  48. * @var bool
  49. */
  50. protected $usingHandler = true;
  51. /**
  52. * @var array
  53. */
  54. protected $htmlClasses = [];
  55. /**
  56. * Action constructor.
  57. *
  58. * @param string $title
  59. */
  60. public function __construct($title = null)
  61. {
  62. if ($title) {
  63. $this->title = $title;
  64. }
  65. }
  66. /**
  67. * 是否禁用动作.
  68. *
  69. * @param bool $disable
  70. *
  71. * @return $this
  72. */
  73. public function disable(bool $disable = true)
  74. {
  75. $this->disabled = $disable;
  76. return $this;
  77. }
  78. /**
  79. * @return bool
  80. */
  81. public function allowed()
  82. {
  83. return ! $this->disabled;
  84. }
  85. /**
  86. * Get primary key value of action.
  87. *
  88. * @return array|string
  89. */
  90. public function getKey()
  91. {
  92. return $this->primaryKey;
  93. }
  94. /**
  95. * 设置主键.
  96. *
  97. * @param mixed $key
  98. *
  99. * @return void
  100. */
  101. public function setKey($key)
  102. {
  103. $this->primaryKey = $key;
  104. }
  105. /**
  106. * @return string
  107. */
  108. protected function getElementClass()
  109. {
  110. return ltrim($this->selector(), '.');
  111. }
  112. /**
  113. * 获取动作标题.
  114. *
  115. * @return string
  116. */
  117. public function title()
  118. {
  119. return $this->title;
  120. }
  121. /**
  122. * @return mixed|string
  123. */
  124. public function selector()
  125. {
  126. if (is_null($this->selector)) {
  127. return $this->makeSelector($this->selectorPrefix);
  128. }
  129. return $this->selector;
  130. }
  131. /**
  132. * @param string $prefix
  133. * @param string $class
  134. *
  135. * @return string
  136. */
  137. public function makeSelector($prefix, $class = null)
  138. {
  139. $class = $class ?: static::class;
  140. if (! isset(static::$selectors[$class])) {
  141. static::$selectors[$class] = uniqid($prefix);
  142. }
  143. return static::$selectors[$class];
  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 setupHandler()
  177. {
  178. if (
  179. ! $this->usingHandler
  180. || ! method_exists($this, 'handle')
  181. ) {
  182. return;
  183. }
  184. if ($confirm = $this->confirm()) {
  185. $this->setHtmlAttribute('data-confirm', json_encode((array) $confirm));
  186. }
  187. $this->addHandlerScript();
  188. }
  189. /**
  190. * @return string
  191. */
  192. public function render()
  193. {
  194. if (! $this->allowed()) {
  195. return '';
  196. }
  197. $this->setupHandler();
  198. $this->setupHtmlAttributes();
  199. if ($script = $this->script()) {
  200. Admin::script($script);
  201. }
  202. return $this->html();
  203. }
  204. /**
  205. * @return string
  206. */
  207. protected function formatHtmlClasses()
  208. {
  209. return implode(' ', array_unique($this->htmlClasses));
  210. }
  211. /**
  212. * @return void
  213. */
  214. protected function setupHtmlAttributes()
  215. {
  216. $this->addHtmlClass($this->getElementClass());
  217. $attributes = [
  218. 'class' => $this->formatHtmlClasses(),
  219. ];
  220. if (method_exists($this, 'href') && ($href = $this->href())) {
  221. $this->usingHandler = false;
  222. $attributes['href'] = $href;
  223. }
  224. $this->defaultHtmlAttribute('style', 'cursor: pointer');
  225. $this->setHtmlAttribute($attributes);
  226. }
  227. /**
  228. * @return string
  229. */
  230. public function __toString()
  231. {
  232. return Helper::render($this->render());
  233. }
  234. /**
  235. * @param mixed ...$params
  236. *
  237. * @return $this
  238. */
  239. public static function make(...$params)
  240. {
  241. return new static(...$params);
  242. }
  243. }