Browse Source

Grid常用配置项支持在配置文件配置

jqh 4 years ago
parent
commit
3b2f1b5ad6

+ 21 - 5
config/admin.php

@@ -153,15 +153,31 @@ return [
         'enable_session_middleware' => false,
     ],
 
+    /*
+    |--------------------------------------------------------------------------
+    | The global Grid setting
+    |--------------------------------------------------------------------------
+    */
     'grid' => [
 
-        /*
-        |--------------------------------------------------------------------------
-        | The global Grid action display class.
-        |--------------------------------------------------------------------------
-        */
+        // The global Grid action display class.
         'grid_action_class' => Dcat\Admin\Grid\Displayers\DropdownActions::class,
 
+        // The global Grid batch action display class.
+        'batch_action_class' => Dcat\Admin\Grid\Tools\BatchActions::class,
+
+        // The global Grid pagination display class.
+        'paginator_class' => Dcat\Admin\Grid\Tools\Paginator::class,
+
+        'actions' => [
+            'view' => Dcat\Admin\Grid\Actions\Show::class,
+            'edit' => Dcat\Admin\Grid\Actions\Edit::class,
+            'quick_edit' => Dcat\Admin\Grid\Actions\QuickEdit::class,
+            'delete' => Dcat\Admin\Grid\Actions\Delete::class,
+            'batch_delete' => Dcat\Admin\Grid\Tools\BatchDelete::class,
+        ],
+
+        // The global Grid column selector setting.
         'column_selector' => [
             'store' => Dcat\Admin\Grid\ColumnSelector\SessionStore::class,
             'store_params' => [

+ 21 - 5
src/Console/stubs/config.stub

@@ -150,15 +150,31 @@ return [
 
     ],
 
+    /*
+    |--------------------------------------------------------------------------
+    | The global Grid setting
+    |--------------------------------------------------------------------------
+    */
     'grid' => [
 
-        /*
-        |--------------------------------------------------------------------------
-        | The global Grid action display class.
-        |--------------------------------------------------------------------------
-        */
+        // The global Grid action display class.
         'grid_action_class' => Dcat\Admin\Grid\Displayers\DropdownActions::class,
 
+        // The global Grid batch action display class.
+        'batch_action_class' => Dcat\Admin\Grid\Tools\BatchActions::class,
+
+        // The global Grid pagination display class.
+        'paginator_class' => Dcat\Admin\Grid\Tools\Paginator::class,
+
+        'actions' => [
+            'view' => Dcat\Admin\Grid\Actions\Show::class,
+            'edit' => Dcat\Admin\Grid\Actions\Edit::class,
+            'quick_edit' => Dcat\Admin\Grid\Actions\QuickEdit::class,
+            'delete' => Dcat\Admin\Grid\Actions\Delete::class,
+            'batch_delete' => Dcat\Admin\Grid\Tools\BatchDelete::class,
+        ],
+
+        // The global Grid column selector setting.
         'column_selector' => [
             'store' => Dcat\Admin\Grid\ColumnSelector\SessionStore::class,
             'store_params' => [

+ 19 - 16
src/Grid.php

@@ -152,22 +152,25 @@ class Grid
      * @var array
      */
     protected $options = [
-        'pagination'        => true,
-        'filter'            => true,
-        'actions'           => true,
-        'quick_edit_button' => false,
-        'edit_button'       => true,
-        'view_button'       => true,
-        'delete_button'     => true,
-        'row_selector'      => true,
-        'create_button'     => true,
-        'bordered'          => false,
-        'table_collapse'    => true,
-        'toolbar'           => true,
-        'create_mode'       => self::CREATE_MODE_DEFAULT,
-        'dialog_form_area'  => ['700px', '670px'],
-        'table_class'       => ['table', 'custom-data-table', 'data-table'],
-        'scrollbar_x'       => false,
+        'pagination'          => true,
+        'filter'              => true,
+        'actions'             => true,
+        'quick_edit_button'   => false,
+        'edit_button'         => true,
+        'view_button'         => true,
+        'delete_button'       => true,
+        'row_selector'        => true,
+        'create_button'       => true,
+        'bordered'            => false,
+        'table_collapse'      => true,
+        'toolbar'             => true,
+        'create_mode'         => self::CREATE_MODE_DEFAULT,
+        'dialog_form_area'    => ['700px', '670px'],
+        'table_class'         => ['table', 'custom-data-table', 'data-table'],
+        'scrollbar_x'         => false,
+        'actions_class'       => null,
+        'batch_actions_class' => null,
+        'paginator_class'     => null,
     ];
 
     /**

+ 3 - 10
src/Grid/Concerns/HasActions.php

@@ -15,13 +15,6 @@ trait HasActions
      */
     protected $actionsCallback = [];
 
-    /**
-     * Actions column display class.
-     *
-     * @var string
-     */
-    protected $actionsClass;
-
     /**
      * @param string $actionClass
      *
@@ -29,7 +22,7 @@ trait HasActions
      */
     public function setActionClass(string $actionClass)
     {
-        $this->actionsClass = $actionClass;
+        $this->options['actions_class'] = $actionClass;
 
         return $this;
     }
@@ -41,8 +34,8 @@ trait HasActions
      */
     public function getActionClass()
     {
-        if ($this->actionsClass) {
-            return $this->actionsClass;
+        if ($this->options['actions_class']) {
+            return $this->options['actions_class'];
         }
 
         if ($class = config('admin.grid.grid_action_class')) {

+ 9 - 8
src/Grid/Concerns/HasPaginator.php

@@ -25,11 +25,6 @@ trait HasPaginator
      */
     protected $perPage = 20;
 
-    /**
-     * @var string
-     */
-    protected $paginatorClass = Tools\Paginator::class;
-
     /**
      * Paginate the grid.
      *
@@ -73,7 +68,7 @@ trait HasPaginator
      */
     public function setPaginatorClass(string $paginator)
     {
-        $this->paginatorClass = $paginator;
+        $this->options['paginator_class'] = $paginator;
 
         return $this;
     }
@@ -81,11 +76,17 @@ trait HasPaginator
     /**
      * Get the grid paginator.
      *
-     * @return mixed
+     * @return \Dcat\Admin\Grid\Tools\Paginator
      */
     public function paginator()
     {
-        return $this->paginator ?: ($this->paginator = new $this->paginatorClass($this));
+        if (! $this->paginator) {
+            $paginatorClass = $this->options['paginator_class'] ?: (config('admin.grid.paginator_class') ?: Tools\Paginator::class);
+
+            $this->paginator = new $paginatorClass($this);
+        }
+
+        return $this->paginator;
     }
 
     /**

+ 22 - 17
src/Grid/Displayers/Actions.php

@@ -11,9 +11,12 @@ use Dcat\Admin\Grid\RowAction;
 use Dcat\Admin\Support\Helper;
 use Illuminate\Contracts\Support\Htmlable;
 use Illuminate\Contracts\Support\Renderable;
+use Illuminate\Support\Traits\Macroable;
 
 class Actions extends AbstractDisplayer
 {
+    use Macroable;
+
     /**
      * @var array
      */
@@ -76,7 +79,7 @@ class Actions extends AbstractDisplayer
     /**
      * @param mixed $action
      *
-     * @return void
+     * @return mixed
      */
     protected function prepareAction(&$action)
     {
@@ -85,6 +88,8 @@ class Actions extends AbstractDisplayer
                 ->setColumn($this->column)
                 ->setRow($this->row);
         }
+
+        return $action;
     }
 
     public function view(bool $value = true)
@@ -248,10 +253,10 @@ class Actions extends AbstractDisplayer
      */
     protected function renderView()
     {
-        return Show::make($this->getViewLabel())
-            ->setGrid($this->grid)
-            ->setRow($this->row)
-            ->render();
+        $action = config('admin.grid.actions.view') ?: Show::class;
+        $action = $action::make($this->getViewLabel());
+
+        return $this->prepareAction($action);
     }
 
     /**
@@ -271,10 +276,10 @@ class Actions extends AbstractDisplayer
      */
     protected function renderEdit()
     {
-        return Edit::make($this->getEditLabel())
-            ->setGrid($this->grid)
-            ->setRow($this->row)
-            ->render();
+        $action = config('admin.grid.actions.edit') ?: Edit::class;
+        $action = $action::make($this->getEditLabel());
+
+        return $this->prepareAction($action);
     }
 
     /**
@@ -292,10 +297,10 @@ class Actions extends AbstractDisplayer
      */
     protected function renderQuickEdit()
     {
-        return QuickEdit::make($this->getQuickEditLabel())
-            ->setGrid($this->grid)
-            ->setRow($this->row)
-            ->render();
+        $action = config('admin.grid.actions.quick_edit') ?: QuickEdit::class;
+        $action = $action::make($this->getQuickEditLabel());
+
+        return $this->prepareAction($action);
     }
 
     /**
@@ -315,10 +320,10 @@ class Actions extends AbstractDisplayer
      */
     protected function renderDelete()
     {
-        return Delete::make($this->getDeleteLabel())
-            ->setGrid($this->grid)
-            ->setRow($this->row)
-            ->render();
+        $action = config('admin.grid.actions.delete') ?: Delete::class;
+        $action = $action::make($this->getDeleteLabel());
+
+        return $this->prepareAction($action);
     }
 
     /**

+ 26 - 22
src/Grid/Displayers/DropdownActions.php

@@ -2,28 +2,16 @@
 
 namespace Dcat\Admin\Grid\Displayers;
 
-use Dcat\Admin\Grid\Actions\Delete;
-use Dcat\Admin\Grid\Actions\Edit;
-use Dcat\Admin\Grid\Actions\QuickEdit;
-use Dcat\Admin\Grid\Actions\Show;
 use Dcat\Admin\Support\Helper;
 
 class DropdownActions extends Actions
 {
-    /**
-     * @var array
-     */
-    protected $default = [];
+    protected $view = 'admin::grid.dropdown-actions';
 
     /**
      * @var array
      */
-    protected $defaultActions = [
-        'view'      => Show::class,
-        'edit'      => Edit::class,
-        'quickEdit' => QuickEdit::class,
-        'delete'    => Delete::class,
-    ];
+    protected $default = [];
 
     public function prepend($action)
     {
@@ -33,13 +21,13 @@ class DropdownActions extends Actions
     /**
      * @param mixed $action
      *
-     * @return void
+     * @return mixed
      */
     protected function prepareAction(&$action)
     {
         parent::prepareAction($action);
 
-        $action = $this->wrapCustomAction($action);
+        return $action = $this->wrapCustomAction($action);
     }
 
     /**
@@ -68,11 +56,7 @@ class DropdownActions extends Actions
                 continue;
             }
 
-            $action = new $this->defaultActions[$action]();
-
-            $this->prepareAction($action);
-
-            array_push($this->default, $action);
+            array_push($this->default, $this->{'render'.ucfirst($action)}());
         }
     }
 
@@ -95,6 +79,26 @@ class DropdownActions extends Actions
             'selector' => ".{$this->grid->getRowName()}-checkbox",
         ];
 
-        return view('admin::grid.dropdown-actions', $actions);
+        return view($this->view, $actions);
+    }
+
+    protected function getViewLabel()
+    {
+        return;
+    }
+
+    protected function getEditLabel()
+    {
+        return;
+    }
+
+    protected function getQuickEditLabel()
+    {
+        return;
+    }
+
+    protected function getDeleteLabel()
+    {
+        return;
     }
 }

+ 11 - 1
src/Grid/Tools.php

@@ -13,9 +13,12 @@ use Illuminate\Contracts\Support\Htmlable;
 use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Support\Collection;
 use Illuminate\Support\Str;
+use Illuminate\Support\Traits\Macroable;
 
 class Tools implements Renderable
 {
+    use Macroable;
+
     /**
      * Parent grid.
      *
@@ -54,11 +57,18 @@ class Tools implements Renderable
      */
     protected function appendDefaultTools()
     {
-        $this->append(new BatchActions())
+        $this->append($this->makeBatchActions())
             ->append(new RefreshButton())
             ->append(new FilterButton());
     }
 
+    protected function makeBatchActions()
+    {
+        $class = $this->grid->option('batch_actions_class') ?: (config('admin.grid.batch_action_class') ?: BatchActions::class);
+
+        return new $class();
+    }
+
     /**
      * Append tools.
      *

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

@@ -4,10 +4,17 @@ namespace Dcat\Admin\Grid\Tools;
 
 use Dcat\Admin\Admin;
 use Dcat\Admin\Grid\BatchAction;
+use Dcat\Admin\Traits\HasVariables;
 use Illuminate\Support\Collection;
+use Illuminate\Support\Traits\Macroable;
 
 class BatchActions extends AbstractTool
 {
+    use Macroable;
+    use HasVariables;
+
+    protected $view = 'admin::grid.batch-actions';
+
     /**
      * @var Collection
      */
@@ -40,7 +47,14 @@ class BatchActions extends AbstractTool
      */
     protected function appendDefaultAction()
     {
-        $this->add(new BatchDelete(trans('admin.delete')));
+        $this->add($this->makeBatchDelete());
+    }
+
+    protected function makeBatchDelete()
+    {
+        $class = config('admin.grid.actions.batch_delete') ?: BatchDelete::class;
+
+        return new $class(trans('admin.delete'));
     }
 
     /**
@@ -97,6 +111,16 @@ class BatchActions extends AbstractTool
         }
     }
 
+    protected function defaultVariables()
+    {
+        return [
+            'actions'                 => $this->actions,
+            'selectAllName'           => $this->parent->getSelectAllName(),
+            'isHoldSelectAllCheckbox' => $this->isHoldSelectAllCheckbox,
+            'parent'                  => $this->parent,
+        ];
+    }
+
     /**
      * Render BatchActions button groups.
      *
@@ -114,13 +138,6 @@ class BatchActions extends AbstractTool
 
         $this->prepareActions();
 
-        $data = [
-            'actions'                 => $this->actions,
-            'selectAllName'           => $this->parent->getSelectAllName(),
-            'isHoldSelectAllCheckbox' => $this->isHoldSelectAllCheckbox,
-            'parent'                  => $this->parent,
-        ];
-
-        return Admin::view('admin::grid.batch-actions', $data);
+        return Admin::view($this->view, $this->variables());
     }
 }

+ 1 - 1
src/Traits/LazyWidget.php

@@ -5,7 +5,7 @@ namespace Dcat\Admin\Traits;
 use Dcat\Admin\Admin;
 
 /**
- * Trait LazyWidget
+ * Trait LazyWidget.
  *
  * @package Dcat\Admin\Traits
  *