Преглед изворни кода

action类优化,去除判断selector是否重复功能

jqh пре 4 година
родитељ
комит
acfa77f977

+ 2 - 2
resources/assets/dcat/js/Dcat.js

@@ -116,10 +116,10 @@ export default class Dcat {
         setTimeout(function () {
             initialized[selector] = $.initialize(selector, function () {
                 var $this = $(this);
-                if ($this.hasClass('initialized')) {
+                if ($this.attr('initialized')) {
                     return;
                 }
-                $this.addClass('initialized');
+                $this.attr('initialized', '1');
 
                 // 如果没有ID,则自动生成
                 var id = $this.attr('id');

+ 4 - 25
src/Actions/Action.php

@@ -18,11 +18,6 @@ abstract class Action implements Renderable
     use HasHtmlAttributes;
     use HasActionHandler;
 
-    /**
-     * @var array
-     */
-    protected static $selectors = [];
-
     /**
      * @var array|string
      */
@@ -38,11 +33,6 @@ abstract class Action implements Renderable
      */
     protected $selector;
 
-    /**
-     * @var string
-     */
-    public $selectorPrefix = '.admin-action-';
-
     /**
      * @var string
      */
@@ -149,30 +139,19 @@ abstract class Action implements Renderable
      */
     public function selector()
     {
-        if (is_null($this->selector)) {
-            return $this->makeSelector($this->selectorPrefix);
-        }
-
-        return $this->selector;
+        return $this->selector ?: ($this->selector = $this->makeSelector());
     }
 
     /**
-     * 选择器.
+     * 生成选择器.
      *
      * @param string $prefix
-     * @param string $class
      *
      * @return string
      */
-    public function makeSelector($prefix, $class = null)
+    public function makeSelector()
     {
-        $key = $this->getSelectorKey($prefix, $class);
-
-        if (! isset(static::$selectors[$key])) {
-            static::$selectors[$key] = $prefix.Str::random(8);
-        }
-
-        return static::$selectors[$key];
+        return 'act-'.Str::random();
     }
 
     /**

+ 0 - 5
src/Form/AbstractTool.php

@@ -12,11 +12,6 @@ abstract class AbstractTool extends Action
      */
     protected $parent;
 
-    /**
-     * @var string
-     */
-    public $selectorPrefix = '.form-tool-action-';
-
     /**
      * @var string
      */

+ 0 - 11
src/Grid/Actions/QuickEdit.php

@@ -39,15 +39,4 @@ class QuickEdit extends RowAction
 
         return parent::render(); // TODO: Change the autogenerated stub
     }
-
-    /**
-     * @param string $prefix
-     * @param string $class
-     *
-     * @return string
-     */
-    public function getSelectorKey($prefix, $class = null)
-    {
-        return $class ?: static::class;
-    }
 }

+ 0 - 5
src/Grid/BatchAction.php

@@ -4,11 +4,6 @@ namespace Dcat\Admin\Grid;
 
 abstract class BatchAction extends GridAction
 {
-    /**
-     * @var string
-     */
-    public $selectorPrefix = '.grid-batch-action-';
-
     /**
      * {@inheritdoc}
      */

+ 0 - 5
src/Grid/GridAction.php

@@ -15,11 +15,6 @@ abstract class GridAction extends Action
      */
     protected $parent;
 
-    /**
-     * @var string
-     */
-    public $selectorPrefix = '.grid-action-';
-
     /**
      * @param Grid $grid
      *

+ 4 - 6
src/Grid/Row.php

@@ -4,6 +4,7 @@ namespace Dcat\Admin\Grid;
 
 use Closure;
 use Dcat\Admin\Grid;
+use Dcat\Admin\Support\Helper;
 use Illuminate\Contracts\Support\Arrayable;
 use Illuminate\Contracts\Support\Htmlable;
 use Illuminate\Contracts\Support\Jsonable;
@@ -86,12 +87,7 @@ class Row implements Arrayable
      */
     private function formatHtmlAttributes($attributes = [])
     {
-        $attrArr = [];
-        foreach ($attributes as $name => $val) {
-            $attrArr[] = "$name=\"$val\"";
-        }
-
-        return implode(' ', $attrArr);
+        return Helper::buildHtmlAttributes($attributes);
     }
 
     /**
@@ -102,6 +98,8 @@ class Row implements Arrayable
     public function setAttributes(array $attributes)
     {
         $this->attributes = $attributes;
+
+        return $this;
     }
 
     /**

+ 0 - 19
src/Grid/RowAction.php

@@ -16,11 +16,6 @@ abstract class RowAction extends GridAction
      */
     protected $column;
 
-    /**
-     * @var string
-     */
-    public $selectorPrefix = '.grid-row-action-';
-
     /**
      * Get primary key value of current row.
      *
@@ -81,18 +76,4 @@ abstract class RowAction extends GridAction
 
         return $this;
     }
-
-    /**
-     * 生成选择器键名.
-     * 需要保证每个行操作的选择器都不同.
-     *
-     * @param string $prefix
-     * @param string $class
-     *
-     * @return string
-     */
-    public function getSelectorKey($prefix, $class = null)
-    {
-        return parent::getSelectorKey($prefix, $class).$this->getKey();
-    }
 }

+ 0 - 5
src/Grid/Tools/AbstractTool.php

@@ -6,11 +6,6 @@ use Dcat\Admin\Grid;
 
 abstract class AbstractTool extends Grid\GridAction
 {
-    /**
-     * @var string
-     */
-    public $selectorPrefix = '.tool-action-';
-
     /**
      * @var string
      */

+ 0 - 5
src/Show/AbstractTool.php

@@ -12,11 +12,6 @@ abstract class AbstractTool extends Action
      */
     protected $parent;
 
-    /**
-     * @var string
-     */
-    public $selectorPrefix = '.show-tool-action-';
-
     /**
      * @var string
      */

+ 0 - 5
src/Tree/AbstractTool.php

@@ -12,11 +12,6 @@ abstract class AbstractTool extends Action
      */
     protected $parent;
 
-    /**
-     * @var string
-     */
-    public $selectorPrefix = '.tree-tool-action-';
-
     /**
      * @var string
      */

+ 0 - 16
src/Tree/RowAction.php

@@ -16,8 +16,6 @@ class RowAction extends Action
      */
     protected $row;
 
-    public $selectorPrefix = '.tree-row-action-';
-
     /**
      * 获取主键值.
      *
@@ -66,18 +64,4 @@ class RowAction extends Action
     {
         $this->row = $row;
     }
-
-    /**
-     * 生成选择器键名.
-     * 需要保证每个行操作的选择器都不同.
-     *
-     * @param string $prefix
-     * @param string $class
-     *
-     * @return string
-     */
-    public function getSelectorKey($prefix, $class = null)
-    {
-        return parent::getSelectorKey($prefix, $class).$this->getKey();
-    }
 }