jqh %!s(int64=6) %!d(string=hai) anos
pai
achega
2b24af8a7f

+ 1 - 0
resources/lang/en/admin.php

@@ -186,6 +186,7 @@ return [
     ],
     'import_extension_confirm' => 'Are you sure import the extension?',
     'selected_must_less_then'  => 'Only supports maximum :num options.',
+    'quick_create'             => 'Quick create',
     'validation'               => [
         'match'     => 'The :attribute and :other must match.',
         'minlength' => 'The :attribute must be at least :min characters.',

+ 1 - 0
resources/lang/zh-CN/admin.php

@@ -187,6 +187,7 @@ return [
     ],
     'import_extension_confirm' => '确认导入拓展?',
     'selected_must_less_then'  => '最多只能选择:num个选项',
+    'quick_create'             => '快速创建',
     'validation'               => [
         'match'     => '与 :attribute 不匹配。',
         'minlength' => ':attribute 字符长度不能少于 :min。',

+ 3 - 0
resources/views/grid/quick-create/date.blade.php

@@ -0,0 +1,3 @@
+<div class="input-group input-group-sm">
+    <input style="width: 125px;" {!! $attributes !!} placeholder="{{ $label }}" />
+</div>

+ 20 - 0
resources/views/grid/quick-create/form.blade.php

@@ -0,0 +1,20 @@
+<thead>
+<tr class="quick-create">
+    <td colspan="{{ $columnCount }}" style="height: 43px;padding-left: 57px;background-color: #f9f9f9; vertical-align: middle;">
+
+        <span class="create" style="color: #bdbdbd;cursor: pointer;display: block;">
+             <i class="ti-plus"></i>&nbsp;{{ __('admin.quick_create') }}
+        </span>
+
+        <form class="form-inline create-form" style="display: none;" method="post">
+            @foreach($fields as $field)
+                &nbsp;{!! $field->render() !!}
+            @endforeach
+                &nbsp;
+            <button class="btn btn-primary btn-sm">{{ __('admin.submit') }}</button>&nbsp;
+            <a href="javascript:void(0);" class="cancel">{{ __('admin.cancel') }}</a>
+            {{ csrf_field() }}
+        </form>
+    </td>
+</tr>
+</thead>

+ 10 - 0
resources/views/grid/quick-create/multipleselect.blade.php

@@ -0,0 +1,10 @@
+<div class="input-group input-group-sm">
+    <select class="form-control {{$class}}" style="width: 100%;" name="{{$name}}" {!! $attributes !!} multiple data-placeholder="{{ $label }}">
+
+        <option value=""></option>
+        @foreach($options as $select => $option)
+            <option value="{{$select}}" {{ $select == old($column, $value) ?'selected':'' }}>{{$option}}</option>
+        @endforeach
+    </select>
+</div>
+

+ 10 - 0
resources/views/grid/quick-create/select.blade.php

@@ -0,0 +1,10 @@
+<div class="input-group input-group-sm">
+    <select class="form-control {{$class}}" style="width: 100%;" name="{{$name}}" {!! $attributes !!} >
+
+        <option value=""></option>
+        @foreach($options as $select => $option)
+            <option value="{{$select}}" {{ $select == old($column, $value) ?'selected':'' }}>{{$option}}</option>
+        @endforeach
+    </select>
+</div>
+

+ 3 - 0
resources/views/grid/quick-create/text.blade.php

@@ -0,0 +1,3 @@
+<div class="input-group input-group-sm">
+    <input {!! $attributes !!} placeholder="{{ $label }}"/>
+</div>

+ 4 - 0
resources/views/grid/table.blade.php

@@ -46,6 +46,10 @@
             </tr>
             </thead>
 
+            @if ($grid->hasQuickCreate())
+                {!! $grid->renderQuickCreate() !!}
+            @endif
+
             <tbody>
             @foreach($grid->rows() as $row)
                 <tr {!! $row->rowAttributes() !!}>

+ 36 - 20
src/Grid.php

@@ -32,10 +32,15 @@ class Grid
         Concerns\HasMultipleHeaders,
         Concerns\HasQuickSearch,
         Concerns\HasSelector,
+        Concerns\HasQuickCreate,
         Macroable {
             __call as macroCall;
         }
 
+    const CREATE_MODE_DEFAULT = 'default';
+    const CREATE_MODE_MODAL = 'modal';
+    const CREATE_MODE_QUICK = 'quick';
+
     /**
      * The grid data model instance.
      *
@@ -149,19 +154,20 @@ class Grid
      * @var array
      */
     protected $options = [
-        'show_pagination'        => true,
-        'show_filter'            => true,
-        'show_actions'           => true,
-        'show_quick_edit_button' => false,
-        'show_edit_button'       => true,
-        'show_view_button'       => true,
-        'show_delete_button'     => true,
-        'show_row_selector'      => true,
-        'show_create_btn'        => true,
-        'show_quick_create_btn'  => false,
-        'show_bordered'          => false,
-        'show_toolbar'           => true,
-        'show_exporter'          => false,
+        'show_pagination'          => true,
+        'show_filter'              => true,
+        'show_actions'             => true,
+        'show_quick_edit_button'   => false,
+        'show_edit_button'         => true,
+        'show_view_button'         => true,
+        'show_delete_button'       => true,
+        'show_row_selector'        => true,
+        'show_create_button'       => true,
+        'show_quick_create_button' => false,
+        'show_bordered'            => false,
+        'show_toolbar'             => true,
+        'show_exporter'            => false,
+        'create_mode'              => self::CREATE_MODE_DEFAULT,
 
         'row_selector_style'     => 'primary',
         'row_selector_circle'    => true,
@@ -513,7 +519,7 @@ HTML
      */
     public function renderCreateButton()
     {
-        if (! $this->options['show_create_btn'] && ! $this->options['show_quick_create_btn']) {
+        if (! $this->options['show_create_button'] && ! $this->options['show_quick_create_button']) {
             return '';
         }
 
@@ -674,7 +680,7 @@ HTML;
      */
     public function disableCreateButton(bool $disable = true)
     {
-        return $this->option('show_create_btn', ! $disable);
+        return $this->option('show_create_button', ! $disable);
     }
 
     /**
@@ -694,7 +700,7 @@ HTML;
      */
     public function disableQuickCreateButton(bool $disable = true)
     {
-        return $this->option('show_quick_create_btn', ! $disable);
+        return $this->option('show_quick_create_button', ! $disable);
     }
 
     /**
@@ -712,9 +718,9 @@ HTML;
      *
      * @return bool
      */
-    public function allowCreateBtn()
+    public function allowCreateButton()
     {
-        return $this->options['show_create_btn'];
+        return $this->options['show_create_button'];
     }
 
     /**
@@ -722,9 +728,19 @@ HTML;
      *
      * @return bool
      */
-    public function allowQuickCreateBtn()
+    public function allowQuickCreateButton()
+    {
+        return $this->options['show_quick_create_button'];
+    }
+
+    /**
+     * @param string $mode
+     *
+     * @return $this
+     */
+    public function createMode(string $mode)
     {
-        return $this->options['show_quick_create_btn'];
+        return $this->option('create_mode', $mode);
     }
 
     /**

+ 50 - 0
src/Grid/Concerns/HasQuickCreate.php

@@ -0,0 +1,50 @@
+<?php
+
+namespace Dcat\Admin\Grid\Concerns;
+
+use Closure;
+use Dcat\Admin\Grid\Tools\QuickCreate;
+
+trait HasQuickCreate
+{
+    /**
+     * @var QuickCreate
+     */
+    protected $quickCreate;
+
+    /**
+     * @param Closure $callback
+     *
+     * @return $this
+     */
+    public function quickCreate(\Closure $callback)
+    {
+        $this->quickCreate = new QuickCreate($this);
+
+        call_user_func($callback, $this->quickCreate);
+
+        return $this;
+    }
+
+    /**
+     * Indicates grid has quick-create.
+     *
+     * @return bool
+     */
+    public function hasQuickCreate()
+    {
+        return !is_null($this->quickCreate);
+    }
+
+    /**
+     * Render quick-create form.
+     *
+     * @return array|string
+     */
+    public function renderQuickCreate()
+    {
+        $columnCount = $this->columns->count();
+
+        return $this->quickCreate->render($columnCount);
+    }
+}

+ 2 - 2
src/Grid/Concerns/HasTools.php

@@ -194,8 +194,8 @@ trait HasTools
             && (
                 $this->tools()->has()
                 || $this->allowExporter()
-                || $this->allowCreateBtn()
-                || $this->allowQuickCreateBtn()
+                || $this->allowCreateButton()
+                || $this->allowQuickCreateButton()
                 || $this->allowResponsive()
                 || ! empty($this->variables['title'])
             )

+ 4 - 4
src/Grid/Tools/CreateButton.php

@@ -20,7 +20,7 @@ class CreateButton implements Renderable
 
     protected function renderQuickCreateButton()
     {
-        if (! $this->grid->option('show_quick_create_btn')) {
+        if (! $this->grid->option('show_quick_create_button')) {
             return;
         }
 
@@ -35,14 +35,14 @@ class CreateButton implements Renderable
             ->dimensions($width, $height)
             ->render();
 
-        $text = $this->grid->option('show_create_btn') ? '<i class="fa fa-clone"></i>' : "<i class='ti-plus'></i><span class='hidden-xs'> &nbsp; $new</span>";
+        $text = $this->grid->option('show_create_button') ? '<i class="fa fa-clone"></i>' : "<i class='ti-plus'></i><span class='hidden-xs'> &nbsp; $new</span>";
 
         return "<a data-url='$url' class='btn btn-sm btn-success {$this->grid->rowName()}-create'>$text</a>";
     }
 
     protected function renderCreateButton()
     {
-        if (! $this->grid->option('show_create_btn')) {
+        if (! $this->grid->option('show_create_button')) {
             return;
         }
 
@@ -56,7 +56,7 @@ class CreateButton implements Renderable
 
     public function render()
     {
-        if (! $this->grid->option('show_create_btn') && ! $this->grid->option('show_quick_create_btn')) {
+        if (! $this->grid->option('show_create_button') && ! $this->grid->option('show_quick_create_button')) {
             return;
         }
 

+ 335 - 0
src/Grid/Tools/QuickCreate.php

@@ -0,0 +1,335 @@
+<?php
+
+namespace Dcat\Admin\Grid\Tools;
+
+use Dcat\Admin\Admin;
+use Dcat\Admin\Form\Field;
+use Dcat\Admin\Form\Field\MultipleSelect;
+use Dcat\Admin\Form\Field\Select;
+use Dcat\Admin\Form\Field\Text;
+use Dcat\Admin\Grid;
+use Illuminate\Contracts\Support\Renderable;
+use Illuminate\Support\Collection;
+
+class QuickCreate implements Renderable
+{
+    /**
+     * @var Grid
+     */
+    protected $parent;
+
+    /**
+     * @var Collection
+     */
+    protected $fields;
+
+    /**
+     * QuickCreate constructor.
+     *
+     * @param Grid $grid
+     */
+    public function __construct(Grid $grid)
+    {
+        $this->parent = $grid;
+        $this->fields = Collection::make();
+    }
+
+    protected function formatPlaceholder($placeholder)
+    {
+        return array_filter((array) $placeholder);
+    }
+
+    /**
+     * @param string $column
+     * @param string $placeholder
+     *
+     * @return Text
+     */
+    public function text($column, $placeholder = '')
+    {
+        $field = new Text($column, $this->formatPlaceholder($placeholder));
+
+        $this->addField($field->width('200px'));
+
+        return $field;
+    }
+
+    /**
+     * @param string $column
+     * @param string $placeholder
+     *
+     * @return Text
+     */
+    public function email($column, $placeholder = '')
+    {
+        return $this->text($column, $placeholder)
+            ->inputmask(['alias' => 'email']);
+    }
+
+    /**
+     * @param string $column
+     * @param string $placeholder
+     *
+     * @return Text
+     */
+    public function ip($column, $placeholder = '')
+    {
+        return $this->text($column, $placeholder)
+            ->inputmask(['alias' => 'ip'])
+            ->width('120px');
+    }
+
+    /**
+     * @param string $column
+     * @param string $placeholder
+     *
+     * @return Text
+     */
+    public function url($column, $placeholder = '')
+    {
+        return $this->text($column, $placeholder)
+            ->inputmask(['alias' => 'url']);
+    }
+
+    /**
+     * @param string $column
+     * @param string $placeholder
+     *
+     * @return Text
+     */
+    public function password($column, $placeholder = '')
+    {
+        return $this->text($column, $placeholder)
+            ->attribute('type', 'password')
+            ->width('100px');
+    }
+
+    /**
+     * @param string $column
+     * @param string $placeholder
+     *
+     * @return Text
+     */
+    public function mobile($column, $placeholder = '')
+    {
+        return $this->text($column, $placeholder)
+            ->inputmask(['mask' => '99999999999'])
+            ->width('100px');
+    }
+
+    /**
+     * @param string $column
+     * @param string $placeholder
+     *
+     * @return Text
+     */
+    public function integer($column, $placeholder = '')
+    {
+        return $this->text($column, $placeholder)
+            ->inputmask(['alias' => 'integer'])
+            ->width('120px');
+    }
+
+    /**
+     * @param string $column
+     * @param string $placeholder
+     *
+     * @return Select
+     */
+    public function select($column, $placeholder = '')
+    {
+        Admin::style(
+            <<<CSS
+.quick-create .select2-selection--single {
+    padding: 3px 12px !important;
+    height: 30px !important;
+    width: 150px !important;
+}
+CSS
+        );
+
+        $field = new Select($column, $this->formatPlaceholder($placeholder));
+
+        $this->addField($field);
+
+        return $field;
+    }
+
+    /**
+     * @param string $column
+     * @param string $placeholder
+     *
+     * @return MultipleSelect
+     */
+    public function multipleSelect($column, $placeholder = '')
+    {
+        Admin::style(
+            <<<CSS
+.quick-create .select2-selection--multiple {
+    padding: 0 !important;
+    height: 30px !important;
+    width: 200px !important;
+    min-height: 30px !important;
+}
+CSS
+        );
+
+        $field = new MultipleSelect($column, $this->formatPlaceholder($placeholder));
+
+        $this->addField($field);
+
+        return $field;
+    }
+
+    /**
+     * @param string $column
+     * @param string $placeholder
+     *
+     * @return Field\Date
+     */
+    public function datetime($column, $placeholder = '')
+    {
+        return $this->date($column, $placeholder)->format('YYYY-MM-DD HH:mm:ss');
+    }
+
+    /**
+     * @param string $column
+     * @param string $placeholder
+     *
+     * @return Field\Date
+     */
+    public function time($column, $placeholder = '')
+    {
+        return $this->date($column, $placeholder)->format('HH:mm:ss');
+    }
+
+    /**
+     * @param string $column
+     * @param string $placeholder
+     *
+     * @return Field\Date
+     */
+    public function date($column, $placeholder = '')
+    {
+        $field = new Field\Date($column, $this->formatPlaceholder($placeholder));
+
+        $this->addField($field);
+
+        return $field;
+    }
+
+    /**
+     * @param Field $field
+     *
+     * @return Field
+     */
+    protected function addField(Field $field)
+    {
+        $elementClass = array_merge(['quick-create'], $field->elementClass());
+
+        $field->addElementClass($elementClass);
+
+        $field->view($this->resolveView(get_class($field)));
+
+        $this->fields->push($field);
+
+        return $field;
+    }
+
+    /**
+     * @param string $class
+     *
+     * @return string
+     */
+    protected function resolveView($class)
+    {
+        $path = explode('\\', $class);
+
+        $name = strtolower(array_pop($path));
+
+        return "admin::grid.quick-create.{$name}";
+    }
+
+    protected function script()
+    {
+        $url = request()->url();
+
+        $script = <<<JS
+
+(function () {
+    $('.quick-create .create').click(function () {
+        $('.quick-create .create-form').show();
+        $(this).hide();
+    });
+    
+    $('.quick-create .cancel').click(function () {
+        $('.quick-create .create-form').hide();
+        $('.quick-create .create').show();
+    });
+    
+    $('.quick-create .create-form').submit(function (e) {
+        e.preventDefault();
+    
+        $.ajax({
+            url: '{$url}',
+            type: 'POST',
+            data: $(this).serialize(),
+            success: function(data, textStatus, jqXHR) {
+                console.info(data);
+                
+                if (data.status == true) {
+                    LA.success(data.message);
+                    LA.reload();
+                    return;
+                }
+                
+                if (typeof data.validation !== 'undefined') {
+                    LA.warning(data.message)
+                }
+            },
+            error:function(xhq, textStatus){
+                var json = xhq.responseJSON;
+                if (typeof json === 'object') {
+                    if (json.message) {
+                        LA.error(json.message);
+                    } else if (json.errors) {
+                        var i, errors = [];
+                        for (i in json.errors) {
+                            errors.push(json.errors[i].join("<br>"));
+                        } 
+                        
+                        LA.error(errors.join("<br>"));
+                    }
+                }
+            }
+        });
+        
+        return false;
+    });
+})();
+JS;
+
+        Admin::script($script);
+    }
+
+    /**
+     * @param int $columnCount
+     *
+     * @return array|string
+     */
+    public function render($columnCount = 0)
+    {
+        if ($this->fields->isEmpty()) {
+            return '';
+        }
+
+        $this->script();
+
+        $vars = [
+            'columnCount' => $columnCount,
+            'fields'      => $this->fields,
+        ];
+
+        return view('admin::grid.quick-create.form', $vars)->render();
+    }
+}

+ 7 - 8
src/Models/Repositories/Administrator.php

@@ -17,14 +17,14 @@ class Administrator extends EloquentRepository
 
     public function get(Grid\Model $model)
     {
-        $model = parent::get($model);
+        $results = parent::get($model);
 
-        $isPaginator = $model instanceof AbstractPaginator;
+        $isPaginator = $results instanceof AbstractPaginator;
 
-        $items = collect($isPaginator ? $model->items() : $model)->toArray();
+        $items = collect($isPaginator ? $results->items() : $results)->toArray();
 
         if (! $items) {
-            return $model;
+            return $results;
         }
 
         $roleModel = config('admin.database.roles_model');
@@ -36,8 +36,7 @@ class Administrator extends EloquentRepository
         $roleIds = $items
             ->pluck('roles')
             ->flatten(1)
-            ->keyBy($roleKeyName)
-            ->keys()
+            ->pluck($roleKeyName)
             ->toArray();
 
         $permissions = $roleModel::getPermissionId($roleIds);
@@ -57,9 +56,9 @@ class Administrator extends EloquentRepository
         }
 
         if ($isPaginator) {
-            $model->setCollection($items);
+            $results->setCollection($items);
 
-            return $model;
+            return $results;
         }
 
         return $items;

+ 1 - 2
src/Models/Role.php

@@ -100,10 +100,9 @@ class Role extends Model
         $keyName = $model->getKeyName();
 
         return $model->newQuery()
-            ->select(['permission_id', 'role_id'])
             ->leftJoin($related, $keyName, '=', 'role_id')
             ->whereIn($keyName, $roleIds)
-            ->get()
+            ->get(['permission_id', 'role_id'])
             ->groupBy('role_id')
             ->map(function ($v) {
                 $v = $v instanceof Arrayable ? $v->toArray() : $v;