Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(633)

Side by Side Diff: pkg/polymer/lib/boot.js

Issue 23224003: move polymer.dart into dart svn (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: add --deploy to todomvc sample Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/polymer/example/scoped_style/my_test.html ('k') | pkg/polymer/lib/component_build.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
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
7 // it inlines all <polymer-element> definitions on the top-level page (needed by
8 // registerPolymerElement), and it removes script tags that appear inside
9 // those tags. It finally rewrites the main entrypoint to call an initialization
10 // function on each of the declared <polymer-elements>.
11 //
12 // This script is needed only when running polymer.dart in Dartium. It should be
13 // removed by the polymer deployment commands.
14
15 // As an example, given an input of this form:
16 // <polymer-element name="c1">
17 // <template></template>
18 // <script type="application/dart" src="url0.dart"></script>
19 // </polymer-element>
20 // <element name="c2">
21 // <template></template>
22 // <script type="application/dart">main() => { print('body2'); }</script>
23 // </element>
24 // <c1></c1>
25 // <c2></c2>
26 // <script type="application/dart" src="url2.dart"></script>
27 // <script src="packages/polymer/boot.js"></script>
28 //
29 // This script will simplifies the page as follows:
30 // <polymer-element name="c1">
31 // <template></template>
32 // </polymer-element>
33 // <polymer-element name="c2">
34 // <template></template>
35 // </polymer-element>
36 // <c1></c1>
37 // <c2></c2>
38 // <script type="application/dart">
39 // import 'url0.dart' as i0;
40 // import "data:application/dart;base64,CiAgICBtYWluKCkgewogICAgICBwcmludCgn Ym9keTInKTsKICAgIH0KICAgIA==" as i1;
41 // import 'url2.dart' as i2;
42 // ...
43 // main() {
44 // // code that checks which libraries have a 'main' and invokes them.
45 // // practically equivalent to: i0._init(); i1._init(); i2.main();
46 // }
47 // </script>
48
49
50 (function() {
51 // Only run in Dartium.
52 if (!navigator.webkitStartDart) {
53 // TODO(sigmund): rephrase when we split build.dart in two: analysis vs
54 // deploy pieces.
55 console.warn('boot.js only works in Dartium. Run the build.dart' +
56 ' tool to compile a depolyable JavaScript version')
57 return;
58 }
59 document.write(
60 '<script src="packages/html_import/html_import.min.js"></script>');
61
62 // Whether [node] is under `<body>` or `<head>` and not nested in an
63 // `<element>` or `<polymer-element>` tag.
64 function isTopLevel(node) {
65 var parent = node.parentNode;
66 if (parent == null || parent == document.body || parent == document.head) {
67 return true;
68 }
69 if (parent.localName && (
70 parent.localName == 'element' ||
71 parent.localName == 'polymer-element')) {
72 return false;
73 }
74 return isTopLevel(parent);
75 }
76
77 // Extract a Dart import URL from a script tag, which is the 'src' attribute
78 // of the script tag, or a data-url with the script contents for inlined code.
79 function getScriptUrl(script) {
80 var url = script.src;
81 if (url) {
82 // Normalize package: urls
83 var index = url.indexOf('packages/');
84 if (index == 0 || (index > 0 && url[index - 1] == '/')) {
85 url = "package:" + url.slice(index + 9);
86 }
87
88 // TODO(sigmund): remove the timestamp. We added this to work around
89 // a caching bug in dartium (see http://dartbug.com/12074). Note this
90 // doesn't fix caching problems with other libraries imported further in,
91 // and it can also introduce canonicalization problems if the files under
92 // these urls are being imported from other libraries.
93 var time = new Date().getTime();
94 return url + '?' + time;
95 } else {
96 // TODO(sigmund): investigate how to eliminate the warning in Dartium
97 // (changing to text/javascript hides the warning, but seems wrong).
98 return "data:application/dart;base64," + window.btoa(script.textContent);
99 }
100 }
101
102 // Moves <polymer-elements> from imported documents into the top-level page.
103 function inlinePolymerElements(content, ref, seen) {
104 if (!seen) seen = {};
105 var links = content.querySelectorAll('link[rel="import"]');
106 for (var i = 0; i < links.length; i++) {
107 var link = links[i].import;
108 if (seen[link.href]) continue;
109 seen[link.href] = link;
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 }
118 }
119 }
120
121 // Creates a Dart program that imports [urls] and [mainUrl] and invokes the
122 // _init methods of each library in urls (if present) followed by the main
123 // method of [mainUrl].
124 function createMain(urls, mainUrl) {
125 var imports = Array(urls.length + 2);
126 for (var i = 0; i < urls.length; ++i) {
127 imports[i] = 'import "' + urls[i] + '" as i' + i + ';';
128 }
129 imports[urls.length] = 'import "package:polymer/polymer.dart" as polymer;';
130 imports[urls.length + 1 ] = 'import "' + mainUrl + '" as userMain;';
131 var firstArg = urls.length == 0 ? '[]' :
132 ('[\n "' + urls.join('",\n "') + '"\n ]');
133 return (imports.join('\n') +
134 '\n\nmain() {\n' +
135 ' polymer.initPolymer(' + firstArg + ', userMain.main);\n' +
136 '}\n');
137 }
138
139 // Finds all top-level <script> tags, and <script> tags in custom elements
140 // and merges them into a single entrypoint.
141 function mergeScripts() {
142 var scripts = document.getElementsByTagName("script");
143 var length = scripts.length;
144
145 var dartScripts = []
146 var urls = [];
147
148 // Collect the information we need to replace the script tags
149 for (var i = 0; i < length; ++i) {
150 var script = scripts[i];
151 if (script.type == "application/dart") {
152 dartScripts.push(script);
153 if (isTopLevel(script)) continue;
154 urls.push(getScriptUrl(script));
155 }
156 }
157
158 // Removes all the original script tags under elements, and replace
159 // top-level script tags so we first call each element's _init.
160 for (var i = 0; i < dartScripts.length; ++i) {
161 var script = dartScripts[i];
162 if (isTopLevel(script)) {
163 var newScript = document.createElement('script');
164 newScript.type = "application/dart";
165 newScript.textContent = createMain(urls, getScriptUrl(script));
166 script.parentNode.replaceChild(newScript, script);
167 } else {
168 script.parentNode.removeChild(script);
169 }
170 }
171 }
172
173 var alreadyRan = false;
174 window.addEventListener('HTMLImportsLoaded', function (e) {
175 if (alreadyRan) {
176 console.warn('HTMLImportsLoaded fired again.');
177 return;
178 }
179 alreadyRan = true;
180 var ref = document.body.children[0];
181 inlinePolymerElements(document, ref);
182 mergeScripts();
183 if (!navigator.webkitStartDart()) {
184 document.body.innerHTML = 'This build has expired. Please download a ' +
185 'new Dartium at http://www.dartlang.org/dartium/index.html';
186 }
187 });
188 })();
OLDNEW
« no previous file with comments | « pkg/polymer/example/scoped_style/my_test.html ('k') | pkg/polymer/lib/component_build.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698