123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- <?php
- namespace Dcat\Admin\Widgets;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Contracts\LazyRenderable;
- use Dcat\Admin\Grid\LazyRenderable as LazyGrid;
- use Dcat\Admin\Layout\Content;
- use Dcat\Admin\Support\Helper;
- use Dcat\Admin\Traits\HasHtmlAttributes;
- use Illuminate\Contracts\Support\Arrayable;
- use Illuminate\Contracts\Support\Renderable;
- use Illuminate\Support\Arr;
- /**
- * @method $this class(string $class, bool $append = false)
- * @method $this style(string $style, bool $append = true)
- * @method $this id(string $id = null)
- */
- abstract class Widget implements Renderable
- {
- use HasHtmlAttributes;
- /**
- * @var array
- */
- public static $css = [];
- /**
- * @var array
- */
- public static $js = [];
- /**
- * @var string
- */
- protected $view;
- /**
- * @var string
- */
- protected $script = '';
- /**
- * @var array
- */
- protected $variables = [];
- /**
- * @var array
- */
- protected $options = [];
- /**
- * @var bool
- */
- protected $runScript = true;
- /**
- * @param mixed ...$params
- *
- * @return static
- */
- public static function make(...$params)
- {
- return new static(...$params);
- }
- /**
- * 批量设置选项.
- *
- * @param array $options
- *
- * @return $this
- */
- public function options($options = [])
- {
- if ($options instanceof Arrayable) {
- $options = $options->toArray();
- }
- $this->options = array_merge($this->options, $options);
- return $this;
- }
- /**
- * 设置或获取配置选项.
- *
- * @param string $key
- * @param mixed $value
- *
- * @return $this
- */
- public function option($key, $value = null)
- {
- if ($value === null) {
- return Arr::get($this->options, $key);
- } else {
- Arr::set($this->options, $key, $value);
- }
- return $this;
- }
- /**
- * 获取所有选项.
- *
- * @return array
- */
- public function getOptions()
- {
- return $this->options;
- }
- /**
- * 获取视图变量.
- *
- * @return array
- */
- public function variables()
- {
- return array_merge([
- 'attributes' => $this->formatHtmlAttributes(),
- 'options' => $this->options,
- ], $this->variables);
- }
- /**
- * 设置视图变量.
- *
- * @param string|array $key
- * @param mixed $value
- *
- * @return $this
- */
- public function with($key, $value = null)
- {
- if (is_array($key)) {
- $this->variables = array_merge($this->variables, $key);
- } else {
- $this->variables[$key] = $value;
- }
- return $this;
- }
- /**
- * 收集静态资源.
- */
- public static function requireAssets()
- {
- static::$js && Admin::js(static::$js);
- static::$css && Admin::css(static::$css);
- }
- /**
- * 运行JS.
- */
- protected function withScript()
- {
- if ($this->runScript && $this->script) {
- Admin::script($this->script);
- }
- }
- /**
- * @param $value
- *
- * @return string
- */
- protected function toString($value)
- {
- return Helper::render($value);
- }
- /**
- * @return string
- */
- public function render()
- {
- static::requireAssets();
- $html = $this->html();
- $this->withScript();
- return $html;
- }
- /**
- * 获取元素选择器.
- *
- * @return string
- */
- public function getElementSelector()
- {
- return '#'.$this->id();
- }
- /**
- * 渲染HTML.
- *
- * @return string
- */
- public function html()
- {
- if (! $this->view) {
- return;
- }
- $result = Admin::resolveHtml(view($this->view, $this->variables()), ['runScript' => $this->runScript]);
- $this->script = $result['script'];
- return $result['html'];
- }
- /**
- * 自动调用render方法.
- *
- * @return void
- */
- protected function autoRender()
- {
- Content::composed(function () {
- if ($results = Helper::render($this->render())) {
- Admin::html($results);
- }
- });
- }
- /**
- * 设置模板.
- *
- * @param string $view
- */
- public function view($view)
- {
- $this->view = $view;
- }
- /**
- * 设置是否执行JS代码.
- *
- * @param bool $run
- *
- * @return $this
- */
- public function runScript(bool $run = true)
- {
- $this->runScript = $run;
- return $this;
- }
- /**
- * @return string
- */
- public function getScript()
- {
- return $this->script;
- }
- /**
- * @param mixed $content
- *
- * @return Lazy|LazyTable|mixed
- */
- protected function formatRenderable($content)
- {
- if ($content instanceof LazyGrid) {
- return LazyTable::make($content);
- }
- if ($content instanceof LazyRenderable) {
- return Lazy::make($content);
- }
- return $content;
- }
- /**
- * @param $method
- * @param $parameters
- *
- * @return $this
- */
- public function __call($method, $parameters)
- {
- if ($method === 'style' || $method === 'class') {
- $value = $parameters[0] ?? null;
- $append = $parameters[1] ?? ($method === 'class' ? false : true);
- if ($append) {
- $original = $this->htmlAttributes[$method] ?? '';
- $de = $method === 'style' ? ';' : ' ';
- $value = $original.$de.$value;
- }
- return $this->setHtmlAttribute($method, $value);
- }
- // 获取属性
- if (count($parameters) === 0 || $parameters[0] === null) {
- return $this->getHtmlAttribute($method);
- }
- // 设置属性
- $this->setHtmlAttribute($method, $parameters[0]);
- return $this;
- }
- /**
- * @param string $key
- *
- * @return mixed
- */
- public function __get($key)
- {
- return $this->htmlAttributes[$key] ?? null;
- }
- /**
- * @param string $key
- * @param mixed $value
- *
- * @return void
- */
- public function __set($key, $value)
- {
- $this->htmlAttributes[$key] = $value;
- }
- /**
- * @return mixed
- */
- public function __toString()
- {
- return $this->render();
- }
- }
|