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(new CustomEvent('WebComponentsReady', { bubbles: true }));
|
51 | }
|
52 |
|
53 | function batchCustomElements() {
|
54 | if (window.customElements && customElements.polyfillWrapFlushCallback) {
|
55 | customElements.polyfillWrapFlushCallback(function (flushCallback) {
|
56 | flushFn = flushCallback;
|
57 | if (allowUpgrades) {
|
58 | flushFn();
|
59 | }
|
60 | });
|
61 | }
|
62 | }
|
63 |
|
64 | function asyncReady() {
|
65 | batchCustomElements();
|
66 | ready();
|
67 | }
|
68 |
|
69 | function ready() {
|
70 |
|
71 | if (window.HTMLTemplateElement && HTMLTemplateElement.bootstrap) {
|
72 | HTMLTemplateElement.bootstrap(window.document);
|
73 | }
|
74 | polyfillsLoaded = true;
|
75 | runWhenLoadedFns().then(fireEvent);
|
76 | }
|
77 |
|
78 | function runWhenLoadedFns() {
|
79 | allowUpgrades = false;
|
80 | var fnsMap = whenLoadedFns.map(function(fn) {
|
81 | return fn instanceof Function ? fn() : fn;
|
82 | });
|
83 | whenLoadedFns = [];
|
84 | return Promise.all(fnsMap).then(function() {
|
85 | allowUpgrades = true;
|
86 | flushFn && flushFn();
|
87 | }).catch(function(err) {
|
88 | console.error(err);
|
89 | });
|
90 | }
|
91 |
|
92 | window.WebComponents = window.WebComponents || {};
|
93 | window.WebComponents.ready = window.WebComponents.ready || false;
|
94 | window.WebComponents.waitFor = window.WebComponents.waitFor || function(waitFn) {
|
95 | if (!waitFn) {
|
96 | return;
|
97 | }
|
98 | whenLoadedFns.push(waitFn);
|
99 | if (polyfillsLoaded) {
|
100 | runWhenLoadedFns();
|
101 | }
|
102 | };
|
103 | window.WebComponents._batchCustomElements = batchCustomElements;
|
104 |
|
105 | var name = 'webcomponents-loader.js';
|
106 |
|
107 | var polyfills = [];
|
108 | if (!('attachShadow' in Element.prototype && 'getRootNode' in Element.prototype) ||
|
109 | (window.ShadyDOM && window.ShadyDOM.force)) {
|
110 | polyfills.push('sd');
|
111 | }
|
112 | if (!window.customElements || window.customElements.forcePolyfill) {
|
113 | polyfills.push('ce');
|
114 | }
|
115 |
|
116 | var needsTemplate = (function() {
|
117 |
|
118 | var t = document.createElement('template');
|
119 | if (!('content' in t)) {
|
120 | return true;
|
121 | }
|
122 |
|
123 | if (!(t.content.cloneNode() instanceof DocumentFragment)) {
|
124 | return true;
|
125 | }
|
126 |
|
127 | var t2 = document.createElement('template');
|
128 | t2.content.appendChild(document.createElement('div'));
|
129 | t.content.appendChild(t2);
|
130 | var clone = t.cloneNode(true);
|
131 | return (clone.content.childNodes.length === 0 ||
|
132 | clone.content.firstChild.content.childNodes.length === 0);
|
133 | })();
|
134 |
|
135 |
|
136 |
|
137 | if (!window.Promise || !Array.from || !window.URL || !window.Symbol || needsTemplate) {
|
138 | polyfills = ['sd-ce-pf'];
|
139 | }
|
140 |
|
141 | if (polyfills.length) {
|
142 | var url;
|
143 | var polyfillFile = 'bundles/webcomponents-' + polyfills.join('-') + '.js';
|
144 |
|
145 |
|
146 | if (window.WebComponents.root) {
|
147 | url = window.WebComponents.root + polyfillFile;
|
148 | } else {
|
149 | var script = document.querySelector('script[src*="' + name +'"]');
|
150 |
|
151 | url = script.src.replace(name, polyfillFile);
|
152 | }
|
153 |
|
154 | var newScript = document.createElement('script');
|
155 | newScript.src = url;
|
156 |
|
157 | if (document.readyState === 'loading') {
|
158 |
|
159 | newScript.setAttribute('onload', 'window.WebComponents._batchCustomElements()');
|
160 | document.write(newScript.outerHTML);
|
161 | document.addEventListener('DOMContentLoaded', ready);
|
162 | } else {
|
163 | newScript.addEventListener('load', function () {
|
164 | asyncReady();
|
165 | });
|
166 | newScript.addEventListener('error', function () {
|
167 | throw new Error('Could not load polyfill bundle' + url);
|
168 | });
|
169 | document.head.appendChild(newScript);
|
170 | }
|
171 | } else {
|
172 |
|
173 | if (document.readyState === 'complete') {
|
174 | polyfillsLoaded = true;
|
175 | fireEvent();
|
176 | } else {
|
177 |
|
178 | window.addEventListener('load', ready);
|
179 | window.addEventListener('DOMContentLoaded', function() {
|
180 | window.removeEventListener('load', ready);
|
181 | ready();
|
182 | })
|
183 | }
|
184 | }
|
185 | })();
|