-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathlayout.js
executable file
·275 lines (249 loc) · 8.35 KB
/
layout.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
define(function () {
'use strict';
/**
* Layout types.
* @inner
* @const
* @type {Object}
*/
var LAYOUT = {
NODISPLAY: 'nodisplay',
FIXED: 'fixed',
FIXED_HEIGHT: 'fixed-height',
RESPONSIVE: 'responsive',
CONTAINER: 'container',
FILL: 'fill',
FLEX_ITEM: 'flex-item'
};
/**
* Natural dimensions.
* @inner
* @const
* @type {Object}
*/
var NATURAL_DIMENSIONS = {
'mip-pix': {width: '1px', height: '1px'},
'mip-stats': {width: '1px', height: '1px'},
'mip-audio': null
};
/**
* Loading elements.
* @inner
* @const
* @type {Object}
*/
var LOADING_ELEMENTS = {
'mip-anim': true,
'mip-brightcove': true,
'mip-embed': true,
'mip-iframe': true,
'mip-img': true,
'mip-list': true,
'mip-video': true
};
/**
* Layout for MIPElement.
* @class
*/
function Layout() {
}
/**
* @param {string} s
* @return {Layout|undefined} Returns undefined in case of failure to parse
* the layout string.
*/
Layout.prototype.parseLayout = function(s) {
for (var i in LAYOUT) {
if (LAYOUT[i] == s) {
return s;
}
}
return undefined;
}
/**
* @param {Layout} layout
* @return {string}
*/
Layout.prototype.getLayoutClass = function(layout) {
return 'mip-layout-' + layout;
}
/**
* Whether an element with this layout inherently defines the size.
* @param {Layout} layout
* @return {boolean}
*/
Layout.prototype.isLayoutSizeDefined = function (layout) {
return (layout == LAYOUT.FIXED ||
layout == LAYOUT.FIXED_HEIGHT ||
layout == LAYOUT.RESPONSIVE ||
layout == LAYOUT.FILL ||
layout == LAYOUT.FLEX_ITEM);
}
/**
* Parses the CSS length value. If no units specified, the assumed value is
* "px". Returns undefined in case of parsing error.
* @param {string|undefined} s
* @return {!LengthDef|undefined}
*/
Layout.prototype.parseLength = function(s) {
if (typeof s == 'number') {
return s + 'px';
}
if (!s) {
return undefined;
}
if (!/^\d+(\.\d+)?(px|em|rem|vh|vw|vmin|vmax|cm|mm|q|in|pc|pt)?$/.test(s)) {
return undefined;
}
if (/^\d+(\.\d+)?$/.test(s)) {
return s + 'px';
}
return s;
}
/**
* Returns the numeric value of a CSS length value.
* @param {string} length
* @return {number}
*/
Layout.prototype.getLengthNumeral = function(length) {
return parseFloat(length);
}
/**
* Determines whether the tagName is a known element that has natural dimensions
* in our runtime or the browser.
* @param {string} tagName The element tag name.
* @return {DimensionsDef}
*/
Layout.prototype.hasNaturalDimensions = function(tagName) {
tagName = tagName.toLowerCase();
return NATURAL_DIMENSIONS[tagName] !== undefined;
}
/**
* Determines the default dimensions for an element which could vary across
* different browser implementations, like <audio> for instance.
* This operation can only be completed for an element whitelisted by
* `hasNaturalDimensions`.
* @param {!Element} element
* @return {DimensionsDef}
*/
Layout.prototype.getNaturalDimensions = function(element) {
var tagName = element.tagName.toLowerCase();
if (!NATURAL_DIMENSIONS[tagName]) {
var doc = element.ownerDocument;
var naturalTagName = tagName.replace(/^mip\-/, '');
var temp = doc.createElement(naturalTagName);
// For audio, should no-op elsewhere.
temp.controls = true;
temp.style.position = 'absolute';
temp.style.visibility = 'hidden';
doc.body.appendChild(temp);
NATURAL_DIMENSIONS[tagName] = {
width: (temp.offsetWidth || 1) + 'px',
height: (temp.offsetHeight || 1) + 'px',
};
doc.body.removeChild(temp);
}
return NATURAL_DIMENSIONS[tagName];
}
/**
* Whether the loading can be shown for the specified elemeent. This set has
* to be externalized since the element's implementation may not be
* downloaded yet.
* @param {string} tagName The element tag name.
* @return {boolean}
*/
Layout.prototype.isLoadingAllowed = function(tagName) {
return LOADING_ELEMENTS[tagName.toLowerCase()] || false;
}
/**
* Apply layout for a MIPElement.
* @param {MIPElement} element
* @return {string}
*/
Layout.prototype.applyLayout = function (element) {
if (element._layoutInited) {
return;
}
element._layoutInited = true;
var layoutAttr = element.getAttribute('layout');
var widthAttr = element.getAttribute('width');
var heightAttr = element.getAttribute('height');
var sizesAttr = element.getAttribute('sizes');
var heightsAttr = element.getAttribute('heights');
// Input layout attributes.
var inputLayout = layoutAttr ? this.parseLayout(layoutAttr) : null;
var inputWidth = (widthAttr && widthAttr != 'auto') ?
this.parseLength(widthAttr) : widthAttr;
var inputHeight = heightAttr ? this.parseLength(heightAttr) : null;
// Effective layout attributes. These are effectively constants.
var width;
var height;
var layout;
// Calculate effective width and height.
if ((!inputLayout || inputLayout == LAYOUT.FIXED ||
inputLayout == LAYOUT.FIXED_HEIGHT) &&
(!inputWidth || !inputHeight) && this.hasNaturalDimensions(element.tagName)) {
// Default width and height: handle elements that do not specify a
// width/height and are defined to have natural browser dimensions.
var dimensions = this.getNaturalDimensions(element);
width = (inputWidth || inputLayout == LAYOUT.FIXED_HEIGHT) ? inputWidth :
dimensions.width;
height = inputHeight || dimensions.height;
} else {
width = inputWidth;
height = inputHeight;
}
// Calculate effective layout.
if (inputLayout) {
layout = inputLayout;
} else if (!width && !height) {
layout = LAYOUT.CONTAINER;
} else if (height && (!width || width == 'auto')) {
layout = LAYOUT.FIXED_HEIGHT;
} else if (height && width && (sizesAttr || heightsAttr)) {
layout = LAYOUT.RESPONSIVE;
} else {
layout = LAYOUT.FIXED;
}
// Apply UI.
element.classList.add(this.getLayoutClass(layout));
if (this.isLayoutSizeDefined(layout)) {
element.classList.add('mip-layout-size-defined');
}
if (layout == LAYOUT.NODISPLAY) {
element.style.display = 'none';
} else if (layout == LAYOUT.FIXED) {
element.style.width = width;
element.style.height = height;
} else if (layout == LAYOUT.FIXED_HEIGHT) {
element.style.height = height;
} else if (layout == LAYOUT.RESPONSIVE) {
var space = element.ownerDocument.createElement('mip-i-space');
space.style.display = 'block';
space.style.paddingTop =
((this.getLengthNumeral(height) / this.getLengthNumeral(width)) * 100) + '%';
element.insertBefore(space, element.firstChild);
element._spaceElement = space;
} else if (layout == LAYOUT.FILL) {
// Do nothing.
} else if (layout == LAYOUT.CONTAINER) {
// Do nothing. Elements themselves will check whether the supplied
// layout value is acceptable. In particular container is only OK
// sometimes.
} else if (layout == LAYOUT.FLEX_ITEM) {
// Set height and width to a flex item if they exist.
// The size set to a flex item could be overridden by `display: flex` later.
if (width) {
element.style.width = width;
}
if (height) {
element.style.height = height;
}
}
if (element.classList.contains('mip-hidden')) {
element.classList.remove('mip-hidden');
}
return layout;
};
return new Layout();
});