Action.php 4.9 KB

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