-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
199 lines (170 loc) · 4.8 KB
/
index.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
var imxQuery = require('imxquery');
module.exports = (function imxInit() {
/**
* call from outside to register new listener
*
* @param params
* @returns {boolean|object*}
*/
const addListener = (params) => {
const baseParams = {
type: 'viewport',
node: null,
initCallback: null,
updateCallback: null
};
// update input config with base params for fallback
const listener = imxQuery.extendObject(params, baseParams);
// check if the listener type is in the whitelist
if(typeList.indexOf(listener.type) === -1) {
return false;
}else {
_registerNewListener(listener);
return listener;
}
};
/**
* register a new listener
*
* @param listener
* @private
*/
const _registerNewListener = (listener) => {
// if this is the first listener of that type …
if(typeof (library[listener.type]) !== 'object') {
// … create an empty array for that type
library[listener.type] = [];
}
// preset rendered flag to false to prevent user submiting rendered state in config params
listener.rendered = false;
// create the needed eventlistener for the listener
_createOriginEvents(listener);
// add the listener to the central library
library[listener.type].push(listener);
};
/**
* create the origin eventListener for every type
*
* @param listener
* @private
*/
const _createOriginEvents = (listener) => {
switch(listener.type) {
case 'viewport':
if(!globalTypes.viewport) {
globalTypes.viewport = true;
_createOriginEvents_viewport();
}
break;
case 'hover':
_createOriginEvents_hover(listener);
break;
case 'click':
_createOriginEvents_click(listener);
break;
}
};
/**
* this is a complex one - will listen to scroll and resize event on window an calculate if any nodes are in viewport
*
* @private
*/
const _createOriginEvents_viewport = () => {
const handleEvent = () => {
const scrollPosition = window.pageYOffset;
const windowHeight = window.innerHeight;
for(let listener of library.viewport) {
const nodeHeight = listener.node.offsetHeight;
const nodeOffset = imxQuery.offsetTop(listener.node);
// obviously the current node is inside the viewport
if(scrollPosition + windowHeight >= nodeOffset && scrollPosition < nodeOffset + nodeHeight) {
if(!listener.rendered) {
_dispatchInitEvent(listener);
}else {
_dispatchUpdateEvent(listener);
}
}
}
};
window.addEventListener('scroll', handleEvent);
window.addEventListener('resize', handleEvent);
imxQuery.documentReady(handleEvent);
};
/**
* register hover events on given listener
*
* @param listener
* @private
*/
const _createOriginEvents_hover = (listener) => {
const handleEvent = () => {
if(!listener.rendered) {
_dispatchInitEvent(listener);
}else {
_dispatchUpdateEvent(listener);
}
};
listener.node.addEventListener('mouseover', handleEvent);
};
/**
* register click events on given listener
*
* @param listener
* @private
*/
const _createOriginEvents_click = (listener) => {
const handleEvent = () => {
if(!listener.rendered) {
_dispatchInitEvent(listener);
}else {
_dispatchUpdateEvent(listener);
}
};
listener.node.addEventListener('click', handleEvent);
};
/**
* will dispatch the init event and call callback functions for the given listener
*
* @param listener
* @private
*/
const _dispatchInitEvent = (listener) => {
listener.rendered = true;
console.log('imx_init: firing init', listener.node);
listener.node.dispatchEvent(initEvent);
if(typeof (listener.initCallback) == 'function') {
listener.initCallback();
}
listener.node.setAttribute('data-imx_init', 'init');
};
const _dispatchUpdateEvent = (listener) => {
listener.node.dispatchEvent(updateEvent);
if(typeof (listener.updateCallback) == 'function') {
listener.updateCallback();
}
};
/**
* helper function to create an event object
*
* @param name
* @returns {Event}
* @private
*/
const _createEvent = (name) => {
if(typeof window.Event === 'function') {
return new Event(name);
}else {
const customEvent = document.createEvent('Event');
customEvent.initEvent(name, false, true);
return customEvent;
}
}
const initEvent = _createEvent('imx_init');
const updateEvent = _createEvent('imx_update');
const typeList = ['viewport', 'hover', 'click'];
const globalTypes = {viewport: false};
const library = {};
return {
addListener: addListener
};
})();