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 | ready: false,
|
95 | _batchCustomElements: batchCustomElements,
|
96 | waitFor: function(waitFn) {
|
97 | if (!waitFn) {
|
98 | return;
|
99 | }
|
100 | whenLoadedFns.push(waitFn);
|
101 | if (polyfillsLoaded) {
|
102 | runWhenLoadedFns();
|
103 | }
|
104 | }
|
105 | };
|
106 |
|
107 | var name = 'webcomponents-loader.js';
|
108 |
|
109 | var polyfills = [];
|
110 | if (!('attachShadow' in Element.prototype && 'getRootNode' in Element.prototype) ||
|
111 | (window.ShadyDOM && window.ShadyDOM.force)) {
|
112 | polyfills.push('sd');
|
113 | }
|
114 | if (!window.customElements || window.customElements.forcePolyfill) {
|
115 | polyfills.push('ce');
|
116 | }
|
117 |
|
118 | var needsTemplate = (function() {
|
119 |
|
120 | var t = document.createElement('template');
|
121 | if (!('content' in t)) {
|
122 | return true;
|
123 | }
|
124 |
|
125 | if (!(t.content.cloneNode() instanceof DocumentFragment)) {
|
126 | return true;
|
127 | }
|
128 |
|
129 | var t2 = document.createElement('template');
|
130 | t2.content.appendChild(document.createElement('div'));
|
131 | t.content.appendChild(t2);
|
132 | var clone = t.cloneNode(true);
|
133 | return (clone.content.childNodes.length === 0 ||
|
134 | clone.content.firstChild.content.childNodes.length === 0);
|
135 | })();
|
136 |
|
137 |
|
138 |
|
139 | if (!window.Promise || !Array.from || !window.URL || !window.Symbol || needsTemplate) {
|
140 | polyfills = ['sd-ce-pf'];
|
141 | }
|
142 |
|
143 | if (polyfills.length) {
|
144 | var script = document.querySelector('script[src*="' + name +'"]');
|
145 | var newScript = document.createElement('script');
|
146 |
|
147 | var replacement = 'bundles/webcomponents-' + polyfills.join('-') + '.js';
|
148 | var url = script.src.replace(name, replacement);
|
149 | newScript.src = url;
|
150 |
|
151 | if (document.readyState === 'loading') {
|
152 |
|
153 | newScript.setAttribute('onload', 'window.WebComponents._batchCustomElements()');
|
154 | document.write(newScript.outerHTML);
|
155 | document.addEventListener('DOMContentLoaded', ready);
|
156 | } else {
|
157 | newScript.addEventListener('load', function () {
|
158 | asyncReady();
|
159 | });
|
160 | newScript.addEventListener('error', function () {
|
161 | throw new Error('Could not load polyfill bundle' + url);
|
162 | });
|
163 | document.head.appendChild(newScript);
|
164 | }
|
165 | } else {
|
166 | polyfillsLoaded = true;
|
167 | if (document.readyState === 'complete') {
|
168 | fireEvent()
|
169 | } else {
|
170 |
|
171 | window.addEventListener('load', ready);
|
172 | window.addEventListener('DOMContentLoaded', function() {
|
173 | window.removeEventListener('load', ready);
|
174 | ready();
|
175 | })
|
176 | }
|
177 | }
|
178 | })();
|