Browse Source

Merge branch 'master' into dev

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

+ 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') {

+ 39 - 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,35 @@ class Form implements Renderable
         });
     }
 
+    /**
+     * @example
+     *     $form->if(true)->next(function (Form $form) {
+     *          $form->text('name');
+     *     });
+     *
+     *     $form->if(function (Form $form) {
+     *         return $form->model()->id > 5;
+     *     })->next(function (Form $form) {
+     *         $form->text('name');
+     *     });
+     *
+     *     $form->if(true)->then(function (Form $form) {
+     *         $form->text('name');
+     *     });
+     *
+     *     $form->if(true)->creating(function (Form $form) {});
+     *
+     *     $form->if(true)->removeField('name');
+     *
+     * @param bool|\Closure $condition
+     *
+     * @return Condition|$this
+     */
+    public function if($condition)
+    {
+        return $this->conditions[] = new Condition($condition, $this);
+    }
+
     /**
      * @return void
      */
@@ -1126,6 +1161,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->next($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;

+ 1 - 1
src/Grid/Tools/QuickSearch.php

@@ -3,7 +3,7 @@
 namespace Dcat\Admin\Grid\Tools;
 
 use Dcat\Admin\Admin;
-use Dcat\Admin\Grid\Concerns\QuickSearch as QuickSearchConcerns;
+use Dcat\Admin\Grid\Concerns\HasQuickSearch as QuickSearchConcerns;
 use Illuminate\Support\Arr;
 
 class QuickSearch extends AbstractTool

+ 8 - 2
src/Support/helpers.php

@@ -153,10 +153,16 @@ if (!function_exists('admin_trans')) {
      */
     function admin_trans($key, $replace = [], $locale = null)
     {
+        static $method = null;
+
+        if ($method === null) {
+            $method = version_compare(app()->version(), '6.0', '>=') ? 'get' : 'trans';
+        }
+
         $translator = app('translator');
 
         if ($translator->has($key)) {
-            return $translator->trans($key, $replace, $locale);
+            return $translator->$method($key, $replace, $locale);
         }
         if (
             strpos($key, 'global.') !== 0
@@ -170,7 +176,7 @@ if (!function_exists('admin_trans')) {
                 return end($arr);
             }
 
-            return $translator->trans($key, $replace, $locale);
+            return $translator->$method($key, $replace, $locale);
         }
 
         return last(explode('.', $key));