Widget.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. protected function collectAssets()
  108. {
  109. $this->script && Admin::script($this->script);
  110. static::$js && Admin::js(static::$js);
  111. static::$css && Admin::css(static::$css);
  112. }
  113. /**
  114. * @param $value
  115. *
  116. * @return string
  117. */
  118. protected function toString($value)
  119. {
  120. return Helper::render($value);
  121. }
  122. /**
  123. * @return string
  124. */
  125. public function render()
  126. {
  127. $this->collectAssets();
  128. return $this->html();
  129. }
  130. /**
  131. * @return string
  132. */
  133. public function html()
  134. {
  135. return view($this->view, $this->variables())->render();
  136. }
  137. /**
  138. * 自动调用render方法.
  139. *
  140. * @return void
  141. */
  142. protected function autoRender()
  143. {
  144. Content::composed(function () {
  145. if ($results = Helper::render($this->render())) {
  146. Admin::html($results);
  147. }
  148. });
  149. }
  150. /**
  151. * 设置模板.
  152. *
  153. * @param string $view
  154. */
  155. public function view($view)
  156. {
  157. $this->view = $view;
  158. }
  159. /**
  160. * @return string
  161. */
  162. public function getScript()
  163. {
  164. return $this->script;
  165. }
  166. /**
  167. * @param $method
  168. * @param $parameters
  169. *
  170. * @return $this
  171. */
  172. public function __call($method, $parameters)
  173. {
  174. if ($method === 'style' || $method === 'class') {
  175. $value = $parameters[0] ?? null;
  176. $append = $parameters[1] ?? ($method === 'class' ? false : true);
  177. if ($append) {
  178. $original = $this->htmlAttributes[$method] ?? '';
  179. $de = $method === 'style' ? ';' : ' ';
  180. $value = $original.$de.$value;
  181. }
  182. return $this->setHtmlAttribute($method, $value);
  183. }
  184. // 获取属性
  185. if (count($parameters) === 0) {
  186. return $this->getHtmlAttribute($method);
  187. }
  188. // 设置属性
  189. $this->setHtmlAttribute($method, $parameters[0]);
  190. return $this;
  191. }
  192. /**
  193. * @param string $key
  194. *
  195. * @return mixed
  196. */
  197. public function __get($key)
  198. {
  199. return $this->htmlAttributes[$key] ?? null;
  200. }
  201. /**
  202. * @param string $key
  203. * @param mixed $value
  204. *
  205. * @return void
  206. */
  207. public function __set($key, $value)
  208. {
  209. $this->htmlAttributes[$key] = $value;
  210. }
  211. /**
  212. * @return mixed
  213. */
  214. public function __toString()
  215. {
  216. return $this->render();
  217. }
  218. }