فهرست منبع

HasAjaxRequest

jqh 5 سال پیش
والد
کامیت
970650f7f2

+ 1 - 1
src/Grid/Tools/RowSelector.php

@@ -46,7 +46,7 @@ class RowSelector
         return $this;
     }
 
-    public function rowClickable(bool $value = true)
+    public function click(bool $value = true)
     {
         $this->rowClickable = $value;
 

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

@@ -3,7 +3,7 @@
 namespace Dcat\Admin\Widgets\Chart;
 
 use Dcat\Admin\Admin;
-use Dcat\Admin\Widgets\AjaxRequestBuilder;
+use Dcat\Admin\Widgets\HasAjaxRequest;
 use Dcat\Admin\Widgets\Color;
 use Dcat\Admin\Widgets\Widget;
 use Illuminate\Support\Str;
@@ -18,7 +18,7 @@ use Illuminate\Support\Str;
  */
 abstract class Chart extends Widget
 {
-    use AjaxRequestBuilder;
+    use HasAjaxRequest;
 
     public static $globalSettings = [
         'defaultFontColor'  => '#555',
@@ -400,7 +400,7 @@ abstract class Chart extends Widget
             $globalSettings .= sprintf('Chart.defaults.global.%s="%s";', $k, $v);
         }
 
-        if (! $this->allowBuildFetchingScript()) {
+        if (! $this->allowBuildRequestScript()) {
             return <<<JS
 {$globalSettings}
 setTimeout(function(){ new Chart($("#{$this->id}").get(0).getContext("2d"), $options) },60)
@@ -409,19 +409,19 @@ JS;
 
         $this->fetched(
             <<<JS
-if (!result.status) {
-    return LA.error(result.message || 'Server internal error.');
+if (!response.status) {
+    return LA.error(response.message || 'Server internal error.');
 }        
 var id = '{$this->id}', opt = $options, prev = window['obj'+id];
-opt.options = $.extend(opt.options, result.options || {});
-opt.data.datasets = result.datasets || opt.data.datasets;
+opt.options = $.extend(opt.options, response.options || {});
+opt.data.datasets = response.datasets || opt.data.datasets;
 if (prev) prev.destroy();
 
 window['obj'+id] = new Chart($("#"+id).get(0).getContext("2d"), opt);
 JS
         );
 
-        return $globalSettings.$this->buildFetchingScript();
+        return $globalSettings.$this->buildRequestScript();
     }
 
     /**

+ 9 - 9
src/Widgets/DataCard/Card.php

@@ -3,7 +3,7 @@
 namespace Dcat\Admin\Widgets\DataCard;
 
 use Dcat\Admin\Admin;
-use Dcat\Admin\Widgets\AjaxRequestBuilder;
+use Dcat\Admin\Widgets\HasAjaxRequest;
 use Dcat\Admin\Widgets\Dropdown;
 use Dcat\Admin\Widgets\Widget;
 use Illuminate\Contracts\Support\Renderable;
@@ -11,7 +11,7 @@ use Illuminate\Support\Str;
 
 class Card extends Widget
 {
-    use AjaxRequestBuilder;
+    use HasAjaxRequest;
 
     protected $view = 'admin::widgets.data-card';
 
@@ -142,13 +142,13 @@ class Card extends Widget
      */
     protected function script()
     {
-        if (! $this->allowBuildFetchingScript()) {
+        if (! $this->allowBuildRequestScript()) {
             return;
         }
 
         $this->setupFetchScript();
 
-        return $this->buildFetchingScript();
+        return $this->buildRequestScript();
     }
 
     /**
@@ -167,14 +167,14 @@ JS
 
         $this->fetched(
             <<<'JS'
-if (!result.status) {
-    return LA.error(result.message || 'Server internal error.');
+if (!response.status) {
+    return LA.error(response.message || 'Server internal error.');
 }     
-var w = (result.progress || 0) + '%', pg = card.find('.progress-bar');
+var w = (response.progress || 0) + '%', pg = card.find('.progress-bar');
  
 card.loading(false)
-card.find('.right-content').html(result.content.right || '');
-card.find('.main-content').html(result.content.left || '');
+card.find('.right-content').html(response.content.right || '');
+card.find('.main-content').html(response.content.left || '');
 pg.css({width: 0});
 setTimeout(function(){ pg.css({width: w});}, 150);
 card.find('number').counterUp({time: 550});

+ 1 - 1
src/Widgets/DataCard/DoughnutChartCard.php

@@ -134,7 +134,7 @@ HTML;
     {
         $this->buildDots();
 
-        if (! $this->allowBuildFetchingScript()) {
+        if (! $this->allowBuildRequestScript()) {
             return;
         }
 

+ 1 - 1
src/Widgets/DataCard/LineChartCard.php

@@ -42,7 +42,7 @@ class LineChartCard extends Card
 
     protected function script()
     {
-        if (! $this->allowBuildFetchingScript()) {
+        if (! $this->allowBuildRequestScript()) {
             return;
         }
 

+ 40 - 37
src/Widgets/AjaxRequestBuilder.php → src/Widgets/HasAjaxRequest.php

@@ -4,7 +4,7 @@ namespace Dcat\Admin\Widgets;
 
 use Illuminate\Support\Str;
 
-trait AjaxRequestBuilder
+trait HasAjaxRequest
 {
     /**
      * @var string
@@ -14,17 +14,12 @@ trait AjaxRequestBuilder
     /**
      * @var array
      */
-    protected $buttonSelectors = [];
-
-    /**
-     * @var string
-     */
-    protected $fn;
+    protected $__selectors = [];
 
     /**
      * @var array
      */
-    protected $javascripts = [
+    protected $__scripts = [
         'fetching' => [],
         'fetched'  => [],
     ];
@@ -80,9 +75,9 @@ trait AjaxRequestBuilder
     /**
      * @return array
      */
-    public function getJavascripts()
+    public function getScripts()
     {
-        return $this->javascripts;
+        return $this->__scripts;
     }
 
     /**
@@ -94,8 +89,8 @@ trait AjaxRequestBuilder
      */
     public function refetch($selector)
     {
-        $this->buttonSelectors =
-            array_merge($this->buttonSelectors, (array) $selector);
+        $this->__selectors =
+            array_merge($this->__selectors, (array) $selector);
 
         return $this;
     }
@@ -105,7 +100,7 @@ trait AjaxRequestBuilder
      */
     public function getButtonSelectors()
     {
-        return $this->buttonSelectors;
+        return $this->__selectors;
     }
 
     /**
@@ -117,7 +112,7 @@ trait AjaxRequestBuilder
      */
     public function fetching(string $script)
     {
-        $this->javascripts['fetching'][] = $script;
+        $this->__scripts['fetching'][] = $script;
 
         return $this;
     }
@@ -131,7 +126,7 @@ trait AjaxRequestBuilder
      */
     public function fetched(string $script)
     {
-        $this->javascripts['fetched'][] = $script;
+        $this->__scripts['fetched'][] = $script;
 
         return $this;
     }
@@ -139,46 +134,54 @@ trait AjaxRequestBuilder
     /**
      * @return bool
      */
-    public function allowBuildFetchingScript()
+    public function allowBuildRequestScript()
     {
         return $this->__url === null ? false : true;
     }
 
+    /**
+     * @return string
+     */
+    public function generateScriptFunctionName()
+    {
+        return 'ajax_request_'.Str::random(8);
+    }
+
     /**
      * @return bool|string
      */
-    public function buildFetchingScript()
+    public function buildRequestScript()
     {
-        if (! $this->allowBuildFetchingScript()) {
+        if (! $this->allowBuildRequestScript()) {
             return false;
         }
 
-        $this->fn = 'frd_'.Str::random(8);
-
-        $fetching = implode(';', $this->javascripts['fetching']);
-        $fetched = implode(';', $this->javascripts['fetched']);
+        $fetching = implode(';', $this->__scripts['fetching']);
+        $fetched = implode(';', $this->__scripts['fetched']);
 
         $binding = '';
-        foreach ($this->buttonSelectors as $v) {
-            $binding .= "$('{$v}').click(function () { {$this->fn}($(this).data()) });";
+        foreach ($this->__selectors as $v) {
+            $binding .= "$('{$v}').click(function () { request($(this).data()) });";
         }
 
         return <<<JS
-window.{$this->fn} = function (p) {
-    $fetching;     
-    $.getJSON('{$this->__url}', $.extend({_token:LA.token}, p || {}), function (result) {
-        {$fetched};
-    });
-}
-{$this->fn}();
-$binding;
+(function () {
+    function request(p) {
+        $fetching;     
+        $.getJSON('{$this->__url}', $.extend({_token:LA.token}, p || {}), function (response) {
+            {$fetched};
+        });
+    }
+    request();
+    $binding
+})();
 JS;
     }
 
     /**
      * Copy the given AjaxRequestBuilder.
      *
-     * @param AjaxRequestBuilder $fetcher
+     * @param HasAjaxRequest $fetcher
      *
      * @return $this
      */
@@ -186,12 +189,12 @@ JS;
     {
         $this->__url = $fetcher->getUrl();
 
-        $this->buttonSelectors = $fetcher->getButtonSelectors();
+        $this->__selectors = $fetcher->getButtonSelectors();
 
-        $scripts = $fetcher->getJavascripts();
+        $scripts = $fetcher->getScripts();
 
-        $this->javascripts['fetching'] = array_merge($this->javascripts['fetching'], $scripts['fetching']);
-        $this->javascripts['fetched'] = array_merge($this->javascripts['fetched'], $scripts['fetched']);
+        $this->__scripts['fetching'] = array_merge($this->__scripts['fetching'], $scripts['fetching']);
+        $this->__scripts['fetched'] = array_merge($this->__scripts['fetched'], $scripts['fetched']);
 
         return $this;
     }

+ 8 - 8
src/Widgets/Sparkline/Sparkline.php

@@ -3,7 +3,7 @@
 namespace Dcat\Admin\Widgets\Sparkline;
 
 use Dcat\Admin\Admin;
-use Dcat\Admin\Widgets\AjaxRequestBuilder;
+use Dcat\Admin\Widgets\HasAjaxRequest;
 use Dcat\Admin\Widgets\Widget;
 use Illuminate\Contracts\Support\Arrayable;
 use Illuminate\Support\Str;
@@ -22,7 +22,7 @@ use Illuminate\Support\Str;
  */
 class Sparkline extends Widget
 {
-    use AjaxRequestBuilder;
+    use HasAjaxRequest;
 
     protected static $optionMethods = [
         'highlightSpotColor',
@@ -177,7 +177,7 @@ $('#{$this->id}').sparkline($v, $o);
 JS;
         }
 
-        if (! $this->allowBuildFetchingScript()) {
+        if (! $this->allowBuildRequestScript()) {
             return <<<JS
 $('#{$this->id}').sparkline($values, $options);
 {$combos};
@@ -186,16 +186,16 @@ JS;
 
         $this->fetched(
             <<<JS
-if (!result.status) {
-    return LA.error(result.message || 'Server internal error.');
+if (!response.status) {
+    return LA.error(response.message || 'Server internal error.');
 }        
 var id = '{$this->id}', opt = $options;
-opt = $.extend(opt, result.options || {});
-$('#'+id).sparkline(result.values || $values, opt);
+opt = $.extend(opt, response.options || {});
+$('#'+id).sparkline(response.values || $values, opt);
 JS
         );
 
-        return $this->buildFetchingScript();
+        return $this->buildRequestScript();
     }
 
     /**