-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.update.js
More file actions
176 lines (151 loc) · 6.02 KB
/
jquery.update.js
File metadata and controls
176 lines (151 loc) · 6.02 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
*
* @author Max Shirshin
* @description DOM polling plugin to track any type of change on any element, by creating native jQuery events
* @version 3.0.1
*
*/
(function ($, undefined) {
$.definePollingEvent = function(eventName, newCfg) {
if (eventName === undefined) {
throw new Error('DOM Polling plugin: event name is required');
}
// throw exception if an event with the same name was defined already
if ($.event.special.hasOwnProperty(eventName)) {
throw new Error('DOM Polling plugin: custom event ' + eventName + ' is already defined');
}
// key for .data() to store the cached value
var valueCache = eventName + ':prev',
// id's for timeouts
timeoutId,
cacheTimeoutId,
// jQuery object to store the polled elements
$pool = $(),
// jQuery object to store the original elements the handlers are attached to
$source = $(),
// initialize the polling counter
counter = -1,
// how many elements were polled in a polling round
aggregated = 0,
// default options
cfg = {
// polling interval
delay: 100, // msec
// how often the cache should be updated, -1 for "never"
cacheTimeout: 2000, // msec
// how many elements may be queried in a polling round
aggregateNum: 5,
// jQuery selector to choose the elements to be polled
elementSelector: 'input,textarea,select',
// function to determine the value of an element;
// accepts DOM element as a parameter
valFn: function(el) { return $(el).val() },
// function to determine whether two values are equal or not
eqFn: function(oldVal, newVal, el) {
return oldVal === newVal;
}
};
// define configuration
$.extend(cfg, newCfg || {});
// polling function
function poll() {
// one round of aggregated polling
//
// aggregateNum may still be more than the actual number of elements to poll,
// thus an extra check
while (++aggregated <= Math.min($pool.length, cfg.aggregateNum)) {
var el = $pool.get(++counter) || $pool.get(counter = 0);
if (el) {
processEvent(el);
}
}
aggregated = 0;
timeoutId = setTimeout(poll, cfg.delay);
}
// check whether the actual value has changed
function processEvent(el) {
var cachedVal = $.data(el, valueCache),
val = cfg.valFn(el),
isEqual = cfg.eqFn(cachedVal, val, el);
if (cachedVal === undefined || isEqual) {
// cache value
$.data(el, valueCache, val);
} else if (! isEqual) {
$(el)
.data(valueCache, val)
.trigger(eventName, {
oldValue: cachedVal,
newValue: val
});
}
}
function refreshCache() {
// empty the polling pool
$pool = $();
// filter out dead elements
$source = $source
.filter(function() {
// elements removed from DOM shouldn't be polled anymore
return $.contains(document.documentElement, this);
})
.each(function() {
addToPolling(this);
});
// schedule next run, unless user explicitly stated
// that periodical updates aren't required
cacheTimeoutId = (cfg.cacheTimeout !== -1) ?
setTimeout(refreshCache, cfg.cacheTimeout) :
undefined;
if ($pool.length === 0 && $source.length === 0) {
clearTimeout(timeoutId);
clearTimeout(cacheTimeoutId);
timeoutId = cacheTimeoutId = undefined;
}
}
function addToPolling(el) {
// add elements to the polling list, jQuery cares for the uniqueness
$pool = $pool.add($getPolled(el));
}
// see which elements are being polled for this element.
// it can either be the element itself or its descendants (if event delegation was used)
function $getPolled(el) {
var $el = $(el);
return $el
.find(cfg.elementSelector)
.add( $el.filter(cfg.elementSelector) );
}
function refreshCacheASAP() {
// schedule cache refresh with a minimal timeout;
// this way we avoid successive incovations
// when event handlers are added in a cycle
clearTimeout(cacheTimeoutId);
cacheTimeoutId = setTimeout(refreshCache, 0);
}
// provide a wrapper function similar to other events
$.fn[eventName] = function(fn) {
return fn ? this.bind(eventName, fn) : this.trigger(eventName);
};
// define debugging API
$.extend($.fn[eventName], {
debug: function() {
return [$pool, $source];
}
});
$.event.special[eventName] = {
setup: function() {
$source = $source.add(this);
addToPolling(this);
// start polling
timeoutId || poll();
// schedule cache refresh to a minimal timeout
refreshCacheASAP();
},
teardown: function() {
$source = $source.not(this);
refreshCacheASAP();
// this is the last event handler for this element, so we remove .data() properties
$(this).removeData(valueCache);
}
};
};
})(jQuery);