debian-mirror-gitlab/app/assets/javascripts/profile/add_ssh_key_validation.js

53 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-11-08 19:23:39 +05:30
export default class AddSshKeyValidation {
2022-03-02 08:16:31 +05:30
constructor(
supportedAlgorithms,
inputElement,
warningElement,
originalSubmitElement,
confirmSubmitElement,
) {
2018-11-08 19:23:39 +05:30
this.inputElement = inputElement;
this.form = inputElement.form;
2022-03-02 08:16:31 +05:30
this.supportedAlgorithms = supportedAlgorithms;
this.publicKeyRegExp = new RegExp(`^(${this.supportedAlgorithms.join('|')})`);
2018-11-08 19:23:39 +05:30
this.warningElement = warningElement;
this.originalSubmitElement = originalSubmitElement;
this.confirmSubmitElement = confirmSubmitElement;
this.isValid = false;
}
register() {
2021-03-08 18:12:59 +05:30
this.form.addEventListener('submit', (event) => this.submit(event));
2018-11-08 19:23:39 +05:30
this.confirmSubmitElement.addEventListener('click', () => {
this.isValid = true;
this.form.submit();
});
this.inputElement.addEventListener('input', () => this.toggleWarning(false));
}
submit(event) {
2022-03-02 08:16:31 +05:30
this.isValid = this.isPublicKey(this.inputElement.value);
2018-11-08 19:23:39 +05:30
if (this.isValid) return true;
event.preventDefault();
this.toggleWarning(true);
return false;
}
toggleWarning(isVisible) {
this.warningElement.classList.toggle('hide', !isVisible);
this.originalSubmitElement.classList.toggle('hide', isVisible);
}
2022-03-02 08:16:31 +05:30
isPublicKey(value) {
return this.publicKeyRegExp.test(value);
2018-11-08 19:23:39 +05:30
}
}