浏览代码

update action

update
jqh 5 年之前
父节点
当前提交
fd3a167b14

+ 55 - 0
src/Actions/Action.php

@@ -6,6 +6,11 @@ use Dcat\Admin\Support\Helper;
 use Dcat\Admin\Traits\HasHtmlAttributes;
 use Illuminate\Contracts\Support\Renderable;
 
+/**
+ * Class Action
+ *
+ * @method string href
+ */
 abstract class Action implements Renderable
 {
     use HasHtmlAttributes, HasActionHandler;
@@ -55,6 +60,11 @@ abstract class Action implements Renderable
      */
     protected $usingHandler = true;
 
+    /**
+     * @var array
+     */
+    protected $htmlClasses = [];
+
     /**
      * Action constructor.
      *
@@ -161,6 +171,18 @@ abstract class Action implements Renderable
         return static::$selectors[$class];
     }
 
+    /**
+     * @param string|array $class
+     *
+     * @return $this
+     */
+    public function addHtmlClass($class)
+    {
+        $this->htmlClasses = array_merge($this->htmlClasses, (array) $class);
+
+        return $this;
+    }
+
     /**
      * @return void
      */
@@ -173,6 +195,9 @@ abstract class Action implements Renderable
      */
     protected function html()
     {
+        return <<<HTML
+<a {$this->formatHtmlAttributes()}>{$this->title()}</a>
+HTML;
     }
 
     /**
@@ -205,10 +230,40 @@ abstract class Action implements Renderable
 
         $this->setupHandler();
         $this->addScript();
+        $this->setupHtmlAttributes();
 
         return $this->html();
     }
 
+    /**
+     * @return string
+     */
+    protected function formatHtmlClasses()
+    {
+        return implode(' ', array_unique($this->htmlClasses));
+    }
+
+    /**
+     * @return void
+     */
+    protected function setupHtmlAttributes()
+    {
+        $this->addHtmlClass($this->elementClass());
+
+        $attributes = [
+            'class' => $this->formatHtmlClasses(),
+        ];
+
+        if (method_exists($this, 'href') && ($href = $this->href())) {
+            $this->usingHandler = false;
+
+            $attributes['href'] = $href;
+        }
+
+        $this->defaultHtmlAttribute('style', 'cursor: pointer');
+        $this->setHtmlAttribute($attributes);
+    }
+
     /**
      * @return string
      */

+ 19 - 11
src/Actions/HasActionHandler.php

@@ -37,7 +37,7 @@ trait HasActionHandler
     /**
      * @return array
      */
-    public function parameters()
+    protected function parameters()
     {
         return [];
     }
@@ -154,7 +154,7 @@ JS;
      */
     protected function resolverScript()
     {
-        return <<<'JS'
+        return <<<JS
 function (data, target) {
     var response = data[0],
         target   = data[1];
@@ -163,8 +163,6 @@ function (data, target) {
         return LA.error({type: 'error', title: 'Oops!'});
     }
     
-    response = response.data;
-    
     var then = function (then) {
         switch (then.action) {
             case 'refresh':
@@ -181,22 +179,32 @@ function (data, target) {
                 break;
         }
     };
-    
-    if (typeof response.html === 'string') {
-        target.html(response.html);
+
+    if (typeof response.html === 'string' && response.html) {
+        {$this->handleHtmlResponse()};
     }
 
-    if (typeof response.message === 'string' && response.type) {
-        LA[response.type](response.message);
+    if (typeof response.data.message === 'string' && response.data.type) {
+        LA[response.data.type](response.data.message);
     }
     
-    if (response.then) {
-      then(response.then);
+    if (response.data.then) {
+      then(response.data.then);
     }
 }
 JS;
     }
 
+    /**
+     * @return string
+     */
+    protected function handleHtmlResponse()
+    {
+        return <<<'JS'
+target.html(response.html);
+JS;
+    }
+
     /**
      * @return string
      */

+ 2 - 1
src/Actions/Response.php

@@ -2,6 +2,7 @@
 
 namespace Dcat\Admin\Actions;
 
+use Dcat\Admin\Support\Helper;
 use Illuminate\Validation\ValidationException;
 
 class Response
@@ -194,7 +195,7 @@ class Response
         $data = ['status' => $this->status, 'data' => $this->data];
 
         if ($this->html) {
-            $data['html'] = $this->html;
+            $data['html'] = Helper::render($this->html);
         }
 
         return response()->json($data);

+ 1 - 13
src/Form/AbstractTool.php

@@ -58,13 +58,6 @@ abstract class AbstractTool extends Action
         return $this->parent ? $this->parent->key() : null;
     }
 
-    /**
-     * @return string|void
-     */
-    protected function href()
-    {
-    }
-
     /**
      * @return string
      */
@@ -86,17 +79,12 @@ abstract class AbstractTool extends Action
      */
     public function html()
     {
-        if ($href = $this->href()) {
-            $this->disabledHandler = true;
-        }
-
         $this->setHtmlAttribute([
             'data-_key' => $this->key(),
-            'href'      => $href ?: 'javascript:void(0);',
             'class'     => $this->style.' '.$this->elementClass(),
         ]);
 
-        return "<a {$this->formatHtmlAttributes()}>{$this->title()}</a>";
+        return parent::html();
     }
 
     /**

+ 1 - 13
src/Grid/RowAction.php

@@ -82,13 +82,6 @@ abstract class RowAction extends GridAction
         return $this;
     }
 
-    /**
-     * @return string
-     */
-    public function href()
-    {
-    }
-
     /**
      * Render row action.
      *
@@ -96,16 +89,11 @@ abstract class RowAction extends GridAction
      */
     public function html()
     {
-        if ($href = $this->href()) {
-            $this->usingHandler = true;
-        }
-
         $this->setHtmlAttribute([
             'data-_key' => $this->key(),
-            'href'      => $this->href() ?: 'javascript:void(0);',
             'class'     => $this->elementClass(),
         ]);
 
-        return "<a {$this->formatHtmlAttributes()}>{$this->title()}</a>";
+        return parent::html();
     }
 }

+ 1 - 13
src/Grid/Tools/AbstractTool.php

@@ -16,28 +16,16 @@ abstract class AbstractTool extends Grid\GridAction
      */
     protected $style = 'btn btn-sm btn-primary';
 
-    /**
-     * @return string|void
-     */
-    protected function href()
-    {
-    }
-
     /**
      * @return string|void
      */
     public function html()
     {
-        if ($href = $this->href()) {
-            $this->usingHandler = false;
-        }
-
         $this->setHtmlAttribute([
             'data-_key' => $this->key(),
-            'href'      => $href ?: 'javascript:void(0);',
             'class'     => $this->style.' '.$this->elementClass(),
         ]);
 
-        return "<a {$this->formatHtmlAttributes()}>{$this->title()}</a>";
+        return parent::html();
     }
 }

+ 1 - 13
src/Show/AbstractTool.php

@@ -44,28 +44,16 @@ abstract class AbstractTool extends Action
         return $this->parent ? $this->parent->key() : null;
     }
 
-    /**
-     * @return string|void
-     */
-    protected function href()
-    {
-    }
-
     /**
      * @return string|void
      */
     public function html()
     {
-        if ($href = $this->href()) {
-            $this->disabledHandler = true;
-        }
-
         $this->setHtmlAttribute([
             'data-_key' => $this->key(),
-            'href'      => $href ?: 'javascript:void(0);',
             'class'     => $this->style.' '.$this->elementClass(),
         ]);
 
-        return "<a {$this->formatHtmlAttributes()}>{$this->title()}</a>";
+        return parent::html();
     }
 }

+ 1 - 13
src/Tree/AbstractTool.php

@@ -32,28 +32,16 @@ abstract class AbstractTool extends Action
         $this->parent = $parent;
     }
 
-    /**
-     * @return string|void
-     */
-    protected function href()
-    {
-    }
-
     /**
      * @return string|void
      */
     public function html()
     {
-        if ($href = $this->href()) {
-            $this->disabledHandler = true;
-        }
-
         $this->setHtmlAttribute([
             'data-_key' => $this->key(),
-            'href'      => $href ?: 'javascript:void(0);',
             'class'     => $this->style.' '.$this->elementClass(),
         ]);
 
-        return "<a {$this->formatHtmlAttributes()}>{$this->title()}</a>";
+        return parent::html();
     }
 }