cleaner submit button setup and teardown in massinput js util
This commit is contained in:
parent
d030ec1b5f
commit
351f7ace74
@ -47,11 +47,14 @@
|
||||
|
||||
massInputFormSubmitHandler = makeSubmitHandler();
|
||||
|
||||
// "unarm" submit buttons inside this massinput so browser
|
||||
// setup submit buttons inside this massinput so browser
|
||||
// uses correct submit button for form submission.
|
||||
// contents of the massinput will be replaced either way,
|
||||
// so unarming is no problem
|
||||
unarmSubmitButtons(massInputFormSubmitHandler);
|
||||
var buttons = getMassInputSubmitButtons();
|
||||
buttons.forEach(function(button) {
|
||||
setupSubmitButton(button);
|
||||
});
|
||||
|
||||
massInputForm.addEventListener('submit', massInputFormSubmitHandler);
|
||||
|
||||
@ -83,7 +86,7 @@
|
||||
|
||||
return function(event) {
|
||||
var activeElement;
|
||||
|
||||
|
||||
// check if event occured from either a mass input add/delete button or
|
||||
// from inside one of massinput's inputs (i.e. they are focused/active)
|
||||
if (event.type === 'click') {
|
||||
@ -91,7 +94,7 @@
|
||||
} else {
|
||||
activeElement = element.querySelector(':focus, :active');
|
||||
}
|
||||
|
||||
|
||||
if (!activeElement) {
|
||||
return false;
|
||||
}
|
||||
@ -137,14 +140,20 @@
|
||||
};
|
||||
}
|
||||
|
||||
function unarmSubmitButtons(submitHandler) {
|
||||
var buttons = Array.from(element.querySelectorAll('button[type="submit"][name][value]'));
|
||||
buttons.forEach(function(button) {
|
||||
button.setAttribute('type', 'button');
|
||||
button.classList.add(MASS_INPUT_SUBMIT_BUTTON_CLASS);
|
||||
function getMassInputSubmitButtons() {
|
||||
return Array.from(element.querySelectorAll('button[type="submit"][name][value], .' + MASS_INPUT_SUBMIT_BUTTON_CLASS));
|
||||
}
|
||||
|
||||
button.addEventListener('click', submitHandler);
|
||||
});
|
||||
function setupSubmitButton(button) {
|
||||
button.setAttribute('type', 'button');
|
||||
button.classList.add(MASS_INPUT_SUBMIT_BUTTON_CLASS);
|
||||
button.addEventListener('click', massInputFormSubmitHandler);
|
||||
}
|
||||
|
||||
function resetSubmitButton(button) {
|
||||
button.setAttribute('type', 'submit');
|
||||
button.classList.remove(MASS_INPUT_SUBMIT_BUTTON_CLASS);
|
||||
button.removeEventListener('click', massInputFormSubmitHandler);
|
||||
}
|
||||
|
||||
function processResponse(response) {
|
||||
@ -200,10 +209,9 @@
|
||||
element.classList.remove(MASS_INPUT_INITIALIZED_CLASS);
|
||||
massInputForm.removeEventListener('submit', massInputFormSubmitHandler)
|
||||
|
||||
Array.from(element.querySelectorAll('.' + MASS_INPUT_SUBMIT_BUTTON_CLASS)).forEach(function(button) {
|
||||
button.removeEventListener('click', massInputFormSubmitHandler);
|
||||
button.classList.remove(MASS_INPUT_SUBMIT_BUTTON_CLASS);
|
||||
button.setAttribute('type', 'submit');
|
||||
var buttons = getMassInputSubmitButtons();
|
||||
buttons.forEach(function(button) {
|
||||
resetSubmitButton(button);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user