debian-mirror-gitlab/spec/javascripts/dirty_submit/helper.js

49 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-12-13 13:39:08 +05:30
import DirtySubmitForm from '~/dirty_submit/dirty_submit_form';
import setTimeoutPromiseHelper from '../helpers/set_timeout_promise_helper';
2019-03-02 22:35:43 +05:30
function isCheckableType(type) {
return /^(radio|checkbox)$/.test(type);
}
export function setInputValue(element, value) {
const { type } = element;
let eventType;
if (isCheckableType(type)) {
element.checked = !element.checked;
eventType = 'change';
} else {
element.value = value;
eventType = 'input';
}
2018-12-13 13:39:08 +05:30
element.dispatchEvent(
2019-03-02 22:35:43 +05:30
new Event(eventType, {
2018-12-13 13:39:08 +05:30
bubbles: true,
}),
);
return setTimeoutPromiseHelper(DirtySubmitForm.THROTTLE_DURATION);
}
2019-03-02 22:35:43 +05:30
export function getInputValue(input) {
return isCheckableType(input.type) ? input.checked : input.value;
}
export function createForm(type = 'text') {
2018-12-13 13:39:08 +05:30
const form = document.createElement('form');
form.innerHTML = `
2019-03-02 22:35:43 +05:30
<input type="${type}" name="${type}" class="js-input"/>
2018-12-13 13:39:08 +05:30
<button type="submit" class="js-dirty-submit"></button>
`;
2019-03-02 22:35:43 +05:30
2018-12-13 13:39:08 +05:30
const input = form.querySelector('.js-input');
const submit = form.querySelector('.js-dirty-submit');
return {
form,
input,
submit,
};
}