Browse Source

增加快速创建表单类型

jqh 5 years ago
parent
commit
f2b38a5487

+ 2 - 23
resources/views/form/selectresource.blade.php

@@ -12,7 +12,7 @@
             <div {!! $attributes !!}>
             </div>
             <div class="input-group-btn">
-                <div class="btn btn-{{$style}} " id="{{$formId}}{!! $class !!}-select-source-btn">
+                <div class="btn btn-{{$style}} " id="{{ $btnId }}">
                     &nbsp;<i class="fa fa-long-arrow-up"></i>&nbsp;
                 </div>
             </div>
@@ -21,25 +21,4 @@
         @include('admin::form.help-block')
 
     </div>
-</div>
-<script data-exec-on-popstate>
-    LA.ready(function () {
-        LA.ResourceSelector({
-            title: '{!! ucfirst(trans('admin.choose')) !!} {!! $label !!}', {{--弹窗标题--}}
-            column: "{!! $name !!}", {{--字段名称--}}
-            source: '{!! $source !!}', {{--资源地址--}}
-            selector: '#{{$formId}}{!! $class !!}-select-source-btn', {{--选择按钮选择器--}}
-            maxItem: {!! (int)$maxItem !!}, {{--最大选项数量,0为不限制--}}
-            area: {!! $area !!},
-            items: {!! $value !!}, {{--默认选中项,key => value 键值对--}}
-            placeholder: '{!! $placeholder !!}',
-            showCloseButton: false,
-            closeButtonText: '{!! ucfirst(trans('admin.close')) !!}',
-            exceedMaxItemTip: '{{trans('admin.selected_must_less_then', ['num' => $maxItem])}}',
-            selectedOptionsTip: '{{trans('admin.selected_options')}}',
-            disabled: '{!! $disabled !!}',
-            displayer: 'navList',
-            $displayerContainer: $('#{{$inputContainerId}}'),
-        });
-    });
-</script>
+</div>

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

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

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

@@ -1,5 +1,5 @@
 <thead>
-<tr class="{{ $elementClass }}" @if($hidden) style="display: none" @endif>
+<tr class="{{ $elementClass }} quick-create" @if($hidden) style="display: none" @endif>
     <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;">

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

@@ -1,4 +1,4 @@
-<div class="input-group input-group-sm">
+<div class="input-group input-group-sm quick-form-field">
     <select class="form-control {{$class}}" style="width: 100%;" name="{{$name}}" {!! $attributes !!} multiple data-placeholder="{{ $label }}">
 
         <option value=""></option>

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

@@ -1,4 +1,4 @@
-<div class="input-group input-group-sm">
+<div class="input-group input-group-sm quick-form-field">
     <select class="form-control {{$class}}" style="width: 100%;" name="{{$name}}" {!! $attributes !!} >
 
         <option value=""></option>

+ 15 - 0
resources/views/grid/quick-create/selectresource.blade.php

@@ -0,0 +1,15 @@
+<div class="input-group input-group-sm quick-form-field">
+    <app></app>
+    <div class="input-group-sm">
+        @if(!$disabled)
+            <input name="{{$name}}" type="hidden" />
+        @endif
+        <div {!! $attributes !!}>
+        </div>
+        <div class="input-group-btn">
+            <div class="btn btn-sm btn-{{$style}} " id="{{ $btnId }}">
+                &nbsp;<i class="fa fa-long-arrow-up"></i>&nbsp;
+            </div>
+        </div>
+    </div>
+</div>

+ 8 - 0
resources/views/grid/quick-create/tags.blade.php

@@ -0,0 +1,8 @@
+<div class="input-group input-group-sm quick-form-field">
+    <select class="form-control {{$class}}" style="width: 100%;" name="{{$name}}[]" multiple="multiple" data-placeholder="{{ $placeholder }}" {!! $attributes !!} >
+        @foreach($options as $key => $option)
+            <option value="{{ $keyAsValue ? $key : $option}}" {{ in_array($option, $value) ? 'selected' : '' }}>{{$option}}</option>
+        @endforeach
+    </select>
+    <input type="hidden" name="{{$name}}[]" />
+</div>

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

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

+ 57 - 18
src/Form/Field/SelectResource.php

@@ -24,6 +24,8 @@ class SelectResource extends Field
 
     protected $style = 'primary';
 
+    protected $btnId;
+
     /**
      * Set window's area.
      *
@@ -161,41 +163,78 @@ class SelectResource extends Field
         return Helper::array($value, true);
     }
 
+    protected function setupScript()
+    {
+        $label = ucfirst(trans('admin.choose')).' '.$this->label;
+        $area = json_encode($this->area);
+        $closeLabel = ucfirst(trans('admin.close'));
+        $lessThenLabel = trans('admin.selected_must_less_then', ['num' => $this->maxItem]);
+        $selectedOptionsLabel = trans('admin.selected_options');
+        $disabled = empty($this->attributes['disabled']) ? '' : 'disabled';
+        $containerId = $this->id.$this->formElementId();
+
+        Admin::script(
+            <<<JS
+LA.ResourceSelector({
+    title: '{$label}',
+    column: "{$this->elementName()}",
+    source: '{$this->source}',
+    selector: '#{$this->btnId}',
+    maxItem: {$this->maxItem}, 
+    area: {$area},
+    items: {$this->value()},
+    placeholder: '{$this->placeholder()}',
+    showCloseButton: false,
+    closeButtonText: '{$closeLabel}',
+    exceedMaxItemTip: '{$lessThenLabel}',
+    selectedOptionsTip: '{$selectedOptionsLabel}',
+    disabled: '{$disabled}',
+    displayer: 'navList',
+    \$displayerContainer: $('#{$containerId}'),
+});
+JS
+        );
+    }
+
+    protected function setupStyle()
+    {
+        if (! $this->maxItem || $this->maxItem > 1) {
+            $primayDark = Color::primarydark();
+
+            Admin::style(
+                ".select-resource .nav li a{padding:8px 10px;font-size:13px;font-weight:bold;color:{$primayDark}}.select-resource .nav li a.red{cursor:pointer}.select-resource .nav-stacked>li{border-bottom:1px solid #eee;background: #fff;}.select-resource .nav {border: 1px solid #eee;margin-bottom:5px;}"
+            );
+        }
+    }
+
     /**
      * {@inheritdoc}
      */
     public function render()
     {
+        $this->btnId = $this->id.'-select-resource';
+
         $this->formatOptions();
         $this->formatValue();
         $this->setDefaultSource();
-
-        if (! $this->maxItem || $this->maxItem > 1) {
-            $primayDark = Color::primarydark();
-
-            Admin::style(".select-resource .nav li a{padding:8px 10px;font-size:13px;font-weight:bold;color:{$primayDark}}.select-resource .nav li a.red{cursor:pointer}.select-resource .nav-stacked>li{border-bottom:1px solid #eee;background: #fff;}.select-resource .nav {border: 1px solid #eee;margin-bottom:5px;}");
-        }
-
-        $this->defaultAttribute('class', 'form-control '.$this->elementClassString());
+        $this->setupStyle();
+        $this->setupScript();
 
         $name = $this->elementName ?: $this->formatName($this->column);
 
         $this->prepend('<i class="fa fa-long-arrow-up"></i>')
+            ->defaultAttribute('class', 'form-control '.$this->elementClassString())
             ->defaultAttribute('type', 'text')
             ->defaultAttribute('id', $this->id.$this->formElementId())
             ->defaultAttribute('name', $name);
 
         $this->addVariables([
-            'className'        => str_replace(['[', ']'], '_', $name),
-            'prepend'          => $this->prepend,
-            'append'           => $this->append,
-            'area'             => json_encode($this->area),
-            'maxItem'          => $this->maxItem,
-            'source'           => $this->source,
-            'placeholder'      => $this->placeholder(),
-            'style'            => $this->style,
-            'disabled'         => empty($this->attributes['disabled']) ? '' : 'disabled',
-            'inputContainerId' => $this->id.$this->formElementId(),
+            'className'   => str_replace(['[', ']'], '_', $name),
+            'prepend'     => $this->prepend,
+            'append'      => $this->append,
+            'placeholder' => $this->placeholder(),
+            'style'       => $this->style,
+            'btnId'       => $this->btnId,
         ]);
 
         return parent::render();

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

@@ -49,7 +49,7 @@ class CreateButton implements Renderable
 
         $new = trans('admin.new');
 
-        return "<a class='btn btn-sm btn-success {$this->grid->getName()}-quick-create-button'><i class='ti-plus'></i><span class='hidden-xs'>&nbsp; $new</span></a>";
+        return "<a class='btn btn-sm btn-success quick-create-button-{$this->grid->getName()}'><i class='ti-plus'></i><span class='hidden-xs'>&nbsp; $new</span></a>";
     }
 
     protected function renderCreateButton()

+ 56 - 6
src/Grid/Tools/QuickCreate.php

@@ -130,6 +130,21 @@ class QuickCreate implements Renderable
             ->attribute('style', 'width:150px');
     }
 
+    /**
+     * @param string $column
+     * @param string $placeholder
+     *
+     * @return Field\SelectResource
+     */
+    public function selectResource($column, $placeholder = '')
+    {
+        $field = new Field\SelectResource($column, $this->formatPlaceholder($placeholder));
+
+        $this->addField($field->attribute('style', 'width:150px'));
+
+        return $field;
+    }
+
     /**
      * @param string $column
      * @param string $placeholder
@@ -155,6 +170,32 @@ CSS
         return $field;
     }
 
+    /**
+     * @param string $column
+     * @param string $placeholder
+     *
+     * @return Field\Tags
+     */
+    public function tags($column, $placeholder = '')
+    {
+        Admin::style(
+            <<<'CSS'
+.quick-create .select2-selection--multiple {
+    padding-left: 5px !important;
+    height: 30px !important;
+    width: 200px !important;
+    min-height: 30px !important;
+}
+CSS
+        );
+
+        $field = new Field\Tags($column, $this->formatPlaceholder($placeholder));
+
+        $this->addField($field);
+
+        return $field;
+    }
+
     /**
      * @param string $column
      * @param string $placeholder
@@ -164,9 +205,9 @@ CSS
     public function multipleSelect($column, $placeholder = '')
     {
         Admin::style(
-            <<<'CSS'
+            <<<CSS
 .quick-create .select2-selection--multiple {
-    padding: 0 !important;
+    padding-left: 5px !important;
     height: 30px !important;
     width: 200px !important;
     min-height: 30px !important;
@@ -225,7 +266,7 @@ CSS
      */
     protected function addField(Field $field)
     {
-        $elementClass = array_merge(['quick-create'], $field->elementClass());
+        $elementClass = array_merge([$this->elementClass()], $field->elementClass());
 
         $field->addElementClass($elementClass);
 
@@ -260,8 +301,8 @@ CSS
 
         $script = <<<JS
 (function () {
-    var ctr = $('.{$uniqueName}-quick-create'),
-        btn = $('.{$uniqueName}-quick-create-button');
+    var ctr = $('.{$this->elementClass()}'),
+        btn = $('.quick-create-button-{$uniqueName}');
     
     btn.click(function () {
         ctr.toggle().click();
@@ -338,6 +379,13 @@ JS;
         Admin::script($script);
     }
 
+    public function elementClass()
+    {
+        $name = $this->parent->getName();
+
+        return 'quick-create'.($name ? "-{$name}" : '');
+    }
+
     /**
      * @param int $columnCount
      *
@@ -351,10 +399,12 @@ JS;
 
         $this->script();
 
+        Admin::style('.quick-form-field{margin: 5px 0}');
+
         $vars = [
             'columnCount'  => $columnCount,
             'fields'       => $this->fields,
-            'elementClass' => $this->parent->getName().'-quick-create',
+            'elementClass' => $this->elementClass(),
             'hidden'       => $this->parent->option('create_mode') === Grid::CREATE_MODE_QUICK,
         ];