diff --git a/assets/js/scripts.js b/assets/js/scripts.js index 366a53f..c595123 100644 --- a/assets/js/scripts.js +++ b/assets/js/scripts.js @@ -16,97 +16,113 @@ $(function () { const $groups = $('.wpcf7-field-groups'); // Only need to work if there is any group. - if ($groups.length) { - // Let's grab the groups models to append them when necessary. - $groups.each(function () { - $(this).data( - 'group-model', - $(this).find('.wpcf7-field-group').eq(0).clone() - ); - }); + if (!$groups.length) { + return; + } - $('body').on( - 'wpcf7-field-groups/change', - '.wpcf7-field-groups', - function () { - // For each group inside this we have to adjust name parameter. - const $groupsInside = $(this).find('.wpcf7-field-group'); - $groupsInside.each(function (index) { - $(this) - .find('.wpcf7-field-group-remove') - .toggle(index > 0); - const i = index + 1; - $(this) - .find('[name]') - .each(function () { - const $$ = $(this), - $formControlWrap = $$.closest( - '.wpcf7-form-control-wrap' - ), - name = $$.attr('name'), - isArray = name.indexOf('[]') > -1, - rawName = name.replace('[]', ''); - let newName = - rawName.replace(/__[0-9]*/, '') + '__' + i; + // Let's grab the groups models to append them when necessary. + $groups.each(function () { + $(this).data( + 'group-model', + $(this).find('.wpcf7-field-group').eq(0).clone() + ); + }); - // The form control wrap class doesn't have `[]` chars... - if ( - $formControlWrap.length && - !$formControlWrap.hasClass(newName) - ) { - $formControlWrap - .removeClass(rawName) - .addClass(newName); - } - // but the field can have. - newName += isArray ? '[]' : ''; - $$.attr('name', newName); - $formControlWrap.attr('data-name', newName); - }); - }); + $('body').on( + 'wpcf7-field-groups/change', + '.wpcf7-field-groups', + function () { + // For each group inside this we have to adjust name parameter. + const $groupsInside = $(this).find('.wpcf7-field-group'); + $groupsInside.each(function (index) { $(this) - .find('.wpcf7-field-group-count') - .val($groupsInside.length); - } - ); - // Set thing up for the first time. - $groups.trigger('wpcf7-field-groups/change'); + .find('.wpcf7-field-group-remove') + .toggle(index > 0); + const i = index + 1; + $(this) + .find('[name]') + .each(function () { + const $$ = $(this), + $formControlWrap = $$.closest( + '.wpcf7-form-control-wrap' + ), + name = $$.attr('name'), + isArray = name.indexOf('[]') > -1, + rawName = name.replace('[]', ''); + let newName = + rawName.replace(/__[0-9]*/, '') + '__' + i; - // Handle the buttons action. - $('body').on( - 'click', - '.wpcf7-field-group-add, .wpcf7-field-group-remove', - function () { - const $$ = $(this), - $allGroups = $$.closest('.wpcf7-field-groups'); + // The form control wrap class doesn't have `[]` chars... + if ( + $formControlWrap.length && + !$formControlWrap.hasClass(newName) + ) { + $formControlWrap + .removeClass(rawName) + .addClass(newName); + } + // but the field can have. + newName += isArray ? '[]' : ''; + $$.attr('name', newName); + $formControlWrap.attr('data-name', newName); + }); + }); + $(this) + .find('.wpcf7-field-group-count') + .val($groupsInside.length); + } + ); + // Set thing up for the first time. + $groups.trigger('wpcf7-field-groups/change'); - if ($$.hasClass('wpcf7-field-group-add')) { - const $newGroup = $allGroups - .data('group-model') - .clone(true); - $allGroups.append($newGroup); - $$.trigger('wpcf7-field-groups/added', $newGroup); - } else { - $$.trigger('wpcf7-field-groups/removed'); - $$.closest('.wpcf7-field-group').remove(); - } - $allGroups.trigger('wpcf7-field-groups/change'); - return false; - } - ); + // Handle the buttons action. + $('body').on( + 'click', + '.wpcf7-field-group-add, .wpcf7-field-group-remove', + function () { + const $$ = $(this), + $allGroups = $$.closest('.wpcf7-field-groups'); - // Exclusive Checkbox - $groups.on( - 'click', - '.wpcf7-exclusive-checkbox input:checkbox', - function () { - const name = $(this).attr('name'); - $groups - .find('input:checkbox[name="' + name + '"]') - .not(this) - .prop('checked', false); + if ($$.hasClass('wpcf7-field-group-add')) { + const $newGroup = $allGroups + .data('group-model') + .clone(true); + $allGroups.append($newGroup); + $$.trigger('wpcf7-field-groups/added', $newGroup); + } else { + $$.trigger('wpcf7-field-groups/removed'); + $$.closest('.wpcf7-field-group').remove(); } - ); - } + $allGroups.trigger('wpcf7-field-groups/change'); + return false; + } + ); + + // Exclusive Checkbox + $groups.on( + 'click', + '.wpcf7-exclusive-checkbox input:checkbox', + function () { + const name = $(this).attr('name'); + $groups + .find('input:checkbox[name="' + name + '"]') + .not(this) + .prop('checked', false); + } + ); }); })(jQuery); + +const setupCf7RepeaterFields = () => { + const groups = document.querySelectorAll('.wpcf7-field-groups'); + if (!groups.length) { + return; + } + + groups.forEach((group) => { + group.dataset.group_model = group + .querySelector('.wpcf7-field-group') + .cloneNode(true); + }); +}; +document.addEventListener('DOMContentLoaded', setupCf7RepeaterFields); diff --git a/dist/scripts.js b/dist/scripts.js index 75f395b..7cc42db 100644 --- a/dist/scripts.js +++ b/dist/scripts.js @@ -1 +1 @@ -!function(e){"use strict";e((function(){const c=e(".wpcf7-field-groups");c.length&&(c.each((function(){e(this).data("group-model",e(this).find(".wpcf7-field-group").eq(0).clone())})),e("body").on("wpcf7-field-groups/change",".wpcf7-field-groups",(function(){const c=e(this).find(".wpcf7-field-group");c.each((function(c){e(this).find(".wpcf7-field-group-remove").toggle(c>0);const o=c+1;e(this).find("[name]").each((function(){const c=e(this),t=c.closest(".wpcf7-form-control-wrap"),n=c.attr("name"),i=n.indexOf("[]")>-1,f=n.replace("[]","");let p=f.replace(/__[0-9]*/,"")+"__"+o;t.length&&!t.hasClass(p)&&t.removeClass(f).addClass(p),p+=i?"[]":"",c.attr("name",p),t.attr("data-name",p)}))})),e(this).find(".wpcf7-field-group-count").val(c.length)})),c.trigger("wpcf7-field-groups/change"),e("body").on("click",".wpcf7-field-group-add, .wpcf7-field-group-remove",(function(){const c=e(this),o=c.closest(".wpcf7-field-groups");if(c.hasClass("wpcf7-field-group-add")){const e=o.data("group-model").clone(!0);o.append(e),c.trigger("wpcf7-field-groups/added",e)}else c.trigger("wpcf7-field-groups/removed"),c.closest(".wpcf7-field-group").remove();return o.trigger("wpcf7-field-groups/change"),!1})),c.on("click",".wpcf7-exclusive-checkbox input:checkbox",(function(){const o=e(this).attr("name");c.find('input:checkbox[name="'+o+'"]').not(this).prop("checked",!1)})))}))}(jQuery); \ No newline at end of file +!function(e){"use strict";e((function(){const o=e(".wpcf7-field-groups");o.length&&(o.each((function(){e(this).data("group-model",e(this).find(".wpcf7-field-group").eq(0).clone())})),e("body").on("wpcf7-field-groups/change",".wpcf7-field-groups",(function(){const o=e(this).find(".wpcf7-field-group");o.each((function(o){e(this).find(".wpcf7-field-group-remove").toggle(o>0);const c=o+1;e(this).find("[name]").each((function(){const o=e(this),t=o.closest(".wpcf7-form-control-wrap"),n=o.attr("name"),f=n.indexOf("[]")>-1,i=n.replace("[]","");let r=i.replace(/__[0-9]*/,"")+"__"+c;t.length&&!t.hasClass(r)&&t.removeClass(i).addClass(r),r+=f?"[]":"",o.attr("name",r),t.attr("data-name",r)}))})),e(this).find(".wpcf7-field-group-count").val(o.length)})),o.trigger("wpcf7-field-groups/change"),e("body").on("click",".wpcf7-field-group-add, .wpcf7-field-group-remove",(function(){const o=e(this),c=o.closest(".wpcf7-field-groups");if(o.hasClass("wpcf7-field-group-add")){const e=c.data("group-model").clone(!0);c.append(e),o.trigger("wpcf7-field-groups/added",e)}else o.trigger("wpcf7-field-groups/removed"),o.closest(".wpcf7-field-group").remove();return c.trigger("wpcf7-field-groups/change"),!1})),o.on("click",".wpcf7-exclusive-checkbox input:checkbox",(function(){const c=e(this).attr("name");o.find('input:checkbox[name="'+c+'"]').not(this).prop("checked",!1)})))}))}(jQuery),document.addEventListener("DOMContentLoaded",(()=>{const e=document.querySelectorAll(".wpcf7-field-groups");e.length&&e.forEach((e=>{e.dataset.group_model=e.querySelector(".wpcf7-field-group").cloneNode(!0)}))})); \ No newline at end of file