Преглед изворни кода

Merge branch '2.0' into gitee2

jqh пре 4 година
родитељ
комит
c327853ad6

+ 3 - 0
resources/assets/dcat/extra/select-table.js

@@ -58,16 +58,19 @@
                     self.resetSelected();
                     self.resetSelected();
                 });
                 });
 
 
+                // 取消按钮
                 self.$cancel.on('click', function () {
                 self.$cancel.on('click', function () {
                     self.$dialog.trigger('dialog:close');
                     self.$dialog.trigger('dialog:close');
                 });
                 });
 
 
+                // 绑定相关事件
                 self.bind();
                 self.bind();
 
 
                 // 重置已选中数据
                 // 重置已选中数据
                 self.resetSelected();
                 self.resetSelected();
             });
             });
 
 
+            // 渲染选中的数据
             self.render(values);
             self.render(values);
         },
         },
 
 

+ 54 - 0
resources/assets/dcat/js/extensions/Helpers.js

@@ -263,4 +263,58 @@ export default class Helpers {
             Dcat.handleAjaxError(a, b, c);
             Dcat.handleAjaxError(a, b, c);
         })
         })
     }
     }
+
+    /**
+     * 联动多个字段.
+     *
+     * @param _this
+     * @param options
+     */
+    loadFields(_this, options) {
+        let refreshOptions = function(url, target) {
+            Dcat.loading();
+
+            $.ajax(url).then(function(data) {
+                Dcat.loading(false);
+                target.find("option").remove();
+
+                $.map(data, function (d) {
+                    target.append(new Option(d[options.textField], d[options.idField], false, false));
+                });
+
+                $(target).val(String(target.data('value')).split(',')).trigger('change');
+            });
+        };
+
+        let promises = [],
+            values = [];
+
+        if (! options.values) {
+            $(_this).find('option:selected').each(function () {
+                if (String(this.value) === '0' || this.value) {
+                    values.push(this.value)
+                }
+            });
+        } else {
+            values = options.values;
+            if (typeof values === 'string') {
+                values = [values];
+            }
+        }
+
+        if (! values.length) {
+            return;
+        }
+
+        options.fields.forEach(function(field, index){
+            var target = $(_this).closest(options.group).find('.' + options.fields[index]);
+
+            if (! values.length) {
+                return;
+            }
+            promises.push(refreshOptions(options.urls[index] + (options.urls[index].match(/\?/)?'&':'?') + "q="+ values.join(','), target));
+        });
+
+        $.when(promises).then(function() {});
+    }
 }
 }

Разлика између датотеке није приказан због своје велике величине
+ 0 - 0
resources/dist/dcat/extra/select-table.js.map


Разлика између датотеке није приказан због своје велике величине
+ 0 - 0
resources/dist/dcat/js/dcat-app.js


Разлика између датотеке није приказан због своје велике величине
+ 0 - 0
resources/dist/dcat/js/dcat-app.js.map


+ 0 - 2
resources/views/filter/select.blade.php

@@ -28,5 +28,3 @@
     $("{!! $selector !!}").select2(configs);
     $("{!! $selector !!}").select2(configs);
     @endif
     @endif
 </script>
 </script>
-
-@yield('admin.select-load')

+ 29 - 0
resources/views/form/checkbox.blade.php

@@ -26,4 +26,33 @@
         $(this).parents('.form-field').find('input[type="checkbox"]:not(:first)').prop('checked', this.checked).trigger('change');
         $(this).parents('.form-field').find('input[type="checkbox"]:not(:first)').prop('checked', this.checked).trigger('change');
     });
     });
 </script>
 </script>
+@endif
+
+@if(! empty($loads))
+<script once>
+    var selector = '{!! $selector !!}',
+        fields = '{!! $loads['fields'] !!}'.split('^'),
+        urls = '{!! $loads['urls'] !!}'.split('^');
+
+    $(document).off('change', selector);
+    $(document).on('change', selector, function () {
+        var values = [];
+
+        $(selector+':checked').each(function () {
+            if (String(this.value) === '0' || this.value) {
+                values.push(this.value)
+            }
+        });
+
+        Dcat.helpers.loadFields(this, {
+            group: '.fields-group',
+            urls: urls,
+            fields: fields,
+            textField: "{{ $loads['textField'] }}",
+            idField: "{{ $loads['idField'] }}",
+            values: values,
+        });
+    });
+    $(selector+':checked').trigger('change')
+</script>
 @endif
 @endif

+ 23 - 0
resources/views/form/radio.blade.php

@@ -14,3 +14,26 @@
 
 
     </div>
     </div>
 </div>
 </div>
+
+@if(! empty($loads))
+<script once>
+    var selector = '{!! $selector !!}',
+        fields = '{!! $loads['fields'] !!}'.split('^'),
+        urls = '{!! $loads['urls'] !!}'.split('^');
+
+    $(document).off('change', selector);
+    $(document).on('change', selector, function () {
+        var values = this.value;
+
+        Dcat.helpers.loadFields(this, {
+            group: '.fields-group',
+            urls: urls,
+            fields: fields,
+            textField: "{{ $loads['textField'] }}",
+            idField: "{{ $loads['idField'] }}",
+            values: values,
+        });
+    });
+    $(selector+':checked').trigger('change')
+</script>
+@endif

+ 0 - 52
resources/views/form/select-script.blade.php

@@ -29,55 +29,3 @@
     {!! $cascadeScript !!}
     {!! $cascadeScript !!}
 </script>
 </script>
 
 
-@if(isset($loads))
-    {{--loads联动--}}
-    <script once>
-        var selector = '{!! $selector !!}';
-
-        var fields = '{!! $loads['fields'] !!}'.split('^');
-        var urls = '{!! $loads['urls'] !!}'.split('^');
-
-        var refreshOptions = function(url, target) {
-            Dcat.loading();
-
-            $.ajax(url).then(function(data) {
-                Dcat.loading(false);
-                target.find("option").remove();
-                $(target).select2({
-                    data: $.map(data, function (d) {
-                        d.id = d.{{ $loads['idField'] }};
-                        d.text = d.{{ $loads['textField'] }};
-                        return d;
-                    })
-                }).val(String(target.data('value')).split(',')).trigger('change');
-            });
-        };
-
-        $(document).off('change', selector);
-        $(document).on('change', selector, function () {
-            var _this = this;
-            var promises = [];
-            var values = [];
-
-            $(this).find('option:selected').each(function () {
-                if (String(this.value) === '0'|| this.value) {
-                    values.push(this.value)
-                }
-            });
-
-            fields.forEach(function(field, index){
-                var target = $(_this).closest('.fields-group').find('.' + fields[index]);
-
-                if (! values.length) {
-                    return;
-                }
-                promises.push(refreshOptions(urls[index] + (urls[index].match(/\?/)?'&':'?') + "q="+ values.join(','), target));
-            });
-
-            $.when(promises).then(function() {});
-        });
-        $(selector).trigger('change');
-    </script>
-@endif
-
-@yield('admin.select-load')

+ 20 - 1
resources/views/form/selecttable.blade.php

@@ -25,15 +25,34 @@
 
 
 <script require="@select-table" init="{!! $selector !!}">
 <script require="@select-table" init="{!! $selector !!}">
     var dialogId = $this.parent().find('{!! $dialogSelector !!}').attr('id');
     var dialogId = $this.parent().find('{!! $dialogSelector !!}').attr('id');
+    var $input = $(this).find('input');
 
 
     Dcat.grid.SelectTable({
     Dcat.grid.SelectTable({
         dialog: '[data-id="' + dialogId + '"]',
         dialog: '[data-id="' + dialogId + '"]',
         container: $this,
         container: $this,
-        input: $(this).find('input'),
+        input: $input,
         @if(isset($max))
         @if(isset($max))
         multiple: true,
         multiple: true,
         max: {{ $max }},
         max: {{ $max }},
         @endif
         @endif
         values: {!! json_encode($options) !!},
         values: {!! json_encode($options) !!},
     });
     });
+
+    @if(! empty($loads))
+    var fields = '{!! $loads['fields'] !!}'.split('^');
+    var urls = '{!! $loads['urls'] !!}'.split('^');
+
+    $input.on('change', function () {
+        var values = this.value;
+
+        Dcat.helpers.loadFields(this, {
+            group: '.fields-group',
+            urls: urls,
+            fields: fields,
+            textField: "{{ $loads['textField'] }}",
+            idField: "{{ $loads['idField'] }}",
+            values: values,
+        });
+    }).trigger('change');
+    @endif
 </script>
 </script>

+ 21 - 34
resources/views/scripts/select.blade.php

@@ -36,39 +36,26 @@
 @overwrite
 @overwrite
 </script>
 </script>
 
 
-@section('admin.select-load')
-    @if(isset($load))
-    <script once>
-        var selector = '{!! $selector !!}';
 
 
-        $(document).off('change', selector);
-        $(document).on('change', selector, function () {
-            var target = $(this).closest('{{ $load['group'] ?? '.fields-group' }}').find(".{{ $load['class'] }}");
-            var values = [];
-
-            $(this).find('option:selected').each(function () {
-                if (String(this.value) === '0'|| this.value) {
-                    values.push(this.value)
-                }
-            });
-
-            if (! values.length) {
-                return;
-            }
-            target.find("option").remove();
-
-            Dcat.loading();
-
-            $.ajax("{!! $load['url'].(strpos($load['url'],'?')?'&':'?') !!}q="+values.join(',')).then(function (data) {
-                Dcat.loading(false);
-
-                $.map(data, function (d) {
-                    target.append(new Option(d.{{ $load['textField'] }}, d.{{ $load['idField'] }}, false, false));
-                });
-                target.val(String(target.attr('data-value')).split(',')).trigger('change');
-            });
+@if(isset($loads))
+{{--loads联动--}}
+<script once>
+    var selector = '{!! $selector !!}';
+
+    var fields = '{!! $loads['fields'] !!}'.split('^');
+    var urls = '{!! $loads['urls'] !!}'.split('^');
+
+    $(document).off('change', selector);
+    $(document).on('change', selector, function () {
+        Dcat.helpers.loadFields(this, {
+            group: '{{ $loads['group'] ?? '.fields-group' }}',
+            urls: urls,
+            fields: fields,
+            textField: "{{ $loads['textField'] }}",
+            idField: "{{ $loads['idField'] }}",
         });
         });
-        $(selector).trigger('change');
-    </script>
-    @endif
-@overwrite
+    });
+    $(selector).trigger('change');
+</script>
+@endif
+

+ 2 - 2
src/Extend/ServiceProvider.php

@@ -440,11 +440,11 @@ abstract class ServiceProvider extends LaravelServiceProvider
     protected function addExceptRoutes()
     protected function addExceptRoutes()
     {
     {
         if (! empty($this->exceptRoutes['permission'])) {
         if (! empty($this->exceptRoutes['permission'])) {
-            Admin::context()->addMany('permission.except', (array) $this->exceptRoutes['permission']);
+            Admin::context()->merge('permission.except', (array) $this->exceptRoutes['permission']);
         }
         }
 
 
         if (! empty($this->exceptRoutes['auth'])) {
         if (! empty($this->exceptRoutes['auth'])) {
-            Admin::context()->addMany('auth.except', (array) $this->exceptRoutes['auth']);
+            Admin::context()->merge('auth.except', (array) $this->exceptRoutes['auth']);
         }
         }
     }
     }
 
 

+ 54 - 0
src/Form/Field/CanLoadFields.php

@@ -0,0 +1,54 @@
+<?php
+
+namespace Dcat\Admin\Form\Field;
+
+use Illuminate\Support\Str;
+
+trait CanLoadFields
+{
+    /**
+     * 联动加载.
+     *
+     * @param string $field
+     * @param string $sourceUrl
+     * @param string $idField
+     * @param string $textField
+     *
+     * @return $this
+     */
+    public function load($field, $sourceUrl, string $idField = 'id', string $textField = 'text')
+    {
+        return $this->loads($field, $sourceUrl, $idField, $textField);
+    }
+
+    /**
+     * 联动加载多个字段.
+     *
+     * @param array|string $fields
+     * @param array|string $sourceUrls
+     * @param string $idField
+     * @param string $textField
+     *
+     * @return $this
+     */
+    public function loads($fields = [], $sourceUrls = [], string $idField = 'id', string $textField = 'text')
+    {
+        $fieldsStr = implode('^', array_map(function ($field) {
+            if (Str::contains($field, '.')) {
+                return $this->normalizeElementClass($field).'_';
+            }
+
+            return $this->normalizeElementClass($field);
+        }, (array) $fields));
+        $urlsStr = implode('^', array_map(function ($url) {
+            return admin_url($url);
+        }, (array) $sourceUrls));
+
+        return $this->addVariables(['loads' => [
+            'fields'    => $fieldsStr,
+            'urls'      => $urlsStr,
+            'idField'   => $idField,
+            'textField' => $textField,
+        ]]);
+    }
+}

+ 1 - 0
src/Form/Field/Checkbox.php

@@ -8,6 +8,7 @@ use Dcat\Admin\Widgets\Checkbox as WidgetCheckbox;
 class Checkbox extends MultipleSelect
 class Checkbox extends MultipleSelect
 {
 {
     use CanCascadeFields;
     use CanCascadeFields;
+    use CanLoadFields;
 
 
     protected $style = 'primary';
     protected $style = 'primary';
 
 

+ 1 - 0
src/Form/Field/Radio.php

@@ -9,6 +9,7 @@ use Dcat\Admin\Widgets\Radio as WidgetRadio;
 class Radio extends Field
 class Radio extends Field
 {
 {
     use CanCascadeFields;
     use CanCascadeFields;
+    use CanLoadFields;
 
 
     protected $style = 'primary';
     protected $style = 'primary';
 
 

+ 1 - 55
src/Form/Field/Select.php

@@ -7,11 +7,11 @@ use Dcat\Admin\Form\Field;
 use Dcat\Admin\Support\Helper;
 use Dcat\Admin\Support\Helper;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Arr;
-use Illuminate\Support\Str;
 
 
 class Select extends Field
 class Select extends Field
 {
 {
     use CanCascadeFields;
     use CanCascadeFields;
+    use CanLoadFields;
 
 
     protected $cascadeEvent = 'change';
     protected $cascadeEvent = 'change';
 
 
@@ -84,60 +84,6 @@ class Select extends Field
         return $this;
         return $this;
     }
     }
 
 
-    /**
-     * Load options for other select on change.
-     *
-     * @param string $field
-     * @param string $sourceUrl
-     * @param string $idField
-     * @param string $textField
-     *
-     * @return $this
-     */
-    public function load($field, $sourceUrl, string $idField = 'id', string $textField = 'text')
-    {
-        if (Str::contains($field, '.')) {
-            $field = $this->formatName($field);
-        }
-
-        $class = $this->normalizeElementClass($field);
-
-        $url = admin_url($sourceUrl);
-
-        return $this->addVariables(['load' => compact('url', 'class', 'idField', 'textField')]);
-    }
-
-    /**
-     * Load options for other selects on change.
-     *
-     * @param string $fields
-     * @param string $sourceUrls
-     * @param string $idField
-     * @param string $textField
-     *
-     * @return $this
-     */
-    public function loads($fields = [], $sourceUrls = [], string $idField = 'id', string $textField = 'text')
-    {
-        $fieldsStr = implode('^', array_map(function ($field) {
-            if (Str::contains($field, '.')) {
-                return $this->normalizeElementClass($field).'_';
-            }
-
-            return $this->normalizeElementClass($field);
-        }, (array) $fields));
-        $urlsStr = implode('^', array_map(function ($url) {
-            return admin_url($url);
-        }, (array) $sourceUrls));
-
-        return $this->addVariables(['loads' => [
-            'fields'    => $fieldsStr,
-            'urls'      => $urlsStr,
-            'idField'   => $idField,
-            'textField' => $textField,
-        ]]);
-    }
-
     /**
     /**
      * Load options from current selected resource(s).
      * Load options from current selected resource(s).
      *
      *

+ 1 - 0
src/Form/Field/SelectTable.php

@@ -10,6 +10,7 @@ use Dcat\Admin\Widgets\DialogTable;
 class SelectTable extends Field
 class SelectTable extends Field
 {
 {
     use PlainInput;
     use PlainInput;
+    use CanLoadFields;
 
 
     /**
     /**
      * @var DialogTable
      * @var DialogTable

+ 28 - 6
src/Grid/Filter/Presenter/Select.php

@@ -287,13 +287,35 @@ class Select extends Presenter
      */
      */
     public function load($target, string $resourceUrl, string $idField = 'id', string $textField = 'text'): self
     public function load($target, string $resourceUrl, string $idField = 'id', string $textField = 'text'): self
     {
     {
-        $class = $this->filter->formatColumnClass($target);
-
-        $url = admin_url($resourceUrl);
-
-        $group = 'form';
+        return $this->loads($target, $resourceUrl, $idField, $textField);
+    }
 
 
-        return $this->addVariables(['load' => compact('url', 'class', 'idField', 'textField', 'group')]);
+    /**
+     * 联动加载多个字段.
+     *
+     * @param array|string $fields
+     * @param array|string $sourceUrls
+     * @param string $idField
+     * @param string $textField
+     *
+     * @return $this
+     */
+    public function loads($fields = [], $sourceUrls = [], string $idField = 'id', string $textField = 'text')
+    {
+        $fieldsStr = implode('^', array_map(function ($field) {
+            return $this->filter->formatColumnClass($field);
+        }, (array) $fields));
+        $urlsStr = implode('^', array_map(function ($url) {
+            return admin_url($url);
+        }, (array) $sourceUrls));
+
+        return $this->addVariables(['loads' => [
+            'fields'    => $fieldsStr,
+            'urls'      => $urlsStr,
+            'idField'   => $idField,
+            'textField' => $textField,
+            'group'     => 'form',
+        ]]);
     }
     }
 
 
     /**
     /**

+ 1 - 1
src/Support/Context.php

@@ -51,7 +51,7 @@ class Context extends Fluent
         return $this->set($key, $results);
         return $this->set($key, $results);
     }
     }
 
 
-    public function addMany($key, array $value)
+    public function merge($key, array $value)
     {
     {
         $results = $this->getArray($key);
         $results = $this->getArray($key);
 
 

Неке датотеке нису приказане због велике количине промена