-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHSDelayedInputTrigger.js
More file actions
98 lines (90 loc) · 3.52 KB
/
Copy pathHSDelayedInputTrigger.js
File metadata and controls
98 lines (90 loc) · 3.52 KB
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
/*************************************************************************
* Heartstring.js
* https://github.com/writersky/DelayedInputTrigger
* Copyright 2013 Writersky.
*
* Licensed under the The GNU Lesser General Public License,
* version 2.1 (LGPL-2.1).
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://opensource.org/licenses/lgpl-2.1.php
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**************************************************************************/
/************************************************************************/
/* HSDelayedInputTrigger will only fire events after a given time. */
/* Ideal to use for input text that pervents too many calls to the API. */
/* */
/* @author Harris Wong */
/* @date July 22nd, 2013 */
/************************************************************************/
// $Id: HSDelayedInputTrigger.js 471 2013-05-12 22:24:53Z harris $
Heartstring.HSDelayedInputTrigger = Heartstring.HSDelayedInputTrigger || {};
/**
* component code
*/
Heartstring.HSDelayedInputTrigger = function (container, config) {
var that = Heartstring.init(Heartstring.HSDelayedInputTrigger, config);
that.container = container;
/**
* Bind toggler
*/
that.bindKeyup = function (that) {
var stopwatch;
var timeoutTracker;
Heartstring.select(that.container).bind('keyup', function(e) {
if (Heartstring.select(that.container).val() === "") {
//If input is empty, treat it as reset.
that.config.callback(that);
return;
}
var currentTime = new Date().getTime();
if (!stopwatch) {
//first keystroke
stopwatch = new Date().getTime();
}
if (currentTime - stopwatch > that.config.TIMEDELAY) {
//trigger event
that.config.callback(that);
}
//Update the timer for the next keystroke.
stopwatch = new Date().getTime();
//The last letter will never trigger anything
clearTimeout(timeoutTracker);
timeoutTracker = setTimeout(function() {
that.config.callback(that);
stopwatch = null; //if the user stopped typing, then we reset timer from the next keystroke
}, that.config.TIMEDELAY);
});
};
return that;
}
/**
* Content init
*/
Heartstring.HSDelayedInputTrigger.init = function(container, config) {
// create this component
var that = Heartstring.HSDelayedInputTrigger(container, config);
// Bind events
Heartstring.HSDelayedInputTrigger.bind(that);
};
Heartstring.HSDelayedInputTrigger.bind = function(that) {
that.bindKeyup(that);
}
//default settings for this component
Heartstring.HSDelayedInputTrigger.defaults = {
container: "",
config: {
TIMEDELAY: 500, //in ms, 0 for no delay.
selectors: {
},
callback: function(that) {
//custom function goes here.
}
}
};