forms: update attribute names after block row deletion (#62919)

This commit is contained in:
Frédéric Péters 2022-04-10 09:30:58 +02:00
parent 8ca17f4147
commit d51b91ae6f
2 changed files with 27 additions and 8 deletions

View File

@ -593,7 +593,26 @@ $(function() {
$('form').on('click', '.BlockSubWidget button.remove-button', function() {
if ($(this).parents('.BlockWidget').find('.BlockSubWidget').length > 1) {
$(this).parents('.BlockWidget').find('.list-add').show();
$(this).parents('.BlockSubWidget').remove();
/* rename attributes in following blocks */
const $subwidget = $(this).parents('.BlockSubWidget').first();
const name_parts = $subwidget.data('widget-name').match(/(.*)(\d+)$/);
const prefix = name_parts[1];
const idx = parseInt(name_parts[2]);
function replace_prefix(elem, prefix1, prefix2) {
$.each(elem.attributes, function() {
if (this.specified) {
this.value = this.value.replace(prefix1, prefix2);
}
});
}
$subwidget.nextAll('.BlockSubWidget').each(function(i, elem) {
const prefix1 = prefix + (idx + i + 1);
const prefix2 = prefix + (idx + i);
replace_prefix(elem, prefix1, prefix2);
$(elem).find('*').each(function(i, elem_child) { replace_prefix(elem_child, prefix1, prefix2); });
});
/* then remove row */
$subwidget.remove();
disable_single_block_remove_button();
}
return false;

View File

@ -76,7 +76,7 @@ $(function() {
var weekday = $option.data('weekday');
nb_days += 1;
current_day_div = $('<div><div class="head">' + weekday + '<br>' + day_label + '</div></div>');
current_day_div.appendTo($('#form_{{widget.name}}_table'));
current_day_div.appendTo($table);
current_date = option_date;
}
var day_time = $option.data('time');
@ -102,8 +102,8 @@ $(function() {
}
var go_prev = $('<button class="prev"></button>');
var go_next = $('<button class="next"></button>');
go_prev.prependTo($('#form_{{widget.name}}_table'));
go_next.appendTo($('#form_{{widget.name}}_table'));
go_prev.prependTo($table);
go_next.appendTo($table);
go_prev.on('click', function() {
current_offset = Math.max(0, current_offset - 1);
display(current_offset);
@ -116,7 +116,7 @@ $(function() {
});
function display(offset) {
$('#form_{{widget.name}}_table > div').each(function(idx, elem) {
$table.children('div').each(function(idx, elem) {
if (idx >= offset && idx < offset+column_count) {
$(elem).show();
} else {
@ -156,10 +156,10 @@ $(function() {
layout_change_timeout_id = setTimeout(set_layout, 200);
});
$('#form_{{widget.name}}_table span.selectable').on('click', function() {
$('#form_{{widget.name}}_table span.timetable-cell').removeClass('on');
$table.find('span.selectable').on('click', function() {
$table.find('span.timetable-cell').removeClass('on');
$(this).addClass('on');
$('#form_{{widget.name}}').val($(options[$(this).data('idx')]).attr('value'));
$select.val($(options[$(this).data('idx')]).attr('value'));
$select.parents('div.widget-prefilled').removeClass('widget-prefilled');
$select.trigger('wcs:change');
});