-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRetinizr.js
376 lines (300 loc) · 11.4 KB
/
Retinizr.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
371
372
373
374
375
376
/*!preserve
* Retinizr - version 1.0.2
* Copyright (c) 2013-2015 Leonardo D. Schlossmacher (leods92.com).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
;(function(window, document){
// If the browser doesn't set the pixel density it's not being run in a high density display device and so we won't proceed.
// Since high density screens are only available in modern devices we'll rely on forEach method of the Array object.
if (!window.devicePixelRatio || !Array.prototype.forEach) {
return;
}
var R = {};
//
// Options
//
var options = {};
var defaults = {
// Apple Retina devices have 2.0 ratio (except iPhone 6+)
// but I opted the default to be 1.5 as that's the ratio
// of some Android devices displays.
minPixelRatio: 1.5
, images: {
// The CSS class your upgradeable images have.
cssClass: 'js-retinizr-image'
// The text before high resolution images' extension.
// E.g.: logo.png => logo@2x.png.
// Note: regular expression characters must be escaped.
, sourceSuffix: '@2x'
// ~> data-retinizr-hires-url
, dataAttribute: 'retinizr-hires-url'
}
, googleStaticMaps: {
cssClass: 'js-retinizr-map'
// fluidCssClass is used to automatically change width
// based on image's container.
// This affects also images that won't be scaled to
// the higher resolution.
//
// Watch out: this feature is inteded for mobile devices.
// That said, if you use the free static maps API,
// Google will limit 'browser width' to 640px
, fluidCssClass: 'js-retinizr-fluid'
// Google also provides scale=4 for Business API users
, scale: 2
}
, gravatars: {
cssClass: 'js-retinizr-gravatar'
// Be careful not to exceed Gravatar's limit that is 2048px.
// Note that even though the image provided will be in the requested
// size, user's source image might not be that big and
// so the quality may be compromised.
, scale: 2
}
};
R.setOptions = function(newOptions) {
newOptions = newOptions || {};
var pickOption = function(custom, _default) {
return typeof custom !== 'undefined' ? custom : _default;
};
for (var prop in defaults) {
if (typeof defaults[prop] === 'object') {
// In case a boolean was set, we'll set all options as default.
if (typeof newOptions[prop] !== 'object') {
options[prop] = defaults[prop];
continue;
}
options[prop] = {};
for (var childProp in defaults[prop]) {
options[prop][childProp] =
pickOption(newOptions[prop][childProp], defaults[prop][childProp]);
}
continue;
}
options[prop] = pickOption(newOptions[prop], defaults[prop]);
}
};
//
// DOM searching
//
R.getList = function(cssClass) {
return document.getElementsByClassName(cssClass);
};
//
// Images
//
R.scaleImages = function() {
var images = R.getImagesList();
R.scaleItems('scaleImage', images);
};
R.getImagesList = function() {
return R.getList(options.images.cssClass);
};
R.scaledImageName = function(img) {
var dataAttribute = 'data-' + options.images.dataAttribute
, manualUrl = img.getAttribute(dataAttribute)
;
if (manualUrl) {
return manualUrl;
}
else {
// about '.*?' - the '?' prevents it from not matching the extension
return img.src.replace(/^(.*?)(\.[a-z]{3,4})?$/, '$1' + options.images.sourceSuffix + '$2');
}
};
R.scaleImage = function(img) {
var new_src = R.scaledImageName(img)
, dimensions = [img.width, img.height]
;
R.loadImage(new_src, function() {
img.width = dimensions[0];
img.height = dimensions[1];
// Retinizr will first load the image then replace
// for a better user experience.
img.src = new_src;
});
};
R.loadImage = function(src, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', src, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
// Only images that really exists will replace current images.
callback();
}
};
xhr.send();
};
//
// Google Static Maps
//
R.googleStaticMapsUrlSizeRegexp = /size=([0-9]+)x([0-9]+)/;
R.scaleGoogleStaticMaps = function() {
var maps = R.getGoogleStaticMapsList();
R.scaleItems('scaleGoogleStaticMap', maps);
};
R.getGoogleStaticMapsList = function() {
return R.getList(options.googleStaticMaps.cssClass);
};
// If measurements have no unit, px is assumed.
R.updateGoogleStaticMapDimensions = function(map, width, height) {
// It's important to set width/height via .style,
// otherwise because width is set height is ignored
// (to keep proportion, apparently.)
// Height must be set beforehand, to avoid page reflow.
map.style.height = typeof height == 'string' ? height : height + 'px';
map.style.width = typeof width == 'string' ? width : width + 'px';
};
R.updateFluidGoogleStaticMapResizeTimeout = null;
// This also handles mobile devices orientation changes.
R.updateFluidGoogleStaticMapOnResize = function(map) {
clearTimeout(R.updateFluidGoogleStaticMapResizeTimeout);
R.updateFluidGoogleStaticMapResizeTimeout = setTimeout(function() {
R.updateFluidGoogleStaticMap(map);
}, 500);
};
R.updateFluidGoogleStaticMapSrc = function(map, src, beforeRequestCb) {
// Hiding image not to display distorted image while the new one loads.
map.style.visibility = 'hidden';
if (beforeRequestCb) { beforeRequestCb(); }
map.src = src;
map.addEventListener('retinizr:img:load', function() {
map.style.visibility = 'visible';
});
map.addEventListener('load', function() {
R.triggerEvent(map, 'retinizr:img:load');
});
// Triggering load manually when image is already cached.
if (map.complete) {
R.triggerEvent(map, 'retinizr:img:load');
}
};
R.updateFluidGoogleStaticMap = function(map, src) {
// Using clientWidth not offsetWidth not to get borders.
var newWidth = map.parentNode.clientWidth
, cachedHeight = map.offsetHeight
, newSize = 'size=' + newWidth + 'x$2'
;
// Useful to be called when src is being changed by another function.
// Thus preventing a second HTTP request from being performed.
src = src || map.src;
src = src.replace(R.googleStaticMapsUrlSizeRegexp, newSize);
if (map.src != src) {
R.updateFluidGoogleStaticMapSrc(map, src, function() {
R.updateGoogleStaticMapDimensions(map, newWidth, cachedHeight);
});
}
// 'If multiple identical EventListeners are registered on the same
// EventTarget with the same parameters, the duplicate instances
// are discarded.'
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Multiple_identical_event_listeners
window.addEventListener('resize', function() {
R.updateFluidGoogleStaticMapOnResize(map);
});
};
R.scaleGoogleStaticMap = function(map) {
var dimensions = map.src.match(R.googleStaticMapsUrlSizeRegexp)
, scaleRegExp = /scale=1/
, newScale = options.googleStaticMaps.scale
, newSrc = map.src
;
// Saving 'displayed resolution' so that new scale keeps size
// but increses pixel density.
R.updateGoogleStaticMapDimensions(map, dimensions[1], dimensions[2]);
if (map.src.match(scaleRegExp)) {
newSrc = newSrc.replace(scaleRegExp, 'scale=' + newScale);
}
else {
newSrc += '&scale=' + newScale;
}
if (~map.className.indexOf(options.googleStaticMaps.fluidCssClass)) {
R.updateFluidGoogleStaticMap(map, newSrc);
}
else {
map.src = newSrc;
}
};
//
// Gravatars
//
R.scaleGravatars = function() {
var gravatars = R.getGravatarsList();
R.scaleItems('scaleGravatar', gravatars);
};
R.getGravatarsList = function() {
return R.getList(options.gravatars.cssClass);
};
R.scaleGravatar = function(gravatar) {
var regex = /(size|s)=([0-9]+)/
, size = gravatar.src.match(regex)[2]
;
gravatar.width = size;
gravatar.src = gravatar.src.replace(regex, '$1=' + parseInt(size, 10) * options.gravatars.scale);
};
//
// Helpers
//
R.triggerEvent = function(element, eventName) {
var event = document.createEvent('Event');
event.initEvent(eventName, true, true);
element.dispatchEvent(event);
};
R.scaleItems = function(scalingFunction, items) {
if (!items) return; // returns if null or undefined
// Ensures images are fully loaded before scaling them.
// This is necessary because we need image's dimensions.
function scale() {
// It'll only replace the image if it wasn't scaled yet.
// Otherwise we'd be in a loop, given that new image
// may also trigger the load event.
if (R.elWasScaled(this)) { return; }
if (R.deviceRequiresRetinazation()) {
R[scalingFunction](this);
}
else if (scalingFunction == 'scaleGoogleStaticMap') {
// To save an extra HTTP request, scaleGoogleStaticMaps
// already makes width fluid when retinizing (above).
R.updateFluidGoogleStaticMap(this);
}
R.setElWasScaled(this);
}
Array.prototype.forEach.call(items, function(item) {
R.checkHTMLElement(item);
item.addEventListener('retinizr:img:load', scale);
item.addEventListener('load', function() {
R.triggerEvent(item, 'retinizr:img:load');
});
// If image is cached, no load event is triggered.
// Therefore its callbacks should be triggered manually.
if (item.complete) {
R.triggerEvent(item, 'retinizr:img:load');
}
});
};
R.checkHTMLElement = function(el) {
if (!(el instanceof HTMLImageElement)) {
throw 'Element is not an img element.';
}
};
R.elWasScaled = function(el) {
return !!el.getAttribute('data-retinizr-scaled');
};
R.setElWasScaled = function(el) {
el.setAttribute('data-retinizr-scaled', true);
};
R.deviceRequiresRetinazation = function() {
return window.devicePixelRatio >= options.minPixelRatio;
};
//
// Exposes Retinizr
//
window.Retinizr = function(options) {
R.setOptions(options);
if (options.scaleImages !== false) R.scaleImages();
if (options.googleStaticMaps) R.scaleGoogleStaticMaps();
if (options.gravatars) R.scaleGravatars();
};
})(window, window.document);