Prechádzať zdrojové kódy

增加暂存分步表单数据功能

jqh 5 rokov pred
rodič
commit
70279c0cf5

+ 14 - 5
src/Form/Concerns/HasEvents.php

@@ -187,16 +187,25 @@ trait HasEvents
      */
     protected function callListeners($name)
     {
+        $response = null;
+
         foreach ($this->__hooks[$name] as $func) {
             $this->model && $func->bindTo($this->model);
 
-            if (($ret = $func($this)) instanceof Response) {
-                if ($ret instanceof RedirectResponse && $this->isAjaxRequest()) {
-                    return;
-                }
+            $ret = $func($this);
 
-                return $ret;
+            if (
+                $response ||
+                ! $ret ||
+                ! $ret instanceof Response ||
+                ($ret instanceof RedirectResponse && $this->isAjaxRequest())
+            ) {
+                continue;
             }
+
+            $response = $ret;
         }
+
+        return $response;
     }
 }

+ 2 - 0
src/Form/Concerns/HasSteps.php

@@ -32,6 +32,8 @@ trait HasSteps
             return;
         }
 
+        $stepBuilder->stash($data);
+
         $steps = $stepBuilder->all();
 
         if ($this->isStepFormValidationRequest()) {

+ 59 - 2
src/Form/StepBuilder.php

@@ -30,12 +30,15 @@ class StepBuilder
      * @var array
      */
     protected $options = [
-        'width' => '1000px',
+        'width'    => '1000px',
+        'remember' => false,
     ];
 
     public function __construct(Form $form)
     {
-        $this->form = $form;
+        $this->form = $form->saved(function () {
+            $this->flushStash();
+        });
 
         $this->collectAssets();
     }
@@ -111,6 +114,15 @@ class StepBuilder
         return $this->option('width', $width);
     }
 
+    /**
+     * @param bool $value
+     * @return $this
+     */
+    public function remember(bool $value = true)
+    {
+        return $this->option('remember', $value);
+    }
+
     /**
      * @param string|Closure $title
      * @param Closure|null $callback
@@ -163,6 +175,51 @@ class StepBuilder
         });
     }
 
+    /**
+     * @param array $data
+     * @return void
+     */
+    public function stash(array $data)
+    {
+        if (! $this->options['remember']) {
+            return;
+        }
+
+        session()->put($this->getStashKey(), $data);
+    }
+
+    /**
+     * @return array
+     */
+    public function fetchStash()
+    {
+        if (! $this->options['remember']) {
+            return [];
+        }
+
+        return session()->get($this->getStashKey()) ?: [];
+    }
+
+    /**
+     * @return void
+     */
+    public function flushStash()
+    {
+        if (! $this->options['remember']) {
+            return;
+        }
+
+        session()->remove($this->getStashKey());
+    }
+
+    /**
+     * @return string
+     */
+    protected function getStashKey()
+    {
+        return 'step-form-input:'.admin_controller_slug();
+    }
+
     /**
      * @return void
      */

+ 31 - 1
src/Form/StepForm.php

@@ -22,6 +22,11 @@ class StepForm extends WidgetForm
      */
     protected $form;
 
+    /**
+     * @var StepBuilder
+     */
+    protected $parent;
+
     /**
      * @var int
      */
@@ -46,7 +51,8 @@ class StepForm extends WidgetForm
      */
     public function __construct(Form $form, int $index = 0, string $title = null)
     {
-        $this->form  = $form;
+        $this->form   = $form;
+        $this->parent = $form->builder()->getStepBuilder();
         $this->index = $index;
 
         $this->title($title);
@@ -123,4 +129,28 @@ HTML;
         return '</div>';
     }
 
+    /**
+     * @return void
+     */
+    protected function fillStash()
+    {
+        if ($this->data) {
+            return;
+        }
+
+        if ($input = $this->parent->fetchStash()) {
+            $this->fill($input);
+        }
+    }
+
+    /**
+     * @return string
+     */
+    public function render()
+    {
+        $this->fillStash();
+
+        return parent::render(); // TODO: Change the autogenerated stub
+    }
+
 }

+ 11 - 1
src/Widgets/Form.php

@@ -7,6 +7,7 @@ use Dcat\Admin\Admin;
 use Dcat\Admin\Form\Field;
 use Dcat\Admin\Support\Helper;
 use Dcat\Admin\Traits\HasHtmlAttributes;
+use Illuminate\Contracts\Support\Arrayable;
 use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Support\Traits\Macroable;
 use Illuminate\Support\Arr;
@@ -210,7 +211,7 @@ class Form implements Renderable
     }
 
     /**
-     * @param $data
+     * @param array|Arrayable|Closure $data
      * @return $this
      */
     public function data($data)
@@ -220,6 +221,15 @@ class Form implements Renderable
         return $this;
     }
 
+    /**
+     * @param array|Arrayable|Closure $data
+     * @return $this
+     */
+    public function fill($data)
+    {
+        return $this->data($data);
+    }
+
     /**
      * @return Fluent
      */