]> _ Git - cubist_cms-back.git/commitdiff
wip #3448 @1.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 2 Mar 2020 18:04:32 +0000 (19:04 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 2 Mar 2020 18:04:32 +0000 (19:04 +0100)
src/public/bunchmultiple/bunchmultiple.js

index 3a448f638a9e9808eb13ab120199652d10d1b889..20f69dff68c369a8b28be58867f3a40f9cc9a86b 100644 (file)
                 return true;
             });
 
-            this.element.on('keyup', 'input,select,textarea', function () {
+            this.element.on('change', '.bunchmultiple', function () {
+                $this.update();
+                return true;
+            });
+
+            this.element.on('keyup', 'input' + this.bmidselector + ',select' + this.bmidselector + ',textarea' + this.bmidselector, function () {
                 $this.updateLegends();
                 return true;
             });
@@ -68,7 +73,7 @@
             var val = [];
             var $this = this;
             var id = this.element.attr('id');
-            $.each(this.element.find('.item' + this.bmidselector + ':not(.sample) .subform'+ this.bmidselector), function () {
+            $.each(this.element.find('.item' + this.bmidselector + ':not(.sample) .subform' + this.bmidselector), function () {
                 var data = $(this).find(':input' + $this.bmidselector).serializeArray();
                 var filteredData = {};
                 $.each(data, function (k, v) {
                     filteredData[name] = v.value;
                 });
 
+                $(this).find('.hiddenfield').each(function () {
+                    var e = $(this).attr('name').split('___');
+                    var name = e.pop();
+                    filteredData[name] = JSON.parse($(this).attr('value'));
+                });
+
                 val.push(filteredData);
             });
-
-            console.log(val);
             this.element.find('.hiddenfield' + this.bmidselector).val(JSON.stringify(val));
         },
 
         },
 
         initItem: function (values) {
+            var $this = this;
             var collapsed = true;
             var isNew = false;
             if (values === undefined) {
             if (collapsed) {
                 $(item).addClass('collapsed');
             }
+            // Handle nested multiple bunch
+            $(item).find('.subform>.bunchmultiple').each(function () {
+                var newrand = $this.id + '_' + Math.round(Math.random() * 10000000);
+                var sampleId = $(this).attr('id');
+                var html = replaceAll(sampleId, newrand, $(this).html());
+                $(this).html(html);
+                $(this).attr('id', newrand)
+            });
+
             $(this.element).find(".bunchmultiple__items" + this.bmidselector).append($(item));
             this.setValues(item, values);
+            $(item).find('.subform>.bunchmultiple').bunchmultiple();
+
+            $(item).find('.form-group').find('input, select, .dropzone, textarea').each(function () {
+                if ($(this).closest('.bunchmultiple').attr('id') !== $this.id) {
+                    return;
+                }
+                $(this).attr('data-bmid', $this.id);
+            });
 
-            $(item).find('.form-group').find('input, select, .dropzone, textarea').attr('data-bmid', this.id);
 
             $(document).trigger('cubist.bunchmultiple.added');
+            $(this).trigger('change');
         },
 
         setValues(item, values) {
             var id = this.element.attr('id');
             try {
                 $.each(values, function (k, v) {
-                    var inputs = $(item).find('[name="' + id + '___' + k + '"]');
-                    if (inputs.filter('[type="checkbox"]').length > 0) {
-                        inputs.filter('[type="checkbox"]').prop('checked', v === '1');
+                    var input = $(item).find('[name="' + id + '___' + k + '"]');
+                    if (input.is('.hiddenfield')) {
+                        $(input).parent().attr('data-values', JSON.stringify(v));
+                    } else if (input.filter('[type="checkbox"]').length > 0) {
+                        input.filter('[type="checkbox"]').prop('checked', v === '1');
                     } else {
-                        inputs.val(v);
+                        input.val(v);
                     }
                 });
             } catch (e) {
@@ -187,3 +216,8 @@ jQuery.cachedScript = function (url, options) {
     // Return the jqXHR object so we can chain callbacks
     return jQuery.ajax(options);
 };
+
+function replaceAll(from, to, str) {
+    var e = str.split(from);
+    return e.join(to);
+}