OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // This script dynamically prepares a set of files to run polymer.dart. It uses | 5 // This script dynamically prepares a set of files to run polymer.dart. It uses |
6 // the html_import polyfill to search for all imported files, then | 6 // the html_import polyfill to search for all imported files, then |
7 // it inlines all <polymer-element> definitions on the top-level page (needed by | 7 // it inlines all <polymer-element> definitions on the top-level page (needed by |
8 // registerPolymerElement), and it removes script tags that appear inside | 8 // registerPolymerElement), and it removes script tags that appear inside |
9 // those tags. It finally rewrites the main entrypoint to call an initialization | 9 // those tags. It finally rewrites the main entrypoint to call an initialization |
10 // function on each of the declared <polymer-elements>. | 10 // function on each of the declared <polymer-elements>. |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 var time = new Date().getTime(); | 93 var time = new Date().getTime(); |
94 return url + '?' + time; | 94 return url + '?' + time; |
95 } else { | 95 } else { |
96 // TODO(sigmund): investigate how to eliminate the warning in Dartium | 96 // TODO(sigmund): investigate how to eliminate the warning in Dartium |
97 // (changing to text/javascript hides the warning, but seems wrong). | 97 // (changing to text/javascript hides the warning, but seems wrong). |
98 return "data:application/dart;base64," + window.btoa(script.textContent); | 98 return "data:application/dart;base64," + window.btoa(script.textContent); |
99 } | 99 } |
100 } | 100 } |
101 | 101 |
102 // Moves <polymer-elements> from imported documents into the top-level page. | 102 // Moves <polymer-elements> from imported documents into the top-level page. |
103 function inlinePolymerElements(content, seen) { | 103 function inlinePolymerElements(content, ref, seen) { |
104 if (!seen) seen = {}; | 104 if (!seen) seen = {}; |
105 var links = content.querySelectorAll('link[rel="import"]'); | 105 var links = content.querySelectorAll('link[rel="import"]'); |
106 | |
107 if (content != document) { // no need to do anything for the top-level page | |
108 var elements = content.querySelectorAll('polymer-element'); | |
109 var ref = document.body.firstChild; | |
110 for (var i = 0; i < elements.length; ++i) { | |
111 document.body.insertBefore(elements[i], ref); | |
112 } | |
113 } | |
114 | |
115 for (var i = 0; i < links.length; i++) { | 106 for (var i = 0; i < links.length; i++) { |
116 var link = links[i].import; | 107 var link = links[i].import; |
117 if (seen[link.href]) continue; | 108 if (seen[link.href]) continue; |
118 seen[link.href] = link; | 109 seen[link.href] = link; |
119 inlinePolymerElements(link.content, seen); | 110 inlinePolymerElements(link.content, ref, seen); |
| 111 } |
| 112 |
| 113 if (content != document) { // no need to do anything for the top-level page |
| 114 var elements = content.querySelectorAll('polymer-element'); |
| 115 for (var i = 0; i < elements.length; i++) { |
| 116 document.body.insertBefore(elements[i], ref); |
| 117 } |
120 } | 118 } |
121 } | 119 } |
122 | 120 |
123 // Creates a Dart program that imports [urls] and [mainUrl] and invokes the | 121 // Creates a Dart program that imports [urls] and [mainUrl] and invokes the |
124 // _init methods of each library in urls (if present) followed by the main | 122 // _init methods of each library in urls (if present) followed by the main |
125 // method of [mainUrl]. | 123 // method of [mainUrl]. |
126 function createMain(urls, mainUrl) { | 124 function createMain(urls, mainUrl) { |
127 var imports = Array(urls.length + 2); | 125 var imports = Array(urls.length + 2); |
128 for (var i = 0; i < urls.length; ++i) { | 126 for (var i = 0; i < urls.length; ++i) { |
129 imports[i] = 'import "' + urls[i] + '" as i' + i + ';'; | 127 imports[i] = 'import "' + urls[i] + '" as i' + i + ';'; |
130 } | 128 } |
131 imports[urls.length] = 'import "package:polymer/polymer.dart" as polymer;'; | 129 imports[urls.length] = 'import "package:polymer/polymer.dart" as polymer;'; |
132 imports[urls.length + 1 ] = 'import "' + mainUrl + '" as userMain;'; | 130 imports[urls.length + 1 ] = 'import "' + mainUrl + '" as userMain;'; |
133 var firstArg = urls.length == 0 ? '[]' : | 131 var firstArg = urls.length == 0 ? '[]' : |
134 ('[\n "' + urls.join('",\n "') + '"\n ]'); | 132 ('[\n "' + urls.join('",\n "') + '"\n ]'); |
135 return (imports.join('\n') + | 133 return (imports.join('\n') + |
136 '\n\nmain() {\n' + | 134 '\n\nmain() {\n' + |
137 ' polymer.initPolymer(' + firstArg + ', userMain.main);\n' + | 135 ' polymer.initPolymer(' + firstArg + ', userMain.main);\n' + |
138 '}\n'); | 136 '}\n'); |
139 } | 137 } |
140 | 138 |
141 // Finds all top-level <script> tags, and <script> tags in custom elements | 139 // Finds all top-level <script> tags, and <script> tags in custom elements |
142 // and merges them into a single entrypoint. | 140 // and merges them into a single entrypoint. |
143 function mergeScripts() { | 141 function mergeScripts() { |
144 var scripts = document.getElementsByTagName("script"); | 142 var scripts = document.getElementsByTagName("script"); |
(...skipping 27 matching lines...) Expand all Loading... |
172 } | 170 } |
173 } | 171 } |
174 | 172 |
175 var alreadyRan = false; | 173 var alreadyRan = false; |
176 window.addEventListener('HTMLImportsLoaded', function (e) { | 174 window.addEventListener('HTMLImportsLoaded', function (e) { |
177 if (alreadyRan) { | 175 if (alreadyRan) { |
178 console.warn('HTMLImportsLoaded fired again.'); | 176 console.warn('HTMLImportsLoaded fired again.'); |
179 return; | 177 return; |
180 } | 178 } |
181 alreadyRan = true; | 179 alreadyRan = true; |
182 inlinePolymerElements(document); | 180 var ref = document.body.children[0]; |
| 181 inlinePolymerElements(document, ref); |
183 mergeScripts(); | 182 mergeScripts(); |
184 if (!navigator.webkitStartDart()) { | 183 if (!navigator.webkitStartDart()) { |
185 document.body.innerHTML = 'This build has expired. Please download a ' + | 184 document.body.innerHTML = 'This build has expired. Please download a ' + |
186 'new Dartium at http://www.dartlang.org/dartium/index.html'; | 185 'new Dartium at http://www.dartlang.org/dartium/index.html'; |
187 } | 186 } |
188 }); | 187 }); |
189 })(); | 188 })(); |
OLD | NEW |