Browse Source

优化行操作Selector生成功能

jqh 4 years ago
parent
commit
834c0a9f2b
4 changed files with 34 additions and 15 deletions
  1. 4 3
      src/Actions/Action.php
  2. 28 6
      src/Actions/HasActionHandler.php
  3. 1 3
      src/Grid/RowAction.php
  4. 1 3
      src/Tree/RowAction.php

+ 4 - 3
src/Actions/Action.php

@@ -183,7 +183,8 @@ abstract class Action implements Renderable
      */
     public function getSelectorKey($prefix, $class)
     {
-        return $prefix.'-'.($class ?: static::class);
+        return $prefix.'-'.($class ?: static::class)
+            .md5($this->normalizeConfirmData().$this->normalizeParameters());
     }
 
     /**
@@ -245,7 +246,7 @@ HTML;
 
         $this->prepareHandler();
 
-        $this->setupHtmlAttributes();
+        $this->setUpHtmlAttributes();
 
         if ($script = $this->script()) {
             Admin::script($script);
@@ -265,7 +266,7 @@ HTML;
     /**
      * @return void
      */
-    protected function setupHtmlAttributes()
+    protected function setUpHtmlAttributes()
     {
         $this->addHtmlClass($this->getElementClass());
 

+ 28 - 6
src/Actions/HasActionHandler.php

@@ -15,6 +15,10 @@ trait HasActionHandler
      * @var Response
      */
     protected $response;
+    
+    private $confirmString;
+    
+    private $paramString;
 
     /**
      * @return Response
@@ -70,14 +74,32 @@ trait HasActionHandler
     }
 
     /**
-     * @return void
+     * @return string
      */
-    protected function addHandlerScript()
+    protected function normalizeConfirmData()
     {
-        $data = json_encode($this->parameters());
+        if ($this->confirmString !== null) {
+            return $this->confirmString;
+        }
+        
         $confirm = $this->confirm();
-        $confirm = $confirm ? admin_javascript_json((array) $confirm) : 'false';
 
+        return $this->confirmString = ($confirm ? admin_javascript_json((array) $confirm) : 'false');
+    }
+
+    /**
+     * @return string
+     */
+    protected function normalizeParameters()
+    {
+        return $this->paramString ?: ($this->paramString = json_encode($this->parameters()));
+    }
+
+    /**
+     * @return void
+     */
+    protected function addHandlerScript()
+    {
         $script = <<<JS
 Dcat.Action({
     selector: '{$this->selector()}',
@@ -85,8 +107,8 @@ Dcat.Action({
     method: '{$this->method()}',
     key: '{$this->getKey()}',
     url: '{$this->handlerRoute()}',
-    data: {$data},
-    confirm: {$confirm},
+    data: {$this->normalizeParameters()},
+    confirm: {$this->normalizeConfirmData()},
     calledClass: '{$this->makeCalledClass()}',
     before: {$this->actionScript()},
     html: {$this->handleHtmlResponse()},

+ 1 - 3
src/Grid/RowAction.php

@@ -93,8 +93,6 @@ abstract class RowAction extends GridAction
      */
     public function getSelectorKey($prefix, $class = null)
     {
-        $class = $class ?: static::class;
-
-        return $prefix.'-'.$class.'-'.$this->getKey();
+        return parent::getSelectorKey($prefix, $class).$this->getKey();
     }
 }

+ 1 - 3
src/Tree/RowAction.php

@@ -78,8 +78,6 @@ class RowAction extends Action
      */
     public function getSelectorKey($prefix, $class = null)
     {
-        $class = $class ?: static::class;
-
-        return $prefix.'-'.$class.'-'.$this->getKey();
+        return parent::getSelectorKey($prefix, $class).$this->getKey();
     }
 }