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