]> _ Git - cubist_cms-back.git/commitdiff
wip #5301 @3
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 7 Jun 2022 11:39:24 +0000 (13:39 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 7 Jun 2022 11:39:24 +0000 (13:39 +0200)
src/public/bunchmultiple/bunchmultiple.js
src/resources/views/fields/select2_from_array.blade.php
src/resources/views/when.blade.php

index 5a256d89616fd2ab175a9ee589a08a13822f6296..746dc0e4d659392c4c885b62a96c95723232e20e 100644 (file)
         },
 
         initItem: function (values) {
-            console.log(values);
+
             var $this = this;
             var collapsed = true;
             var isNew = false;
             try {
                 $.each(values, function (k, v) {
                     var input = $(item).find('[name="' + id + '___' + k + '"]');
+
                     if (input.is('.hiddenfield')) {
                         $(input).parent().attr('data-values', JSON.stringify(v));
                     } else if ($(input).nextAll('[type="checkbox"]').length > 0) {
                         var cb = $(input).nextAll('[type="checkbox"]');
                         cb.attr('name', cb.prev('[type="hidden"]').attr('name')).attr('value', '1');
                         cb.prop('checked', v === true || v === '1' || v === 1);
-                    } else {
+                    } else if ($(input).is('.select2_from_array')) {
+                        try {
+                            if ($(input).data('ajax')!==null && $(input).find('option[value="' + k + '"]').length === 0) {
+                                var options = $(input).data('options');
+                                var newOption = new Option(options[v], v, false, false);
+                                $(input).append(newOption).trigger('change');
+                            }
+                        } catch (e) {
+                        }
+                        input.val(v);
+                    } else if (input) {
                         input.val(v);
                     }
                 });
index c1a31e50ce3a2ad32516f6d0ae0404631e20d928..a04b1789716a5d7fd15e9a0ac4192da71074c771 100644 (file)
@@ -1,16 +1,15 @@
 @php
     $o=$field['value'] ?? old(square_brackets_to_dots($field['name']))??$field['default'];
 
-    $is_null=null===$o;
+    $is_null=null===$o || $o==='null';
     if(!function_exists('select2_from_array_selected')){
         function select2_from_array_selected($key,$o){
-            if(null===$o){
+            if(null===$o || $o==='null'){
                 return false;
             }
             if(!is_array($o)){
                 return $o==$key;
-            }
-            if(is_array($o)){
+            }else {
                 return in_array($key,$o);
             }
             return false;
@@ -38,6 +37,7 @@ if($ajax){
     @if (isset($field['allows_multiple']) && $field['allows_multiple']==true)multiple @endif
     @if($ajax) data-ajax="{{$field['ajax']}}" @endif
     @if($allows_html) data-allow-html="1" @endif
+    data-options="{{ json_encode($field['options']) }}"
 >
     @if (isset($field['allows_null']) && $field['allows_null']==true)
         @if($is_null)
@@ -49,17 +49,13 @@ if($ajax){
 
     @if (count($field['options']))
         @foreach ($field['options'] as $key => $value)
-            @php
-                $selected=select2_from_array_selected($key,$o);
-            @endphp
-            @if($selected)
+            @if(!$is_null && select2_from_array_selected($key,$o))
                 <option value="{{ $key }}" selected>{{ $value }}</option>
             @elseif(!$ajax)
                 <option value="{{ $key }}">{{ $value }}</option>
             @endif
         @endforeach
     @endif
-
 </select>
 
 {{-- HINT --}}
index 2084f0ebd44ab895dc1f13e373cbd1b0822a72a8..9ef1945ec16c61e844869beb67261af79540ea21 100644 (file)
 @push('crud_fields_scripts')
-<script type="text/javascript">
-    jQuery('document').ready(function ($) {
-        var triggersWhenChange = function (f) {
-            var form;
-            var prefix = '';
-            if (f === undefined) {
-                form = $('section.content form[method="post"]');
-            } else {
-                if ($(f).closest('.subform').length > 0) {
-                    form = $(f).closest('.subform');
-                    prefix = $(form).data('bmid') + '___';
+    <script type="text/javascript">
+        jQuery('document').ready(function ($) {
+            var triggersWhenChange = function (f) {
+                var form;
+                var prefix = '';
+                if (f === undefined) {
+                    form = $('section.content form[method="post"]');
                 } else {
-                    form = $(f).closest('form');
+                    if ($(f).closest('.subform').length > 0) {
+                        form = $(f).closest('.subform');
+                        prefix = $(form).data('bmid') + '___';
+                    } else {
+                        form = $(f).closest('form');
+                    }
                 }
-            }
 
-            $(form).find('[data-when]').each(function () {
-                if ($(this).parent().hasClass('bunchmultiple__wrapper')) {
-                    $(this).parent().parent().attr('data-when', $(this).attr('data-when'));
-                    $(this).attr('data-when', null);
-                }
-            });
+                $(form).find('[data-when]').each(function () {
+                    if ($(this).parent().hasClass('bunchmultiple__wrapper')) {
+                        $(this).parent().parent().attr('data-when', $(this).attr('data-when'));
+                        $(this).attr('data-when', null);
+                    }
+                });
 
-            $(form).find('[data-when]').removeClass('when-visible').each(function () {
-                if ($(this).data('when-normalized') === undefined) {
-                    $(this).data('when-normalized', normalizeWhen($(this).data('when')));
-                }
-                var when = $(this).data('when-normalized');
+                $(form).find('[data-when]').removeClass('when-visible').each(function () {
+                    if ($(this).data('when-normalized') === undefined) {
+                        $(this).data('when-normalized', normalizeWhen($(this).data('when')));
+                    }
+                    var when = $(this).data('when-normalized');
 
-                var match = true;
-                var order = 1000;
-                $.each(when, function (property, conditions) {
-                    var fname = prefix + property;
-                    var e = $(form).find('[name="' + fname + '"]');
-                    var val;
-                    if ($(e).length === 1) {
-                        val = $(e).val().toString();
-                    } else {
-                        var f = $(e).filter(':checked,:not(:checkbox)').last();
-                        if ($(f).length === 0) {
-                            val = '';
+                    var match = true;
+                    var order = 1000;
+                    $.each(when, function (property, conditions) {
+                        var fname = prefix + property;
+                        var e = $(form).find('[name="' + fname + '"]');
+                        var val;
+                        if ($(e).length === 1) {
+                            try {
+                                val = $(e).val().toString();
+                            } catch (e) {
+                                val = '';
+                            }
                         } else {
-                            val = $(f).val().toString();
+                            var f = $(e).filter(':checked,:not(:checkbox)').last();
+                            if ($(f).length === 0) {
+                                val = '';
+                            } else {
+                                try {
+                                    val = $(f).val().toString();
+                                } catch (e) {
+                                    val = '';
+                                }
+                            }
                         }
-                    }
-                    var matchproperty = false;
-                    $.each(conditions, function (k, condition) {
-                        if (condition.id === val) {
-                            matchproperty = true;
-                            order = Math.min(order, condition.order);
+                        var matchproperty = false;
+                        $.each(conditions, function (k, condition) {
+                            if (condition.id === val) {
+                                matchproperty = true;
+                                order = Math.min(order, condition.order);
+                                return false;
+                            }
+                        });
+                        if (matchproperty === false) {
+                            match = false
                             return false;
                         }
                     });
-                    if (matchproperty === false) {
-                        match = false
-                        return false;
+
+                    if (match) {
+                        $(this).addClass('when-visible');
+                        $(this).attr('data-when-order', order);
                     }
                 });
 
-                if (match) {
-                    $(this).addClass('when-visible');
-                    $(this).attr('data-when-order', order);
-                }
-            });
-
-            //orderWhen();
-        };
+                //orderWhen();
+            };
 
-        function isNumber(n) {
-            return typeof n === 'number' && !isNaN(n);
-        }
+            function isNumber(n) {
+                return typeof n === 'number' && !isNaN(n);
+            }
 
-        $(document).on('change', 'input,select,textarea', function () {
-            triggersWhenChange($(this));
-        });
-        $(document).on('cubist.bunchmultiple.added', function () {
-            $('.subform[data-bmid]').each(function () {
+            $(document).on('change', 'input,select,textarea', function () {
                 triggersWhenChange($(this));
             });
-        });
-
-        $('main form').each(function () {
-            triggersWhenChange($(this));
-        });
+            $(document).on('cubist.bunchmultiple.added', function () {
+                $('.subform[data-bmid]').each(function () {
+                    triggersWhenChange($(this));
+                });
+            });
 
-        function orderWhen() {
-            $("[data-when]").parent().each(function () {
-                // Inspired from https://stackoverflow.com/a/14160529/1082031
-                $(this).find('[data-when-order]').sort(function (a, b) {
-                    return $(a).data('when-order') - $(b).data('when-order');
-                }).appendTo(this);
+            $('main form').each(function () {
+                triggersWhenChange($(this));
             });
-        }
 
-        function normalizeWhen(w) {
-            var res = {};
-            $.each(w, function (property, conditions) {
-                if (!Array.isArray(conditions)) {
-                    conditions = [conditions];
-                }
-                var normalizedConditions = [];
-                $.each(conditions, function (k, condition) {
-                    if (!isObject(condition)) {
-                        condition = {id: condition};
-                    }
-                    if (!condition.hasOwnProperty('order')) {
-                        condition.order = k;
+            function orderWhen() {
+                $("[data-when]").parent().each(function () {
+                    // Inspired from https://stackoverflow.com/a/14160529/1082031
+                    $(this).find('[data-when-order]').sort(function (a, b) {
+                        return $(a).data('when-order') - $(b).data('when-order');
+                    }).appendTo(this);
+                });
+            }
+
+            function normalizeWhen(w) {
+                var res = {};
+                $.each(w, function (property, conditions) {
+                    if (!Array.isArray(conditions)) {
+                        conditions = [conditions];
                     }
-                    condition.id = condition.id.toString();
-                    normalizedConditions[k] = condition;
+                    var normalizedConditions = [];
+                    $.each(conditions, function (k, condition) {
+                        if (!isObject(condition)) {
+                            condition = {id: condition};
+                        }
+                        if (!condition.hasOwnProperty('order')) {
+                            condition.order = k;
+                        }
+                        condition.id = condition.id.toString();
+                        normalizedConditions[k] = condition;
+                    });
+                    res[property] = normalizedConditions;
                 });
-                res[property] = normalizedConditions;
-            });
-            return res;
-        }
+                return res;
+            }
 
-        function isObject(value) {
-            return value && typeof value === 'object' && value.constructor === Object;
-        }
+            function isObject(value) {
+                return value && typeof value === 'object' && value.constructor === Object;
+            }
 
-    });
+        });
 
-</script>
+    </script>
 @endpush
 
 @push('crud_fields_styles')
-<style type="text/css">
-    [data-when] {
-        display: none;
-    }
+    <style type="text/css">
+        [data-when] {
+            display: none;
+        }
 
-    .when-visible[data-when] {
-        display: block;
-    }
-</style>
+        .when-visible[data-when] {
+            display: block;
+        }
+    </style>
 @endpush