-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixboost.js
370 lines (322 loc) · 12 KB
/
pixboost.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
'use strict';
var _window = typeof global !== 'undefined' ? global.window : window;
function getBrowser() {
var ua=_window.navigator.userAgent,tem,M=ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if(/trident/i.test(M[1])){
tem=/\brv[ :]+(\d+)/g.exec(ua) || [];
return {name:'IE',version:(tem[1]||'')};
}
if(M[1]==='Chrome'){
tem=ua.match(/\bOPR|Edge\/(\d+)/)
if(tem!=null) {return {name:'Opera', version:tem[1]};}
}
M=M[2]? [M[1], M[2]]: [_window.navigator.appName, _window.navigator.appVersion, '-?'];
if((tem=ua.match(/version\/(\d+)/i))!=null) {M.splice(1,1,tem[1]);}
return {
name: M[0],
version: M[1]
};
}
function hasAttribute(el, attr) {
if (el.hasAttribute) {
return el.hasAttribute(attr)
} else {
return !!(el.attributes[attr] && el.attributes[name].specified);
}
}
_window.Pixboost = {
_BREAKPOINTS: [
{
name: 'lg',
mediaQuery: '(min-width: 990px)'
},
{
name: 'md',
mediaQuery: '(min-width: 640px)'
},
{
name: 'sm',
mediaQuery: ''
}
],
_apiKey: '',
_domain: '',
_disabled: false,
_browser: '',
_pixboostUrl: function (src, op, domain, apiKey, disabled) {
if (op.indexOf('hide') === 0) {
return 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
}
src = src.replace('https:https:', 'https:').replace('https:http:', 'http:');
if (disabled) {
return src;
}
var hasParams = op.indexOf('?') > -1;
return 'https://' + domain + '/api/2/img/' + src + '/' + op + (hasParams ? '&' : '?') + 'auth=' + apiKey;
},
_lazyLoadHook: function() {
if (_window.lozad && _window.IntersectionObserver) {
var observer = _window.lozad('[data-lazy]', {
threshold: 0.01,
rootMargin: '40px 0px 0px 0px',
loaded: function() {
_window.Pixboost._picturefillHook();
}
});
observer.observe();
}
},
_picturefillHook: function() {
if (_window.picturefill && typeof _window.picturefill === 'function') {
_window.picturefill();
}
},
/**
* This function is called on DOMContentLoaded event.
*
* Looks up <script> tag with id="pb-script" and loads settings from attributes.
* Supported attributes:
* - data-autoload - if present then will execute picture replacement
* - data-api-key - api ket that will be used. Must be set if data-autoload is set
* - data-domain - custom domain name if setup to use instead of pixboost.com
* - data-events - comma separated list of events that will trigger update
* - data-jquery-events - comma separated list of JQuery events that will trigger event. window.$ should be exist
* at the time of the function call
* - data-disabled - if attribute presents then won't do any URL transformations. Original URL on picture and img
* tags will be used.
*
* Also, this function will setup listener for pbUpdate event that will execute picture replacement.
* It would be useful if content is loaded through AJAX requests.
*/
init: function () {
_window.Pixboost._apiKey = '';
_window.Pixboost._domain = '';
_window.Pixboost._disabled = false;
_window.Pixboost._browser = getBrowser();
var scriptTag = _window.document.getElementById('pb-script');
if (typeof scriptTag !== 'undefined' && scriptTag) {
var autoload = scriptTag.hasAttribute('data-autoload');
var apiKey = scriptTag.getAttribute('data-api-key');
var domain = scriptTag.getAttribute('data-domain');
var events = scriptTag.getAttribute('data-events');
var jqueryEvents = scriptTag.getAttribute('data-jquery-events');
var disabled = scriptTag.hasAttribute('data-disabled');
var enableByCookie = scriptTag.getAttribute('data-cookie-enable');
var runUpdate = function(apiKey) {
_window.Pixboost.picture({apiKey: apiKey});
_window.Pixboost.image({apiKey: apiKey});
_window.Pixboost.background({apiKey: apiKey});
};
if (apiKey) {
_window.Pixboost._apiKey = apiKey;
}
if (domain) {
_window.Pixboost._domain = domain;
}
_window.Pixboost._disabled = disabled;
if (!disabled && enableByCookie) {
var cookieRegExp = new RegExp('(?:(?:^|.*;\\s*)' + enableByCookie + '\\s*\\=\\s*([^;]*).*$)|^.*$');
var cookieValue = _window.document.cookie.replace(cookieRegExp, "$1");
if (cookieValue !== 'true') {
_window.Pixboost._disabled = true;
}
}
if (autoload) {
runUpdate(apiKey);
}
var onEvent = function(e) {
var eventApiKey = e.detail ? e.detail.apiKey : undefined;
runUpdate(eventApiKey);
};
_window.document.addEventListener('pbUpdate', onEvent);
if (events) {
var eventsList = events.split(',');
for (var i = 0; i < eventsList.length; i++) {
_window.document.addEventListener(eventsList[i], onEvent);
}
}
if (jqueryEvents && _window.$) {
var $eventsList = jqueryEvents.split(',');
for (var i = 0; i < $eventsList.length; i++) {
_window.$(document).on($eventsList[i], onEvent);
}
}
_window.addEventListener('orientationchange', function () {
_window.Pixboost.background();
});
}
},
/**
* Generates <picture> tag into all tags that have
* data-pb-picture attribute.
* @param {object} options
* @param {string} options.apiKey Pixboost api key that will be used
* @param {string} options.domain Custom domain name if setup to use instead of pixboost.com
*/
picture: function (options) {
var self = this;
var doc = _window.document;
var browser = _window.Pixboost._browser;
var isIE9 = browser.name === 'MSIE' && browser.version === '9';
options = options || {};
var apiKey = options.apiKey || _window.Pixboost._apiKey;
if (!apiKey) {
throw 'apiKey option is mandatory';
}
var domain = options.domain || _window.Pixboost._domain || 'pixboost.com';
var createImage = function (url, op, isIE9, alt) {
var imgEl = doc.createElement('img');
imgEl.setAttribute('src', self._pixboostUrl(url, op, domain, apiKey, _window.Pixboost._disabled));
if (alt) {
imgEl.setAttribute('alt', alt);
}
if (isIE9) {
if (imgEl.hasAttribute('width')) {
imgEl.removeAttribute('width');
}
if (imgEl.hasAttribute('height')) {
imgEl.removeAttribute('height');
}
}
return imgEl;
};
var createSource = function (mediaQuery, src, op) {
var el = doc.createElement('source');
el.setAttribute('srcset', self._pixboostUrl(src, op, domain, apiKey, _window.Pixboost._disabled));
el.setAttribute('media', mediaQuery);
return el;
};
//Replacing all pixboost tags with picture
var pbPictures = doc.querySelectorAll('[data-pb-picture]');
for (var i = 0; i < pbPictures.length; i++) {
var el = pbPictures[i];
var attrPrefix = 'data-',
defaultUrl = el.getAttribute(attrPrefix + 'url'),
isLazy = hasAttribute(el, attrPrefix + 'lazy') && _window.IntersectionObserver,
alt = el.getAttribute(attrPrefix + 'alt'),
pic = doc.createElement('picture');
//Make <picture> work in IE9 - https://scottjehl.github.io/picturefill/#ie9
var video;
if (isIE9) {
video = doc.createElement('video');
video.setAttribute('style', 'display: none;');
pic.appendChild(video);
}
if(isLazy) {
pic.setAttribute('data-lazy', "");
if (alt) {
pic.setAttribute('data-alt', alt);
}
}
self._BREAKPOINTS.forEach(function (bp, idx) {
var attrUrl = el.getAttribute(attrPrefix + bp.name + '-url'),
attrOp = el.getAttribute(attrPrefix + bp.name),
isLast = idx === self._BREAKPOINTS.length - 1,
url = attrUrl || defaultUrl;
if (isLast) {
if (isLazy) {
pic.appendChild(createSource(bp.mediaQuery, url, attrOp));
} else {
pic.appendChild(createImage(url, attrOp, isIE9, alt));
}
} else if (isIE9) {
video.appendChild(createSource(bp.mediaQuery, url, attrOp));
} else {
pic.appendChild(createSource(bp.mediaQuery, url, attrOp));
}
});
el.parentNode.replaceChild(pic, el);
}
self._lazyLoadHook();
self._picturefillHook();
},
/**
* Finds all <img> tags that have data-pb-image attribute
* and inserts src attribute using value of data-src attribute
* as source and operation. For instance:
* <img data-src="https://site.com/logo.png" data-operation="resize?size=300"/>
* will be replaced with
* <img data-src="https://pixboost.com/api/2/img/https://site.com/logo.png/resize?size=300&auth=YOUR_API_KEY"/>
* @param {object} options
* @param {string} options.apiKey Pixboost api key that will be used
* @param {string} options.domain Custom domain name if setup to use instead of pixboost.com
*/
image: function(options) {
var doc = _window.document;
var self = this;
options = options || {};
var apiKey = options.apiKey || _window.Pixboost._apiKey;
if (!apiKey) {
throw 'apiKey option is mandatory';
}
var domain = options.domain || _window.Pixboost._domain || 'pixboost.com';
var pbImages = doc.querySelectorAll('img[data-pb-image]');
for (var i = 0; i < pbImages.length; i++) {
var el = pbImages[i];
var attrPrefix = 'data-',
src = el.getAttribute(attrPrefix + 'src'),
op = el.getAttribute(attrPrefix + 'op'),
isLazy = hasAttribute(el, attrPrefix + 'lazy') && _window.IntersectionObserver,
url = self._pixboostUrl(src, op, domain, apiKey, _window.Pixboost._disabled);
el.removeAttribute('data-pb-image');
if (!isLazy) {
el.setAttribute('src', url);
} else {
el.setAttribute('data-src', url);
}
}
self._lazyLoadHook();
},
background: function(options) {
var doc = _window.document;
var self = this;
options = options || {};
var apiKey = options.apiKey || _window.Pixboost._apiKey;
if (!apiKey) {
throw 'apiKey option is mandatory';
}
var domain = options.domain || _window.Pixboost._domain || 'pixboost.com';
var pbBackgrounds = doc.querySelectorAll('[data-pb-background]');
for (var i = 0; i < pbBackgrounds.length; i++) {
var el = pbBackgrounds[i];
var attrPrefix = 'data-',
defaultUrl = el.getAttribute(attrPrefix + 'url'),
defaultOp = el.getAttribute(attrPrefix + 'op'),
isLazy = hasAttribute(el, attrPrefix + 'lazy') && _window.IntersectionObserver,
isLoaded = hasAttribute(el, attrPrefix + 'loaded');
var op = defaultOp;
var url = defaultUrl;
if (!defaultUrl || !defaultOp) {
var activeBreakpoint = self._BREAKPOINTS[2]; //Small by default
for (var bpidx = 0; bpidx < self._BREAKPOINTS.length; bpidx++) {
var bp = self._BREAKPOINTS[bpidx];
if (bp.mediaQuery && _window.matchMedia(bp.mediaQuery).matches) {
activeBreakpoint = bp;
break;
}
}
var bpUrl = el.getAttribute(attrPrefix + activeBreakpoint.name + '-url'),
bpOp = el.getAttribute(attrPrefix + activeBreakpoint.name);
url = bpUrl || url;
op = bpOp || op;
}
var pixboostUrl = self._pixboostUrl(url, op, domain, apiKey, _window.Pixboost._disabled);
if (isLazy && !isLoaded) {
el.setAttribute('data-background-image', pixboostUrl);
} else {
el.style.backgroundImage = 'url(\'' + pixboostUrl + '\')';
}
}
self._lazyLoadHook()
}
};
if (typeof _window.document !== 'undefined') {
if (_window.document.readyState === 'complete' ||
// !IE 8-10
(_window.document.readyState !== 'loading' && !_window.document.documentElement.doScroll)
) {
_window.Pixboost.init();
} else {
_window.document.addEventListener('DOMContentLoaded', _window.Pixboost.init);
}
}