forked from aFarkas/lazysizes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.parent-fit.js
157 lines (124 loc) · 4.23 KB
/
ls.parent-fit.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
(function(window, factory) {
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(window.lazySizes) {
globalInstall();
} else {
window.addEventListener('lazyunveilread', globalInstall, true);
}
}(window, function(window, document, lazySizes) {
'use strict';
if(!window.addEventListener){return;}
var regDescriptors = /\s+(\d+)(w|h)\s+(\d+)(w|h)/;
var regCssFit = /parent-fit["']*\s*:\s*["']*(contain|cover|width)/;
var regCssObject = /parent-container["']*\s*:\s*["']*(.+?)(?=(\s|$|,|'|"|;))/;
var regPicture = /^picture$/i;
var getCSS = function (elem){
return (getComputedStyle(elem, null) || {});
};
var parentFit = {
getParent: function(element, parentSel){
var parent = element;
var parentNode = element.parentNode;
if((!parentSel || parentSel == 'prev') && parentNode && regPicture.test(parentNode.nodeName || '')){
parentNode = parentNode.parentNode;
}
if(parentSel != 'self'){
if(parentSel == 'prev'){
parent = element.previousElementSibling;
} else if(parentSel && (parentNode.closest || window.jQuery)){
parent = (parentNode.closest ?
parentNode.closest(parentSel) :
jQuery(parentNode).closest(parentSel)[0]) ||
parentNode
;
} else {
parent = parentNode;
}
}
return parent;
},
getFit: function(element){
var tmpMatch, parentObj;
var css = getCSS(element);
var content = css.content || css.fontFamily;
var obj = {
fit: element._lazysizesParentFit || element.getAttribute('data-parent-fit')
};
if(!obj.fit && content && (tmpMatch = content.match(regCssFit))){
obj.fit = tmpMatch[1];
}
if(obj.fit){
parentObj = element._lazysizesParentContainer || element.getAttribute('data-parent-container');
if(!parentObj && content && (tmpMatch = content.match(regCssObject))){
parentObj = tmpMatch[1];
}
obj.parent = parentFit.getParent(element, parentObj);
} else {
obj.fit = css.objectFit;
}
return obj;
},
getImageRatio: function(element){
var i, srcset, media, ratio, match;
var parent = element.parentNode;
var elements = parent && regPicture.test(parent.nodeName || '') ?
parent.querySelectorAll('source, img') :
[element]
;
for(i = 0; i < elements.length; i++){
element = elements[i];
srcset = element.getAttribute(lazySizesConfig.srcsetAttr) || element.getAttribute('srcset') || element.getAttribute('data-pfsrcset') || element.getAttribute('data-risrcset') || '';
media = element._lsMedia || element.getAttribute('media');
media = lazySizesConfig.customMedia[element.getAttribute('data-media') || media] || media;
if(srcset && (!media || (window.matchMedia && matchMedia(media) || {}).matches )){
ratio = parseFloat(element.getAttribute('data-aspectratio'));
if(!ratio && (match = srcset.match(regDescriptors))){
if(match[2] == 'w'){
ratio = match[1] / match[3];
} else {
ratio = match[3] / match[1];
}
}
break;
}
}
return ratio;
},
calculateSize: function(element, width){
var displayRatio, height, imageRatio, retWidth;
var fitObj = this.getFit(element);
var fit = fitObj.fit;
var fitElem = fitObj.parent;
if(fit != 'width' && ((fit != 'contain' && fit != 'cover') || !(imageRatio = this.getImageRatio(element)))){
return width;
}
if(fitElem){
width = fitElem.clientWidth;
} else {
fitElem = element;
}
retWidth = width;
if(fit == 'width'){
retWidth = width;
} else {
height = fitElem.clientHeight;
if(height > 40 && (displayRatio = width / height) && ((fit == 'cover' && displayRatio < imageRatio) || (fit == 'contain' && displayRatio > imageRatio))){
retWidth = width * (imageRatio / displayRatio);
}
}
return retWidth;
}
};
lazySizes.parentFit = parentFit;
document.addEventListener('lazybeforesizes', function(e){
if(e.defaultPrevented || e.detail.instance != lazySizes){return;}
var element = e.target;
e.detail.width = parentFit.calculateSize(element, e.detail.width);
});
}));