Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 101 additions & 85 deletions assets/js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
2 changes: 1 addition & 1 deletion dist/scripts.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.