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

Side by Side Diff: runtime/bin/vmservice/client/deployed/web/packages/polymer/boot.js

Issue 443713004: Rename vmservice/client to vmservice/observatory to match package name. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 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
OLDNEW
(Empty)
1 // Copyright (c) 2014, 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 /// Experimental bootstrap to initialize polymer applications. This library is
6 /// not used by default, and may be replaced by Dart code in the near future.
7 ///
8 /// This script contains logic to bootstrap polymer apps during development. It
9 /// internally discovers Dart script tags through HTML imports, and constructs
10 /// a new entrypoint for the application that is then launched in an isolate.
11 ///
12 /// For each script tag found, we will load the corresponding Dart library and
13 /// execute all methods annotated with `@initMethod` and register all classes
14 /// labeled with `@CustomTag`. We keep track of the order of imports and execute
15 /// initializers in the same order.
16 ///
17 /// You can this experimental bootstrap logic by including the
18 /// polymer_experimental.html import, instead of polymer.html:
19 ///
20 /// <link rel="import" href="packages/polymer/polymer_experimental.html">
21 ///
22 /// This bootstrap replaces `initPolymer` so Dart code might need to be changed
23 /// too. If you loaded init.dart directly, you can remove it. But if you invoke
24 /// initPolymer in your main, you should remove that call and change to use
25 /// `@initMethod` instead. The current bootstrap doesn't support having Dart
26 /// script tags in the main page, so you may need to move some code into an HTML
27 /// import. For example, If you need to run some initialization code before any
28 /// other code is executed, include an HTML import to an html file with a
29 /// "application/dart" script tag that contains an initializer
30 /// method with the body of your old main, and make sure this tag is placed
31 /// above other html-imports that load the rest of the application.
32 /// Initialization methods are executed in the order in which they are
33 /// discovered in the HTML document.
34 (function() {
35 // Only run in Dartium.
36 if (navigator.userAgent.indexOf('(Dart)') === -1) return;
37
38 // Extract a Dart import URL from a script tag, which is the 'src' attribute
39 // of the script tag, or a data-url with the script contents for inlined code.
40 function getScriptUrl(script) {
41 var url = script.src;
42 if (url) {
43 // Normalize package: urls
44 var index = url.indexOf('packages/');
45 if (index == 0 || (index > 0 && url[index - 1] == '/')) {
46 url = "package:" + url.slice(index + 9);
47 }
48 return url;
49 }
50
51 // TODO(sigmund): change back to application/dart: using application/json is
52 // wrong but it hides a warning in Dartium (dartbug.com/18000).
53 return "data:application/json;base64," + window.btoa(script.textContent);
54 }
55
56 // Creates a Dart program that imports [urls] and passes them to
57 // startPolymerInDevelopment, which in turn will invoke methods marked with
58 // @initMethod, and register any custom tag labeled with @CustomTag in those
59 // libraries.
60 function createMain(urls, mainUrl) {
61 var imports = Array(urls.length + 1);
62 for (var i = 0; i < urls.length; ++i) {
63 imports[i] = 'import "' + urls[i] + '" as i' + i + ';';
64 }
65 imports[urls.length] = 'import "package:polymer/src/mirror_loader.dart";';
66 var arg = urls.length == 0 ? '[]' :
67 ('[\n "' + urls.join('",\n "') + '"\n ]');
68 return (imports.join('\n') +
69 '\n\nmain() {\n' +
70 ' startPolymerInDevelopment(' + arg + ');\n' +
71 '}\n');
72 }
73
74 function discoverScripts(content, state, importedDoc) {
75 if (!state) {
76 // internal state tracking documents we've visited, the resulting list of
77 // scripts, and any tags with the incorrect mime-type.
78 state = {seen: {}, scripts: [], badTags: []};
79 }
80 if (!content) return state;
81
82 // Note: we visit both script and link-imports together to ensure we
83 // preserve the order of the script tags as they are discovered.
84 var nodes = content.querySelectorAll('script,link[rel="import"]');
85 for (var i = 0; i < nodes.length; i++) {
86 var node = nodes[i];
87 if (node instanceof HTMLLinkElement) {
88 // TODO(jmesserly): figure out why ".import" fails in content_shell but
89 // works in Dartium.
90 if (node.import && node.import.href) node = node.import;
91
92 if (state.seen[node.href]) continue;
93 state.seen[node.href] = node;
94 discoverScripts(node.import, state, true);
95 } else if (node instanceof HTMLScriptElement) {
96 if (node.type != 'application/dart') continue;
97 if (importedDoc) {
98 state.scripts.push(getScriptUrl(node));
99 } else {
100 state.badTags.push(node);
101 }
102 }
103 }
104 return state;
105 }
106
107 // TODO(jmesserly): we're using this function because DOMContentLoaded can
108 // be fired too soon: https://www.w3.org/Bugs/Public/show_bug.cgi?id=23526
109 HTMLImports.whenImportsReady(function() {
110 // Append a new script tag that initializes everything.
111 var newScript = document.createElement('script');
112 newScript.type = "application/dart";
113
114 var results = discoverScripts(document);
115 if (results.badTags.length > 0) {
116 console.warn('The experimental polymer boostrap does not support '
117 + 'having script tags in the main document. You can move the script '
118 + 'tag to an HTML import instead. Also make sure your script tag '
119 + 'doesn\'t have a main, but a top-level method marked with '
120 + '@initMethod instead');
121 for (var i = 0; i < results.badTags.length; i++) {
122 console.warn(results.badTags[i]);
123 }
124 }
125 newScript.textContent = createMain(results.scripts);
126 document.body.appendChild(newScript);
127 });
128 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698