-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrive.js
315 lines (263 loc) · 9.85 KB
/
arrive.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
"use strict";
/*
* arrive.js
* v2.0.0
* https://github.com/uzairfarooq/arrive
* MIT licensed
*
* Copyright (c) 2014 Uzair Farooq
*/
(function(window, $, undefined) {
var utils = (function() {
var matches = HTMLElement.prototype.matches || HTMLElement.prototype.webkitMatchesSelector || HTMLElement.prototype.mozMatchesSelector
|| HTMLElement.prototype.msMatchesSelector;
return {
matchesSelector: function(elem, selector) {
return elem instanceof HTMLElement && matches.call(elem, selector);
},
// to enable function overriding - By John Resig (MIT Licensed)
addMethod: function (object, name, fn) {
var old = object[ name ];
object[ name ] = function(){
if ( fn.length == arguments.length )
return fn.apply( this, arguments );
else if ( typeof old == 'function' )
return old.apply( this, arguments );
};
}
};
})();
// Class to mantain state of all registered events of a single type
var EventsBucket = (function() {
var EventsBucket = function() {
// holds all the events
this._eventsBucket = [],
// function to be called while adding an event, the function should do the event initialization/registration
this._beforeAdding = null,
// function to be called while removing an event, the function should do the event destruction
this._beforeRemoving = null;
};
EventsBucket.prototype.addEvent = function(target, selector, options, callback) {
var newEvent = {
target: target,
selector: selector,
options: options,
callback: callback,
firedElems: []
};
if (this._beforeAdding) {
this._beforeAdding(newEvent);
}
this._eventsBucket.push(newEvent);
return newEvent;
};
EventsBucket.prototype.removeEvent = function(compareFunction) {
for (var i=this._eventsBucket.length - 1, registeredEvent; registeredEvent = this._eventsBucket[i]; i--) {
if (compareFunction(registeredEvent)) {
if (this._beforeRemoving) {
this._beforeRemoving(registeredEvent);
}
this._eventsBucket.splice(i, 1);
}
}
};
EventsBucket.prototype.beforeAdding = function(beforeAdding) {
this._beforeAdding = beforeAdding;
};
EventsBucket.prototype.beforeRemoving = function(beforeRemoving) {
this._beforeRemoving = beforeRemoving;
};
return EventsBucket;
})();
// General class for binding/unbinding arrive and leave events
var MutationEvents = function(getObserverConfig, defaultOptions, onMutation) {
var eventsBucket = new EventsBucket(),
me = this;
// actual event registration before adding it to bucket
eventsBucket.beforeAdding(function(registrationData) {
var
target = registrationData.target,
selector = registrationData.selector,
callback = registrationData.callback,
observer;
// mutation observer does not work on window or document
if (target === window.document || target === window)
target = document.getElementsByTagName("html")[0];
// Create an observer instance
observer = new MutationObserver(function(e) {
onMutation.call(this, e, registrationData);
});
var config = getObserverConfig(registrationData.options);
observer.observe(target, config);
registrationData.observer = observer;
});
// cleanup/unregister before removing an event
eventsBucket.beforeRemoving(function (eventData) {
eventData.observer.disconnect();
});
function toArray(elements) {
if (typeof elements.length !== "number") {
elements = [elements];
}
return elements;
}
this.bindEvent = function(selector, options, callback) {
if (typeof callback === "undefined") {
callback = options;
options = defaultOptions;
}
var elements = toArray(this);
for (var i = 0; i < elements.length; i++) {
eventsBucket.addEvent(elements[i], selector, options, callback);
}
};
this.unbindEvent = function() {
var elements = toArray(this);
eventsBucket.removeEvent(function(eventObj) {
for (var i = 0; i < elements.length; i++) {
if (eventObj.target === elements[i]) {
return true;
}
}
return false;
});
};
this.unbindEventWithSelectorOrCallback = function(selector) {
var elements = toArray(this),
callback = selector,
compareFunction;
if (typeof selector === "function") {
compareFunction = function(eventObj) {
for (var i = 0; i < elements.length; i++) {
if (eventObj.target === elements[i] && eventObj.callback === callback) {
return true;
}
}
return false;
};
}
else {
compareFunction = function(eventObj) {
for (var i = 0; i < elements.length; i++) {
if (eventObj.target === elements[i] && eventObj.selector === selector) {
return true;
}
}
return false;
};
}
eventsBucket.removeEvent(compareFunction);
};
this.unbindEventWithSelectorAndCallback = function(selector, callback) {
var elements = toArray(this);
eventsBucket.removeEvent(function(eventObj) {
for (var i = 0; i < elements.length; i++) {
if (eventObj.target === elements[i] && eventObj.selector === selector && eventObj.callback === callback) {
return true;
}
}
return false;
});
};
return this;
};
// traverse through all descendants of a node to check if event should be fired for any descendant
function checkChildNodesRecursively(nodes, registrationData, callbacksToBeCalled) {
// check each new node if it matches the selector
for (var i=0, node; node = nodes[i]; i++) {
if (utils.matchesSelector(node, registrationData.selector)) {
// make sure the arrive event is not already fired for the element
if (registrationData.firedElems.indexOf(node) == -1) {
registrationData.firedElems.push(node);
callbacksToBeCalled.push({ callback: registrationData.callback, elem: node });
}
}
if (node.childNodes.length > 0) {
checkChildNodesRecursively(node.childNodes, registrationData, callbacksToBeCalled);
}
}
}
function callCallbacks(callbacksToBeCalled) {
for (var i = 0, cb; cb = callbacksToBeCalled[i]; i++) {
cb.callback.call(cb.elem);
}
}
function onArriveMutation(mutations, registrationData) {
mutations.forEach(function( mutation ) {
var newNodes = mutation.addedNodes,
targetNode = mutation.target,
callbacksToBeCalled = [];
// If new nodes are added
if( newNodes !== null && newNodes.length > 0 ) {
checkChildNodesRecursively(newNodes, registrationData, callbacksToBeCalled);
}
else if (mutation.type === "attributes") {
if(utils.matchesSelector(targetNode, registrationData.selector)) {
// make sure the arrive event is not already fired for the element
if (registrationData.firedElems.indexOf(targetNode) == -1) {
registrationData.firedElems.push(targetNode);
callbacksToBeCalled.push({ callback: registrationData.callback, elem: targetNode });
}
}
}
callCallbacks(callbacksToBeCalled);
});
}
function onLeaveMutation(mutations, registrationData) {
mutations.forEach(function( mutation ) {
var removedNodes = mutation.removedNodes,
targetNode = mutation.target,
callbacksToBeCalled = [];
if( removedNodes !== null && removedNodes.length > 0 ) {
checkChildNodesRecursively(removedNodes, registrationData, callbacksToBeCalled);
}
callCallbacks(callbacksToBeCalled);
});
}
function getArriveObserverConfig(options) {
var config = {
attributes: false,
childList: true,
subtree: true
};
if (options.fireOnAttributesModification) {
config.attributes = true;
}
return config;
}
function getLeaveObserverConfig(options) {
var config = {
childList: true,
subtree: true
};
return config;
}
// Default options
var arriveDefaultOptions = {
fireOnAttributesModification: false
},
leaveDefaultOptions = {};
var arriveEvents = new MutationEvents(getArriveObserverConfig, arriveDefaultOptions, onArriveMutation),
leaveEvents = new MutationEvents(getLeaveObserverConfig, leaveDefaultOptions, onLeaveMutation);
/*** expose APIs ***/
function exposeApi(exposeTo) {
exposeTo.arrive = arriveEvents.bindEvent;
// expose unbindArrive function with overriding
utils.addMethod(exposeTo, "unbindArrive", arriveEvents.unbindEvent);
utils.addMethod(exposeTo, "unbindArrive", arriveEvents.unbindEventWithSelectorOrCallback);
utils.addMethod(exposeTo, "unbindArrive", arriveEvents.unbindEventWithSelectorAndCallback);
exposeTo.leave = leaveEvents.bindEvent;
// expose unbindLeave function with overriding
utils.addMethod(exposeTo, "unbindLeave", leaveEvents.unbindEvent);
utils.addMethod(exposeTo, "unbindLeave", leaveEvents.unbindEventWithSelectorOrCallback);
utils.addMethod(exposeTo, "unbindLeave", leaveEvents.unbindEventWithSelectorAndCallback);
}
if ($) {
exposeApi($.fn);
}
exposeApi(HTMLElement.prototype);
exposeApi(NodeList.prototype);
exposeApi(HTMLCollection.prototype);
exposeApi(HTMLDocument.prototype);
exposeApi(Window.prototype);
})(this, typeof jQuery === 'undefined' ? null : jQuery);