-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypasync.js
123 lines (114 loc) · 3.8 KB
/
typasync.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
(function(object){
function isFunc(obj) {
return typeof obj == 'function';
}
function initializeInput(input, triggers, onChange) {
if (null === input) {
return;
}
if (typeof input == 'string') {
input = document.getElementById(input);
if (!input) {
input = document.querySelector(input);
}
} else if (typeof input == 'object' && typeof input[0] != 'undefined' && typeof input[0].value == 'string') {
input = input[0];
}
var changed = function() {
if (isFunc(onChange)) {
onChange(this.value);
}
};
if (typeof triggers == 'string') {
triggers = [triggers];
}
for (var event in triggers) {
input.removeEventListener(triggers[event], function(){});
input.addEventListener(triggers[event], changed, false);
}
return input;
}
function TypAsync(options) {
var me = this;
var baseOpts = {
input: null,
timeout: 500,
processing: false,
triggers: ['keyup', 'blur'],
change: function(value) {
me.change.call(me, value);
}
};
if (typeof options != 'object') {
options = {};
}
for (var opt in baseOpts) {
if (typeof options[opt] == 'undefined') {
options[opt] = baseOpts[opt];
}
}
initializeInput(options.input, options.triggers, options.change);
this.options = options;
this.increment = 0;
this.processing = false;
this.oldValue = '';
this.value = '';
this.timeout = undefined;
this._events = {
change: function() {},
empty: function() {},
skip: function() {},
process: function() {},
complete: function() {},
value: function(val) {
return val;
}
};
}
TypAsync.prototype.on = function(event, callback) {
var me = this;
if (isFunc(callback) && isFunc(this._events[event])) {
this._events[event] = function() {
return callback.apply(me, arguments);
};
}
return me;
};
TypAsync.prototype.change = function(value) {
try {
this._events.skip(value);
clearTimeout(this.timeout);
} catch(e){}
var me = this;
this.timeout = setTimeout(function() {
me.value = me._events.value(typeof value == 'string' ? value : '');
if ((me.increment < 1 || me.oldValue != '') && value == '') {
me._events.empty();
}
switch (me.value) {
case '':
me._events.skip(me.value);
break;
case me.oldValue:
break;
default:
if (!me.options.processing || (me.options.processing && !me.processing)) {
if (me.options.processing) {
me.processing = true;
me._events.process();
}
++me.increment;
me._events.change(value, function() {
me.processing = false;
me._events.complete.call(me);
});
} else {
return me._events.skip(me.value);
}
break;
}
me.oldValue = me.value;
}, this.options.timeout);
};
object.TypAsync = TypAsync;
})(typeof module != 'undefined' && module.exports ? module.exports : window);