-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
85 lines (76 loc) · 2.6 KB
/
main.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
LiveFormValidator = {
keyUpTimer: [],
timeWaitingForTyping: 2000,
timeShowingSuccess: 5000,
/**
* Configure package options
* @param options: {timeWaitingForTyping: xxx, timeShowingSuccess: xxx}
*/
config: function(options) {
var timeWaitingForTyping = parseInt(options.timeWaitingForTyping);
var timeShowingSuccess = parseInt(options.timeShowingSuccess);
if(timeWaitingForTyping != undefined && timeWaitingForTyping >= 0) {
this.timeWaitingForTyping = timeWaitingForTyping;
}
if(timeShowingSuccess != undefined && timeShowingSuccess >= 0) {
this.timeShowingSuccess = timeShowingSuccess;
}
},
/**
* Validate Semantic UI form fiels on live
* @param options: {event: xxx, currentForm: xxx, save: xxx}
* event is mandatory
* currentForm is mandatory
*/
validateFieldOnKeyUp: function (options) {
var currentInput = $(options.event.currentTarget);
var currentForm = options.currentForm;
var save = options.save;
var corner = currentInput.siblings(".corner");
var icon = corner.find('.icon');
// Config params
var timeWaitingForTyping = this.timeWaitingForTyping;
var timeShowingSuccess = this.timeShowingSuccess;
if(currentForm.hasClass('success')) {
currentForm.removeClass('success');
}
if(!icon.hasClass('loading') && !currentInput.parent().parent().hasClass('error')) {
icon.removeClass('checkmark');
icon.removeClass('asterisk');
corner.addClass('blue');
icon.addClass('loading');
}
if (this.keyUpTimer[currentInput.attr('name')]) {
clearTimeout(this.keyUpTimer[currentInput.attr('name')]);
}
this.keyUpTimer[currentInput.attr('name')] = setTimeout(function () {
// Validate current field
if (currentForm.form('validate field', currentForm.form('get validation rules', currentInput))) {
corner.removeClass('blue');
icon.removeClass('loading');
icon.removeClass('asterisk');
corner.addClass('green');
icon.addClass('checkmark');
if(timeShowingSuccess != 0) {
setTimeout(function () {
corner.removeClass('green');
icon.removeClass('checkmark');
icon.addClass('asterisk');
}, timeShowingSuccess);
}
}
else {
// Remove positive check
corner.removeClass('green');
corner.removeClass('blue');
icon.removeClass('loading');
icon.removeClass('checkmark');
icon.addClass('asterisk');
}
// Submit form
if(save) {
currentForm.form('submit');
}
}, timeWaitingForTyping);
}
};