Procházet zdrojové kódy

修复行操作action handle方法获取key重复bug

jqh před 5 roky
rodič
revize
6f8bb39ec0

+ 161 - 0
resources/assets/dcat/extra/grid-row-action.js

@@ -0,0 +1,161 @@
+(function (Dcat) {
+
+    class RowAction {
+        constructor(options) {
+            this.options = $.extend({
+                selector: null, // 按钮选择器
+                event: 'click',
+                method: 'POST',
+                key: null, // 行主键
+                url: null,
+                data: {}, // 发送到接口的附加参数
+                calledClass: null,
+                before: function (data, target) {}, // 发起请求之前回调,返回false可以中断请求
+                html: function (target, html, data) { // 处理返回的HTML代码
+                    target.html(html);
+                },
+                success: function (target, results) {}, // 请求成功回调,返回false可以中断默认的成功处理逻辑
+                error: function (target, results) {}, // 请求出错回调,返回false可以中断默认的错误处理逻辑
+            }, options);
+
+            this._bind();
+        }
+
+        _bind() {
+            let _this = this, options = _this.options;
+
+            $(options.selector).off(options.event).on(options.event, function (e) {
+                let data = $(this).data(),
+                    target = $(this);
+                if (target.attr('loading') > 0) {
+                    return;
+                }
+
+                if (options.before(data, target) === false) {
+                    return;
+                }
+
+                // 发起请求
+                function request() {
+                    target.attr('loading', 1);
+
+                    Object.assign(data, options.data);
+
+                    _this._buildActionPromise(target, data).then(_this._resolver()).catch(_this._reject());
+                }
+
+                var conform = data['confirm'];
+
+                delete data['confirm'];
+
+                if (conform) {
+                    Dcat.confirm(conform[0], conform[1], request);
+                } else {
+                    request()
+                }
+            });
+        }
+
+        _resolver() {
+            let options = this.options;
+
+            return function (result) {
+                var response = result[0],
+                    target   = result[1];
+
+                if (options.success(target, response) === false) {
+                    return;
+                }
+
+                if (typeof response !== 'object') {
+                    return Dcat.error({type: 'error', title: 'Oops!'});
+                }
+
+                var then = function (then) {
+                    switch (then.action) {
+                        case 'refresh':
+                            Dcat.reload();
+                            break;
+                        case 'download':
+                            window.open(then.value, '_blank');
+                            break;
+                        case 'redirect':
+                            Dcat.reload(then.value);
+                            break;
+                        case 'location':
+                            window.location = then.value;
+                            break;
+                        case 'script':
+                            (function () {
+                                eval(then.value);
+                            })();
+                            break;
+                    }
+                };
+
+                if (typeof response.html === 'string' && response.html) {
+                    // 处理api返回的HTML代码
+                    options.html(target, response.html, response);
+                }
+
+                if (typeof response.data.message === 'string' && response.data.type) {
+                    Dcat[response.data.type](response.data.message);
+                }
+
+                if (response.data.then) {
+                    then(response.data.then);
+                }
+            };
+        }
+
+        _reject() {
+            return function (result) {
+                var request = result[0], target = result[1];
+
+                if (options.success(target, request) === false) {
+                    return;
+                }
+
+                if (request && typeof request.responseJSON === 'object') {
+                    Dcat.error(request.responseJSON.message)
+                }
+                console.error(request);
+            }
+        }
+
+        _buildActionPromise(target, data) {
+            let options = this.options;
+
+            return new Promise(function (resolve, reject) {
+                Object.assign(data, {
+                    _token: Dcat.token,
+                    _action: options.calledClass,
+                    _key: options.key,
+                });
+
+                Dcat.NP.start();
+
+                $.ajax({
+                    method: options.method,
+                    url: options.url,
+                    data: data,
+                    success: function (data) {
+                        target.attr('loading', 0);
+                        Dcat.NP.done();
+                        resolve([data, target]);
+                    },
+                    error:function(request){
+                        target.attr('loading', 0);
+                        Dcat.NP.done();
+                        reject([request, target]);
+                    }
+                });
+            });
+        }
+    }
+
+    Dcat.grid.RowAction = function (opts) {
+        return new RowAction(opts);
+    };
+})(Dcat);
+

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 0 - 0
resources/dist/adminlte/adminlte.js


Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 0 - 0
resources/dist/adminlte/adminlte.js.map


Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 0 - 0
resources/dist/dcat/extra/grid-row-action.js


Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 0 - 0
resources/dist/dcat/extra/grid-row-action.js.map


Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 0 - 0
resources/dist/dcat/extra/resource-selector.js


Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 0 - 0
resources/dist/dcat/extra/resource-selector.js.map


Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 0 - 0
resources/dist/dcat/extra/upload.js


Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 0 - 0
resources/dist/dcat/extra/upload.js.map


Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 0 - 0
resources/dist/dcat/js/dcat-app.js


Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 0 - 0
resources/dist/dcat/js/dcat-app.js.map


+ 4 - 2
src/Actions/Action.php

@@ -146,7 +146,7 @@ abstract class Action implements Renderable
     public function selector()
     {
         if (is_null($this->selector)) {
-            return static::makeSelector($this->selectorPrefix);
+            return $this->makeSelector($this->selectorPrefix);
         }
 
         return $this->selector;
@@ -158,7 +158,7 @@ abstract class Action implements Renderable
      *
      * @return string
      */
-    public static function makeSelector($prefix, $class = null)
+    public function makeSelector($prefix, $class = null)
     {
         $class = $class ?: static::class;
 
@@ -231,7 +231,9 @@ HTML;
         }
 
         $this->setupHandler();
+
         $this->setupHtmlAttributes();
+
         if ($script = $this->script()) {
             Admin::script($script);
         }

+ 26 - 125
src/Actions/HasActionHandler.php

@@ -74,42 +74,26 @@ trait HasActionHandler
      */
     protected function addHandlerScript()
     {
+        $data = json_encode($this->parameters());
+
         $script = <<<JS
-$('{$this->selector()}').off('{$this->event}').on('{$this->event}', function() {
-    var data = $(this).data(),
-        target = $(this);
-    if (target.attr('loading') > 0) {
-        return;
-    }
-    {$this->actionScript()}
-    {$this->buildRequestScript()}
+Dcat.grid.RowAction({
+    selector: '{$this->selector()}',
+    event: '{$this->event}',
+    method: '{$this->method()}',
+    key: '{$this->getKey()}',
+    url: '{$this->handlerRoute()}',
+    data: {$data},
+    calledClass: '{$this->makeCalledClass()}',
+    before: {$this->actionScript()},
+    html: {$this->handleHtmlResponse()},
+    success: {$this->resolverScript()}, 
+    error: {$this->rejectScript()},
 });
 JS;
 
         Admin::script($script);
-    }
-
-    /**
-     * @return string
-     */
-    protected function buildRequestScript()
-    {
-        $parameters = json_encode($this->parameters());
-
-        return <<<JS
-function request() {
-    target.attr('loading', 1);
-    Object.assign(data, {$parameters});
-    {$this->buildActionPromise()}
-    {$this->handleActionPromise()}
-}
-
-if (data['confirm']) {
-     Dcat.confirm(data['confirm'][0], data['confirm'][1], request);
-} else {
-    request()
-}
-JS;
+        Admin::js('@admin/dcat/extra/grid-row-action.js');
     }
 
     /**
@@ -117,38 +101,9 @@ JS;
      */
     protected function actionScript()
     {
-        return '';
-    }
-
-    /**
-     * @return string
-     */
-    protected function buildActionPromise()
-    {
-        return <<<JS
-var process = new Promise(function (resolve,reject) {
-    Object.assign(data, {
-        _token: Dcat.token,
-        _action: '{$this->makeCalledClass()}',
-        _key: '{$this->getKey()}',
-    });
-    Dcat.NP.start();
-    $.ajax({
-        method: '{$this->method()}',
-        url: '{$this->handlerRoute()}',
-        data: data,
-        success: function (data) {
-            target.attr('loading', 0);
-            Dcat.NP.done();
-            resolve([data, target]);
-        },
-        error:function(request){
-            target.attr('loading', 0);
-            Dcat.NP.done();
-            reject([request, target]);
-        }
-    });
-});
+        // 发起请求之前回调,返回false可以中断请求
+        return <<<'JS'
+function (data, target) { }
 JS;
     }
 
@@ -157,49 +112,9 @@ JS;
      */
     protected function resolverScript()
     {
-        return <<<JS
-function (data) {
-    var response = data[0],
-        target   = data[1];
-        
-    if (typeof response !== 'object') {
-        return Dcat.error({type: 'error', title: 'Oops!'});
-    }
-    
-    var then = function (then) {
-        switch (then.action) {
-            case 'refresh':
-                Dcat.reload();
-                break;
-            case 'download':
-                window.open(then.value, '_blank');
-                break;
-            case 'redirect':
-                Dcat.reload(then.value);
-                break;
-            case 'location':
-                window.location = then.value;
-                break;
-            case 'script':
-                (function () {
-                    eval(then.value);
-                })();
-                break;
-        }
-    };
-
-    if (typeof response.html === 'string' && response.html) {
-        {$this->handleHtmlResponse()};
-    }
-
-    if (typeof response.data.message === 'string' && response.data.type) {
-        Dcat[response.data.type](response.data.message);
-    }
-    
-    if (response.data.then) {
-      then(response.data.then);
-    }
-}
+        // 请求成功回调,返回false可以中断默认的成功处理逻辑
+        return <<<'JS'
+function (target, results) {}
 JS;
     }
 
@@ -209,7 +124,9 @@ JS;
     protected function handleHtmlResponse()
     {
         return <<<'JS'
-target.html(response.html);
+function (target, html, data) { // 处理返回的HTML代码
+    target.html(html);
+}
 JS;
     }
 
@@ -218,25 +135,9 @@ JS;
      */
     protected function rejectScript()
     {
+        // // 请求出错回调,返回false可以中断默认的错误处理逻辑
         return <<<'JS'
-function (data) {
-    var request = data[0], target = data[1];
-    
-    if (request && typeof request.responseJSON === 'object') {
-        Dcat.error(request.responseJSON.message)
-    }
-    console.error(request);
-}
-JS;
-    }
-
-    /**
-     * @return string
-     */
-    public function handleActionPromise()
-    {
-        return <<<JS
-process.then({$this->resolverScript()}).catch({$this->rejectScript()});
+function (target, results) {}
 JS;
     }
 

+ 19 - 0
src/Grid/RowAction.php

@@ -81,4 +81,23 @@ abstract class RowAction extends GridAction
 
         return $this;
     }
+
+    /**
+     * @param string $prefix
+     * @param string $class
+     *
+     * @return string
+     */
+    public function makeSelector($prefix, $class = null)
+    {
+        $class = $class ?: static::class;
+
+        $key = $class.'-'.$this->getKey();
+
+        if (! isset(static::$selectors[$key])) {
+            static::$selectors[$key] = uniqid($prefix);
+        }
+
+        return static::$selectors[$key];
+    }
 }

Některé soubory nejsou zobrazeny, neboť je v těchto rozdílových datech změněno mnoho souborů