cartodb-4.42/lib/assets/javascripts/dashboard/helpers/password-validated-form.js

48 lines
1.4 KiB
JavaScript
Raw Normal View History

2024-04-06 13:25:13 +08:00
const $ = require('jquery');
const ConfirmPasswordModal = require('dashboard/components/password-confirmation/password-confirmation-view.js');
class PasswordValidatedForm {
static bindEventTo (formSelector, options) {
const form = $(formSelector);
const saveButton = form.find('.js-save');
saveButton.bind('click', event => {
event.preventDefault();
event.stopPropagation();
if (!options.passwordConfirmationNeeded) {
return options.onPasswordTyped ? options.onPasswordTyped() : this.addPasswordToForm(form);
}
this.showPasswordModal({
modalService: options.modals,
onPasswordTyped: options.onPasswordTyped || this.addPasswordToForm.bind(this, form)
});
});
}
static showPasswordModal (options) {
options.modalService.create(modalModel => {
return new ConfirmPasswordModal({
modalModel,
onPasswordTyped: options.onPasswordTyped,
updatePassword: options.updatePassword
});
});
}
static addPasswordToForm (form, password, options = {}) {
const passwordInput = document.createElement('input');
passwordInput.setAttribute('type', 'hidden');
passwordInput.setAttribute('name', 'password_confirmation');
passwordInput.setAttribute('value', password);
form.prepend(passwordInput);
if (!options.doNotSubmit) {
form.submit();
}
}
}
module.exports = PasswordValidatedForm;