forms: use ajax to add a block row (#51369)

This commit is contained in:
Frédéric Péters 2021-02-23 14:08:41 +01:00
parent b10a29ed11
commit e75c8cd976
1 changed files with 45 additions and 16 deletions

View File

@ -366,10 +366,14 @@ $(function() {
}
});
});
$('form div[data-live-source] input, form div[data-live-source] select, form div[data-live-source] textarea').on('change input paste wcs:change', function(ev) {
var modified_field = $(this).parents('[data-field-id]').data('field-id');
$(this).parents('form').trigger('wcs:change', {modified_field: modified_field});
});
if ($('div[data-live-source]').length) {
$('form').on('change input paste wcs:change',
'div[data-live-source] input, div[data-live-source] select, div[data-live-source] textarea',
function(ev) {
var modified_field = $(this).parents('[data-field-id]').data('field-id');
$(this).parents('form').trigger('wcs:change', {modified_field: modified_field});
});
}
$('form div[data-live-source]').parents('form').trigger('wcs:change', {modified_field: 'init'});
/* searchable select */
@ -479,18 +483,43 @@ $(function() {
});
}
disable_single_block_remove_button();
$('.BlockSubWidget button.remove-button').on('click', function() {
if ($(this).parents('.BlockWidget').find('.BlockSubWidget').length > 1) {
$(this).parents('.BlockWidget').find('.list-add').show();
$(this).parents('.BlockSubWidget').remove();
disable_single_block_remove_button();
}
return false;
});
if ($('.BlockWidget').length) {
disable_single_block_remove_button();
$('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();
disable_single_block_remove_button();
}
return false;
});
const $added_sub_widget = $('.wcs-block-add-clicked').find('.BlockSubWidget').last();
if ($added_sub_widget.length) {
$added_sub_widget[0].scrollIntoView({behavior: "instant", block: "center", inline: "nearest"});
$('form').on('click keyup', 'div.BlockWidget .list-add button', function(ev) {
if (event.type == 'keyup' && event.keyCode !== 13) {
return;
}
ev.preventDefault();
const $block = $(this).parents('.BlockWidget');
const block_id = $block.data('field-id');
const $button = $(this);
const $form = $(this).parents('form');
var form_data = $form.serialize();
form_data += '&' + $button.attr('name') + '=' + $button.val();
$.ajax({
type: 'POST',
url: $form.attr('action') || window.location.pathname,
data: form_data,
headers: {'x-wcs-ajax-action': 'block-add-row'},
success: function(result, text_status, jqXHR) {
result = $(result);
$block.replaceWith(result.find('[data-field-id="' + block_id + '"]'));
}
});
});
const $added_sub_widget = $('.wcs-block-add-clicked').find('.BlockSubWidget').last();
if ($added_sub_widget.length) {
$added_sub_widget[0].scrollIntoView({behavior: "instant", block: "center", inline: "nearest"});
}
}
});