Selaa lähdekoodia

重写默认Actions按钮控制功能

jqh 5 vuotta sitten
vanhempi
commit
0b6ce5e724

+ 3 - 3
resources/views/form/footer.blade.php

@@ -7,18 +7,18 @@
 
     <div class="col-md-{{$width['field']}}">
 
-        @if(in_array('submit', $buttons))
+        @if(! empty($buttons['submit']))
         <div class="btn-group pull-right">
             <button type="submit" class="btn btn-primary">{{ trans('admin.submit') }}</button>
         </div>
 
-        @if ($checkboxes)
+        @if($checkboxes)
             <label class="pull-right" style="margin:0 15px 0 0;">{!! $checkboxes !!}</label>
         @endif
 
         @endif
 
-        @if(in_array('reset', $buttons))
+        @if(! empty($buttons['reset']))
         <div class="btn-group pull-left">
             <button type="reset" class="btn btn-warning">{{ trans('admin.reset') }}</button>
         </div>

+ 2 - 2
resources/views/widgets/form.blade.php

@@ -17,13 +17,13 @@
         <div class="col-md-2"></div>
 
         <div class="col-md-8">
-            @if(in_array('reset', $buttons))
+            @if(! empty($buttons['reset']))
             <div class="btn-group pull-left">
                 <button type="reset" class="btn btn-warning pull-right">{{ trans('admin.reset') }}</button>
             </div>
             @endif
 
-            @if(in_array('submit', $buttons))
+            @if(! empty($buttons['submit']))
             <div class="btn-group pull-right">
                 <button type="submit" class="btn btn-primary pull-right">{{ trans('admin.submit') }}</button>
             </div>

+ 10 - 31
src/Form/Footer.php

@@ -2,7 +2,6 @@
 
 namespace Dcat\Admin\Form;
 
-use Dcat\Admin\Support\Helper;
 use Dcat\Admin\Widgets\Checkbox;
 use Illuminate\Contracts\Support\Renderable;
 
@@ -27,14 +26,14 @@ class Footer implements Renderable
      *
      * @var array
      */
-    protected $buttons = ['reset', 'submit'];
+    protected $buttons = ['reset' => true, 'submit' => true];
 
     /**
      * Available checkboxes.
      *
      * @var array
      */
-    protected $checkboxes = ['view', 'continue_editing', 'continue_creating'];
+    protected $checkboxes = ['view' => true, 'continue_editing' => true, 'continue_creating' => true];
 
     /**
      * Footer constructor.
@@ -55,11 +54,7 @@ class Footer implements Renderable
      */
     public function disableReset(bool $disable = true)
     {
-        if ($disable) {
-            Helper::deleteByValue($this->buttons, 'reset');
-        } elseif (! in_array('reset', $this->buttons)) {
-            array_push($this->buttons, 'reset');
-        }
+        $this->buttons['reset'] = ! $disable;
 
         return $this;
     }
@@ -73,11 +68,7 @@ class Footer implements Renderable
      */
     public function disableSubmit(bool $disable = true)
     {
-        if ($disable) {
-            Helper::deleteByValue($this->buttons, 'submit');
-        } elseif (! in_array('submit', $this->buttons)) {
-            array_push($this->buttons, 'submit');
-        }
+        $this->buttons['submit'] = ! $disable;
 
         return $this;
     }
@@ -91,11 +82,7 @@ class Footer implements Renderable
      */
     public function disableViewCheck(bool $disable = true)
     {
-        if ($disable) {
-            Helper::deleteByValue($this->checkboxes, 'view');
-        } elseif (! in_array('view', $this->checkboxes)) {
-            array_push($this->checkboxes, 'view');
-        }
+        $this->checkboxes['view'] = ! $disable;
 
         return $this;
     }
@@ -109,11 +96,7 @@ class Footer implements Renderable
      */
     public function disableEditingCheck(bool $disable = true)
     {
-        if ($disable) {
-            Helper::deleteByValue($this->checkboxes, 'continue_editing');
-        } elseif (! in_array('continue_editing', $this->checkboxes)) {
-            array_push($this->checkboxes, 'continue_editing');
-        }
+        $this->checkboxes['continue_editing'] = ! $disable;
 
         return $this;
     }
@@ -127,11 +110,7 @@ class Footer implements Renderable
      */
     public function disableCreatingCheck(bool $disable = true)
     {
-        if ($disable) {
-            Helper::deleteByValue($this->checkboxes, 'continue_creating');
-        } elseif (! in_array('continue_creating', $this->checkboxes)) {
-            array_push($this->checkboxes, 'continue_creating');
-        }
+        $this->checkboxes['continue_creating'] = ! $disable;
 
         return $this;
     }
@@ -149,15 +128,15 @@ class Footer implements Renderable
 
         $options = [];
 
-        if (in_array('continue_editing', $this->checkboxes)) {
+        if ($this->checkboxes['continue_editing']) {
             $options[1] = sprintf('<span class="text-80 text-bold">%s</span>', trans('admin.continue_editing'));
         }
 
-        if (in_array('continue_creating', $this->checkboxes)) {
+        if ($this->checkboxes['continue_creating']) {
             $options[2] = sprintf('<span class="text-80 text-bold">%s</span>', trans('admin.continue_creating'));
         }
 
-        if (in_array('view', $this->checkboxes)) {
+        if ($this->checkboxes['view']) {
             $options[3] = sprintf('<span class="text-80 text-bold">%s</span>', trans('admin.view'));
         }
 

+ 55 - 43
src/Grid/Displayers/Actions.php

@@ -28,7 +28,12 @@ class Actions extends AbstractDisplayer
      *
      * @var array
      */
-    protected $actions = ['view', 'edit', 'quickEdit', 'delete'];
+    protected $actions = [
+        'view'      => true,
+        'edit'      => true,
+        'quickEdit' => true,
+        'delete'    => true,
+    ];
 
     /**
      * @var string
@@ -44,9 +49,7 @@ class Actions extends AbstractDisplayer
      */
     public function append($action)
     {
-        if ($action instanceof RowAction) {
-            $this->prepareAction($action);
-        }
+        $this->prepareAction($action);
 
         array_push($this->appends, $action);
 
@@ -62,9 +65,7 @@ class Actions extends AbstractDisplayer
      */
     public function prepend($action)
     {
-        if ($action instanceof RowAction) {
-            $this->prepareAction($action);
-        }
+        $this->prepareAction($action);
 
         array_unshift($this->prepends, $action);
 
@@ -72,75 +73,76 @@ class Actions extends AbstractDisplayer
     }
 
     /**
-     * @param RowAction $action
+     * @param mixed $action
+     *
+     * @return void
      */
-    protected function prepareAction(RowAction $action)
+    protected function prepareAction(&$action)
     {
-        $action->setGrid($this->grid)
-            ->setColumn($this->column)
-            ->setRow($this->row);
+        if ($action instanceof RowAction) {
+            $action->setGrid($this->grid)
+                ->setColumn($this->column)
+                ->setRow($this->row);
+        }
     }
 
     /**
      * Disable view action.
      *
+     * @param bool $disable
+     *
      * @return $this
      */
     public function disableView(bool $disable = true)
     {
-        if ($disable) {
-            Helper::deleteByValue($this->actions, 'view');
-        } elseif (! in_array('view', $this->actions)) {
-            array_push($this->actions, 'view');
-        }
-
-        return $this;
+        return $this->disableDefaultAction('view', $disable);
     }
 
     /**
      * Disable delete.
      *
+     * @param bool $disable
+     *
      * @return $this.
      */
     public function disableDelete(bool $disable = true)
     {
-        if ($disable) {
-            Helper::deleteByValue($this->actions, 'delete');
-        } elseif (! in_array('delete', $this->actions)) {
-            array_push($this->actions, 'delete');
-        }
-
-        return $this;
+        return $this->disableDefaultAction('delete', $disable);
     }
 
     /**
      * Disable edit.
      *
+     * @param bool $disable
+     *
      * @return $this.
      */
     public function disableEdit(bool $disable = true)
     {
-        if ($disable) {
-            Helper::deleteByValue($this->actions, 'edit');
-        } elseif (! in_array('edit', $this->actions)) {
-            array_push($this->actions, 'edit');
-        }
-
-        return $this;
+        return $this->disableDefaultAction('edit', $disable);
     }
 
     /**
      * Disable quick edit.
      *
+     * @param bool $disable
+     *
      * @return $this.
      */
     public function disableQuickEdit(bool $disable = true)
     {
-        if ($disable) {
-            Helper::deleteByValue($this->actions, 'quickEdit');
-        } elseif (! in_array('quickEdit', $this->actions)) {
-            array_push($this->actions, 'quickEdit');
-        }
+        return $this->disableDefaultAction('quickEdit', $disable);
+    }
+
+    /**
+     * @param string $key
+     * @param bool $disable
+     *
+     * @return $this
+     */
+    protected function disableDefaultAction(string $key, bool $disable)
+    {
+        $this->actions[$key] = ! $disable;
 
         return $this;
     }
@@ -170,14 +172,22 @@ class Actions extends AbstractDisplayer
     }
 
     /**
-     * {@inheritdoc}
+     * @return void
      */
-    public function display(array $callbacks = [])
+    protected function resetDefaultActions()
     {
         $this->disableView(! $this->grid->option('show_view_button'));
         $this->disableEdit(! $this->grid->option('show_edit_button'));
         $this->disableQuickEdit(! $this->grid->option('show_quick_edit_button'));
         $this->disableDelete(! $this->grid->option('show_delete_button'));
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function display(array $callbacks = [])
+    {
+        $this->resetDefaultActions();
 
         foreach ($callbacks as $callback) {
             if ($callback instanceof \Closure) {
@@ -191,9 +201,11 @@ class Actions extends AbstractDisplayer
         $appends = array_map($map, $this->appends);
         $actions = &$prepends;
 
-        foreach ($this->actions as $action) {
-            $method = 'render'.ucfirst($action);
-            array_push($actions, $this->{$method}());
+        foreach ($this->actions as $action => $enable) {
+            if ($enable) {
+                $method = 'render'.ucfirst($action);
+                array_push($actions, $this->{$method}());
+            }
         }
 
         $actions = array_merge($actions, $appends);

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

@@ -27,7 +27,12 @@ class DropdownActions extends Actions
     /**
      * @var array
      */
-    protected $defaultClass = [Edit::class, QuickEdit::class, Show::class, Delete::class];
+    protected $defaultClass = [
+        'view'      => Show::class,
+        'edit'      => Edit::class,
+        'quickEdit' => QuickEdit::class,
+        'delete'    => Delete::class,
+    ];
 
     /**
      * Add JS script into pages.
@@ -75,31 +80,27 @@ JS;
         Admin::script($script);
     }
 
+    public function prepend($action)
+    {
+        return $this->append($action);
+    }
+
     /**
-     * @param RowAction|string|Renderable $action
+     * @param mixed $action
      *
-     * @return $this
+     * @return void
      */
-    public function append($action)
+    protected function prepareAction(&$action)
     {
-        if ($action instanceof RowAction) {
-            $this->prepareAction($action);
-        }
+        parent::prepareAction($action);
 
-        array_push($this->custom, $this->wrapCustomAction($action));
-
-        return $this;
-    }
-
-    public function prepend($action)
-    {
-        return $this->append($action);
+        $action = $this->wrapCustomAction($action);
     }
 
     /**
-     * @param $action
+     * @param mixed $action
      *
-     * @return mixed
+     * @return string
      */
     protected function wrapCustomAction($action)
     {
@@ -117,9 +118,12 @@ JS;
      */
     protected function prependDefaultActions()
     {
-        foreach ($this->defaultClass as $class) {
-            /** @var RowAction $action */
-            $action = new $class();
+        foreach ($this->actions as $action => $enable) {
+            if (! $enable) {
+                continue;
+            }
+
+            $action = new $this->defaultClass[$action]();
 
             $this->prepareAction($action);
 
@@ -127,75 +131,6 @@ JS;
         }
     }
 
-    /**
-     * Disable view action.
-     *
-     * @param bool $disable
-     *
-     * @return $this
-     */
-    public function disableView(bool $disable = true)
-    {
-        if ($disable) {
-            Helper::deleteByValue($this->defaultClass, Show::class);
-        } elseif (! in_array(Show::class, $this->defaultClass)) {
-            array_push($this->defaultClass, Show::class);
-        }
-
-        return $this;
-    }
-
-    /**
-     * Disable delete.
-     *
-     * @param bool $disable
-     *
-     * @return $this.
-     */
-    public function disableDelete(bool $disable = true)
-    {
-        if ($disable) {
-            Helper::deleteByValue($this->defaultClass, Delete::class);
-        } elseif (! in_array(Delete::class, $this->defaultClass)) {
-            array_push($this->defaultClass, Delete::class);
-        }
-
-        return $this;
-    }
-
-    /**
-     * Disable edit.
-     *
-     * @param bool $disable
-     *
-     * @return $this
-     */
-    public function disableEdit(bool $disable = true)
-    {
-        if ($disable) {
-            Helper::deleteByValue($this->defaultClass, Edit::class);
-        } elseif (! in_array(Edit::class, $this->defaultClass)) {
-            array_push($this->defaultClass, Edit::class);
-        }
-
-        return $this;
-    }
-
-    /**
-     * Disable quick edit.
-     *
-     * @return $this.
-     */
-    public function disableQuickEdit(bool $disable = true)
-    {
-        if ($disable) {
-            Helper::deleteByValue($this->defaultClass, QuickEdit::class);
-        } elseif (! in_array(Show::class, $this->defaultClass)) {
-            array_push($this->defaultClass, QuickEdit::class);
-        }
-
-        return $this;
-    }
 
     /**
      * @param \Closure[] $callback
@@ -204,10 +139,7 @@ JS;
      */
     public function display($callbacks = [])
     {
-        $this->disableView(! $this->grid->option('show_view_button'));
-        $this->disableEdit(! $this->grid->option('show_edit_button'));
-        $this->disableQuickEdit(! $this->grid->option('show_quick_edit_button'));
-        $this->disableDelete(! $this->grid->option('show_delete_button'));
+        $this->resetDefaultActions();
 
         $this->addScript();
 
@@ -221,7 +153,7 @@ JS;
 
         $actions = [
             'default' => $this->default,
-            'custom'  => $this->custom,
+            'custom'  => $this->appends,
         ];
 
         return view('admin::grid.dropdown-actions', $actions);

+ 3 - 3
src/Widgets/Form.php

@@ -115,7 +115,7 @@ class Form implements Renderable
      *
      * @var array
      */
-    protected $buttons = ['reset', 'submit'];
+    protected $buttons = ['reset' => true, 'submit' => true];
 
     /**
      * @var bool
@@ -380,7 +380,7 @@ class Form implements Renderable
      */
     public function disableResetButton()
     {
-        Helper::deleteByValue($this->buttons, 'reset');
+        $this->buttons['reset'] = false;
 
         return $this;
     }
@@ -392,7 +392,7 @@ class Form implements Renderable
      */
     public function disableSubmitButton()
     {
-        Helper::deleteByValue($this->buttons, 'submit');
+        $this->buttons['submit'] = false;
 
         return $this;
     }