blob: 5152eb23cceba212c070a30c9a1e9e050d54063f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
((global) => {
class UsernameValidator {
constructor() {
this.debounceTimeoutDuration = 1000;
this.errorIconClasses = 'fa fa-exclamation-circle error';
this.usernameInUseMessage = 'Username "$1" is in use!';
this.loadingIconClasses = 'fa fa-spinner fa-spin';
this.successIconClasses = 'fa fa-check-circle success';
this.tooltipPlacement = 'left';
this.inputElement = $('#new_user_username');
this.iconElement = $('<i></i>');
this.inputElement.parent().append(this.iconElement);
let debounceTimeout = _.debounce((username) => {
this.validateUsername(username);
}, this.debounceTimeoutDuration);
this.inputElement.keyup(() => {
this.iconElement.removeClass().tooltip('destroy');
let username = this.inputElement.val();
if (username === '') return;
this.iconElement.addClass(this.loadingIconClasses);
debounceTimeout(username);
});
}
validateUsername(username) {
$.ajax({
type: 'GET',
url: `/u/${username}/exists`,
dataType: 'json',
success: (res) => {
if (res.exists) {
this.iconElement
.removeClass().addClass(this.errorIconClasses)
.tooltip({
title: this.usernameInUseMessage.replace(/\$1/g, username),
placement: this.tooltipPlacement
});
} else {
this.iconElement
.removeClass().addClass(this.successIconClasses)
.tooltip('destroy');
}
}
});
}
}
global.UsernameValidator = UsernameValidator;
})(window);
|