demarches-normaliennes/app/javascript/new_design/champs/repetition.js

34 lines
1.1 KiB
JavaScript
Raw Normal View History

import { delegate, fire } from '@utils';
2019-01-30 16:14:15 +01:00
const CHAMP_SELECTOR = '.editable-champ';
2019-01-30 16:14:15 +01:00
const BUTTON_SELECTOR = '.button.remove-row';
const DESTROY_INPUT_SELECTOR = 'input[type=hidden][name*=_destroy]';
const DOM_ID_INPUT_SELECTOR = 'input[type=hidden][name*=deleted_row_dom_ids]';
2019-01-30 16:14:15 +01:00
2020-04-30 15:42:29 +02:00
delegate('click', BUTTON_SELECTOR, (evt) => {
2019-02-04 21:04:15 +01:00
evt.preventDefault();
2019-01-30 16:14:15 +01:00
2019-02-04 21:04:15 +01:00
const row = evt.target.closest('.row');
2019-01-30 16:14:15 +01:00
2019-02-04 21:04:15 +01:00
for (let input of row.querySelectorAll(DESTROY_INPUT_SELECTOR)) {
input.disabled = false;
input.value = true;
}
row.querySelector(DOM_ID_INPUT_SELECTOR).disabled = false;
2019-02-04 21:04:15 +01:00
for (let champ of row.querySelectorAll(CHAMP_SELECTOR)) {
champ.remove();
}
2019-01-30 16:14:15 +01:00
2019-02-04 21:04:15 +01:00
evt.target.remove();
row.classList.remove('row');
// We could debounce the autosave request, so that row removal would be batched
// with the next changes.
// However *adding* a new repetition row isn't debounced (changes are immediately
// effective server-side).
// So, to avoid ordering issues, enqueue an autosave request as soon as the row
// is removed.
fire(row, 'autosave:trigger');
2019-01-30 16:14:15 +01:00
});