瀏覽代碼

Merge branch 'dev'

jqh 5 年之前
父節點
當前提交
4b7c8d0373
共有 6 個文件被更改,包括 231 次插入128 次删除
  1. 10 10
      resources/views/form/steps.blade.php
  2. 20 30
      src/Form.php
  3. 17 73
      src/Form/Builder.php
  4. 9 3
      src/Form/DoneStep.php
  5. 175 0
      src/Form/StepBuilder.php
  6. 0 12
      src/Form/StepForm.php

+ 10 - 10
resources/views/form/steps.blade.php

@@ -6,11 +6,11 @@
 @endif
 
 <div class="box-body" style="padding:18px 18px 30px">
-    @if($steps)
-        <div class="fields-group la-step-box" style="padding:18px;max-width: 1000px">
+    @if($steps->count())
+        <div class="fields-group la-step-box" style="padding:18px;max-width: {{ $steps->getOption('width') }}">
 
             <ul class="la-step-horizontal la-step-label-horizontal la-step ">
-                @foreach($steps as $step)
+                @foreach($steps->all() as $step)
                 <li class="la-step-item">
                     <a href="#{{ $step->getFormId() }}" class="la-step-item-container">
                         <div class="la-step-line"></div>
@@ -26,24 +26,24 @@
                 @endforeach
 
                 <li class="la-step-item">
-                    <a href="#{{ $doneStep->getElementId() }}" class="la-step-item-container">
+                    <a href="#{{ $steps->getDoneStep()->getElementId() }}" class="la-step-item-container">
                         <div class="la-step-line"></div>
                         <div class="la-step-icons">
-                            <span class="la-step-icon" data-index="{{ count($steps) }}"> {{ count($steps) + 1 }} </span>
+                            <span class="la-step-icon" data-index="{{ $steps->count() }}"> {{ $steps->count() + 1 }} </span>
                         </div>
                         <div class="la-step-content">
-                            <div class="la-step-title">{{ $doneStep->title() }}</div>
+                            <div class="la-step-title">{{ $steps->getDoneStep()->title() }}</div>
                             <div class="la-step-desc"></div>
                         </div>
                     </a>
                 </li>
             </ul>
             <div class="la-step-form">
-                @foreach($steps as $step)
+                @foreach($steps->all() as $step)
                     {!! $step->render() !!}
                 @endforeach
 
-                <div id="{{ $doneStep->getElementId() }}" class="la-done-step" style="display: none;">
+                <div id="{{ $steps->getDoneStep()->getElementId() }}" class="la-done-step" style="display: none;">
                 </div>
             </div>
         </div>
@@ -54,8 +54,8 @@
     {!! $field->render() !!}
 @endforeach
 
-<input type="hidden" class="current-step-input" name="{{ Dcat\Admin\Form\StepForm::CURRENT_VALIDATION_STEP }}" />
-<input type="hidden" class="all-steps-input" name="{{ Dcat\Admin\Form\StepForm::ALL_STEPS }}" />
+<input type="hidden" class="current-step-input" name="{{ Dcat\Admin\Form\StepBuilder::CURRENT_VALIDATION_STEP }}" />
+<input type="hidden" class="all-steps-input" name="{{ Dcat\Admin\Form\StepBuilder::ALL_STEPS }}" />
 <input type="hidden" name="_token" value="{{ csrf_token() }}">
 
 @php

+ 20 - 30
src/Form.php

@@ -7,7 +7,7 @@ use Dcat\Admin\Form\Builder;
 use Dcat\Admin\Form\Condition;
 use Dcat\Admin\Form\Field;
 use Dcat\Admin\Form\Row;
-use Dcat\Admin\Form\StepForm;
+use Dcat\Admin\Form\StepBuilder;
 use Dcat\Admin\Form\Tab;
 use Dcat\Admin\Contracts\Repository;
 use Dcat\Admin\Traits\HasBuilderEvents;
@@ -633,17 +633,20 @@ class Form implements Renderable
      */
     protected function prepareStepFormFields(array $data)
     {
-        $steps = $this->builder->getSteps();
+        $stepBuilder = $this->builder->getStepBuilder();
 
         if (
-            empty($steps)
-            || (! isset($data[StepForm::ALL_STEPS]) && ! $this->isStepFormValidationRequest())
+            empty($stepBuilder)
+            || empty($stepBuilder->count())
+            || (! isset($data[StepBuilder::ALL_STEPS]) && ! $this->isStepFormValidationRequest())
         ) {
             return;
         }
 
+        $steps = $stepBuilder->all();
+
         if ($this->isStepFormValidationRequest()) {
-            $currentIndex = $data[StepForm::CURRENT_VALIDATION_STEP];
+            $currentIndex = $data[StepBuilder::CURRENT_VALIDATION_STEP];
 
             if (empty($steps[$currentIndex])) {
                 return;
@@ -655,7 +658,7 @@ class Form implements Renderable
             return;
         }
 
-        if (! empty($data[StepForm::ALL_STEPS])) {
+        if (! empty($data[StepBuilder::ALL_STEPS])) {
             foreach ($steps as $stepForm) {
                 foreach ($stepForm->field() as $field) {
                     $this->pushField($field);
@@ -669,7 +672,7 @@ class Form implements Renderable
      */
     protected function isStepFormValidationRequest()
     {
-        $index = $this->request->get(StepForm::CURRENT_VALIDATION_STEP);
+        $index = $this->request->get(StepBuilder::CURRENT_VALIDATION_STEP);
 
         return $index !== '' && $index !== null;
     }
@@ -695,13 +698,15 @@ class Form implements Renderable
      */
     protected function responseDoneStep()
     {
-        if (! $done = $this->builder->getDoneStep()) {
+        if (! $builder = $this->builder->getStepBuilder()) {
             return;
         }
 
-        $done->finish();
-
-        return response($done->render());
+        return response(
+            $builder->getDoneStep()
+                ->finish()
+                ->render()
+        );
     }
 
     /**
@@ -1462,27 +1467,12 @@ class Form implements Renderable
     }
 
     /**
-     * @param string $title
-     * @param Closure $callback
-     * @return $this
-     */
-    public function step(string $title, \Closure $callback)
-    {
-        $this->builder->step($title, $callback);
-
-        return $this;
-    }
-
-    /**
-     * @param $title
-     * @param Closure|null $callback
-     * @return $this
+     * @param Closure|null $builder
+     * @return StepBuilder
      */
-    public function done($title, \Closure $callback = null)
+    public function step(\Closure $builder = null)
     {
-        $this->builder->done($title, $callback);
-
-        return $this;
+        return $this->builder->step($builder);
     }
 
     /**

+ 17 - 73
src/Form/Builder.php

@@ -136,14 +136,9 @@ class Builder
     protected $showFooter = true;
 
     /**
-     * @var StepForm[]
+     * @var StepBuilder
      */
-    protected $stepForms = [];
-
-    /**
-     * @var DoneStep
-     */
-    protected $doneStep;
+    protected $stepBuilder;
 
     /**
      * Builder constructor.
@@ -175,10 +170,10 @@ class Builder
     }
 
     /**
-     * @param \Closure $closure
+     * @param Closure $closure
      * @return $this;
      */
-    public function wrap(\Closure $closure)
+    public function wrap(Closure $closure)
     {
         $this->wrapper = $closure;
 
@@ -239,80 +234,30 @@ class Builder
     }
 
     /**
-     * @param string $title
      * @param \Closure $callback
-     * @return void
+     * @return StepBuilder
      */
-    public function step(string $title, \Closure $callback)
+    public function step(?\Closure $callback = null)
     {
-        $this->view = 'admin::form.steps';
-
-        $form = new StepForm($this->form, count($this->stepForms), $title);
-
-        $this->stepForms[] = $form;
-
-        $callback($form);
-    }
-
+        if (! $this->stepBuilder) {
+            $this->view = 'admin::form.steps';
 
-    /**
-     * @param string $title
-     * @param Closure|null $callback
-     * @return $this
-     */
-    public function done($title, \Closure $callback = null)
-    {
-        if ($title instanceof \Closure) {
-            $callback = $title;
-            $title    = trans('admin.done');
+            $this->stepBuilder = new StepBuilder($this->form);
         }
 
-        $this->doneStep = new DoneStep($this->form, $title, $callback);
-
-        return $this;
-    }
-
-    /**
-     * @return StepForm[]
-     */
-    public function getSteps()
-    {
-        return $this->stepForms;
-    }
-
-    /**
-     * @return DoneStep|null
-     */
-    public function getDoneStep()
-    {
-        if (! $this->stepForms) {
-            return;
-        }
-
-        if (! $this->doneStep) {
-            $this->setDefaultDonePage();
+        if ($callback) {
+            $callback($this->stepBuilder);
         }
 
-        return $this->doneStep;
+        return $this->stepBuilder;
     }
 
     /**
-     * @return void
+     * @return StepBuilder
      */
-    protected function setDefaultDonePage()
+    public function getStepBuilder()
     {
-        $this->done(function () {
-            $resource = $this->form->getResource(0);
-
-            $data = [
-                'title'       => trans('admin.save_succeeded'),
-                'description' => '',
-                'createUrl'   => $resource.'/create',
-                'backUrl'     => $resource,
-            ];
-
-            return view('admin::form.done-step', $data);
-        });
+        return $this->stepBuilder;
     }
 
     /**
@@ -814,7 +759,7 @@ class Builder
             $this->setupTabScript();
         }
 
-        if ($this->form->allowAjaxSubmit() && empty($this->stepForms)) {
+        if ($this->form->allowAjaxSubmit() && empty($this->stepBuilder)) {
             $this->setupSubmitScript();
         }
 
@@ -826,8 +771,7 @@ class Builder
             'width'      => $this->width,
             'formId'     => $this->getFormId(),
             'showHeader' => $this->showHeader,
-            'steps'      => $this->stepForms,
-            'doneStep'   => $this->getDoneStep(),
+            'steps'      => $this->stepBuilder,
         ];
 
         $this->layout->prepend(

+ 9 - 3
src/Form/DoneStep.php

@@ -54,7 +54,7 @@ class DoneStep
 
     /**
      * @param string $title
-     * @return void|string
+     * @return $this|string
      */
     public function title($title = null)
     {
@@ -63,15 +63,19 @@ class DoneStep
         }
 
         $this->title = value($title);
+
+        return $this;
     }
 
     /**
      * @param string|\Closure|Renderable $contents
-     * @return void
+     * @return $this
      */
     public function content($contents)
     {
         $this->contents = $contents;
+
+        return $this;
     }
 
     /**
@@ -107,7 +111,7 @@ class DoneStep
     }
 
     /**
-     * @return void
+     * @return $this
      */
     public function finish()
     {
@@ -116,6 +120,8 @@ class DoneStep
         if ($value) {
             $this->content($value);
         }
+
+        return $this;
     }
 
     /**

+ 175 - 0
src/Form/StepBuilder.php

@@ -0,0 +1,175 @@
+<?php
+
+namespace Dcat\Admin\Form;
+
+use Closure;
+use Dcat\Admin\Admin;
+use Dcat\Admin\Form;
+
+class StepBuilder
+{
+    const CURRENT_VALIDATION_STEP = 'CURRENT_VALIDATION_STEP';
+    const ALL_STEPS = 'ALL_STEPS';
+
+    /**
+     * @var Form
+     */
+    protected $form;
+
+    /**
+     * @var StepForm[]
+     */
+    protected $stepForms = [];
+
+    /**
+     * @var DoneStep
+     */
+    protected $doneStep;
+
+    /**
+     * @var array
+     */
+    protected $options = [
+        'width' => '1000px',
+    ];
+
+    public function __construct(Form $form)
+    {
+        $this->form = $form;
+
+        $this->collectAssets();
+    }
+
+    /**
+     * @param string $title
+     * @param \Closure $callback
+     * @return $this
+     */
+    public function add(string $title, \Closure $callback)
+    {
+        $form = new StepForm($this->form, count($this->stepForms), $title);
+
+        $this->stepForms[] = $form;
+
+        $callback($form);
+
+        return $this;
+    }
+
+    /**
+     * @return StepForm[]
+     */
+    public function all()
+    {
+        return $this->stepForms;
+    }
+
+    /**
+     * @return int
+     */
+    public function count()
+    {
+        return count($this->stepForms);
+    }
+
+    /**
+     * @param string|array $key
+     * @param mixed $value
+     * @return $this
+     */
+    public function option($key, $value = null)
+    {
+        if (is_array($key)) {
+            $this->options = array_merge($this->options, $key);
+        } else {
+            $this->options[$key] = $value;
+        }
+
+        return $this;
+    }
+
+    /**
+     * @param string|null $key
+     * @param null $default
+     * @return array|mixed|null
+     */
+    public function getOption($key = null, $default = null)
+    {
+        if ($key === null) {
+            return $this->options;
+        }
+
+        return $this->options[$key] ?? $default;
+    }
+
+    /**
+     * @param string $width
+     * @return $this
+     */
+    public function width(string $width)
+    {
+        return $this->option('width', $width);
+    }
+
+    /**
+     * @param string|Closure $title
+     * @param Closure|null $callback
+     * @return $this
+     */
+    public function done($title, Closure $callback = null)
+    {
+        if ($title instanceof Closure) {
+            $callback = $title;
+            $title    = trans('admin.done');
+        }
+
+        $this->doneStep = new DoneStep($this->form, $title, $callback);
+
+        return $this;
+    }
+
+    /**
+     * @return DoneStep|null
+     */
+    public function getDoneStep()
+    {
+        if (! $this->stepForms) {
+            return;
+        }
+
+        if (! $this->doneStep) {
+            $this->setDefaultDonePage();
+        }
+
+        return $this->doneStep;
+    }
+
+    /**
+     * @return void
+     */
+    protected function setDefaultDonePage()
+    {
+        $this->done(function () {
+            $resource = $this->form->getResource(0);
+
+            $data = [
+                'title'       => trans('admin.save_succeeded'),
+                'description' => '',
+                'createUrl'   => $resource.'/create',
+                'backUrl'     => $resource,
+            ];
+
+            return view('admin::form.done-step', $data);
+        });
+    }
+
+    /**
+     * @return void
+     */
+    protected function collectAssets()
+    {
+        Admin::js('vendor/dcat-admin/SmartWizard/dist/js/jquery.smartWizard.min.js');
+        Admin::css('vendor/dcat-admin/SmartWizard/dist/css/step.min.css');
+    }
+
+}

+ 0 - 12
src/Form/StepForm.php

@@ -2,15 +2,11 @@
 
 namespace Dcat\Admin\Form;
 
-use Dcat\Admin\Admin;
 use Dcat\Admin\Form;
 use Dcat\Admin\Widgets\Form as WidgetForm;
 
 class StepForm extends WidgetForm
 {
-    const CURRENT_VALIDATION_STEP = 'CURRENT_VALIDATION_STEP';
-    const ALL_STEPS = 'ALL_STEPS';
-
     /**
      * @var string
      */
@@ -107,8 +103,6 @@ class StepForm extends WidgetForm
      */
     protected function open()
     {
-        $this->collectAssets();
-
         if ($this->index > 0) {
             $this->setHtmlAttribute('style', 'display:none');
         }
@@ -129,10 +123,4 @@ HTML;
         return '</div>';
     }
 
-    protected function collectAssets()
-    {
-        Admin::js('vendor/dcat-admin/SmartWizard/dist/js/jquery.smartWizard.min.js');
-        Admin::css('vendor/dcat-admin/SmartWizard/dist/css/step.min.css');
-    }
-
 }