-
Notifications
You must be signed in to change notification settings - Fork 61
/
ImgixTag.js
220 lines (177 loc) · 6.33 KB
/
ImgixTag.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
var util = require('./util.js'),
targetWidths = require('./targetWidths.js'),
autoSize = require('./autoSize');
var ImgixTag = (function () {
function ImgixTag(el, opts) {
this.el = el;
this.settings = opts || {};
if (!this.el) {
console.warn('ImgixTag must be passed a DOM element.');
return;
}
this.window = this.settings.window ? this.settings.window : null;
if (this.el.hasAttribute('ix-initialized') && !this.settings.force) {
return;
}
this.ixPathVal = el.getAttribute(this.settings.pathInputAttribute);
this.ixParamsVal = el.getAttribute(this.settings.paramsInputAttribute);
this.ixSrcVal = el.getAttribute(this.settings.srcInputAttribute);
this.ixHostVal =
el.getAttribute(this.settings.hostInputAttribute) || this.settings.host;
if (this.ixPathVal && !this.ixHostVal) {
console.warn(
'You must set a value for `imgix.config.host` or specify an `ix-host` attribute to use `ix-path` and `ix-params`.'
);
return;
}
if (typeof this.ixPathVal === 'string' && this.ixPathVal.length == 0) {
console.warn('`ix-path` cannot accept a value of empty string ""');
return;
}
if (typeof this.ixSrcVal === 'string' && this.ixSrcVal.length == 0) {
console.warn('`ix-src` cannot accept a value of empty string ""');
return;
}
this.baseParams = this._extractBaseParams();
this.baseUrl = this._buildBaseUrl();
this.baseUrlWithoutQuery = this.baseUrl.split('?')[0];
//manual override to use sizes, srcset, and src when lazyloading
if(util.isString(this.el.className)){
if (this.el.className.includes("nolazyload")){
this.settings.sizesAttribute = "sizes";
this.settings.srcsetAttribute = "srcset";
this.settings.srcAttribute = "src";
}
}
if (util.isString(this.settings.sizesAttribute)) {
this.el.setAttribute(this.settings.sizesAttribute, this.sizes());
}
if (util.isString(this.settings.srcsetAttribute)) {
this.el.setAttribute(this.settings.srcsetAttribute, this.srcset());
}
if (
util.isString(this.settings.srcAttribute) &&
this.el.nodeName == 'IMG'
) {
this.el.setAttribute(this.settings.srcAttribute, this.src());
}
this.el.setAttribute('ix-initialized', 'ix-initialized');
}
ImgixTag.prototype._extractBaseParams = function () {
var params = {};
if (
this.settings.defaultParams &&
typeof this.settings.defaultParams === 'object' &&
this.settings.defaultParams !== null
) {
params = Object.assign({}, this.settings.defaultParams);
}
if (this.ixPathVal) {
params = Object.assign({}, params, JSON.parse(this.ixParamsVal) || {});
// Encode any passed Base64 variant params
for (var key in params) {
if (key.substr(-2) === '64') {
params[key] = util.encode64(params[key]);
}
}
} else {
// If the user used `ix-src`, we have to extract the base params
// from that string URL.
var lastQuestion = this.ixSrcVal.lastIndexOf('?');
if (lastQuestion > -1) {
var paramString = this.ixSrcVal.substr(lastQuestion + 1),
splitParams = paramString.split('&');
for (var i = 0, splitParam; i < splitParams.length; i++) {
splitParam = splitParams[i].split('=');
params[splitParam[0]] = splitParam[1];
}
}
}
if (this.settings.includeLibraryParam) {
params.ixlib = 'imgixjs-' + imgix.VERSION;
}
return params;
};
ImgixTag.prototype._buildBaseUrl = function () {
if (this.ixSrcVal) {
return this.ixSrcVal;
}
var path = this.ixPathVal,
protocol = this.settings.useHttps ? 'https' : 'http',
url = protocol + '://' + this.ixHostVal,
hostEndsWithSlash = this.ixHostVal.substr(-1) === '/',
pathStartsWithSlash = path[0] === '/';
// Make sure we don't end up with 2 or 0 slashes between
// the host and path portions of the generated URL
if (hostEndsWithSlash && pathStartsWithSlash) {
url += path.substr(1);
} else if (!hostEndsWithSlash && !pathStartsWithSlash) {
url += '/' + path;
} else {
url += path;
}
url += '?';
var params = [],
param;
for (var key in this.baseParams) {
param = this.baseParams[key];
if (param == null) {
continue;
}
params.push(encodeURIComponent(key) + '=' + encodeURIComponent(param));
}
url += params.join('&');
return url;
};
ImgixTag.prototype._buildSrcsetPair = function (targetWidth) {
var clonedParams = util.shallowClone(this.baseParams);
clonedParams.w = targetWidth;
var url = this.baseUrlWithoutQuery + '?',
val,
params = [];
for (var key in clonedParams) {
val = clonedParams[key];
params.push(key + '=' + val);
}
url += params.join('&');
return url + ' ' + targetWidth + 'w';
};
ImgixTag.prototype.src = function () {
return this.baseUrl;
};
// Returns a comma-separated list of `url widthDescriptor` pairs,
// scaled appropriately to the same aspect ratio as the base image
// as appropriate.
ImgixTag.prototype.srcset = function () {
var pairs = [];
for (var i = 0; i < targetWidths.length; i++) {
pairs.push(this._buildSrcsetPair(targetWidths[i]));
}
return pairs.join(', ');
};
ImgixTag.prototype.sizes = function () {
var existingSizes = this.el.getAttribute('sizes');
var ixSizes = this.el.getAttribute('ix-sizes');
const el = this.el;
const _window = this.window;
/**
*
* The conditionals bellow decide when to override the value for `sizes` for
* the given element.
*
* - If `sizes` set, we leave the value as is even if `ix-sizes` is `auto`
* - If `sizes` not set and `ix-sizes` not auto, set `sizes` to `ix-sizes`
* - If `sizes` not set and `ix-sizes` is auto, set `sizes` automatically
* - If `sizes` and `ix-sizes` not set, set `sizes` to browser default
*/
if (existingSizes == null && ixSizes !== 'auto') {
return ixSizes ? ixSizes : '100vw';
} else if (existingSizes == null && ixSizes === 'auto') {
return autoSize.updateOnResize({ el, existingSizes, ixSizes, _window });
} else {
return existingSizes ? existingSizes : '100vw';
}
};
return ImgixTag;
})();
module.exports = ImgixTag;