From f64e5ed41854d24f72dbbcc8a0cd2d19dfe80965 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 2 Mar 2020 19:04:32 +0100 Subject: [PATCH] wip #3448 @1.5 --- src/public/bunchmultiple/bunchmultiple.js | 52 +++++++++++++++++++---- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/src/public/bunchmultiple/bunchmultiple.js b/src/public/bunchmultiple/bunchmultiple.js index 3a448f6..20f69df 100644 --- a/src/public/bunchmultiple/bunchmultiple.js +++ b/src/public/bunchmultiple/bunchmultiple.js @@ -53,7 +53,12 @@ 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) { @@ -76,10 +81,14 @@ 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)); }, @@ -119,6 +128,7 @@ }, initItem: function (values) { + var $this = this; var collapsed = true; var isNew = false; if (values === undefined) { @@ -134,23 +144,42 @@ 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); +} -- 2.39.5