@tylertech/forge-core 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +32 -0
- package/esm/a11y/a11y.js +17 -0
- package/esm/a11y/index.js +1 -0
- package/esm/constants/date-constants.js +52 -0
- package/esm/constants/index.js +1 -0
- package/esm/custom-elements/component-utils.js +262 -0
- package/esm/custom-elements/decorators/custom-element.js +52 -0
- package/esm/custom-elements/decorators/foundation-property.js +147 -0
- package/esm/custom-elements/decorators/index.js +2 -0
- package/esm/custom-elements/index.js +2 -0
- package/esm/events/event-aware.js +34 -0
- package/esm/events/index.js +1 -0
- package/esm/index.js +13 -0
- package/esm/message-list/index.js +2 -0
- package/esm/message-list/message-list-entry.js +10 -0
- package/esm/message-list/message-list.js +112 -0
- package/esm/scroll/index.js +2 -0
- package/esm/scroll/scroll-axis-observer.js +114 -0
- package/esm/scroll/scroll-types.js +14 -0
- package/esm/services/index.js +1 -0
- package/esm/services/service-adapter.js +12 -0
- package/esm/utils/a11y.js +17 -0
- package/esm/utils/clipboard.js +38 -0
- package/esm/utils/dom-utils.js +780 -0
- package/esm/utils/event-utils.js +30 -0
- package/esm/utils/http-utils.js +26 -0
- package/esm/utils/index.js +11 -0
- package/esm/utils/item-manager.js +82 -0
- package/esm/utils/object-utils.js +101 -0
- package/esm/utils/platform.js +60 -0
- package/esm/utils/position-utils.js +59 -0
- package/esm/utils/string-utils.js +12 -0
- package/esm/utils/utils.js +261 -0
- package/package.json +19 -0
- package/typings/a11y/a11y.d.ts +5 -0
- package/typings/a11y/index.d.ts +1 -0
- package/typings/constants/date-constants.d.ts +6 -0
- package/typings/constants/index.d.ts +1 -0
- package/typings/custom-elements/component-utils.d.ts +125 -0
- package/typings/custom-elements/decorators/custom-element.d.ts +21 -0
- package/typings/custom-elements/decorators/foundation-property.d.ts +20 -0
- package/typings/custom-elements/decorators/index.d.ts +2 -0
- package/typings/custom-elements/index.d.ts +13 -0
- package/typings/events/event-aware.d.ts +16 -0
- package/typings/events/index.d.ts +1 -0
- package/typings/index.d.ts +13 -0
- package/typings/message-list/index.d.ts +2 -0
- package/typings/message-list/message-list-entry.d.ts +9 -0
- package/typings/message-list/message-list.d.ts +54 -0
- package/typings/scroll/index.d.ts +2 -0
- package/typings/scroll/scroll-axis-observer.d.ts +44 -0
- package/typings/scroll/scroll-types.d.ts +28 -0
- package/typings/services/index.d.ts +1 -0
- package/typings/services/service-adapter.d.ts +25 -0
- package/typings/utils/a11y.d.ts +2 -0
- package/typings/utils/clipboard.d.ts +2 -0
- package/typings/utils/dom-utils.d.ts +254 -0
- package/typings/utils/event-utils.d.ts +10 -0
- package/typings/utils/http-utils.d.ts +5 -0
- package/typings/utils/index.d.ts +11 -0
- package/typings/utils/item-manager.d.ts +42 -0
- package/typings/utils/object-utils.d.ts +43 -0
- package/typings/utils/platform.d.ts +26 -0
- package/typings/utils/position-utils.d.ts +56 -0
- package/typings/utils/string-utils.d.ts +6 -0
- package/typings/utils/utils.d.ts +104 -0
|
@@ -0,0 +1,780 @@
|
|
|
1
|
+
import { isArray } from './utils';
|
|
2
|
+
/**
|
|
3
|
+
* Holds regular expressions
|
|
4
|
+
*/
|
|
5
|
+
/* tslint:disable-next-line:require-private-underscore */
|
|
6
|
+
const REGULAR_EXPRESSIONS = {
|
|
7
|
+
placement: {
|
|
8
|
+
auto: /\s?auto?\s?/i,
|
|
9
|
+
primary: /^(top|bottom|left|right)$/,
|
|
10
|
+
secondary: /^(top|bottom|left|right|center)$/,
|
|
11
|
+
topBottom: /^(top|bottom)$/
|
|
12
|
+
},
|
|
13
|
+
overflow: /(auto|scroll)/
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Holds the browser scrollbar width.
|
|
17
|
+
*/
|
|
18
|
+
/* tslint:disable-next-line:require-private-underscore */
|
|
19
|
+
let SCROLLBAR_WIDTH;
|
|
20
|
+
/**
|
|
21
|
+
* Gets the ownerDocument for an element, if null, than returns the document element.
|
|
22
|
+
* @param {Element} element The element to get the ownerDocument for
|
|
23
|
+
* @returns {Document}
|
|
24
|
+
*/
|
|
25
|
+
function _ownerDocument(element) {
|
|
26
|
+
return element.ownerDocument || document;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Retrieves an element based on the provided root and selector.
|
|
30
|
+
* @param {Element} root The root element to search within.
|
|
31
|
+
* @param {string} selector The selector for the child element.
|
|
32
|
+
* @param {boolean} [allowNull=false] Should the method allow the element to be not found? Default is false.
|
|
33
|
+
* @returns {HTMLElement}
|
|
34
|
+
*/
|
|
35
|
+
export function getElement(root, selector, allowNull = false) {
|
|
36
|
+
const element = root.querySelector(selector);
|
|
37
|
+
if (!element && !allowNull) {
|
|
38
|
+
throw new Error(`Element not found with selector: ${selector}`);
|
|
39
|
+
}
|
|
40
|
+
return element;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Checks if an element is a valid element.
|
|
44
|
+
* @param {Element} element The node to test
|
|
45
|
+
* @returns {boolean}
|
|
46
|
+
*/
|
|
47
|
+
export function isElement(element) {
|
|
48
|
+
return element && element.nodeType === 1;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Checks if an element is statically positioned.
|
|
52
|
+
* @param {Element} element The node to test.
|
|
53
|
+
* @returns {boolean}
|
|
54
|
+
*/
|
|
55
|
+
export function isPositionStatic(element) {
|
|
56
|
+
return (window.getComputedStyle(element).position || 'static') === 'static';
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Parses a style string to a numeric value (removes 'px').
|
|
60
|
+
* @param {string} value The style string to parse.
|
|
61
|
+
* @returns {number}
|
|
62
|
+
*/
|
|
63
|
+
export function parseStyle(value) {
|
|
64
|
+
if (!value || !value.length) {
|
|
65
|
+
return 0;
|
|
66
|
+
}
|
|
67
|
+
const parsedValue = parseFloat(value);
|
|
68
|
+
return isFinite(parsedValue) ? parsedValue : 0;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Gets the index of an element in the parent element children.
|
|
72
|
+
* @param {Element} element The element to get the index on.
|
|
73
|
+
* @returns {number}
|
|
74
|
+
*/
|
|
75
|
+
export function elementIndex(element) {
|
|
76
|
+
if (!isElement(element)) {
|
|
77
|
+
throw new Error('DOMUtils - elementIndex: invalid element argument');
|
|
78
|
+
}
|
|
79
|
+
if (!element.parentElement) {
|
|
80
|
+
return -1;
|
|
81
|
+
}
|
|
82
|
+
return Array.from(element.parentElement.children).indexOf(element);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Gets an array of parent elements up to the body element.
|
|
86
|
+
* @param {Element} element The element to get the parents of.
|
|
87
|
+
* @param {Element=} untilElement Optional element where traversal should stop.
|
|
88
|
+
* @returns {Array}
|
|
89
|
+
*/
|
|
90
|
+
export function elementParents(element, untilElement) {
|
|
91
|
+
if (!isElement(element)) {
|
|
92
|
+
throw new Error('DOMUtils - elementParents: invalid element argument');
|
|
93
|
+
}
|
|
94
|
+
const parentElements = [];
|
|
95
|
+
while (element.parentElement) {
|
|
96
|
+
parentElements.push(element.parentElement);
|
|
97
|
+
if (element.parentElement === untilElement || element.parentElement === _ownerDocument(element).body) {
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
// pierce shadow DOM
|
|
101
|
+
if (element.parentElement && element.parentElement.parentNode && element.parentElement.parentNode.nodeType === 11) {
|
|
102
|
+
element = element.parentElement.parentNode.host;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
element = element.parentElement;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return parentElements;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Gets the non-statically positioned parent of an element.
|
|
112
|
+
* @param element The element to get the offset parent of.
|
|
113
|
+
* @returns {Element}
|
|
114
|
+
*/
|
|
115
|
+
export function offsetParent(element) {
|
|
116
|
+
if (!isElement(element)) {
|
|
117
|
+
throw new Error('DOMUtils - offsetParent: invalid element argument');
|
|
118
|
+
}
|
|
119
|
+
let offsetParentElem = element.offsetParent;
|
|
120
|
+
while (offsetParentElem && isPositionStatic(offsetParentElem)) {
|
|
121
|
+
offsetParentElem = offsetParentElem.offsetParent;
|
|
122
|
+
}
|
|
123
|
+
return offsetParentElem || _ownerDocument(element).documentElement;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Gets the browser scrollbar width.
|
|
127
|
+
* @returns {number}
|
|
128
|
+
*/
|
|
129
|
+
export function scrollbarWidth() {
|
|
130
|
+
if (SCROLLBAR_WIDTH === undefined) {
|
|
131
|
+
const elem = document.createElement('div');
|
|
132
|
+
elem.style.position = 'absolute';
|
|
133
|
+
elem.style.top = '-100px';
|
|
134
|
+
elem.style.left = '-100px';
|
|
135
|
+
elem.style.width = '50px';
|
|
136
|
+
elem.style.height = '50px';
|
|
137
|
+
elem.style.overflow = 'scroll';
|
|
138
|
+
document.body.appendChild(elem);
|
|
139
|
+
const width = elem.offsetWidth - elem.clientWidth;
|
|
140
|
+
removeElement(elem);
|
|
141
|
+
SCROLLBAR_WIDTH = isFinite(width) ? width : 0;
|
|
142
|
+
}
|
|
143
|
+
return SCROLLBAR_WIDTH;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Checks if an element is scrollable.
|
|
147
|
+
* @param {Element} element The element to test for scrollability
|
|
148
|
+
* @returns {boolean}
|
|
149
|
+
*/
|
|
150
|
+
export function isScrollable(element) {
|
|
151
|
+
const elemStyle = window.getComputedStyle(element);
|
|
152
|
+
return REGULAR_EXPRESSIONS.overflow.test('' + elemStyle.overflow + elemStyle.overflowY + elemStyle.overflowX);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Gets the scroll parent of an element.
|
|
156
|
+
* @param {Element} element The element to get the scroll parent of.
|
|
157
|
+
* @param {boolean} [includeSelf=false] Should the element be checked for scrollability.
|
|
158
|
+
* @returns {Element}
|
|
159
|
+
*/
|
|
160
|
+
export function scrollParent(element, includeSelf = false) {
|
|
161
|
+
if (!isElement(element)) {
|
|
162
|
+
throw new Error('DOMUtils - scrollParent: invalid element argument');
|
|
163
|
+
}
|
|
164
|
+
const docElem = _ownerDocument(element).documentElement;
|
|
165
|
+
const elemStyle = window.getComputedStyle(element);
|
|
166
|
+
if (includeSelf && REGULAR_EXPRESSIONS.overflow.test('' + elemStyle.overflow + elemStyle.overflowY + elemStyle.overflowX)) {
|
|
167
|
+
return element;
|
|
168
|
+
}
|
|
169
|
+
let excludeStatic = elemStyle.position === 'absolute';
|
|
170
|
+
let scrollParentElem = element.parentElement || docElem;
|
|
171
|
+
if (scrollParentElem === docElem || elemStyle.position === 'fixed') {
|
|
172
|
+
return scrollParentElem;
|
|
173
|
+
}
|
|
174
|
+
while (scrollParentElem && scrollParentElem !== docElem) {
|
|
175
|
+
const scrollParentStyle = window.getComputedStyle(scrollParentElem);
|
|
176
|
+
if (excludeStatic && scrollParentStyle.position !== 'static') {
|
|
177
|
+
excludeStatic = false;
|
|
178
|
+
}
|
|
179
|
+
if (!excludeStatic && REGULAR_EXPRESSIONS.overflow.test('' + scrollParentStyle.overflow + scrollParentStyle.overflowY + scrollParentStyle.overflowX)) {
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
scrollParentElem = scrollParentElem.scrollParent;
|
|
183
|
+
}
|
|
184
|
+
return scrollParentElem || docElem;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Checks if the elements scroll parent scrollbars are visible.
|
|
188
|
+
* @param {Element} element The element to check the scroll parent of.
|
|
189
|
+
* @returns {IScrollbarVisibility}
|
|
190
|
+
*/
|
|
191
|
+
export function isScrollbarVisible(element) {
|
|
192
|
+
if (!isElement(element)) {
|
|
193
|
+
throw new Error('DOMUtils - isDocumentScrolled: invalid element argument');
|
|
194
|
+
}
|
|
195
|
+
const scrollParentElem = scrollParent(element);
|
|
196
|
+
return {
|
|
197
|
+
x: scrollParentElem.scrollWidth > scrollParentElem.clientWidth,
|
|
198
|
+
y: scrollParentElem.scrollHeight > scrollParentElem.clientHeight
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Gets the offset from the element to the parent element edges.
|
|
203
|
+
* If no parentElement is supplied, the documentElement will be used.
|
|
204
|
+
* @param {Element} element The element to compute the offset for.
|
|
205
|
+
* @param {Element=} parentElement Optional parent element to measure from.
|
|
206
|
+
* @returns {DOMRect}
|
|
207
|
+
*/
|
|
208
|
+
export function offset(element, parentElement) {
|
|
209
|
+
if (!isElement(element)) {
|
|
210
|
+
throw new Error('DOMUtils - offset: invalid element argument');
|
|
211
|
+
}
|
|
212
|
+
const elemBCR = element.getBoundingClientRect();
|
|
213
|
+
const win = _ownerDocument(element).defaultView;
|
|
214
|
+
const docElem = parentElement || win.document.documentElement;
|
|
215
|
+
const offsetValues = { width: elemBCR.width, height: elemBCR.width, top: 0, left: 0, bottom: 0, right: 0 };
|
|
216
|
+
if (!parentElement || docElem === win.document.documentElement || docElem === win.document.body) {
|
|
217
|
+
offsetValues.top = win.scrollY + elemBCR.top;
|
|
218
|
+
offsetValues.bottom = docElem.clientHeight - win.scrollY - elemBCR.bottom;
|
|
219
|
+
offsetValues.left = win.scrollX + elemBCR.left;
|
|
220
|
+
offsetValues.right = docElem.clientWidth - win.scrollX - elemBCR.right;
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
if (!isElement(parentElement)) {
|
|
224
|
+
throw new Error('DOMUtils - offset: invalid parentElement argument');
|
|
225
|
+
}
|
|
226
|
+
const parentBCR = parentElement.getBoundingClientRect();
|
|
227
|
+
offsetValues.top = elemBCR.top - parentBCR.top;
|
|
228
|
+
offsetValues.bottom = parentBCR.bottom - elemBCR.bottom;
|
|
229
|
+
offsetValues.left = elemBCR.left - parentBCR.left;
|
|
230
|
+
offsetValues.right = parentBCR.right - elemBCR.right;
|
|
231
|
+
}
|
|
232
|
+
return {
|
|
233
|
+
width: Math.round(elemBCR.width),
|
|
234
|
+
height: Math.round(elemBCR.height),
|
|
235
|
+
top: Math.round(offsetValues.top),
|
|
236
|
+
bottom: Math.round(offsetValues.bottom),
|
|
237
|
+
left: Math.round(offsetValues.left),
|
|
238
|
+
right: Math.round(offsetValues.right)
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Gets the offset from the element to the parent element viewable edges.
|
|
243
|
+
* If no parentElement is supplied, the documentElement will be used.
|
|
244
|
+
* @param {Element} element The element to measure
|
|
245
|
+
* @param {Element=} parentElement The parent element to measure to.
|
|
246
|
+
* @returns {DOMRect}
|
|
247
|
+
*/
|
|
248
|
+
export function viewportOffset(element, parentElement) {
|
|
249
|
+
if (!isElement(element)) {
|
|
250
|
+
throw new Error('DOMUtils - offset: invalid element argument');
|
|
251
|
+
}
|
|
252
|
+
const win = _ownerDocument(element).defaultView;
|
|
253
|
+
parentElement = parentElement || win.document.documentElement;
|
|
254
|
+
const parentElementOffset = offset(element, parentElement);
|
|
255
|
+
const offsetValues = {
|
|
256
|
+
top: parentElementOffset.top,
|
|
257
|
+
bottom: 0,
|
|
258
|
+
left: parentElementOffset.left,
|
|
259
|
+
right: 0
|
|
260
|
+
};
|
|
261
|
+
if (parentElement === win.document.documentElement) {
|
|
262
|
+
offsetValues.top -= win.scrollY;
|
|
263
|
+
offsetValues.left -= win.scrollX;
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
const parentStyle = window.getComputedStyle(parentElement);
|
|
267
|
+
offsetValues.top -= parseStyle('' + parentStyle.borderTopWidth);
|
|
268
|
+
offsetValues.left -= parseStyle('' + parentStyle.borderLeftWidth);
|
|
269
|
+
}
|
|
270
|
+
offsetValues.bottom = parentElement.clientHeight - offsetValues.top - element.offsetHeight;
|
|
271
|
+
offsetValues.right = parentElement.clientWidth - offsetValues.left - element.offsetWidth;
|
|
272
|
+
return {
|
|
273
|
+
width: parentElementOffset.width,
|
|
274
|
+
height: parentElementOffset.height,
|
|
275
|
+
top: Math.round(offsetValues.top),
|
|
276
|
+
bottom: Math.round(offsetValues.bottom),
|
|
277
|
+
left: Math.round(offsetValues.left),
|
|
278
|
+
right: Math.round(offsetValues.right)
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Checks if any part of an element is visible in the viewport.
|
|
283
|
+
* @param {Element} element The element to check.
|
|
284
|
+
* @returns {boolean}
|
|
285
|
+
*/
|
|
286
|
+
export function isElementInViewport(element) {
|
|
287
|
+
if (!isElement(element)) {
|
|
288
|
+
throw new Error('DOMUtils - isElementInViewport: invalid element argument');
|
|
289
|
+
}
|
|
290
|
+
const document = _ownerDocument(element);
|
|
291
|
+
const scrollParentElem = scrollParent(element);
|
|
292
|
+
const elemBCR = element.getBoundingClientRect();
|
|
293
|
+
if (scrollParentElem !== document.documentElement && scrollParentElem !== document.body) {
|
|
294
|
+
const scrollParentOffset = offset(element, scrollParentElem);
|
|
295
|
+
if (scrollParentOffset.top + elemBCR.height < 0 ||
|
|
296
|
+
scrollParentOffset.left + elemBCR.width < 0 ||
|
|
297
|
+
scrollParentOffset.bottom + elemBCR.height - this.scrollbarWidth < 0 ||
|
|
298
|
+
scrollParentOffset.right + elemBCR.width - this.scrollbarWidth < 0) {
|
|
299
|
+
return false;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (elemBCR.top + elemBCR.height < 0 ||
|
|
303
|
+
elemBCR.left + elemBCR.width < 0 ||
|
|
304
|
+
elemBCR.bottom + elemBCR.height > document.documentElement.clientHeight ||
|
|
305
|
+
elemBCR.right + elemBCR.width > document.documentElement.clientWidth) {
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
return true;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Adds an event listener to the document that will call the provided callback function
|
|
312
|
+
* when an element and it's children no longer have focus. The blur and touchstart events are used
|
|
313
|
+
* to evaluate the active element to determine if the callback should be called.
|
|
314
|
+
*
|
|
315
|
+
* @param {Element} element The element to add the event listener to.
|
|
316
|
+
* @param {Function} callback The function to call when the element and children don't have focus.
|
|
317
|
+
* @param {boolean} [delay=false] Should a RAF cycle occur before the callback is called.
|
|
318
|
+
* @returns {Function} The function to call to remove the document events.
|
|
319
|
+
*/
|
|
320
|
+
export function notChildEventListener(element, callback, delay) {
|
|
321
|
+
const evtHandler = (event) => {
|
|
322
|
+
const handle = () => {
|
|
323
|
+
event.stopPropagation();
|
|
324
|
+
if (event.cancelable) {
|
|
325
|
+
event.preventDefault();
|
|
326
|
+
}
|
|
327
|
+
const activeElement = (event.type === 'touchstart' ? event.target : _ownerDocument(element).activeElement);
|
|
328
|
+
if (!element.contains(activeElement)) {
|
|
329
|
+
callback(activeElement);
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
if (delay) {
|
|
333
|
+
window.requestAnimationFrame(() => handle());
|
|
334
|
+
}
|
|
335
|
+
else {
|
|
336
|
+
handle();
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
const docElem = _ownerDocument(element);
|
|
340
|
+
docElem.addEventListener('blur', evtHandler, true);
|
|
341
|
+
docElem.addEventListener('touchstart', evtHandler, true);
|
|
342
|
+
return () => {
|
|
343
|
+
docElem.removeEventListener('blur', evtHandler, true);
|
|
344
|
+
docElem.removeEventListener('touchstart', evtHandler, true);
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Removes all children from a DOM node.
|
|
349
|
+
* @param node The DOM node to remove children from.
|
|
350
|
+
*/
|
|
351
|
+
export function removeAllChildren(node) {
|
|
352
|
+
while (node.lastChild) {
|
|
353
|
+
node.removeChild(node.lastChild);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Replaces one child node of the specified node with another.
|
|
358
|
+
* @param newChild The new node to replace `oldChild`.
|
|
359
|
+
* @param oldChild The existing node to be replaced.
|
|
360
|
+
* @returns {Node} The replaced node. Same node as `oldChild`.
|
|
361
|
+
*/
|
|
362
|
+
export function replaceElement(newChild, oldChild) {
|
|
363
|
+
return oldChild.parentNode.replaceChild(newChild, oldChild);
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Adds a class or array of classes to an element.
|
|
367
|
+
*
|
|
368
|
+
* @param {string | string[]} name The class(es) to add to the element
|
|
369
|
+
* @param {Element} element The element to add class(es) to.
|
|
370
|
+
*/
|
|
371
|
+
export function addClass(name, element) {
|
|
372
|
+
if (isArray(name)) {
|
|
373
|
+
name.forEach(n => element.classList.add(n));
|
|
374
|
+
}
|
|
375
|
+
else {
|
|
376
|
+
element.classList.add(name);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Removes a class or array of classes to an element.
|
|
381
|
+
*
|
|
382
|
+
* @param {string | string[]} name The class(es) to remove from the element
|
|
383
|
+
* @param {Element} element The element to remove class(es) from.
|
|
384
|
+
*/
|
|
385
|
+
export function removeClass(name, element) {
|
|
386
|
+
if (isArray(name)) {
|
|
387
|
+
name.forEach(n => element.classList.remove(n));
|
|
388
|
+
}
|
|
389
|
+
else {
|
|
390
|
+
element.classList.remove(name);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
/** Determines which type of animation event is supported. */
|
|
394
|
+
export function getAnimationEvent() {
|
|
395
|
+
const el = document.createElement('fakeelement');
|
|
396
|
+
// tslint:disable:object-literal-key-quotes
|
|
397
|
+
const animations = {
|
|
398
|
+
'animation': 'animationend',
|
|
399
|
+
'OAnimation': 'oAnimationEnd',
|
|
400
|
+
'MozAnimation': 'animationend',
|
|
401
|
+
'WebkitAnimation': 'webkitAnimationEnd'
|
|
402
|
+
};
|
|
403
|
+
for (const t in animations) {
|
|
404
|
+
if (el.style[t] !== undefined) {
|
|
405
|
+
return animations[t];
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* A helper method to trigger a keyframe animation via adding a class, and removing the class when the animation completes.
|
|
411
|
+
* @param {HTMLElement} element The element to play the animation on.
|
|
412
|
+
* @param {string} className The class to add that triggers the animation.
|
|
413
|
+
*/
|
|
414
|
+
export async function playKeyframeAnimation(element, className, remove = true) {
|
|
415
|
+
element.classList.add(className);
|
|
416
|
+
return new Promise(resolve => {
|
|
417
|
+
const animationEvent = getAnimationEvent();
|
|
418
|
+
const animationCompletedListener = () => {
|
|
419
|
+
if (remove) {
|
|
420
|
+
element.classList.remove(className);
|
|
421
|
+
}
|
|
422
|
+
element.removeEventListener(animationEvent, animationCompletedListener);
|
|
423
|
+
resolve();
|
|
424
|
+
};
|
|
425
|
+
element.addEventListener(animationEvent, animationCompletedListener);
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Removes an element from the DOM using the available remove method for that platform.
|
|
430
|
+
* @param {HTMLElement} element The element to remove.
|
|
431
|
+
*/
|
|
432
|
+
export function removeElement(element) {
|
|
433
|
+
if (element.removeNode) {
|
|
434
|
+
element.removeNode(true);
|
|
435
|
+
}
|
|
436
|
+
else if (element.remove) {
|
|
437
|
+
element.remove();
|
|
438
|
+
}
|
|
439
|
+
else {
|
|
440
|
+
element.parentNode.removeChild(element);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Returns a width string that is safe for css based on the provided input.
|
|
445
|
+
* @param {string | number} width
|
|
446
|
+
* @returns {string | undefined} A width safe for using in CSS.
|
|
447
|
+
*/
|
|
448
|
+
export function safeCssWidth(width) {
|
|
449
|
+
if (typeof width === 'string') {
|
|
450
|
+
if (width[width.length - 1] === '%') {
|
|
451
|
+
return width;
|
|
452
|
+
}
|
|
453
|
+
else if (width.slice(-2) === 'px') {
|
|
454
|
+
return width;
|
|
455
|
+
}
|
|
456
|
+
else if (Number(width) >= 0) {
|
|
457
|
+
return `${width}px`;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
else if (typeof width === 'number') {
|
|
461
|
+
if (width >= 0) {
|
|
462
|
+
return `${width}px`;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
return undefined;
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Calculates the size of an element that is not attached to the DOM.
|
|
469
|
+
* @param {HTMLElement} element The element to calc the size of.
|
|
470
|
+
* @returns {width, height} The size of the element.
|
|
471
|
+
*/
|
|
472
|
+
export function calcSizeUnattached(element) {
|
|
473
|
+
let container = document.createElement('div');
|
|
474
|
+
container.style.position = 'absolute';
|
|
475
|
+
container.style.top = '-99999px';
|
|
476
|
+
container.style.left = '-99999px';
|
|
477
|
+
container.style.visibility = 'hidden';
|
|
478
|
+
container.appendChild(element.cloneNode(true));
|
|
479
|
+
document.body.appendChild(container);
|
|
480
|
+
const size = {
|
|
481
|
+
width: container.scrollWidth,
|
|
482
|
+
height: container.scrollHeight
|
|
483
|
+
};
|
|
484
|
+
removeElement(container);
|
|
485
|
+
container = undefined;
|
|
486
|
+
return size;
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Resolves a promise when the provided element has children.
|
|
490
|
+
* @param {Element} element An element that does or will contain children.
|
|
491
|
+
*/
|
|
492
|
+
export function ensureChildren(element) {
|
|
493
|
+
if (element.children.length) {
|
|
494
|
+
return Promise.resolve();
|
|
495
|
+
}
|
|
496
|
+
return new Promise(resolve => {
|
|
497
|
+
const observer = new MutationObserver(changes => {
|
|
498
|
+
if (element.children.length) {
|
|
499
|
+
observer.disconnect();
|
|
500
|
+
resolve();
|
|
501
|
+
}
|
|
502
|
+
});
|
|
503
|
+
observer.observe(element, { childList: true });
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* Resolves a promise when the provided element has a child that matches a given selector.
|
|
508
|
+
* @param {Element} element An element that does or will contain children.
|
|
509
|
+
* @param {string} selector A CSS selector to use for finding an element.
|
|
510
|
+
*/
|
|
511
|
+
export function ensureChild(element, selector) {
|
|
512
|
+
const initialElements = deepQuerySelectorAll(element, selector);
|
|
513
|
+
if (initialElements.length) {
|
|
514
|
+
return Promise.resolve(initialElements[0]);
|
|
515
|
+
}
|
|
516
|
+
return new Promise(resolve => {
|
|
517
|
+
const observer = new MutationObserver(changes => {
|
|
518
|
+
const hasAddedNodes = changes.reduce((prev, curr) => prev + curr.addedNodes.length, 0) > 0;
|
|
519
|
+
if (hasAddedNodes) {
|
|
520
|
+
const foundElements = deepQuerySelectorAll(element, selector);
|
|
521
|
+
if (foundElements.length) {
|
|
522
|
+
observer.disconnect();
|
|
523
|
+
resolve(foundElements[0]);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
observer.observe(element, { childList: true, subtree: true });
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Resolves a promise when the provided host element has an `<input>` element child
|
|
532
|
+
* @param {HTMLElement} host An element that does or will contain children.
|
|
533
|
+
*/
|
|
534
|
+
export function ensureInputElement(host) {
|
|
535
|
+
return new Promise(resolve => {
|
|
536
|
+
const element = host.querySelector('input');
|
|
537
|
+
if (element) {
|
|
538
|
+
resolve(element);
|
|
539
|
+
}
|
|
540
|
+
const observer = new MutationObserver(changes => {
|
|
541
|
+
const hasAddedNodes = changes.reduce((prev, curr) => prev + curr.addedNodes.length, 0) > 0;
|
|
542
|
+
if (hasAddedNodes) {
|
|
543
|
+
const foundElement = host.querySelector('input');
|
|
544
|
+
if (foundElement) {
|
|
545
|
+
observer.disconnect();
|
|
546
|
+
resolve(foundElement);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
});
|
|
550
|
+
observer.observe(host, { childList: true, subtree: true });
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Walks up the tree starting a specific node and stops when the provided matcher function returns true.
|
|
555
|
+
* @param {Node} node The node to start searching from.
|
|
556
|
+
* @returns {Node | null} The closest matching ancestor node, or null if not found.
|
|
557
|
+
*/
|
|
558
|
+
export function walkUpUntil(node, matcher) {
|
|
559
|
+
let parent = node && node.parentNode;
|
|
560
|
+
while (parent) {
|
|
561
|
+
if (matcher(parent)) {
|
|
562
|
+
return parent;
|
|
563
|
+
}
|
|
564
|
+
parent = parent.parentNode;
|
|
565
|
+
}
|
|
566
|
+
return null;
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Calculates the width of a string given the provided font information.
|
|
570
|
+
*/
|
|
571
|
+
export function calculateFontWidth(value, info) {
|
|
572
|
+
const canvas = document.createElement('canvas');
|
|
573
|
+
const ctx = canvas.getContext('2d');
|
|
574
|
+
const fontSize = info ? info.fontSize : 16;
|
|
575
|
+
const fontFamily = info ? info.fontFamily : 'Roboto';
|
|
576
|
+
ctx.font = `${fontSize}px ${fontFamily}`;
|
|
577
|
+
return ctx.measureText(value).width;
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* Generates a CSS text-shadow style value based on the number of iterations and color provided.
|
|
581
|
+
* @param {number} iterations The number of iterations for how long the shadow should be.
|
|
582
|
+
* @param {string} color The color of the text shadow. Can be any CSS-safe color format. Ex. hex, rgb, rgba, hsl... etc.
|
|
583
|
+
*/
|
|
584
|
+
export function generateTextShadow(iterations, color) {
|
|
585
|
+
const shadows = [];
|
|
586
|
+
for (let i = 1; i <= iterations; i++) {
|
|
587
|
+
shadows.push(`${i}px ${i}px ${color}`);
|
|
588
|
+
}
|
|
589
|
+
return shadows.join(', ');
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* Checks if an element matches any of the provided selectors.
|
|
593
|
+
* @param {Element} el The element to match.
|
|
594
|
+
* @param {string[]} selectors The selectors to check the element against.
|
|
595
|
+
*/
|
|
596
|
+
export function matchesSelectors(el, selectors) {
|
|
597
|
+
if (el.nodeType !== Node.ELEMENT_NODE) {
|
|
598
|
+
return false;
|
|
599
|
+
}
|
|
600
|
+
if (typeof selectors === 'string') {
|
|
601
|
+
selectors = selectors.replace(/\s+/, '').split(',');
|
|
602
|
+
}
|
|
603
|
+
const matchesFn = Element.prototype.matches;
|
|
604
|
+
return selectors.some(selector => matchesFn.call(el, selector));
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* Walks the DOM tree starting at a root element and checks if any of its children
|
|
608
|
+
* match the provided selectors. Similar to the native `querySelectorAll` except
|
|
609
|
+
* that it will traverse the shadow DOM as well as slotted nodes.
|
|
610
|
+
* @param {Element} rootElement The element to start querying from.
|
|
611
|
+
* @param {string[]} selectors An array of CSS selectors.
|
|
612
|
+
* @param {boolean} [checkRootElement] True if the provided root element is to be matched against the selectors.
|
|
613
|
+
*/
|
|
614
|
+
export function deepQuerySelectorAll(rootElement, selectors, checkRootElement = false) {
|
|
615
|
+
let nodes = [];
|
|
616
|
+
if (!rootElement) {
|
|
617
|
+
return nodes;
|
|
618
|
+
}
|
|
619
|
+
if (typeof selectors === 'string') {
|
|
620
|
+
selectors = selectors.replace(/\s+/, '').split(',');
|
|
621
|
+
}
|
|
622
|
+
if (checkRootElement && matchesSelectors(rootElement, selectors) && nodes.indexOf(rootElement) === -1) {
|
|
623
|
+
nodes.push(rootElement);
|
|
624
|
+
}
|
|
625
|
+
if (rootElement.tagName === 'SLOT') {
|
|
626
|
+
const slotNodes = rootElement.assignedNodes();
|
|
627
|
+
slotNodes.forEach(slottedNode => nodes = nodes.concat(deepQuerySelectorAll(slottedNode, selectors, true)));
|
|
628
|
+
}
|
|
629
|
+
else {
|
|
630
|
+
let node = rootElement.shadowRoot ? rootElement.shadowRoot.firstElementChild : rootElement.firstElementChild;
|
|
631
|
+
while (node) {
|
|
632
|
+
nodes = nodes.concat(deepQuerySelectorAll(node, selectors, true));
|
|
633
|
+
node = node.nextElementSibling;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
return nodes;
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Gets the currently focused element within the document by also traversing shadow roots.
|
|
640
|
+
* @returns {Element}
|
|
641
|
+
*/
|
|
642
|
+
export function getActiveElement() {
|
|
643
|
+
const activeElement = document.activeElement;
|
|
644
|
+
if (!activeElement || activeElement === document.body) {
|
|
645
|
+
return activeElement;
|
|
646
|
+
}
|
|
647
|
+
return getActiveShadowElement(activeElement);
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* Gets the active element within the provided elements shadow root. If the element
|
|
651
|
+
* does not have a shadow root, the provided element is returned.
|
|
652
|
+
* @param {Element} element The active element.
|
|
653
|
+
*/
|
|
654
|
+
export function getActiveShadowElement(element) {
|
|
655
|
+
if (element.shadowRoot && element.shadowRoot.activeElement) {
|
|
656
|
+
element = getActiveShadowElement(element.shadowRoot.activeElement);
|
|
657
|
+
}
|
|
658
|
+
return element;
|
|
659
|
+
}
|
|
660
|
+
/** Toggles a CSS class (or classes) on an element based on a boolean. */
|
|
661
|
+
export function toggleClass(el, hasClass, className) {
|
|
662
|
+
if (hasClass) {
|
|
663
|
+
addClass(className, el);
|
|
664
|
+
}
|
|
665
|
+
else {
|
|
666
|
+
removeClass(className, el);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
/** Toggles a value-less attribute on an element. */
|
|
670
|
+
export function toggleAttribute(el, hasAttribute, name, value = '') {
|
|
671
|
+
if (hasAttribute) {
|
|
672
|
+
el.setAttribute(name, value);
|
|
673
|
+
}
|
|
674
|
+
else {
|
|
675
|
+
el.removeAttribute(name);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
/** Toggles part of an attribute on an element. */
|
|
679
|
+
export function toggleOnAttribute(el, attribute, value, force) {
|
|
680
|
+
const oldValue = el.getAttribute(attribute);
|
|
681
|
+
if ((force === undefined || force === true) && (!oldValue || !oldValue.includes(value))) {
|
|
682
|
+
appendToAttribute(el, attribute, value);
|
|
683
|
+
}
|
|
684
|
+
else if (!force) {
|
|
685
|
+
removeFromAttribute(el, attribute, value);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
/** Appends a value to an attribute on an element, first setting it if it doesn't exist. */
|
|
689
|
+
export function appendToAttribute(el, attribute, value) {
|
|
690
|
+
const oldValue = el.getAttribute(attribute);
|
|
691
|
+
if (!oldValue || !oldValue.length) {
|
|
692
|
+
el.setAttribute(attribute, value);
|
|
693
|
+
}
|
|
694
|
+
else {
|
|
695
|
+
el.setAttribute(attribute, `${oldValue} ${value}`);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
/** Removes a value from an attribute on an element, removing the attribute if empty. */
|
|
699
|
+
export function removeFromAttribute(el, attribute, value) {
|
|
700
|
+
if (!el.hasAttribute(attribute)) {
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
703
|
+
const oldValue = el.getAttribute(attribute);
|
|
704
|
+
if (oldValue) {
|
|
705
|
+
let newValue = oldValue === null || oldValue === void 0 ? void 0 : oldValue.replace(value, '');
|
|
706
|
+
newValue = newValue.replace(/\s+/g, ' ').trim();
|
|
707
|
+
if (newValue.length) {
|
|
708
|
+
el.setAttribute(attribute, newValue);
|
|
709
|
+
}
|
|
710
|
+
else {
|
|
711
|
+
el.removeAttribute(attribute);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
/**
|
|
716
|
+
* Attempts to scroll a target element into view within a scrollable parent element, unless already visible within the container.
|
|
717
|
+
* @param scrollElement The scrollable parent element.
|
|
718
|
+
* @param targetElement The element to scroll into view.
|
|
719
|
+
* @param behavior The scroll behavior. Defaults to 'auto'.
|
|
720
|
+
* @param block The block position to anchor the target element to within the scroll element.
|
|
721
|
+
*/
|
|
722
|
+
export function tryScrollIntoView(scrollElement, targetElement, behavior = 'auto', block = 'nearest') {
|
|
723
|
+
if (!scrollElement) {
|
|
724
|
+
return;
|
|
725
|
+
}
|
|
726
|
+
const canScroll = scrollElement.scrollHeight > scrollElement.clientHeight || scrollElement.scrollWidth > scrollElement.clientWidth;
|
|
727
|
+
if (canScroll) {
|
|
728
|
+
const offsetRect = offset(targetElement, scrollElement);
|
|
729
|
+
const isClippedTop = offsetRect.top <= targetElement.clientHeight;
|
|
730
|
+
const isClippedBottom = offsetRect.bottom <= targetElement.clientHeight;
|
|
731
|
+
if (isClippedTop || isClippedBottom) {
|
|
732
|
+
const top = calcBlockScroll(block, isClippedTop, targetElement.offsetTop, targetElement.clientHeight, scrollElement.offsetTop, scrollElement.offsetHeight);
|
|
733
|
+
scrollElement.scrollTo({ top, behavior });
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
const isClippedLeft = offsetRect.left <= targetElement.clientWidth;
|
|
737
|
+
const isClippedRight = offsetRect.right <= targetElement.clientWidth;
|
|
738
|
+
if (isClippedLeft || isClippedRight) {
|
|
739
|
+
const left = calcBlockScroll(block, isClippedLeft, targetElement.offsetLeft, targetElement.clientWidth, scrollElement.offsetLeft, scrollElement.offsetWidth);
|
|
740
|
+
scrollElement.scrollTo({ left, behavior });
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
/** Calculates the block anchor position for a target element within a scrollable parent element. */
|
|
745
|
+
export function calcBlockScroll(block, isClippedStart, targetOffset, targetSize, scrollOffset, scrollSize) {
|
|
746
|
+
if (block === 'nearest') {
|
|
747
|
+
if (isClippedStart) {
|
|
748
|
+
return (targetOffset - scrollOffset) - targetSize;
|
|
749
|
+
}
|
|
750
|
+
return (targetOffset - scrollSize) + targetSize * 2;
|
|
751
|
+
}
|
|
752
|
+
return targetOffset - scrollOffset - scrollSize / 2 + targetSize / 2;
|
|
753
|
+
}
|
|
754
|
+
/**
|
|
755
|
+
* Creates an element from an HTML string.
|
|
756
|
+
*/
|
|
757
|
+
export function elementFromHTML(html) {
|
|
758
|
+
const template = document.createElement('template');
|
|
759
|
+
html = html.trim();
|
|
760
|
+
template.innerHTML = html;
|
|
761
|
+
return template.content.firstElementChild;
|
|
762
|
+
}
|
|
763
|
+
/**
|
|
764
|
+
* Observes changes to the provided attributes on a target element and executes a provided callback when changed.
|
|
765
|
+
* @param element The element to observe.
|
|
766
|
+
* @param listener The callback to execute when an attribute changes on the element.
|
|
767
|
+
* @param attributeFilter The attributes to observe.
|
|
768
|
+
* @returns A `MutationObserver` instasnce.
|
|
769
|
+
*/
|
|
770
|
+
export function createElementAttributeObserver(element, listener, attributeFilter) {
|
|
771
|
+
const observer = new MutationObserver(mutations => {
|
|
772
|
+
for (const mutation of mutations) {
|
|
773
|
+
if (mutation.attributeName) {
|
|
774
|
+
listener(mutation.attributeName, element.getAttribute(mutation.attributeName));
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
});
|
|
778
|
+
observer.observe(element, { attributes: true, attributeFilter });
|
|
779
|
+
return observer;
|
|
780
|
+
}
|