-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathvaadin-form-layout-mixin.js
438 lines (376 loc) · 14.9 KB
/
vaadin-form-layout-mixin.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/**
* @license
* Copyright (c) 2017 - 2025 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import { isElementHidden } from '@vaadin/a11y-base/src/focus-utils.js';
import { ResizeMixin } from '@vaadin/component-base/src/resize-mixin.js';
/**
* @polymerMixin
* @mixes ResizeMixin
*/
export const FormLayoutMixin = (superClass) =>
class extends ResizeMixin(superClass) {
static get properties() {
return {
/**
* @typedef FormLayoutResponsiveStep
* @type {object}
* @property {string} minWidth - The threshold value for this step in CSS length units.
* @property {number} columns - Number of columns. Only natural numbers are valid.
* @property {string} labelsPosition - Labels position option, valid values: `"aside"` (default), `"top"`.
*/
/**
* Allows specifying a responsive behavior with the number of columns
* and the label position depending on the layout width.
*
* Format: array of objects, each object defines one responsive step
* with `minWidth` CSS length, `columns` number, and optional
* `labelsPosition` string of `"aside"` or `"top"`. At least one item is required.
*
* #### Examples
*
* ```javascript
* formLayout.responsiveSteps = [{columns: 1}];
* // The layout is always a single column, labels aside.
* ```
*
* ```javascript
* formLayout.responsiveSteps = [
* {minWidth: 0, columns: 1},
* {minWidth: '40em', columns: 2}
* ];
* // Sets two responsive steps:
* // 1. When the layout width is < 40em, one column, labels aside.
* // 2. Width >= 40em, two columns, labels aside.
* ```
*
* ```javascript
* formLayout.responsiveSteps = [
* {minWidth: 0, columns: 1, labelsPosition: 'top'},
* {minWidth: '20em', columns: 1},
* {minWidth: '40em', columns: 2}
* ];
* // Default value. Three responsive steps:
* // 1. Width < 20em, one column, labels on top.
* // 2. 20em <= width < 40em, one column, labels aside.
* // 3. Width >= 40em, two columns, labels aside.
* ```
*
* @type {!Array<!FormLayoutResponsiveStep>}
*/
responsiveSteps: {
type: Array,
value() {
return [
{ minWidth: 0, columns: 1, labelsPosition: 'top' },
{ minWidth: '20em', columns: 1 },
{ minWidth: '40em', columns: 2 },
];
},
observer: '_responsiveStepsChanged',
sync: true,
},
/**
* Current number of columns in the layout
* @private
*/
_columnCount: {
type: Number,
sync: true,
},
/**
* Indicates that labels are on top
* @private
*/
_labelsOnTop: {
type: Boolean,
sync: true,
},
/** @private */
__isVisible: {
type: Boolean,
},
};
}
static get observers() {
return ['_invokeUpdateLayout(_columnCount, _labelsOnTop)'];
}
/** @protected */
ready() {
// Here we attach a style element that we use for validating
// CSS values in `responsiveSteps`. We can't add this to the `<template>`,
// because Polymer will throw it away. We need to create this before
// `super.ready()`, because `super.ready()` invokes property observers,
// and the observer for `responsiveSteps` does CSS value validation.
this.appendChild(this._styleElement);
super.ready();
this.addEventListener('animationend', this.__onAnimationEnd);
}
constructor() {
super();
this._styleElement = document.createElement('style');
// Ensure there is a child text node in the style element
this._styleElement.textContent = ' ';
this.__intersectionObserver = new IntersectionObserver((entries) => {
// If the browser is busy (e.g. due to slow rendering), multiple entries can
// be queued and then passed to the callback invocation at once. Make sure we
// use the most recent entry to detect whether the layout is visible or not.
// See https://github.com/vaadin/web-components/issues/8564
const entry = [...entries].pop();
if (!entry.isIntersecting) {
// Prevent possible jump when layout becomes visible
this.$.layout.style.opacity = 0;
}
if (!this.__isVisible && entry.isIntersecting) {
this._updateLayout();
this.$.layout.style.opacity = '';
}
this.__isVisible = entry.isIntersecting;
});
}
/** @protected */
connectedCallback() {
super.connectedCallback();
requestAnimationFrame(() => this._selectResponsiveStep());
requestAnimationFrame(() => this._updateLayout());
this._observeChildrenColspanChange();
this.__intersectionObserver.observe(this.$.layout);
}
/** @protected */
disconnectedCallback() {
super.disconnectedCallback();
this.__mutationObserver.disconnect();
this.__childObserver.disconnect();
this.__intersectionObserver.disconnect();
}
/** @private */
_observeChildrenColspanChange() {
// Observe changes in form items' `colspan` attribute and update styles
const mutationObserverConfig = { attributes: true };
this.__mutationObserver = new MutationObserver((mutationRecord) => {
mutationRecord.forEach((mutation) => {
if (
mutation.type === 'attributes' &&
(mutation.attributeName === 'colspan' ||
mutation.attributeName === 'data-colspan' ||
mutation.attributeName === 'hidden')
) {
this._updateLayout();
}
});
});
// Observe changes to initial children
[...this.children].forEach((child) => {
this.__mutationObserver.observe(child, mutationObserverConfig);
});
// Observe changes to lazily added nodes
this.__childObserver = new MutationObserver((mutations) => {
const addedNodes = [];
const removedNodes = [];
mutations.forEach((mutation) => {
addedNodes.push(...this._getObservableNodes(mutation.addedNodes));
removedNodes.push(...this._getObservableNodes(mutation.removedNodes));
});
addedNodes.forEach((child) => {
this.__mutationObserver.observe(child, mutationObserverConfig);
});
if (addedNodes.length > 0 || removedNodes.length > 0) {
this._updateLayout();
}
});
this.__childObserver.observe(this, { childList: true });
}
/** @private */
_getObservableNodes(nodeList) {
const ignore = ['template', 'style', 'dom-repeat', 'dom-if'];
return Array.from(nodeList).filter(
(node) => node.nodeType === Node.ELEMENT_NODE && ignore.indexOf(node.localName.toLowerCase()) === -1,
);
}
/** @private */
_naturalNumberOrOne(n) {
if (typeof n === 'number' && n >= 1 && n < Infinity) {
return Math.floor(n);
}
return 1;
}
/** @private */
_isValidCSSLength(value) {
// Let us choose a CSS property for validating CSS <length> values:
// - `border-spacing` accepts `<length> | inherit`, it's the best! But
// it does not disallow invalid values at all in MSIE :-(
// - `letter-spacing` and `word-spacing` accept
// `<length> | normal | inherit`, and disallows everything else, like
// `<percentage>`, `auto` and such, good enough.
// - `word-spacing` is used since its shorter.
// Disallow known keywords allowed as the `word-spacing` value
if (value === 'inherit' || value === 'normal') {
return false;
}
// Use the value in a stylesheet and check the parsed value. Invalid
// input value results in empty parsed value.
this._styleElement.firstChild.nodeValue = `#styleElement { word-spacing: ${value}; }`;
if (!this._styleElement.sheet) {
// Stylesheet is not ready, probably not attached to the document yet.
return true;
}
// Safari 9 sets invalid CSS rules' value to `null`
return ['', null].indexOf(this._styleElement.sheet.cssRules[0].style.getPropertyValue('word-spacing')) < 0;
}
/** @private */
_responsiveStepsChanged(responsiveSteps, oldResponsiveSteps) {
try {
if (!Array.isArray(responsiveSteps)) {
throw new Error('Invalid "responsiveSteps" type, an Array is required.');
}
if (responsiveSteps.length < 1) {
throw new Error('Invalid empty "responsiveSteps" array, at least one item is required.');
}
responsiveSteps.forEach((step) => {
if (this._naturalNumberOrOne(step.columns) !== step.columns) {
throw new Error(`Invalid 'columns' value of ${step.columns}, a natural number is required.`);
}
if (step.minWidth !== undefined && !this._isValidCSSLength(step.minWidth)) {
throw new Error(`Invalid 'minWidth' value of ${step.minWidth}, a valid CSS length required.`);
}
if (step.labelsPosition !== undefined && ['aside', 'top'].indexOf(step.labelsPosition) === -1) {
throw new Error(
`Invalid 'labelsPosition' value of ${step.labelsPosition}, 'aside' or 'top' string is required.`,
);
}
});
} catch (e) {
if (oldResponsiveSteps && oldResponsiveSteps !== responsiveSteps) {
console.warn(`${e.message} Using previously set 'responsiveSteps' instead.`);
this.responsiveSteps = oldResponsiveSteps;
} else {
console.warn(`${e.message} Using default 'responsiveSteps' instead.`);
this.responsiveSteps = [
{ minWidth: 0, columns: 1, labelsPosition: 'top' },
{ minWidth: '20em', columns: 1 },
{ minWidth: '40em', columns: 2 },
];
}
}
this._selectResponsiveStep();
}
/** @private */
__onAnimationEnd(e) {
if (e.animationName.indexOf('vaadin-form-layout-appear') === 0) {
this._selectResponsiveStep();
}
}
/** @private */
_selectResponsiveStep() {
// Iterate through responsiveSteps and choose the step
let selectedStep;
const tmpStyleProp = 'background-position';
this.responsiveSteps.forEach((step) => {
// Convert minWidth to px units for comparison
this.$.layout.style.setProperty(tmpStyleProp, step.minWidth);
const stepMinWidthPx = parseFloat(getComputedStyle(this.$.layout).getPropertyValue(tmpStyleProp));
// Compare step min-width with the host width, select the passed step
if (stepMinWidthPx <= this.offsetWidth) {
selectedStep = step;
}
});
this.$.layout.style.removeProperty(tmpStyleProp);
// Sometimes converting units is not possible, e.g, when element is
// not connected. Then the `selectedStep` stays `undefined`.
if (selectedStep) {
// Apply the chosen responsive step's properties
this._columnCount = selectedStep.columns;
this._labelsOnTop = selectedStep.labelsPosition === 'top';
}
}
/** @private */
_invokeUpdateLayout() {
this._updateLayout();
}
/**
* Update the layout.
* @protected
*/
_updateLayout() {
// Do not update layout when invisible
if (isElementHidden(this)) {
return;
}
/*
The item width formula:
itemWidth = colspan / columnCount * 100% - columnSpacing
We have to subtract columnSpacing, because the column spacing space is taken
by item margins of 1/2 * spacing on both sides
*/
const style = getComputedStyle(this);
const columnSpacing = style.getPropertyValue('--vaadin-form-layout-column-spacing');
const direction = style.direction;
const marginStartProp = `margin-${direction === 'ltr' ? 'left' : 'right'}`;
const marginEndProp = `margin-${direction === 'ltr' ? 'right' : 'left'}`;
const containerWidth = this.offsetWidth;
let col = 0;
Array.from(this.children)
.filter((child) => child.localName === 'br' || getComputedStyle(child).display !== 'none')
.forEach((child, index, children) => {
if (child.localName === 'br') {
// Reset column count on line break
col = 0;
return;
}
const attrColspan = child.getAttribute('colspan') || child.getAttribute('data-colspan');
let colspan;
colspan = this._naturalNumberOrOne(parseFloat(attrColspan));
// Never span further than the number of columns
colspan = Math.min(colspan, this._columnCount);
const childRatio = colspan / this._columnCount;
// Note: using 99.9% for 100% fixes rounding errors in MS Edge
// (< v16), otherwise the items might wrap, resizing is wobbly.
child.style.width = `calc(${childRatio * 99.9}% - ${1 - childRatio} * ${columnSpacing})`;
if (col + colspan > this._columnCount) {
// Too big to fit on this row, let's wrap it
col = 0;
}
// At the start edge
if (col === 0) {
child.style.setProperty(marginStartProp, '0px');
} else {
child.style.removeProperty(marginStartProp);
}
const nextIndex = index + 1;
const nextLineBreak = nextIndex < children.length && children[nextIndex].localName === 'br';
// At the end edge
if (col + colspan === this._columnCount) {
child.style.setProperty(marginEndProp, '0px');
} else if (nextLineBreak) {
const colspanRatio = (this._columnCount - col - colspan) / this._columnCount;
child.style.setProperty(
marginEndProp,
`calc(${colspanRatio * containerWidth}px + ${colspanRatio} * ${columnSpacing})`,
);
} else {
child.style.removeProperty(marginEndProp);
}
// Move the column counter
col = (col + colspan) % this._columnCount;
if (child.localName === 'vaadin-form-item') {
if (this._labelsOnTop) {
if (child.getAttribute('label-position') !== 'top') {
child.__useLayoutLabelPosition = true;
child.setAttribute('label-position', 'top');
}
} else if (child.__useLayoutLabelPosition) {
delete child.__useLayoutLabelPosition;
child.removeAttribute('label-position');
}
}
});
}
/**
* @protected
* @override
*/
_onResize() {
this._selectResponsiveStep();
}
};