Widget.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. * @param mixed ...$params
  44. *
  45. * @return static
  46. */
  47. public static function make(...$params)
  48. {
  49. return new static(...$params);
  50. }
  51. /**
  52. * 批量设置选项.
  53. *
  54. * @param array $options
  55. *
  56. * @return $this
  57. */
  58. public function options($options = [])
  59. {
  60. if ($options instanceof Arrayable) {
  61. $options = $options->toArray();
  62. }
  63. $this->options = array_merge($this->options, $options);
  64. return $this;
  65. }
  66. /**
  67. * 设置或获取配置选项.
  68. *
  69. * @param string $key
  70. * @param mixed $value
  71. *
  72. * @return $this
  73. */
  74. public function option($key, $value = null)
  75. {
  76. if ($value === null) {
  77. return Arr::get($this->options, $key);
  78. } else {
  79. Arr::set($this->options, $key, $value);
  80. }
  81. return $this;
  82. }
  83. /**
  84. * 获取所有选项.
  85. *
  86. * @return array
  87. */
  88. public function getOptions()
  89. {
  90. return $this->options;
  91. }
  92. /**
  93. * 获取视图变量.
  94. *
  95. * @return array
  96. */
  97. public function variables()
  98. {
  99. return array_merge($this->variables, [
  100. 'attributes' => $this->formatHtmlAttributes(),
  101. 'options' => $this->options,
  102. ]);
  103. }
  104. /**
  105. * 设置视图变量.
  106. *
  107. * @param string|array $key
  108. * @param mixed $value
  109. *
  110. * @return $this
  111. */
  112. public function with($key, $value = null)
  113. {
  114. if (is_array($key)) {
  115. $this->variables = array_merge($this->variables, $key);
  116. } else {
  117. $this->variables[$key] = $value;
  118. }
  119. return $this;
  120. }
  121. /**
  122. * 收集静态资源.
  123. */
  124. protected function collectAssets()
  125. {
  126. $this->script && Admin::script($this->script);
  127. static::$js && Admin::js(static::$js);
  128. static::$css && Admin::css(static::$css);
  129. }
  130. /**
  131. * @param $value
  132. *
  133. * @return string
  134. */
  135. protected function toString($value)
  136. {
  137. return Helper::render($value);
  138. }
  139. /**
  140. * @return string
  141. */
  142. public function render()
  143. {
  144. $this->collectAssets();
  145. return $this->html();
  146. }
  147. /**
  148. * @return string
  149. */
  150. public function html()
  151. {
  152. return view($this->view, $this->variables())->render();
  153. }
  154. /**
  155. * 自动调用render方法.
  156. *
  157. * @return void
  158. */
  159. protected function autoRender()
  160. {
  161. Content::composed(function () {
  162. if ($results = Helper::render($this->render())) {
  163. Admin::html($results);
  164. }
  165. });
  166. }
  167. /**
  168. * 设置模板.
  169. *
  170. * @param string $view
  171. */
  172. public function view($view)
  173. {
  174. $this->view = $view;
  175. }
  176. /**
  177. * @return string
  178. */
  179. public function getScript()
  180. {
  181. return $this->script;
  182. }
  183. /**
  184. * @param $method
  185. * @param $parameters
  186. *
  187. * @return $this
  188. */
  189. public function __call($method, $parameters)
  190. {
  191. if ($method === 'style' || $method === 'class') {
  192. $value = $parameters[0] ?? null;
  193. $append = $parameters[1] ?? ($method === 'class' ? false : true);
  194. if ($append) {
  195. $original = $this->htmlAttributes[$method] ?? '';
  196. $de = $method === 'style' ? ';' : ' ';
  197. $value = $original.$de.$value;
  198. }
  199. return $this->setHtmlAttribute($method, $value);
  200. }
  201. // 获取属性
  202. if (count($parameters) === 0) {
  203. return $this->getHtmlAttribute($method);
  204. }
  205. // 设置属性
  206. $this->setHtmlAttribute($method, $parameters[0]);
  207. return $this;
  208. }
  209. /**
  210. * @param string $key
  211. *
  212. * @return mixed
  213. */
  214. public function __get($key)
  215. {
  216. return $this->htmlAttributes[$key] ?? null;
  217. }
  218. /**
  219. * @param string $key
  220. * @param mixed $value
  221. *
  222. * @return void
  223. */
  224. public function __set($key, $value)
  225. {
  226. $this->htmlAttributes[$key] = $value;
  227. }
  228. /**
  229. * @return mixed
  230. */
  231. public function __toString()
  232. {
  233. return $this->render();
  234. }
  235. }