@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
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Forge Core
|
|
2
|
+
|
|
3
|
+
This library contains the core building blocks and utilities that are used when building Forge Web Components.
|
|
4
|
+
|
|
5
|
+
This package is intended to be used directly with projects built using the Forge CLI, but there are many useful
|
|
6
|
+
generic utilities that can be used directly within any web application.
|
|
7
|
+
|
|
8
|
+
## Usage
|
|
9
|
+
|
|
10
|
+
```shell
|
|
11
|
+
$ npm install @tylertech/forge-core
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Development
|
|
15
|
+
|
|
16
|
+
Install dependencies:
|
|
17
|
+
|
|
18
|
+
```shell
|
|
19
|
+
$ npm install
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Build npm package:
|
|
23
|
+
|
|
24
|
+
```shell
|
|
25
|
+
$ npm run build
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Run tests:
|
|
29
|
+
|
|
30
|
+
```shell
|
|
31
|
+
$ npm test
|
|
32
|
+
```
|
package/esm/a11y/a11y.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hides an element from the user visually, while keeping it in the DOM.
|
|
3
|
+
* @param element The element to hide from view.
|
|
4
|
+
*/
|
|
5
|
+
export function hideElementVisually(element) {
|
|
6
|
+
element.style.border = '0';
|
|
7
|
+
element.style.clip = 'rect(0 0 0 0)';
|
|
8
|
+
element.style.height = '1px';
|
|
9
|
+
element.style.margin = '-1px';
|
|
10
|
+
element.style.overflow = 'hidden';
|
|
11
|
+
element.style.padding = '0';
|
|
12
|
+
element.style.position = 'absolute';
|
|
13
|
+
element.style.width = '1px';
|
|
14
|
+
element.style.outline = '0';
|
|
15
|
+
element.style.setProperty('-webkit-appearance', 'none');
|
|
16
|
+
element.style.setProperty('-moz-appearance', 'none');
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './a11y';
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const days = [
|
|
2
|
+
'Su',
|
|
3
|
+
'Mo',
|
|
4
|
+
'Tu',
|
|
5
|
+
'We',
|
|
6
|
+
'Th',
|
|
7
|
+
'Fr',
|
|
8
|
+
'Sa'
|
|
9
|
+
];
|
|
10
|
+
const daysLong = [
|
|
11
|
+
'Sunday',
|
|
12
|
+
'Monday',
|
|
13
|
+
'Tuesday',
|
|
14
|
+
'Wedensday',
|
|
15
|
+
'Thursday',
|
|
16
|
+
'Friday',
|
|
17
|
+
'Saturday'
|
|
18
|
+
];
|
|
19
|
+
const months = [
|
|
20
|
+
'Jan',
|
|
21
|
+
'Feb',
|
|
22
|
+
'Mar',
|
|
23
|
+
'Apr',
|
|
24
|
+
'May',
|
|
25
|
+
'Jun',
|
|
26
|
+
'Jul',
|
|
27
|
+
'Aug',
|
|
28
|
+
'Sep',
|
|
29
|
+
'Oct',
|
|
30
|
+
'Nov',
|
|
31
|
+
'Dec'
|
|
32
|
+
];
|
|
33
|
+
const monthsLong = [
|
|
34
|
+
'January',
|
|
35
|
+
'February',
|
|
36
|
+
'March',
|
|
37
|
+
'April',
|
|
38
|
+
'May',
|
|
39
|
+
'June',
|
|
40
|
+
'July',
|
|
41
|
+
'August',
|
|
42
|
+
'September',
|
|
43
|
+
'October',
|
|
44
|
+
'November',
|
|
45
|
+
'December'
|
|
46
|
+
];
|
|
47
|
+
export const DATE_CONSTANTS = {
|
|
48
|
+
days,
|
|
49
|
+
daysLong,
|
|
50
|
+
months,
|
|
51
|
+
monthsLong
|
|
52
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './date-constants';
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { replaceElement, isArray, removeAllChildren, walkUpUntil } from '../utils';
|
|
2
|
+
/**
|
|
3
|
+
* Recursively defines a component as a custom elements and all of its dependencies.
|
|
4
|
+
* @param component The component to import.
|
|
5
|
+
*/
|
|
6
|
+
export function defineCustomElement(component) {
|
|
7
|
+
tryDefine(component._customElementName, component);
|
|
8
|
+
if (isArray(component._customElementDependencies)) {
|
|
9
|
+
defineCustomElements(component._customElementDependencies);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Defines the specified custom element components.
|
|
14
|
+
* @param {any[]} components The components to register.
|
|
15
|
+
*/
|
|
16
|
+
export function defineCustomElements(components) {
|
|
17
|
+
components.forEach(defineCustomElement);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Attempts to define the provided custom element name/constructor if not already defined.
|
|
21
|
+
* @param name The name of the custom element to define.
|
|
22
|
+
* @param ctor The custom element constructor.
|
|
23
|
+
*/
|
|
24
|
+
export function tryDefine(name, ctor, options) {
|
|
25
|
+
var _a;
|
|
26
|
+
if ((_a = window === null || window === void 0 ? void 0 : window.customElements) === null || _a === void 0 ? void 0 : _a.get(name)) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
window.customElements.define(name, ctor, options);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Useful when capturing the value of a unupgraded component during the `connectedCallback` upon upgrade.
|
|
33
|
+
*
|
|
34
|
+
* More information here:
|
|
35
|
+
* https://developers.google.com/web/fundamentals/architecture/building-components/best-practices#lazy-properties
|
|
36
|
+
*
|
|
37
|
+
* @param property
|
|
38
|
+
*/
|
|
39
|
+
export function upgradeProperty(instance, property) {
|
|
40
|
+
if (instance.hasOwnProperty(property)) {
|
|
41
|
+
const value = instance[property];
|
|
42
|
+
delete instance[property];
|
|
43
|
+
instance[property] = value;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Traverses up the DOM tree starting from the provided component element to find the specified parent.
|
|
48
|
+
* @param {HTMLElement} component The starting HTMLElement.
|
|
49
|
+
* @param {string} parentTagName The parent tag name we are searching for.
|
|
50
|
+
*/
|
|
51
|
+
export function requireParent(component, parentTagName) {
|
|
52
|
+
let el = component;
|
|
53
|
+
while (el.parentNode) {
|
|
54
|
+
el = el.parentNode;
|
|
55
|
+
if (!el.tagName) {
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
if (!el.tagName || el.tagName.toLowerCase() === parentTagName.toLowerCase()) {
|
|
59
|
+
return el;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Creates a template element from a string.
|
|
66
|
+
* @param template The template HTML string.
|
|
67
|
+
*/
|
|
68
|
+
export function parseTemplateString(template) {
|
|
69
|
+
const templateDocument = new DOMParser().parseFromString(template, 'text/html');
|
|
70
|
+
return templateDocument.querySelector('template');
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Attaches a template to the given web component instance light DOM.
|
|
74
|
+
* @param {T} componentInstance A component instance.
|
|
75
|
+
* @param {string} template The template HTML string.
|
|
76
|
+
*/
|
|
77
|
+
export function attachLightTemplate(componentInstance, template) {
|
|
78
|
+
componentInstance.appendChild(parseTemplateString(template).content.cloneNode(true));
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Attaches a shadow root to the given web component instance.
|
|
82
|
+
* @param {T} componentInstance A component instance.
|
|
83
|
+
* @param {string} elementName The name of the element the shadow root is to be attached to.
|
|
84
|
+
* @param {string} template The shadow root template HTML string.
|
|
85
|
+
* @param {string | string[]} styles The shadow root styles string to be encapsulated by this shadow root.
|
|
86
|
+
* @param {boolean} [delegatesFocus=false] Should the component delagate focus.
|
|
87
|
+
*/
|
|
88
|
+
export function attachShadowTemplate(componentInstance, template, styles, delegatesFocus = false) {
|
|
89
|
+
const templateElement = prepareShadowTemplate(template, styles);
|
|
90
|
+
componentInstance.attachShadow({ mode: 'open', delegatesFocus });
|
|
91
|
+
setShadowTemplate(componentInstance, templateElement);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Replaces the template of an existing shadow root with the provided template.
|
|
95
|
+
* @param {T} componentInstance A component instance.
|
|
96
|
+
* @param {string} elementName The name of the element the shadow root is to be attached to.
|
|
97
|
+
* @param {string} template The shadow root template HTML string.
|
|
98
|
+
* @param {string | string[]} styles The shadow root styles string to be encapsulated by this shadow root.
|
|
99
|
+
*/
|
|
100
|
+
export function replaceShadowTemplate(componentInstance, template, styles) {
|
|
101
|
+
if (!componentInstance.shadowRoot) {
|
|
102
|
+
throw new Error('This element does not contain a shadow root. Did you mean to call `attachShadowTemplate`?');
|
|
103
|
+
}
|
|
104
|
+
const templateElement = prepareShadowTemplate(template, styles);
|
|
105
|
+
if (componentInstance.shadowRoot.children.length) {
|
|
106
|
+
removeAllChildren(componentInstance.shadowRoot);
|
|
107
|
+
}
|
|
108
|
+
setShadowTemplate(componentInstance, templateElement);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Creates and prepares an HTML template element for rendering within a shadow root.
|
|
112
|
+
* @param {string} elementName The name of the element the shadow root is to be attached to.
|
|
113
|
+
* @param {string} template The shadow root template HTML string.
|
|
114
|
+
* @param {string | string[]} styles The shadow root styles string to be encapsulated by this shadow root.
|
|
115
|
+
*/
|
|
116
|
+
export function prepareShadowTemplate(template, styles) {
|
|
117
|
+
const templateElement = parseTemplateString(template);
|
|
118
|
+
if (styles) {
|
|
119
|
+
styles = styles instanceof Array ? styles : [styles];
|
|
120
|
+
const styleElement = document.createElement('style');
|
|
121
|
+
styleElement.type = 'text/css';
|
|
122
|
+
styleElement.textContent = styles.join(' ');
|
|
123
|
+
templateElement.content.appendChild(styleElement);
|
|
124
|
+
}
|
|
125
|
+
return templateElement;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Appends a template to the provided components shadow root.
|
|
129
|
+
* @param {T} componentInstance A component instance.
|
|
130
|
+
* @param {HTMLTemplateElement} templateElement A template element to be cloned.
|
|
131
|
+
*/
|
|
132
|
+
export function setShadowTemplate(componentInstance, templateElement) {
|
|
133
|
+
componentInstance.shadowRoot.appendChild(templateElement.content.cloneNode(true));
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Copies style rules from the provided document stylesheets collection to the provided shadow root stylesheet.
|
|
137
|
+
* @param {Document} fromDocument The document to find the style sheets in.
|
|
138
|
+
* @param {ShadowRoot} shadowRoot The shadow root that contains the stylesheet to copy the rules to.
|
|
139
|
+
* @param {IStyleSheetDescriptor[]} styleSheetDescriptors A collection of style sheet predicates.
|
|
140
|
+
* @param {CSSStyleSheet} shadowStyleSheet The shadow root stylesheet to copy the style rules to.
|
|
141
|
+
*/
|
|
142
|
+
export function provideDocumentStyles(fromDocument, shadowRoot, documentStyleSheets, shadowStyleSheet) {
|
|
143
|
+
if (!shadowStyleSheet) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const documentSheets = [];
|
|
147
|
+
documentStyleSheets.forEach(sheet => {
|
|
148
|
+
const sheetName = typeof sheet === 'string' ? sheet : sheet.name;
|
|
149
|
+
const sheetFilter = sheet.selectorFilter;
|
|
150
|
+
const matchingStyleSheet = _findMatchingStyleSheet(fromDocument.styleSheets, sheetName);
|
|
151
|
+
if (!matchingStyleSheet) {
|
|
152
|
+
throw new Error(`Could not find stylesheet: ${sheetName}`);
|
|
153
|
+
}
|
|
154
|
+
let startIndex = shadowStyleSheet.cssRules.length;
|
|
155
|
+
for (const rule in matchingStyleSheet.cssRules) {
|
|
156
|
+
if (matchingStyleSheet.cssRules.hasOwnProperty(rule) && matchingStyleSheet.cssRules[rule].cssText && (!sheetFilter || new RegExp(sheetFilter).test(matchingStyleSheet.cssRules[rule].selectorText))) {
|
|
157
|
+
shadowStyleSheet.insertRule(matchingStyleSheet.cssRules[rule].cssText, startIndex++);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Finds a stylesheet by name in the provided stylesheet list.
|
|
164
|
+
* @param styleSheetList The stylesheet list to search.
|
|
165
|
+
* @param sheetName The stylesheet name to find.
|
|
166
|
+
* @returns {CSSStyleSheet | undefined}
|
|
167
|
+
*/
|
|
168
|
+
function _findMatchingStyleSheet(styleSheetList, sheetName) {
|
|
169
|
+
for (const prop in styleSheetList) {
|
|
170
|
+
if (styleSheetList.hasOwnProperty(prop) && styleSheetList[prop].href) {
|
|
171
|
+
if (new RegExp(sheetName).test(styleSheetList[prop].href)) {
|
|
172
|
+
return styleSheetList[prop];
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return undefined;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Gets an HTML element using a query selector from the provided components` shadow root.
|
|
180
|
+
* @param {HTMLElement} componentInstance The component instance that contains a shadow root.
|
|
181
|
+
* @param {string} selector The selector to be passed to `querySelector`.
|
|
182
|
+
*/
|
|
183
|
+
export function getShadowElement(componentInstance, selector) {
|
|
184
|
+
return componentInstance.shadowRoot.querySelector(selector);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Gets an HTML element using a query selector from the provided components` light DOM.
|
|
188
|
+
* @param {HTMLElement} componentInstance The component instance.
|
|
189
|
+
* @param {string} selector The selector to be passed to `querySelector`.
|
|
190
|
+
*/
|
|
191
|
+
export function getLightElement(componentInstance, selector) {
|
|
192
|
+
return componentInstance.querySelector(selector);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Creates and dispatches a cross-browser `CustomEvent` with the provided type and data.
|
|
196
|
+
* @param {string} type
|
|
197
|
+
* @param {any} data
|
|
198
|
+
* @param {boolean=} bubble
|
|
199
|
+
*/
|
|
200
|
+
export function emitEvent(component, type, data, bubble = true, cancelable = false) {
|
|
201
|
+
let evt;
|
|
202
|
+
if (typeof CustomEvent === 'function') {
|
|
203
|
+
evt = new CustomEvent(type, {
|
|
204
|
+
detail: data,
|
|
205
|
+
bubbles: bubble,
|
|
206
|
+
cancelable
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
evt = document.createEvent('CustomEvent');
|
|
211
|
+
evt.initCustomEvent(type, bubble, cancelable, data);
|
|
212
|
+
}
|
|
213
|
+
return component.dispatchEvent(evt);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Replaces the provided element with a placeholder comment and vice versa.
|
|
217
|
+
* Useful for hiding and showing elements while retaining their location in the DOM.
|
|
218
|
+
* @param {boolean} isVisible Whether the element is visible or not.
|
|
219
|
+
* @param {string} elementName The element tag name.
|
|
220
|
+
* @param {string} selector The selector used to find the element
|
|
221
|
+
* @param {Node} element The element
|
|
222
|
+
* @param {Comment} placeholder The existing placeholder
|
|
223
|
+
*/
|
|
224
|
+
export function toggleElementPlaceholder(component, isVisible, elementName, selector, element, placeholder) {
|
|
225
|
+
const exists = !!getShadowElement(component, selector);
|
|
226
|
+
if (!placeholder) {
|
|
227
|
+
placeholder = document.createComment(`(${elementName}) ${selector}`);
|
|
228
|
+
}
|
|
229
|
+
if (isVisible && !exists) {
|
|
230
|
+
replaceElement(element, placeholder);
|
|
231
|
+
}
|
|
232
|
+
else if (!isVisible && exists) {
|
|
233
|
+
replaceElement(placeholder, element);
|
|
234
|
+
}
|
|
235
|
+
return placeholder;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Walks up the tree starting a specific node and stops when it finds a shadow root.
|
|
239
|
+
* @param {Node} node The node to start searching from.
|
|
240
|
+
* @returns {ShadowRoot | null} The closest shadow root ancestor, or null if not inside a shadow root.
|
|
241
|
+
*/
|
|
242
|
+
export function getClosestShadowRoot(node) {
|
|
243
|
+
return walkUpUntil(node, current => current.toString() === '[object ShadowRoot]');
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Finds the closest element up the tree from a starting element across shadow boundaries.
|
|
247
|
+
* @param selector The CSS selector for the element to find.
|
|
248
|
+
* @param startElement The element to start finding from.
|
|
249
|
+
*/
|
|
250
|
+
export function closestElement(selector, startElement) {
|
|
251
|
+
function __closestFrom(el) {
|
|
252
|
+
if (!el || el === document || el === window) {
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
if (el.assignedSlot) {
|
|
256
|
+
el = el.assignedSlot;
|
|
257
|
+
}
|
|
258
|
+
const found = el.closest(selector);
|
|
259
|
+
return found || __closestFrom(el.getRootNode().host);
|
|
260
|
+
}
|
|
261
|
+
return __closestFrom(startElement);
|
|
262
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { isFunction } from '../../utils';
|
|
2
|
+
import { defineCustomElement } from '../component-utils';
|
|
3
|
+
export const CUSTOM_ELEMENT_NAME_PROPERTY = '_customElementName';
|
|
4
|
+
export const CUSTOM_ELEMENT_DEPENDENCIES_PROPERTY = '_customElementDependencies';
|
|
5
|
+
/**
|
|
6
|
+
* This decorator is intended to be used on classes that extend `HTMLElement` to
|
|
7
|
+
* extend/modify the behavior of a custom element.
|
|
8
|
+
* @param {ICustomElementConfig} [config={}] The custom element configuration.
|
|
9
|
+
*/
|
|
10
|
+
export function CustomElement({ name, dependencies, define = true }) {
|
|
11
|
+
return (ctor) => {
|
|
12
|
+
patchConnectedCallback(ctor);
|
|
13
|
+
if (name) {
|
|
14
|
+
ctor[CUSTOM_ELEMENT_NAME_PROPERTY] = name;
|
|
15
|
+
}
|
|
16
|
+
if (dependencies && dependencies.length) {
|
|
17
|
+
ctor[CUSTOM_ELEMENT_DEPENDENCIES_PROPERTY] = dependencies;
|
|
18
|
+
}
|
|
19
|
+
if (window.__forgeFlags__autoDefine !== false && define) {
|
|
20
|
+
defineCustomElement(ctor);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function patchConnectedCallback(ctor) {
|
|
25
|
+
const originalConnectedCallback = ctor.prototype.connectedCallback;
|
|
26
|
+
ctor.prototype.connectedCallback = function () {
|
|
27
|
+
if (!this.isConnected) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if (!this._isInitialized) {
|
|
31
|
+
tryUpgradeOwnProperties(this);
|
|
32
|
+
if (isFunction(this.initializedCallback)) {
|
|
33
|
+
this.initializedCallback.apply(this);
|
|
34
|
+
}
|
|
35
|
+
this._isInitialized = true;
|
|
36
|
+
}
|
|
37
|
+
if (isFunction(originalConnectedCallback)) {
|
|
38
|
+
originalConnectedCallback.apply(this);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function tryUpgradeOwnProperties(instance) {
|
|
43
|
+
// We ignore our properties that start with an underscore as those are considered "internal"
|
|
44
|
+
// and are not auto-upgraded for us
|
|
45
|
+
const ownProperties = Object.getOwnPropertyNames(instance)
|
|
46
|
+
.filter(p => !p.startsWith('_'));
|
|
47
|
+
for (const property of ownProperties) {
|
|
48
|
+
const value = instance[property];
|
|
49
|
+
delete instance[property];
|
|
50
|
+
instance[property] = value;
|
|
51
|
+
}
|
|
52
|
+
}
|