-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
219 lines (198 loc) · 6.39 KB
/
utils.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
/* global Fluid, CONFIG */
window.requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame;
Fluid.utils = {
listenScroll: function(callback) {
var dbc = new Debouncer(callback);
window.addEventListener('scroll', dbc, false);
dbc.handleEvent();
return dbc;
},
unlistenScroll: function(callback) {
window.removeEventListener('scroll', callback);
},
scrollToElement: function(target, offset) {
var of = jQuery(target).offset();
if (of) {
jQuery('html,body').animate({
scrollTop: of.top + (offset || 0),
easing : 'swing'
});
}
},
elementVisible: function(element, offsetFactor) {
offsetFactor = offsetFactor && offsetFactor >= 0 ? offsetFactor : 0;
var rect = element.getBoundingClientRect();
var height = window.innerHeight || document.documentElement.clientHeight;
var top = rect.top;
return (top >= 0 && top <= height * (offsetFactor + 1))
|| (top <= 0 && top >= -(height * offsetFactor) - rect.height);
},
waitElementVisible: function(selectorOrElement, callback, offsetFactor) {
var runningOnBrowser = typeof window !== 'undefined';
var isBot = (runningOnBrowser && !('onscroll' in window))
|| (typeof navigator !== 'undefined' && /(gle|ing|ro|msn)bot|crawl|spider|yand|duckgo/i.test(navigator.userAgent));
if (!runningOnBrowser || isBot) {
return;
}
offsetFactor = offsetFactor && offsetFactor >= 0 ? offsetFactor : 0;
function waitInViewport(element) {
if (Fluid.utils.elementVisible(element, offsetFactor)) {
callback();
return;
}
if ('IntersectionObserver' in window) {
var io = new IntersectionObserver(function(entries, ob) {
if (entries[0].isIntersecting) {
callback();
ob.disconnect();
}
}, {
threshold : [0],
rootMargin: (window.innerHeight || document.documentElement.clientHeight) * offsetFactor + 'px'
});
io.observe(element);
} else {
var wrapper = Fluid.utils.listenScroll(function() {
if (Fluid.utils.elementVisible(element, offsetFactor)) {
Fluid.utils.unlistenScroll(wrapper);
callback();
}
});
}
}
if (typeof selectorOrElement === 'string') {
this.waitElementLoaded(selectorOrElement, function(element) {
waitInViewport(element);
});
} else {
waitInViewport(selectorOrElement);
}
},
waitElementLoaded: function(selector, callback) {
var runningOnBrowser = typeof window !== 'undefined';
var isBot = (runningOnBrowser && !('onscroll' in window))
|| (typeof navigator !== 'undefined' && /(gle|ing|ro|msn)bot|crawl|spider|yand|duckgo/i.test(navigator.userAgent));
if (!runningOnBrowser || isBot) {
return;
}
if ('MutationObserver' in window) {
var mo = new MutationObserver(function(records, ob) {
var ele = document.querySelector(selector);
if (ele) {
callback(ele);
ob.disconnect();
}
});
mo.observe(document, { childList: true, subtree: true });
} else {
document.addEventListener('DOMContentLoaded', function() {
var waitLoop = function() {
var ele = document.querySelector(selector);
if (ele) {
callback(ele);
} else {
setTimeout(waitLoop, 100);
}
};
waitLoop();
});
}
},
createScript: function(url, onload) {
var s = document.createElement('script');
s.setAttribute('src', url);
s.setAttribute('type', 'text/javascript');
s.setAttribute('charset', 'UTF-8');
s.async = false;
if (typeof onload === 'function') {
if (window.attachEvent) {
s.onreadystatechange = function() {
var e = s.readyState;
if (e === 'loaded' || e === 'complete') {
s.onreadystatechange = null;
onload();
}
};
} else {
s.onload = onload;
}
}
var ss = document.getElementsByTagName('script');
var e = ss.length > 0 ? ss[ss.length - 1] : document.head || document.documentElement;
e.parentNode.insertBefore(s, e.nextSibling);
},
createCssLink: function(url) {
var l = document.createElement('link');
l.setAttribute('rel', 'stylesheet');
l.setAttribute('type', 'text/css');
l.setAttribute('href', url);
var e = document.getElementsByTagName('link')[0]
|| document.getElementsByTagName('head')[0]
|| document.head || document.documentElement;
e.parentNode.insertBefore(l, e);
},
loadComments: function(selector, loadFunc) {
var ele = document.querySelector('#comments[lazyload]');
if (ele) {
var callback = function() {
loadFunc();
ele.removeAttribute('lazyload');
};
Fluid.utils.waitElementVisible(selector, callback, CONFIG.lazyload.offset_factor);
} else {
loadFunc();
}
},
getBackgroundLightness(selectorOrElement) {
var ele = selectorOrElement;
if (typeof selectorOrElement === 'string') {
ele = document.querySelector(selectorOrElement);
}
var view = ele.ownerDocument.defaultView;
if (!view) {
view = window;
}
var rgbArr = view.getComputedStyle(ele).backgroundColor.replace(/rgba*\(/, '').replace(')', '').split(/,\s*/);
if (rgbArr.length < 3) {
return 0;
}
var colorCast = (0.213 * rgbArr[0]) + (0.715 * rgbArr[1]) + (0.072 * rgbArr[2]);
return colorCast === 0 || colorCast > 255 / 2 ? 1 : -1;
}
};
/**
* Handles debouncing of events via requestAnimationFrame
* @see http://www.html5rocks.com/en/tutorials/speed/animations/
* @param {Function} callback The callback to handle whichever event
*/
function Debouncer(callback) {
this.callback = callback;
this.ticking = false;
}
Debouncer.prototype = {
constructor: Debouncer,
/**
* dispatches the event to the supplied callback
* @private
*/
update: function() {
this.callback && this.callback();
this.ticking = false;
},
/**
* ensures events don't get stacked
* @private
*/
requestTick: function() {
if (!this.ticking) {
requestAnimationFrame(this.rafCallback || (this.rafCallback = this.update.bind(this)));
this.ticking = true;
}
},
/**
* Attach this as the event listeners
*/
handleEvent: function() {
this.requestTick();
}
};