Widget.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Layout\Content;
  5. use Dcat\Admin\Support\Helper;
  6. use Dcat\Admin\Traits\HasHtmlAttributes;
  7. use Illuminate\Contracts\Support\Arrayable;
  8. use Illuminate\Contracts\Support\Renderable;
  9. use Illuminate\Support\Arr;
  10. /**
  11. * @method $this class(string $class, bool $append = false)
  12. * @method $this style(string $style, bool $append = true)
  13. * @method $this id(string $id = null)
  14. */
  15. abstract class Widget implements Renderable
  16. {
  17. use HasHtmlAttributes;
  18. /**
  19. * @var array
  20. */
  21. public static $css = [];
  22. /**
  23. * @var array
  24. */
  25. public static $js = [];
  26. /**
  27. * @var string
  28. */
  29. protected $view;
  30. /**
  31. * @var string
  32. */
  33. protected $script = '';
  34. /**
  35. * @var array
  36. */
  37. protected $variables = [];
  38. /**
  39. * @var array
  40. */
  41. protected $options = [];
  42. /**
  43. * @var bool
  44. */
  45. protected $runScript = true;
  46. /**
  47. * @param mixed ...$params
  48. *
  49. * @return static
  50. */
  51. public static function make(...$params)
  52. {
  53. return new static(...$params);
  54. }
  55. /**
  56. * 批量设置选项.
  57. *
  58. * @param array $options
  59. *
  60. * @return $this
  61. */
  62. public function options($options = [])
  63. {
  64. if ($options instanceof Arrayable) {
  65. $options = $options->toArray();
  66. }
  67. $this->options = array_merge($this->options, $options);
  68. return $this;
  69. }
  70. /**
  71. * 设置或获取配置选项.
  72. *
  73. * @param string $key
  74. * @param mixed $value
  75. *
  76. * @return $this
  77. */
  78. public function option($key, $value = null)
  79. {
  80. if ($value === null) {
  81. return Arr::get($this->options, $key);
  82. } else {
  83. Arr::set($this->options, $key, $value);
  84. }
  85. return $this;
  86. }
  87. /**
  88. * 获取所有选项.
  89. *
  90. * @return array
  91. */
  92. public function getOptions()
  93. {
  94. return $this->options;
  95. }
  96. /**
  97. * 获取视图变量.
  98. *
  99. * @return array
  100. */
  101. public function variables()
  102. {
  103. return array_merge($this->variables, [
  104. 'attributes' => $this->formatHtmlAttributes(),
  105. 'options' => $this->options,
  106. ]);
  107. }
  108. /**
  109. * 设置视图变量.
  110. *
  111. * @param string|array $key
  112. * @param mixed $value
  113. *
  114. * @return $this
  115. */
  116. public function with($key, $value = null)
  117. {
  118. if (is_array($key)) {
  119. $this->variables = array_merge($this->variables, $key);
  120. } else {
  121. $this->variables[$key] = $value;
  122. }
  123. return $this;
  124. }
  125. /**
  126. * 收集静态资源.
  127. */
  128. public static function collectAssets()
  129. {
  130. static::$js && Admin::js(static::$js);
  131. static::$css && Admin::css(static::$css);
  132. }
  133. /**
  134. * 运行JS.
  135. */
  136. protected function withScript()
  137. {
  138. if ($this->runScript && $this->script) {
  139. Admin::script($this->script);
  140. }
  141. }
  142. /**
  143. * @param $value
  144. *
  145. * @return string
  146. */
  147. protected function toString($value)
  148. {
  149. return Helper::render($value);
  150. }
  151. /**
  152. * @return string
  153. */
  154. public function render()
  155. {
  156. static::collectAssets();
  157. $html = $this->html();
  158. $this->withScript();
  159. return $html;
  160. }
  161. /**
  162. * @return string
  163. */
  164. public function html()
  165. {
  166. if (! $this->view) {
  167. return;
  168. }
  169. return view($this->view, $this->variables())->render();
  170. }
  171. /**
  172. * 自动调用render方法.
  173. *
  174. * @return void
  175. */
  176. protected function autoRender()
  177. {
  178. Content::composed(function () {
  179. if ($results = Helper::render($this->render())) {
  180. Admin::html($results);
  181. }
  182. });
  183. }
  184. /**
  185. * 设置模板.
  186. *
  187. * @param string $view
  188. */
  189. public function view($view)
  190. {
  191. $this->view = $view;
  192. }
  193. /**
  194. * 设置是否执行JS代码.
  195. *
  196. * @param bool $run
  197. *
  198. * @return $this
  199. */
  200. public function runScript(bool $run = true)
  201. {
  202. $this->runScript = $run;
  203. return $this;
  204. }
  205. /**
  206. * @return string
  207. */
  208. public function getScript()
  209. {
  210. return $this->script;
  211. }
  212. /**
  213. * @param $method
  214. * @param $parameters
  215. *
  216. * @return $this
  217. */
  218. public function __call($method, $parameters)
  219. {
  220. if ($method === 'style' || $method === 'class') {
  221. $value = $parameters[0] ?? null;
  222. $append = $parameters[1] ?? ($method === 'class' ? false : true);
  223. if ($append) {
  224. $original = $this->htmlAttributes[$method] ?? '';
  225. $de = $method === 'style' ? ';' : ' ';
  226. $value = $original.$de.$value;
  227. }
  228. return $this->setHtmlAttribute($method, $value);
  229. }
  230. // 获取属性
  231. if (count($parameters) === 0) {
  232. return $this->getHtmlAttribute($method);
  233. }
  234. // 设置属性
  235. $this->setHtmlAttribute($method, $parameters[0]);
  236. return $this;
  237. }
  238. /**
  239. * @param string $key
  240. *
  241. * @return mixed
  242. */
  243. public function __get($key)
  244. {
  245. return $this->htmlAttributes[$key] ?? null;
  246. }
  247. /**
  248. * @param string $key
  249. * @param mixed $value
  250. *
  251. * @return void
  252. */
  253. public function __set($key, $value)
  254. {
  255. $this->htmlAttributes[$key] = $value;
  256. }
  257. /**
  258. * @return mixed
  259. */
  260. public function __toString()
  261. {
  262. return $this->render();
  263. }
  264. }