-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixieLib.js
More file actions
345 lines (304 loc) · 9.94 KB
/
pixieLib.js
File metadata and controls
345 lines (304 loc) · 9.94 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
var pixieLib = {
closestNumber: function closestNumber(arr, search) {
var closest = arr.reduce(function (prev, curr) {
return (Math.abs(curr - search) < Math.abs(prev - search) ? curr : prev);
});
return closest;
}
, createCookie: function createCookie (name, value, days) {
var expires
, domain = window.location.hostname !== 'localhost'
? '; domain=' + window.location.hostname
: '';
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = '; expires='+date.toGMTString();
} else {
expires = '';
}
document.cookie = name + '=' + value + expires + '; path=/' + domain;
}
, readCookie: function readCookie (name) {
var nameEQ = name + '='
, ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
, eraseCookie: function eraseCookie(name) {
this.createCookie(name, '', -1);
}
, forEach : function forEach (collection, callback, scope) {
if (Object.prototype.toString.call(collection) === '[object Object]') {
for (var prop in collection) {
if (Object.prototype.hasOwnProperty.call(collection, prop)) {
callback.call(scope, collection[prop], prop, collection);
}
}
} else {
for (var i = 0, len = collection.length; i < len; i++) {
callback.call(scope, collection[i], i, collection);
}
}
}
, getClosest : function getClosest (elem, selector) {
var firstChar = selector.charAt(0);
// Get closest match
for ( ; elem && elem !== document; elem = elem.parentNode ) {
// If selector is a class
if ( firstChar === '.' ) {
if ( elem.className.indexOf( selector.substr(1) ) > -1 ) {
return elem;
}
}
// If selector is an ID
if ( firstChar === '#' ) {
if ( elem.id === selector.substr(1) ) {
return elem;
}
}
// If selector is a data attribute
if ( firstChar === '[' ) {
if ( elem.hasAttribute( selector.substr(1, selector.length - 2) ) ) {
return elem;
}
}
// If selector is a tag
if ( elem.tagName.toLowerCase() === selector ) {
return elem;
}
}
return false;
}
, isTouchDevice : function isTouchDevice () {
return 'ontouchstart' in window // works on most browsers
//|| 'onmsgesturechange' in window // works on ie10
//|| !!('msmaxtouchpoints' in window.navigator)
}
, isArray: function isArray (obj) {
return obj.prototype.toString.call( someVar ) === '[object Array]';
}
, objSize: function objSize (obj) {
var size = 0
, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
}
, filereaderSupported : function filereaderSupported() {
//All of the stuff we need
return (window.File && window.FileReader && window.FileList && window.Blob && window.XMLHttpRequest)
? true : false;
}
, scrollPos: function scrollPos () {
var scrollTop = (window.pageYOffset !== undefined)
? window.pageYOffset
: (document.documentElement || document.body.parentNode || document.body).scrollTop
, scrollLeft = (window.pageXOffset !== undefined)
? window.pageXOffset
: (document.documentElement || document.body.parentNode || document.body).scrollLeft;
return [scrollTop, scrollLeft];
}
, offset: function offset (el, position) {
rect = el.getBoundingClientRect();
switch (position) {
case 'left':
return rect.left; // x position of #world relative to viewport
case 'top':
return rect.top; // y position of #world relative to viewport
default:
return 0;
}
/*
rect.left // x position of #world relative to viewport
rect.top // y position of #world relative to viewport
rect.width // width of #world, including padding and borders
rect.height // height of #world, including padding and borders
rect.offsetWidth // width of #world - IE8 and below
rect.offsetHeight // height of #world - IE8 and below
*/
}
, belowthefold: function belowthefold (el, settings) {
var fold;
if (settings.container === undefined || settings.container === window) {
fold = (window.innerHeight
? window.innerHeight
: Math.max(document.documentElement.clientHeight, document.body.clientHeight)
) + this.scrollPos()[0];
} else {
fold = this.offset(settings.container, 'top') + document.querySelector(settings.container).height;
}
return fold <= this.offset(el, 'top') - (settings.threshold !== undefined ? settings.threshold : 0);
}
, rightoffold: function rightoffold (el, settings) {
var fold;
if (settings.container === undefined || settings.container === window) {
fold = (window.innerWidth
? window.innerWidth
: Math.max(document.documentElement.clientWidth, document.body.clientWidth)
) + this.scrollPos()[1];
} else {
fold = this.offset(settings.container, 'left') + document.querySelector(settings.container).width;
}
return fold <= this.offset(el, 'left') - settings.threshold;
}
, abovethetop: function abovethetop (el, settings) {
var fold;
if (settings.container === undefined || settings.container === window) {
fold = this.scrollPos()[0];
} else {
fold = this.offset(settings.container, 'top');
}
return fold >= this.offset(el, 'top') + settings.threshold + document.querySelector.height;
}
, leftofbegin: function leftofbegin (el, settings) {
var fold;
if (settings.container === undefined || settings.container === window) {
fold = this.scrollPos()[1];
} else {
fold = this.offset(settings.container, 'left');
}
return fold >= this.offset(el, 'left') + settings.threshold + el.width;
}
, inviewport: function inviewport (el, settings) {
return !this.rightoffold(el, settings) && !this.leftofbegin(el, settings) &&
!this.belowthefold(el, settings) && !this.abovethetop(el, settings);
}
, localStorage : {
set: function (key, value, expires) {
if (typeof expires !== 'undefined') {
var timespan;
timespan = expires.split(' ');
expires = (Date.now() / 1000 | 0);
switch (timespan[1]) {
case 'minute':
case 'minutes':
expires += timespan[0] * 60;
break;
case 'hour':
case 'hours':
expires += (timespan[0] * 60 * 60);
break;
case 'day':
case 'days':
expires += (timespan[0] * 60 * 60 * 24);
break;
case 'week':
case 'weeks':
expires += (timespan[0] * 60 * 60 * 24 * 7);
break;
case 'month':
case 'months':
expires += (timespan[0] * 60 * 60 * 24 * 31);
break;
case 'year':
case 'years':
expires += (timespan[0] * 60 * 60 * 24 * 365);
break;
}
value.expires = expires;
}
window.localStorage.setItem( key, JSON.stringify(value) );
},
get: function (key) {
storageItem = JSON.parse( window.localStorage.getItem(key) );
if (storageItem !== null
&& 'expires' in storageItem) {
if (storageItem.expires >= (Date.now() / 1000 | 0)) {
return storageItem;
} else {
localStorage.removeItem(key);
return null;
}
}
return storageItem;
}
}
, getKeyByValue: function getKeyByValue (obj, value, key) {
/**
* get key by given value
*/
for(var prop in obj) {
if (obj.hasOwnProperty(prop)){
if (typeof key == 'undefined') {
if (obj[prop] === value) return prop;
} else if (obj[prop][key] === value) {
return prop;
}
}
}
}
, rawurlencode: function rawurlencode (str) {
str = (str + '').toString();
/**
* Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
* PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
*/
return encodeURIComponent(str)
.replace(/!/g, '%21')
.replace(/'/g, '%27')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')
.replace(/\*/g, '%2A');
}
, rawurldecode: function rawurldecode (str) { // PHP equivelant
return decodeURIComponent(str + '').replace(/&/g, '&');
}
, escapeHtml: function escapeHtml (str) {
return str
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
, timestampToDate: function timestampToDate(UNIX_timestamp, format) {
var format = typeof(format) === 'undefined'
? 'date month year hour:min:sec'
: format
, objDate = new Date(UNIX_timestamp*1000)
, months = ['Jan','Feb','Mrt','Apr','Mei','Jun','Jul','Aug','Sep','Okt','Nov','Dec']
, year = objDate.getFullYear()
, month = months[objDate.getMonth()]
, date = objDate.getDate()
, hour = objDate.getHours()
, min = objDate.getMinutes()
, sec = objDate.getSeconds()
, time = format.replace('date', date)
.replace('month', month)
.replace('year', year)
.replace('hour', hour)
.replace('min', min)
.replace('sec', sec.length > 1 ? sec : '0' + sec);
return time;
}
, viewport: function viewport() {
// real window width, like mediaquery
var e = window, a = 'inner';
if (!('innerWidth' in window )) {
a = 'client';
e = document.documentElement || document.body;
}
return { width : e[ a+'Width' ] , height : e[ a+'Height' ] };
}
, debounce: function debounce(func, wait, immediate) {
// underscore.js debounce
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
};