1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | (function () {
|
12 | 'use strict';
|
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 | var polyfillsLoaded = false;
|
44 | var whenLoadedFns = [];
|
45 | var allowUpgrades = false;
|
46 | var flushFn;
|
47 |
|
48 | function fireEvent() {
|
49 | window.WebComponents.ready = true;
|
50 | document.dispatchEvent(
|
51 | new CustomEvent('WebComponentsReady', {bubbles: true})
|
52 | );
|
53 | }
|
54 |
|
55 | function batchCustomElements() {
|
56 | if (window.customElements && customElements.polyfillWrapFlushCallback) {
|
57 | customElements.polyfillWrapFlushCallback(function (flushCallback) {
|
58 | flushFn = flushCallback;
|
59 | if (allowUpgrades) {
|
60 | flushFn();
|
61 | }
|
62 | });
|
63 | }
|
64 | }
|
65 |
|
66 | function asyncReady() {
|
67 | batchCustomElements();
|
68 | ready();
|
69 | }
|
70 |
|
71 | function ready() {
|
72 |
|
73 | if (window.HTMLTemplateElement && HTMLTemplateElement.bootstrap) {
|
74 | HTMLTemplateElement.bootstrap(window.document);
|
75 | }
|
76 | polyfillsLoaded = true;
|
77 | runWhenLoadedFns().then(fireEvent);
|
78 | }
|
79 |
|
80 | function runWhenLoadedFns() {
|
81 | allowUpgrades = false;
|
82 | var fnsMap = whenLoadedFns.map(function (fn) {
|
83 | return fn instanceof Function ? fn() : fn;
|
84 | });
|
85 | whenLoadedFns = [];
|
86 | return Promise.all(fnsMap)
|
87 | .then(function () {
|
88 | allowUpgrades = true;
|
89 | flushFn && flushFn();
|
90 | })
|
91 | .catch(function (err) {
|
92 | console.error(err);
|
93 | });
|
94 | }
|
95 |
|
96 | window.WebComponents = window.WebComponents || {};
|
97 | window.WebComponents.ready = window.WebComponents.ready || false;
|
98 | window.WebComponents.waitFor =
|
99 | window.WebComponents.waitFor ||
|
100 | function (waitFn) {
|
101 | if (!waitFn) {
|
102 | return;
|
103 | }
|
104 | whenLoadedFns.push(waitFn);
|
105 | if (polyfillsLoaded) {
|
106 | runWhenLoadedFns();
|
107 | }
|
108 | };
|
109 | window.WebComponents._batchCustomElements = batchCustomElements;
|
110 |
|
111 | var name = 'webcomponents-loader.js';
|
112 |
|
113 | var polyfills = [];
|
114 | if (
|
115 | !(
|
116 | 'attachShadow' in Element.prototype && 'getRootNode' in Element.prototype
|
117 | ) ||
|
118 | (window.ShadyDOM && window.ShadyDOM.force)
|
119 | ) {
|
120 | polyfills.push('sd');
|
121 | }
|
122 | if (!window.customElements || window.customElements.forcePolyfill) {
|
123 | polyfills.push('ce');
|
124 | }
|
125 |
|
126 | var needsTemplate = (function () {
|
127 |
|
128 | var t = document.createElement('template');
|
129 | if (!('content' in t)) {
|
130 | return true;
|
131 | }
|
132 |
|
133 | if (!(t.content.cloneNode() instanceof DocumentFragment)) {
|
134 | return true;
|
135 | }
|
136 |
|
137 | var t2 = document.createElement('template');
|
138 | t2.content.appendChild(document.createElement('div'));
|
139 | t.content.appendChild(t2);
|
140 | var clone = t.cloneNode(true);
|
141 | return (
|
142 | clone.content.childNodes.length === 0 ||
|
143 | clone.content.firstChild.content.childNodes.length === 0
|
144 | );
|
145 | })();
|
146 |
|
147 |
|
148 |
|
149 | if (
|
150 | !window.Promise ||
|
151 | !Array.from ||
|
152 | !window.URL ||
|
153 | !window.Symbol ||
|
154 | needsTemplate
|
155 | ) {
|
156 | polyfills = ['sd-ce-pf'];
|
157 | }
|
158 |
|
159 | if (polyfills.length) {
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | var policy = (function () {
|
172 | var identity = function (x) {
|
173 | return x;
|
174 | };
|
175 | var policyOptions = {
|
176 | createHTML: identity,
|
177 | createScript: identity,
|
178 | createScriptURL: identity,
|
179 | };
|
180 | var policy =
|
181 | window.trustedTypes &&
|
182 | window.trustedTypes.createPolicy('webcomponents-loader', policyOptions);
|
183 | return policy || policyOptions;
|
184 | })();
|
185 |
|
186 | var url;
|
187 | var polyfillFile = 'bundles/webcomponents-' + polyfills.join('-') + '.js';
|
188 |
|
189 |
|
190 | if (window.WebComponents.root) {
|
191 | url = window.WebComponents.root + polyfillFile;
|
192 | if (
|
193 | window.trustedTypes &&
|
194 | window.trustedTypes.isScriptURL(window.WebComponents.root)
|
195 | ) {
|
196 | url = policy.createScriptURL(url);
|
197 | }
|
198 | } else {
|
199 | var script = document.querySelector('script[src*="' + name + '"]');
|
200 |
|
201 | url = policy.createScriptURL(script.src.replace(name, polyfillFile));
|
202 | }
|
203 |
|
204 | var newScript = document.createElement('script');
|
205 | newScript.src = url;
|
206 |
|
207 | if (document.readyState === 'loading') {
|
208 |
|
209 | newScript.setAttribute(
|
210 | 'onload',
|
211 | policy.createScript('window.WebComponents._batchCustomElements()')
|
212 | );
|
213 | document.write(policy.createHTML(newScript.outerHTML));
|
214 | document.addEventListener('DOMContentLoaded', ready);
|
215 | } else {
|
216 | newScript.addEventListener('load', function () {
|
217 | asyncReady();
|
218 | });
|
219 | newScript.addEventListener('error', function () {
|
220 | throw new Error('Could not load polyfill bundle' + url);
|
221 | });
|
222 | document.head.appendChild(newScript);
|
223 | }
|
224 | } else {
|
225 |
|
226 | if (document.readyState === 'complete') {
|
227 | polyfillsLoaded = true;
|
228 | fireEvent();
|
229 | } else {
|
230 |
|
231 | window.addEventListener('load', ready);
|
232 | window.addEventListener('DOMContentLoaded', function () {
|
233 | window.removeEventListener('load', ready);
|
234 | ready();
|
235 | });
|
236 | }
|
237 | }
|
238 | })();
|