Browse Source

重写Form tool

jqh 5 years ago
parent
commit
b96cd8d048

+ 1 - 1
src/Actions/Action.php

@@ -69,7 +69,7 @@ abstract class Action implements Renderable
     }
 
     /**
-     * Toggle this button.
+     * Toggle this action.
      *
      * @param bool $disable
      *

+ 4 - 4
src/Actions/Response.php

@@ -91,7 +91,7 @@ class Response
      */
     public function redirect(string $url)
     {
-        return $this->then(['action' => 'redirect', 'value' => $url]);
+        return $this->then(['action' => 'redirect', 'value' => admin_url($url)]);
     }
 
     /**
@@ -103,7 +103,7 @@ class Response
      */
     public function location(string $location)
     {
-        return $this->then(['action' => 'location', 'value' => $location]);
+        return $this->then(['action' => 'location', 'value' => admin_url($location)]);
     }
 
     /**
@@ -167,11 +167,11 @@ class Response
     }
 
     /**
-     * @param \Exception $exception
+     * @param \Throwable $exception
      *
      * @return mixed
      */
-    public static function withException(\Exception $exception)
+    public static function withException(\Throwable $exception)
     {
         $response = new static();
 

+ 4 - 4
src/Controllers/HandleActionController.php

@@ -2,8 +2,8 @@
 
 namespace Dcat\Admin\Controllers;
 
+use Dcat\Admin\Actions\Action;
 use Dcat\Admin\Actions\Response;
-use Dcat\Admin\Grid\GridAction;
 use Exception;
 use Illuminate\Http\Request;
 
@@ -38,9 +38,9 @@ class HandleActionController
      *
      * @throws Exception
      *
-     * @return GridAction
+     * @return Action
      */
-    protected function resolveActionInstance(Request $request): GridAction
+    protected function resolveActionInstance(Request $request): Action
     {
         if (! $request->has('_action')) {
             throw new Exception('Invalid action request.');
@@ -52,7 +52,7 @@ class HandleActionController
             throw new Exception("Action [{$actionClass}] does not exist.");
         }
 
-        /** @var GridAction $action */
+        /** @var Action $action */
         $action = app($actionClass);
 
         if (! method_exists($action, 'handle')) {

+ 17 - 3
src/Form.php

@@ -3,7 +3,9 @@
 namespace Dcat\Admin;
 
 use Closure;
+use Dcat\Admin\Actions\Action;
 use Dcat\Admin\Contracts\Repository;
+use Dcat\Admin\Form\AbstractTool;
 use Dcat\Admin\Form\Builder;
 use Dcat\Admin\Form\Concerns;
 use Dcat\Admin\Form\Condition;
@@ -1363,13 +1365,25 @@ class Form implements Renderable
     /**
      * Tools setting for form.
      *
-     * @param Closure $callback
+     * @param Closure|string|AbstractTool|Renderable|Action|array $callback
      *
      * @return $this;
      */
-    public function tools(Closure $callback)
+    public function tools($callback)
     {
-        $callback->call($this, $this->builder->tools());
+        if ($callback instanceof Closure) {
+            $callback->call($this, $this->builder->tools());
+
+            return $this;
+        }
+
+        if (! is_array($callback)) {
+            $callback = [$callback];
+        }
+
+        foreach ($callback as $tool) {
+            $this->builder->tools()->append($tool);
+        }
 
         return $this;
     }

+ 131 - 0
src/Form/AbstractTool.php

@@ -0,0 +1,131 @@
+<?php
+
+namespace Dcat\Admin\Form;
+
+use Dcat\Admin\Actions\Action;
+use Dcat\Admin\Form;
+
+abstract class AbstractTool extends Action
+{
+    /**
+     * @var Form
+     */
+    protected $parent;
+
+    /**
+     * @var string
+     */
+    public $selectorPrefix = '.form-tool-action-';
+
+    /**
+     * @var string
+     */
+    protected $style = 'btn btn-sm btn-primary';
+
+    /**
+     * Whether the action should only allow in creation page.
+     *
+     * @var bool
+     */
+    public $allowOnlyCreating = false;
+
+    /**
+     * Whether the action should only allow in edit page.
+     *
+     * @var bool
+     */
+    public $allowOnlyEditing = false;
+
+    /**
+     * @param Form $form
+     *
+     * @return void
+     */
+    public function setForm(Form $form)
+    {
+        $this->parent = $form;
+    }
+
+    /**
+     * @return array|mixed|string|null
+     */
+    public function key()
+    {
+        if ($this->primaryKey) {
+            return $this->primaryKey;
+        }
+
+        return $this->parent ? $this->parent->key() : null;
+    }
+
+    /**
+     * @return string|void
+     */
+    protected function href()
+    {
+    }
+
+    /**
+     * @return string
+     */
+    public function render()
+    {
+        if ($this->allowOnlyEditing && ! $this->parent->isEditing()) {
+            return '';
+        }
+
+        if ($this->allowOnlyCreating && ! $this->parent->isCreating()) {
+            return '';
+        }
+
+        return parent::render(); // TODO: Change the autogenerated stub
+    }
+
+    /**
+     * @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>";
+    }
+
+    /**
+     * @param mixed ...$params
+     *
+     * @return $this
+     */
+    public static function allowOnlyCreating(...$params)
+    {
+        $tool = static::make(...$params);
+
+        $tool->allowOnlyCreating = true;
+        $tool->allowOnlyEditing = false;
+
+        return $tool;
+    }
+
+    /**
+     * @param mixed ...$params
+     *
+     * @return $this
+     */
+    public static function allowOnlyEditing(...$params)
+    {
+        $tool = static::make(...$params);
+
+        $tool->allowOnlyEditing = true;
+        $tool->allowOnlyCreating = false;
+
+        return $tool;
+    }
+}

+ 8 - 0
src/Form/Builder.php

@@ -545,6 +545,14 @@ class Builder
         return $this->form->rows();
     }
 
+    /**
+     * @return Form
+     */
+    public function form()
+    {
+        return $this->form;
+    }
+
     /**
      * @return array
      */

+ 18 - 2
src/Form/Tools.php

@@ -65,12 +65,14 @@ class Tools implements Renderable
     /**
      * Append a tools.
      *
-     * @param string|\Closure|Renderable|Htmlable $tool
+     * @param string|\Closure|Renderable|Htmlable|AbstractTool $tool
      *
      * @return $this
      */
     public function append($tool)
     {
+        $this->prepareTool($tool);
+
         $this->appends->push($tool);
 
         return $this;
@@ -79,17 +81,31 @@ class Tools implements Renderable
     /**
      * Prepend a tool.
      *
-     * @param string|\Closure|Renderable|Htmlable $tool
+     * @param string|\Closure|Renderable|Htmlable|AbstractTool $tool
      *
      * @return $this
      */
     public function prepend($tool)
     {
+        $this->prepareTool($tool);
+
         $this->prepends->push($tool);
 
         return $this;
     }
 
+    /**
+     * @param mixed $tool
+     *
+     * @return void
+     */
+    protected function prepareTool($tool)
+    {
+        if ($tool instanceof AbstractTool) {
+            $tool->setForm($this->form->form());
+        }
+    }
+
     /**
      * Disable `list` tool.
      *