forked from aFarkas/lazysizes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.respimg.js
311 lines (264 loc) · 7.73 KB
/
ls.respimg.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
(function(window, factory) {
if(!window) {return;}
var globalInstall = function(){
factory(window.lazySizes);
window.removeEventListener('lazyunveilread', globalInstall, true);
};
factory = factory.bind(null, window, window.document);
if(typeof module == 'object' && module.exports){
factory(require('lazysizes'));
} else if (typeof define == 'function' && define.amd) {
define(['lazysizes'], factory);
} else if(window.lazySizes) {
globalInstall();
} else {
window.addEventListener('lazyunveilread', globalInstall, true);
}
}(typeof window != 'undefined' ?
window : 0, function(window, document, lazySizes) {
/*jshint eqnull:true */
'use strict';
var polyfill;
var lazySizesCfg = lazySizes.cfg;
var img = document.createElement('img');
var supportSrcset = ('sizes' in img) && ('srcset' in img);
var regHDesc = /\s+\d+h/g;
var fixEdgeHDescriptor = (function(){
var regDescriptors = /\s+(\d+)(w|h)\s+(\d+)(w|h)/;
var forEach = Array.prototype.forEach;
return function(){
var img = document.createElement('img');
var removeHDescriptors = function(source){
var ratio, match;
var srcset = source.getAttribute(lazySizesCfg.srcsetAttr);
if(srcset){
if((match = srcset.match(regDescriptors))){
if(match[2] == 'w'){
ratio = match[1] / match[3];
} else {
ratio = match[3] / match[1];
}
if(ratio){
source.setAttribute('data-aspectratio', ratio);
}
source.setAttribute(lazySizesCfg.srcsetAttr, srcset.replace(regHDesc, ''));
}
}
};
var handler = function(e){
if(e.detail.instance != lazySizes){return;}
var picture = e.target.parentNode;
if(picture && picture.nodeName == 'PICTURE'){
forEach.call(picture.getElementsByTagName('source'), removeHDescriptors);
}
removeHDescriptors(e.target);
};
var test = function(){
if(!!img.currentSrc){
document.removeEventListener('lazybeforeunveil', handler);
}
};
document.addEventListener('lazybeforeunveil', handler);
img.onload = test;
img.onerror = test;
img.srcset = 'data:,a 1w 1h';
if(img.complete){
test();
}
};
})();
if(!lazySizesCfg.supportsType){
lazySizesCfg.supportsType = function(type/*, elem*/){
return !type;
};
}
if (window.HTMLPictureElement && supportSrcset) {
if(!lazySizes.hasHDescriptorFix && document.msElementsFromPoint){
lazySizes.hasHDescriptorFix = true;
fixEdgeHDescriptor();
}
return;
}
if(window.picturefill || lazySizesCfg.pf){return;}
lazySizesCfg.pf = function(options){
var i, len;
if(window.picturefill){return;}
for(i = 0, len = options.elements.length; i < len; i++){
polyfill(options.elements[i]);
}
};
// partial polyfill
polyfill = (function(){
var ascendingSort = function( a, b ) {
return a.w - b.w;
};
var regPxLength = /^\s*\d+\.*\d*px\s*$/;
var reduceCandidate = function (srces) {
var lowerCandidate, bonusFactor;
var len = srces.length;
var candidate = srces[len -1];
var i = 0;
for(i; i < len;i++){
candidate = srces[i];
candidate.d = candidate.w / srces.w;
if(candidate.d >= srces.d){
if(!candidate.cached && (lowerCandidate = srces[i - 1]) &&
lowerCandidate.d > srces.d - (0.13 * Math.pow(srces.d, 2.2))){
bonusFactor = Math.pow(lowerCandidate.d - 0.6, 1.6);
if(lowerCandidate.cached) {
lowerCandidate.d += 0.15 * bonusFactor;
}
if(lowerCandidate.d + ((candidate.d - srces.d) * bonusFactor) > srces.d){
candidate = lowerCandidate;
}
}
break;
}
}
return candidate;
};
var parseWsrcset = (function(){
var candidates;
var regWCandidates = /(([^,\s].[^\s]+)\s+(\d+)w)/g;
var regMultiple = /\s/;
var addCandidate = function(match, candidate, url, wDescriptor){
candidates.push({
c: candidate,
u: url,
w: wDescriptor * 1
});
};
return function(input){
candidates = [];
input = input.trim();
input
.replace(regHDesc, '')
.replace(regWCandidates, addCandidate)
;
if(!candidates.length && input && !regMultiple.test(input)){
candidates.push({
c: input,
u: input,
w: 99
});
}
return candidates;
};
})();
var runMatchMedia = function(){
if(runMatchMedia.init){return;}
runMatchMedia.init = true;
addEventListener('resize', (function(){
var timer;
var matchMediaElems = document.getElementsByClassName('lazymatchmedia');
var run = function(){
var i, len;
for(i = 0, len = matchMediaElems.length; i < len; i++){
polyfill(matchMediaElems[i]);
}
};
return function(){
clearTimeout(timer);
timer = setTimeout(run, 66);
};
})());
};
var createSrcset = function(elem, isImage){
var parsedSet;
var srcSet = elem.getAttribute('srcset') || elem.getAttribute(lazySizesCfg.srcsetAttr);
if(!srcSet && isImage){
srcSet = !elem._lazypolyfill ?
(elem.getAttribute(lazySizesCfg.srcAttr) || elem.getAttribute('src')) :
elem._lazypolyfill._set
;
}
if(!elem._lazypolyfill || elem._lazypolyfill._set != srcSet){
parsedSet = parseWsrcset( srcSet || '' );
if(isImage && elem.parentNode){
parsedSet.isPicture = elem.parentNode.nodeName.toUpperCase() == 'PICTURE';
if(parsedSet.isPicture){
if(window.matchMedia){
lazySizes.aC(elem, 'lazymatchmedia');
runMatchMedia();
}
}
}
parsedSet._set = srcSet;
Object.defineProperty(elem, '_lazypolyfill', {
value: parsedSet,
writable: true
});
}
};
var getX = function(elem){
var dpr = window.devicePixelRatio || 1;
var optimum = lazySizes.getX && lazySizes.getX(elem);
return Math.min(optimum || dpr, 2.5, dpr);
};
var matchesMedia = function(media){
if(window.matchMedia){
matchesMedia = function(media){
return !media || (matchMedia(media) || {}).matches;
};
} else {
return !media;
}
return matchesMedia(media);
};
var getCandidate = function(elem){
var sources, i, len, media, source, srces, src, width;
source = elem;
createSrcset(source, true);
srces = source._lazypolyfill;
if(srces.isPicture){
for(i = 0, sources = elem.parentNode.getElementsByTagName('source'), len = sources.length; i < len; i++){
if( lazySizesCfg.supportsType(sources[i].getAttribute('type'), elem) && matchesMedia( sources[i].getAttribute('media')) ){
source = sources[i];
createSrcset(source);
srces = source._lazypolyfill;
break;
}
}
}
if(srces.length > 1){
width = source.getAttribute('sizes') || '';
width = regPxLength.test(width) && parseInt(width, 10) || lazySizes.gW(elem, elem.parentNode);
srces.d = getX(elem);
if(!srces.src || !srces.w || srces.w < width){
srces.w = width;
src = reduceCandidate(srces.sort(ascendingSort));
srces.src = src;
} else {
src = srces.src;
}
} else {
src = srces[0];
}
return src;
};
var p = function(elem){
if(supportSrcset && elem.parentNode && elem.parentNode.nodeName.toUpperCase() != 'PICTURE'){return;}
var candidate = getCandidate(elem);
if(candidate && candidate.u && elem._lazypolyfill.cur != candidate.u){
elem._lazypolyfill.cur = candidate.u;
candidate.cached = true;
elem.setAttribute(lazySizesCfg.srcAttr, candidate.u);
elem.setAttribute('src', candidate.u);
}
};
p.parse = parseWsrcset;
return p;
})();
if(lazySizesCfg.loadedClass && lazySizesCfg.loadingClass){
(function(){
var sels = [];
['img[sizes$="px"][srcset].', 'picture > img:not([srcset]).'].forEach(function(sel){
sels.push(sel + lazySizesCfg.loadedClass);
sels.push(sel + lazySizesCfg.loadingClass);
});
lazySizesCfg.pf({
elements: document.querySelectorAll(sels.join(', '))
});
})();
}
}));