debian-mirror-gitlab/app/assets/javascripts/username_validator.js

134 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
/* eslint-disable comma-dangle, consistent-return, class-methods-use-this, arrow-parens, no-param-reassign, max-len */
2017-09-10 17:25:29 +05:30
import _ from 'underscore';
const debounceTimeoutDuration = 1000;
const invalidInputClass = 'gl-field-error-outline';
const successInputClass = 'gl-field-success-outline';
const unavailableMessageSelector = '.username .validation-error';
const successMessageSelector = '.username .validation-success';
const pendingMessageSelector = '.username .validation-pending';
const invalidMessageSelector = '.username .gl-field-error';
export default class UsernameValidator {
constructor() {
this.inputElement = $('#new_user_username');
this.inputDomElement = this.inputElement.get(0);
this.state = {
available: false,
valid: false,
pending: false,
empty: true
};
const debounceTimeout = _.debounce((username) => {
this.validateUsername(username);
}, debounceTimeoutDuration);
this.inputElement.on('keyup.username_check', () => {
const username = this.inputElement.val();
this.state.valid = this.inputDomElement.validity.valid;
this.state.empty = !username.length;
2016-11-03 12:29:30 +05:30
2017-09-10 17:25:29 +05:30
if (this.state.valid) {
return debounceTimeout(username);
2016-11-03 12:29:30 +05:30
}
2017-09-10 17:25:29 +05:30
this.renderState();
});
2016-11-03 12:29:30 +05:30
2017-09-10 17:25:29 +05:30
// Override generic field validation
this.inputElement.on('invalid', this.interceptInvalid.bind(this));
}
2016-11-03 12:29:30 +05:30
2017-09-10 17:25:29 +05:30
renderState() {
// Clear all state
this.clearFieldValidationState();
2016-11-03 12:29:30 +05:30
2017-09-10 17:25:29 +05:30
if (this.state.valid && this.state.available) {
return this.setSuccessState();
2016-11-03 12:29:30 +05:30
}
2017-09-10 17:25:29 +05:30
if (this.state.empty) {
return this.clearFieldValidationState();
2016-11-03 12:29:30 +05:30
}
2017-09-10 17:25:29 +05:30
if (this.state.pending) {
return this.setPendingState();
2016-11-03 12:29:30 +05:30
}
2017-09-10 17:25:29 +05:30
if (!this.state.available) {
return this.setUnavailableState();
2016-11-03 12:29:30 +05:30
}
2017-09-10 17:25:29 +05:30
if (!this.state.valid) {
return this.setInvalidState();
2016-11-03 12:29:30 +05:30
}
2017-09-10 17:25:29 +05:30
}
2016-11-03 12:29:30 +05:30
2017-09-10 17:25:29 +05:30
interceptInvalid(event) {
event.preventDefault();
event.stopPropagation();
}
2016-11-03 12:29:30 +05:30
2017-09-10 17:25:29 +05:30
validateUsername(username) {
if (this.state.valid) {
this.state.pending = true;
this.state.available = false;
this.renderState();
return $.ajax({
type: 'GET',
url: `${gon.relative_url_root}/users/${username}/exists`,
dataType: 'json',
success: (res) => this.setAvailabilityState(res.exists)
});
2016-11-03 12:29:30 +05:30
}
2017-09-10 17:25:29 +05:30
}
2016-11-03 12:29:30 +05:30
2017-09-10 17:25:29 +05:30
setAvailabilityState(usernameTaken) {
if (usernameTaken) {
this.state.valid = false;
this.state.available = false;
} else {
this.state.available = true;
2016-11-03 12:29:30 +05:30
}
2017-09-10 17:25:29 +05:30
this.state.pending = false;
this.renderState();
}
2016-11-03 12:29:30 +05:30
2017-09-10 17:25:29 +05:30
clearFieldValidationState() {
this.inputElement.siblings('p').hide();
this.inputElement.removeClass(invalidInputClass)
.removeClass(successInputClass);
}
setUnavailableState() {
const $usernameUnavailableMessage = this.inputElement.siblings(unavailableMessageSelector);
this.inputElement.addClass(invalidInputClass).removeClass(successInputClass);
$usernameUnavailableMessage.show();
}
setSuccessState() {
const $usernameSuccessMessage = this.inputElement.siblings(successMessageSelector);
this.inputElement.addClass(successInputClass).removeClass(invalidInputClass);
$usernameSuccessMessage.show();
}
setPendingState() {
const $usernamePendingMessage = $(pendingMessageSelector);
if (this.state.pending) {
$usernamePendingMessage.show();
} else {
$usernamePendingMessage.hide();
2016-11-03 12:29:30 +05:30
}
}
2017-09-10 17:25:29 +05:30
setInvalidState() {
const $inputErrorMessage = $(invalidMessageSelector);
this.inputElement.addClass(invalidInputClass).removeClass(successInputClass);
$inputErrorMessage.show();
}
}