Bläddra i källkod

Widget::runScript

jqh 4 år sedan
förälder
incheckning
73dd68c8dc
3 ändrade filer med 47 tillägg och 13 borttagningar
  1. 8 5
      src/Widgets/ApexCharts/Chart.php
  2. 3 3
      src/Widgets/Metrics/Card.php
  3. 36 5
      src/Widgets/Widget.php

+ 8 - 5
src/Widgets/ApexCharts/Chart.php

@@ -243,7 +243,7 @@ JS;
     /**
      * @return string
      */
-    public function script()
+    public function addScript()
     {
         if (! $this->allowBuildRequest()) {
             return $this->buildDefaultScript();
@@ -271,7 +271,7 @@ if (chartBox.length) {
 JS
         );
 
-        return $this->buildRequestScript();
+        return $this->script = $this->buildRequestScript();
     }
 
     /**
@@ -284,6 +284,11 @@ JS
         }
         $this->built = true;
 
+        return parent::render();
+    }
+
+    public function html()
+    {
         $hasSelector = $this->containerSelector ? true : false;
 
         if (! $hasSelector) {
@@ -293,9 +298,7 @@ JS
             $this->selector('#'.$id);
         }
 
-        $this->script = $this->script();
-
-        $this->collectAssets();
+        $this->addScript();
 
         if ($hasSelector) {
             return;

+ 3 - 3
src/Widgets/Metrics/Card.php

@@ -433,7 +433,7 @@ class Card extends Widget
     /**
      * @return mixed
      */
-    public function script()
+    public function addScript()
     {
         if (! $this->allowBuildRequest()) {
             return;
@@ -470,7 +470,7 @@ JS
         }
 
         // 按钮显示选中文本
-        return <<<JS
+        return $this->script = <<<JS
 $('{$clickable}').on('click', function () {
     $(this).parents('.dropdown').find('.btn').html($(this).text());
 });
@@ -533,7 +533,7 @@ JS;
         $this->setUpChart();
         $this->setUpCardHeight();
 
-        $this->script = $this->script();
+        $this->addScript();
 
         $this->variables['icon'] = $this->icon;
         $this->variables['title'] = $this->title;

+ 36 - 5
src/Widgets/Widget.php

@@ -49,6 +49,11 @@ abstract class Widget implements Renderable
      */
     protected $options = [];
 
+    /**
+     * @var bool
+     */
+    protected $runScript = true;
+
     /**
      * @param mixed ...$params
      *
@@ -141,14 +146,22 @@ abstract class Widget implements Renderable
     /**
      * 收集静态资源.
      */
-    protected function collectAssets()
+    public static function collectAssets()
     {
-        $this->script && Admin::script($this->script);
-
         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
      *
@@ -164,9 +177,13 @@ abstract class Widget implements Renderable
      */
     public function render()
     {
-        $this->collectAssets();
+        static::collectAssets();
+
+        $html = $this->html();
 
-        return $this->html();
+        $this->withScript();
+
+        return $html;
     }
 
     /**
@@ -205,6 +222,20 @@ abstract class Widget implements Renderable
         $this->view = $view;
     }
 
+    /**
+     * 设置是否执行JS代码.
+     *
+     * @param bool $run
+     *
+     * @return $this
+     */
+    public function runScript(bool $run = true)
+    {
+        $this->runScript = $run;
+
+        return $this;
+    }
+
     /**
      * @return string
      */