Explorar el Código

Action优化

update
jqh hace 5 años
padre
commit
43fef67f9a
Se han modificado 3 ficheros con 55 adiciones y 49 borrados
  1. 14 10
      src/Actions/Action.php
  2. 40 38
      src/Actions/HasActionHandler.php
  3. 1 1
      src/Grid/RowAction.php

+ 14 - 10
src/Actions/Action.php

@@ -8,7 +8,7 @@ use Illuminate\Contracts\Support\Renderable;
 
 abstract class Action implements Renderable
 {
-    use HasHtmlAttributes, ActionHandler;
+    use HasHtmlAttributes, HasActionHandler;
 
     /**
      * @var array
@@ -119,7 +119,7 @@ abstract class Action implements Renderable
      */
     protected function elementClass()
     {
-        return ltrim($this->selector($this->selectorPrefix), '.');
+        return ltrim($this->selector(), '.');
     }
 
     /**
@@ -133,27 +133,27 @@ abstract class Action implements Renderable
     }
 
     /**
-     * @param string $prefix
-     *
      * @return mixed|string
      */
-    public function selector($prefix)
+    public function selector()
     {
         if (is_null($this->selector)) {
-            return static::makeSelector(get_called_class(), $prefix);
+            return static::makeSelector($this->selectorPrefix);
         }
 
         return $this->selector;
     }
 
     /**
-     * @param string $class
      * @param string $prefix
+     * @param string $class
      *
      * @return string
      */
-    public static function makeSelector($class, $prefix)
+    public static function makeSelector($prefix, $class = null)
     {
+        $class = $class ?: static::class;
+
         if (! isset(static::$selectors[$class])) {
             static::$selectors[$class] = uniqid($prefix);
         }
@@ -178,7 +178,7 @@ abstract class Action implements Renderable
     /**
      * @return void
      */
-    protected function prepareHandlerScript()
+    protected function setupHandler()
     {
         if (
             ! $this->usingHandler
@@ -187,6 +187,10 @@ abstract class Action implements Renderable
             return;
         }
 
+        if ($confirm = $this->confirm()) {
+            $this->setHtmlAttribute('data-confirm', $confirm);
+        }
+
         $this->addHandlerScript();
     }
 
@@ -199,7 +203,7 @@ abstract class Action implements Renderable
             return '';
         }
 
-        $this->prepareHandlerScript();
+        $this->setupHandler();
         $this->addScript();
 
         return $this->html();

+ 40 - 38
src/Actions/ActionHandler.php → src/Actions/HasActionHandler.php

@@ -7,7 +7,7 @@ use Dcat\Admin\Models\HasPermissions;
 use Illuminate\Contracts\Auth\Authenticatable;
 use Illuminate\Database\Eloquent\Model;
 
-trait ActionHandler
+trait HasActionHandler
 {
     /**
      * @var Response
@@ -72,24 +72,15 @@ trait ActionHandler
      */
     protected function addHandlerScript()
     {
-        $parameters = json_encode($this->parameters());
-
-        $resolveScript = <<<JS
-target.attr('working', 1);
-Object.assign(data, {$parameters});
-{$this->buildActionPromise()}
-{$this->handleActionPromise()}
-JS;
-
         $script = <<<JS
-$('{$this->selector($this->selectorPrefix)}').off('{$this->event}').on('{$this->event}', function() {
+$('{$this->selector()}').off('{$this->event}').on('{$this->event}', function() {
     var data = $(this).data(),
         target = $(this);
     if (target.attr('working') > 0) {
         return;
     }
     {$this->actionScript()}
-    {$this->confirmScript($resolveScript)}
+    {$this->buildRequestScript()}
 });
 JS;
 
@@ -97,20 +88,25 @@ JS;
     }
 
     /**
-     * @param string $resolveScript
-     *
      * @return string
      */
-    protected function confirmScript($resolveScript)
+    protected function buildRequestScript()
     {
-        if (! $message = $this->confirm()) {
-            return $resolveScript;
-        }
+        $parameters = json_encode($this->parameters());
 
         return <<<JS
-LA.confirm('{$message}', function () {
-    {$resolveScript}
-});
+function request() {
+    target.attr('working', 1);
+    Object.assign(data, {$parameters});
+    {$this->buildActionPromise()}
+    {$this->handleActionPromise()}
+}
+
+if (data['confirm']) {
+    LA.confirm(data['confirm'], request);
+} else {
+    request()
+}
 JS;
     }
 
@@ -156,22 +152,10 @@ JS;
     /**
      * @return string
      */
-    public function handleActionPromise()
+    protected function resolverScript()
     {
-        Admin::script($this->buildDefaultPromiseCallbacks());
-
         return <<<'JS'
-process.then(window.ACTION_RSOLVER).catch(window.ACTION_CATCHER);
-JS;
-    }
-
-    /**
-     * @return string
-     */
-    protected function buildDefaultPromiseCallbacks()
-    {
-        return <<<'JS'
-window.ACTION_RSOLVER = function (data) {
+function (data, target) {
     var response = data[0],
         target   = data[1];
         
@@ -209,16 +193,34 @@ window.ACTION_RSOLVER = function (data) {
     if (response.then) {
       then(response.then);
     }
-};
+}
+JS;
+    }
 
-window.ACTION_CATCHER = function (data) {
+    /**
+     * @return string
+     */
+    protected function rejectScript()
+    {
+        return <<<'JS'
+function (data) {
     var request = data[0], target = data[1];
     
     if (request && typeof request.responseJSON === 'object') {
         LA.error(request.responseJSON.message)
     }
     console.error(request);
-};
+}
+JS;
+    }
+
+    /**
+     * @return string
+     */
+    public function handleActionPromise()
+    {
+        return <<<JS
+process.then({$this->resolverScript()}).catch({$this->rejectScript()});
 JS;
     }
 

+ 1 - 1
src/Grid/RowAction.php

@@ -97,7 +97,7 @@ abstract class RowAction extends GridAction
     public function html()
     {
         if ($href = $this->href()) {
-            $this->disabledHandler = true;
+            $this->usingHandler = true;
         }
 
         $this->setHtmlAttribute([