Przeglądaj źródła

重写BatchAction

jqh 5 lat temu
rodzic
commit
f48e8e634d

+ 7 - 9
src/Actions/Action.php

@@ -7,9 +7,7 @@ use Illuminate\Contracts\Support\Renderable;
 
 abstract class Action implements Renderable
 {
-    use HasHtmlAttributes,
-        Authorizable,
-        ActionHandler;
+    use HasHtmlAttributes, ActionHandler;
 
     /**
      * @var array
@@ -17,14 +15,14 @@ abstract class Action implements Renderable
     protected static $selectors = [];
 
     /**
-     * @var mixed
+     * @var array|string
      */
     protected $primaryKey;
 
     /**
      * @var string
      */
-    protected $name;
+    protected $title;
 
     /**
      * @var string
@@ -49,7 +47,7 @@ abstract class Action implements Renderable
     /**
      * Get primary key value of action.
      *
-     * @return mixed
+     * @return array|string
      */
     public function key()
     {
@@ -77,13 +75,13 @@ abstract class Action implements Renderable
     }
 
     /**
-     * Get batch action title.
+     * Get action title.
      *
      * @return string
      */
-    public function name()
+    public function title()
     {
-        return $this->name;
+        return $this->title;
     }
 
     /**

+ 43 - 15
src/Actions/ActionHandler.php

@@ -3,6 +3,9 @@
 namespace Dcat\Admin\Actions;
 
 use Dcat\Admin\Admin;
+use Dcat\Admin\Models\HasPermissions;
+use Illuminate\Contracts\Auth\Authenticatable;
+use Illuminate\Database\Eloquent\Model;
 
 trait ActionHandler
 {
@@ -40,11 +43,11 @@ trait ActionHandler
     }
 
     /**
-     * Return the confirm message.
+     * Confirm message of action.
      *
      * @return string|void
      */
-    protected function confirm()
+    public function confirm()
     {
     }
 
@@ -74,22 +77,20 @@ trait ActionHandler
         $resolveScript = <<<JS
 target.data('working', 1);
 Object.assign(data, {$parameters});
-{$this->actionScript()}
 {$this->buildActionPromise()}
 {$this->handleActionPromise()}
 JS;
 
         $script = <<<JS
-(function ($) {
-    $('{$this->selector($this->selectorPrefix)}').off('{$this->event}').on('{$this->event}', function() {
-        var data = $(this).data(),
-            target = $(this);
-        if (target.data('working')) {
-            return;
-        }
-        {$this->confirmScript($resolveScript)}
-    });
-})(jQuery);
+$('{$this->selector($this->selectorPrefix)}').off('{$this->event}').on('{$this->event}', function() {
+    var data = $(this).data(),
+        target = $(this);
+    if (target.data('working')) {
+        return;
+    }
+    {$this->actionScript()}
+    {$this->confirmScript($resolveScript)}
+});
 JS;
 
         Admin::script($script);
@@ -128,22 +129,23 @@ JS;
     {
         return <<<JS
 var process = new Promise(function (resolve,reject) {
-    
     Object.assign(data, {
         _token: LA.token,
         _action: '{$this->getCalledClass()}',
     });
-
+    LA.NP.start();
     $.ajax({
         method: '{$this->getMethod()}',
         url: '{$this->getHandleRoute()}',
         data: data,
         success: function (data) {
             target.data('working', 0);
+            LA.NP.done();
             resolve([data, target]);
         },
         error:function(request){
             target.data('working', 0);
+            LA.NP.done();
             reject([request, target]);
         }
     });
@@ -219,4 +221,30 @@ window.ACTION_CATCHER = function (data) {
 };
 JS;
     }
+
+    /**
+     * @return bool
+     */
+    public function passesAuthorization()
+    {
+        return $this->authorize(Admin::user());
+    }
+
+    /**
+     * @param Model|Authenticatable|HasPermissions $user
+     *
+     * @return bool
+     */
+    protected function authorize($user): bool
+    {
+        return true;
+    }
+
+    /**
+     * @return Response
+     */
+    public function failedAuthorization()
+    {
+        return $this->response()->error(__('admin.deny'));
+    }
 }

+ 0 - 28
src/Actions/Authorizable.php

@@ -1,28 +0,0 @@
-<?php
-
-namespace Dcat\Admin\Actions;
-
-use Dcat\Admin\Admin;
-
-trait Authorizable
-{
-    /**
-     * @return bool
-     */
-    public function passesAuthorization()
-    {
-        if (method_exists($this, 'authorize')) {
-            return $this->authorize(Admin::user()) == true;
-        }
-
-        return true;
-    }
-
-    /**
-     * @return Response
-     */
-    public function failedAuthorization()
-    {
-        return $this->response()->error(__('admin.deny'));
-    }
-}

+ 1 - 1
src/Grid/Actions/Delete.php

@@ -9,7 +9,7 @@ class Delete extends RowAction
     /**
      * @return array|null|string
      */
-    public function name()
+    public function title()
     {
         return __('admin.delete');
     }

+ 1 - 1
src/Grid/Actions/Edit.php

@@ -9,7 +9,7 @@ class Edit extends RowAction
     /**
      * @return array|null|string
      */
-    public function name()
+    public function title()
     {
         return __('admin.edit');
     }

+ 1 - 1
src/Grid/Actions/QuickEdit.php

@@ -12,7 +12,7 @@ class QuickEdit extends RowAction
     /**
      * @return array|null|string
      */
-    public function name()
+    public function title()
     {
         return __('admin.quick_edit');
     }

+ 1 - 1
src/Grid/Actions/Show.php

@@ -9,7 +9,7 @@ class Show extends RowAction
     /**
      * @return array|null|string
      */
-    public function name()
+    public function title()
     {
         return __('admin.show');
     }

+ 37 - 0
src/Grid/BatchAction.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace Dcat\Admin\Grid;
+
+abstract class BatchAction extends GridAction
+{
+    /**
+     * @var string
+     */
+    public $selectorPrefix = '.grid-batch-action-';
+
+    /**
+     * {@inheritdoc}
+     */
+    public function actionScript()
+    {
+        $warning = __('No data selected!');
+        return <<<JS
+    var key = LA.grid.selected('{$this->parent->getName()}');
+    
+    if (key.length === 0) {
+        LA.warning('{$warning}');
+        return ;
+    }
+    Object.assign(data, {_key:key});
+JS;
+    }
+
+    protected function html()
+    {
+        return sprintf(
+            "<li><a href='javascript:void(0);' class='%s'>%s</a></li>",
+            $this->elementClass(),
+            $this->title()
+        );
+    }
+}

+ 5 - 4
src/Grid/Concerns/HasTools.php

@@ -3,6 +3,7 @@
 namespace Dcat\Admin\Grid\Concerns;
 
 use Closure;
+use Dcat\Admin\Grid\BatchAction;
 use Dcat\Admin\Grid\Tools;
 
 trait HasTools
@@ -43,14 +44,14 @@ trait HasTools
     /**
      * Set grid batch-action callback.
      *
-     * @param Closure $closure
+     * @param Closure|BatchAction|BatchAction[] $value
      *
      * @return $this
      */
-    public function batchActions(Closure $closure)
+    public function batchActions($value)
     {
-        $this->tools(function (Tools $tools) use ($closure) {
-            $tools->batch($closure);
+        $this->tools(function (Tools $tools) use ($value) {
+            $tools->batch($value);
         });
 
         return $this;

+ 1 - 1
src/Grid/RowAction.php

@@ -107,7 +107,7 @@ abstract class RowAction extends GridAction
             $this->key(),
             $href,
             $this->elementClass(),
-            $this->name()
+            $this->title()
         );
     }
 }

+ 19 - 4
src/Grid/Tools.php

@@ -134,13 +134,28 @@ class Tools implements Renderable
     }
 
     /**
-     * @param \Closure $closure
+     * @param \Closure|BatchAction|BatchAction[] $value
      */
-    public function batch(\Closure $closure)
+    public function batch($value)
     {
-        call_user_func($closure, $this->tools->first(function ($tool) {
+        /* @var BatchActions $batchActions */
+        $batchActions = $this->tools->first(function ($tool) {
             return $tool instanceof BatchActions;
-        }));
+        });
+
+        if ($value instanceof \Closure) {
+            $value($batchActions);
+
+            return;
+        }
+
+        if (! is_array($value)) {
+            $value = [$value];
+        }
+
+        foreach ($value as $action) {
+            $batchActions->add($action);
+        }
     }
 
     /**

+ 0 - 104
src/Grid/Tools/BatchAction.php

@@ -1,104 +0,0 @@
-<?php
-
-namespace Dcat\Admin\Grid\Tools;
-
-use Dcat\Admin\Grid;
-use Illuminate\Contracts\Support\Renderable;
-
-abstract class BatchAction implements Renderable
-{
-    /**
-     * @var int
-     */
-    protected $id;
-
-    /**
-     * @var string
-     */
-    protected $title;
-
-    /**
-     * @var string
-     */
-    protected $resource;
-
-    /**
-     * @var Grid
-     */
-    protected $grid;
-
-    /**
-     * @param $id
-     */
-    public function id($id = null)
-    {
-        if ($id === null) {
-            return $this->id;
-        }
-
-        $this->id = $id;
-    }
-
-    public function title($title = null)
-    {
-        if ($title === null) {
-            return $this->title;
-        }
-
-        $this->title = $title;
-    }
-
-    /**
-     * @param Grid $grid
-     */
-    public function setGrid(Grid $grid)
-    {
-        $this->grid = $grid;
-
-        $this->resource = $grid->resource();
-    }
-
-    /**
-     * @return string
-     */
-    public function token()
-    {
-        return csrf_token();
-    }
-
-    /**
-     * @param bool $dotPrefix
-     *
-     * @return string
-     */
-    public function elementClass()
-    {
-        return sprintf(
-            '%s-%s',
-            $this->grid->batchName(),
-            $this->id
-        );
-    }
-
-    /**
-     * @return string
-     */
-    public function elementSelector()
-    {
-        return '.'.$this->elementClass();
-    }
-
-    /**
-     * Script of batch action.
-     *
-     * @return string
-     */
-    abstract public function script();
-
-    public function render()
-    {
-        return <<<HTML
-<li><a href="#" class="{$this->elementClass()}">{$this->title()}</a></li>
-HTML;
-    }
-}

+ 5 - 9
src/Grid/Tools/BatchActions.php

@@ -2,7 +2,7 @@
 
 namespace Dcat\Admin\Grid\Tools;
 
-use Dcat\Admin\Admin;
+use Dcat\Admin\Grid\BatchAction;
 use Illuminate\Support\Collection;
 
 class BatchActions extends AbstractTool
@@ -58,9 +58,7 @@ class BatchActions extends AbstractTool
      */
     public function add(BatchAction $action)
     {
-        $id = $this->actions->count();
-
-        $action->id($id);
+        $action->selectorPrefix = '.grid-batch-action-'.$this->actions->count();
 
         $this->actions->push($action);
 
@@ -68,16 +66,14 @@ class BatchActions extends AbstractTool
     }
 
     /**
-     * Setup scripts of batch actions.
+     * Prepare batch actions.
      *
      * @return void
      */
-    protected function setUpScripts()
+    protected function prepareActions()
     {
         foreach ($this->actions as $action) {
             $action->setGrid($this->grid);
-
-            Admin::script($action->script());
         }
     }
 
@@ -96,7 +92,7 @@ class BatchActions extends AbstractTool
             return '';
         }
 
-        $this->setUpScripts();
+        $this->prepareActions();
 
         $data = [
             'actions' => $this->actions,

+ 3 - 8
src/Grid/Tools/BatchDelete.php

@@ -2,6 +2,8 @@
 
 namespace Dcat\Admin\Grid\Tools;
 
+use Dcat\Admin\Grid\BatchAction;
+
 class BatchDelete extends BatchAction
 {
     public function __construct($title)
@@ -12,14 +14,7 @@ class BatchDelete extends BatchAction
     public function render()
     {
         return <<<HTML
-<li><a href="#" data-name="{$this->grid->getName()}" data-action="batch-delete" data-url="{$this->resource}">{$this->title}</a></li>
+<li><a href="#" data-name="{$this->parent->getName()}" data-action="batch-delete" data-url="{$this->resource()}">{$this->title}</a></li>
 HTML;
     }
-
-    /**
-     * Script of batch delete action.
-     */
-    public function script()
-    {
-    }
 }