Browse Source

增加"Form::if"

jqh 5 năm trước cách đây
mục cha
commit
26b5319b91

+ 31 - 3
src/Admin.php

@@ -140,6 +140,19 @@ class Admin
     /**
      * Build show page.
      *
+     * @example
+     *     $show = Admin::show();
+     *
+     *     $show = Admin::show($id);
+     *     $show = Admin::show(new Repository);
+     *     $show = Admin::show(function (Show $show) {});
+     *
+     *     $show = Admin::show($id, new Repository);
+     *     $show = Admin::show($id, function (Show $show) {});
+     *     $show = Admin::show(new Repository, function (Show $show) {});
+     *
+     *     $show = Admin::show($id, new Repository, function (Show $show) {});
+     *
      * @param $repository
      * @param mixed $callable
      *
@@ -153,13 +166,28 @@ class Admin
 
                 break;
             case 1:
-                $show = new Show(null, $id);
+                if ($id instanceof \Closure) {
+                    $show = new Show(null, $id);
+                } elseif ($id instanceof Repository) {
+                    $show = new Show($id);
+                } else {
+                    $show = new Show();
+                    $show->setId($id);
+                }
 
                 break;
             case 2:
-                $show = new Show(null, $repository);
+                if ($id instanceof Repository && $repository instanceof \Closure) {
+                    $show = new Show($id, $repository);
+                } elseif ($repository instanceof \Closure) {
+                    $show = new Show(null, $repository);
 
-                $show->setId($id);
+                    $show->setId($id);
+                } elseif ($repository instanceof Repository) {
+                    $show = new Show($repository);
+
+                    $show->setId($id);
+                }
                 break;
             case 3:
                 $show = new Show($repository, $callable);

+ 4 - 2
src/Controllers/RouteController.php

@@ -41,9 +41,11 @@ class RouteController extends Controller
                 return "<span class=\"label bg-{$colors[$method]}\">$method</span>";
             })->implode('&nbsp;');
 
-            $grid->uri(trans('admin.uri'))->sortable();
+            $grid->uri(trans('admin.uri'))->sortable()->display(function ($v) {
+                return "<code>$v</code>";
+            });
 
-            $grid->name(trans('admin.alias'));
+            $grid->name(trans('admin.alias'))->bold();
 
             $grid->action(trans('admin.route_action'))->display(function ($uri) {
                 if ($uri === 'Closure') {

+ 20 - 0
src/Form.php

@@ -4,6 +4,7 @@ namespace Dcat\Admin;
 
 use Closure;
 use Dcat\Admin\Form\Builder;
+use Dcat\Admin\Form\Condition;
 use Dcat\Admin\Form\Field;
 use Dcat\Admin\Form\Row;
 use Dcat\Admin\Form\Tab;
@@ -252,6 +253,11 @@ class Form implements Renderable
      */
     protected $validationMessages;
 
+    /**
+     * @var Condition[]
+     */
+    protected $conditions = [];
+
     /**
      * Create a new form instance.
      *
@@ -1100,6 +1106,16 @@ class Form implements Renderable
         });
     }
 
+    /**
+     * @param $condition
+     *
+     * @return Condition|$this
+     */
+    public function if($condition)
+    {
+        return $this->conditions[] = new Condition($condition, $this);
+    }
+
     /**
      * @return void
      */
@@ -1126,6 +1142,10 @@ class Form implements Renderable
         if ($callback = $this->fieldBuilder) {
             $callback($this);
         }
+
+        foreach ($this->conditions as $condition) {
+            $condition->then();
+        }
     }
 
     /**

+ 81 - 0
src/Form/Condition.php

@@ -0,0 +1,81 @@
+<?php
+
+namespace Dcat\Admin\Form;
+
+use Dcat\Admin\Form;
+
+class Condition
+{
+    /**
+     * @var Form
+     */
+    protected $form;
+
+    protected $done = false;
+
+    protected $condition;
+
+    /**
+     * @var \Closure[]
+     */
+    protected $next = [];
+
+    public function __construct($condition, Form $form)
+    {
+        $this->condition = $condition;
+        $this->form = $form;
+    }
+
+    public function next(\Closure $closure)
+    {
+        $this->next[] = $closure;
+
+        return $this;
+    }
+
+    public function then(\Closure $next = null)
+    {
+        if ($this->done) {
+            return;
+        }
+        $this->done = true;
+
+        if (! $this->is()) {
+            return;
+        }
+
+        if ($next) {
+            $this->call($next);
+        }
+
+        foreach ($this->next as $callback) {
+            $this->call($callback);
+        }
+    }
+
+    public function is()
+    {
+        if ($this->condition instanceof \Closure) {
+            $this->condition = $this->call($this->condition);
+        }
+
+        return $this->condition ? true : false;
+    }
+
+    protected function call(\Closure $callback)
+    {
+        return $callback($this->form);
+    }
+
+    public function __call($name, $arguments)
+    {
+        if (! method_exists($this->form, $name)) {
+            return $this;
+        }
+
+        return $this->next(function (Form $form) use ($name, &$arguments) {
+            return $form->$name(...$arguments);
+        });
+    }
+
+}

+ 0 - 1
src/Grid/Concerns/HasQuickSearch.php

@@ -4,7 +4,6 @@ namespace Dcat\Admin\Grid\Concerns;
 
 use Dcat\Admin\Grid\Column;
 use Dcat\Admin\Grid\Tools;
-use Dcat\Admin\Repositories\EloquentRepository;
 use Illuminate\Support\Collection;
 use Dcat\Admin\Grid\Model;
 use Illuminate\Support\Str;