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

Side by Side Diff: pkg/polymer/lib/src/loader.dart

Issue 27518006: Practically remove boot.js, adds the initialization from the Dart side of (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 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
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 part of polymer; 5 part of polymer;
6 6
7 /** Annotation used to automatically register polymer elements. */ 7 /** Annotation used to automatically register polymer elements. */
8 class CustomTag { 8 class CustomTag {
9 final String tagName; 9 final String tagName;
10 const CustomTag(this.tagName); 10 const CustomTag(this.tagName);
11 } 11 }
12 12
13 /** 13 /**
14 * Metadata used to label static or top-level methods that are called 14 * Metadata used to label static or top-level methods that are called
15 * automatically when loading the library of a custom element. 15 * automatically when loading the library of a custom element.
16 */ 16 */
17 const initMethod = const _InitMethodAnnotation(); 17 const initMethod = const _InitMethodAnnotation();
18 18
19 /** 19 /**
20 * Initializes a polymer application as follows: 20 * Initializes a polymer application as follows:
21 * * set up up polling for observable changes 21 * * set up up polling for observable changes
22 * * initialize MDV 22 * * initialize MDV
Jennifer Messerly 2013/10/17 02:04:35 initialize Model-Driven Views (MDV)
Siggi Cherem (dart-lang) 2013/10/17 02:37:40 Done.
23 * * for each library in [libraries], register custom elements labeled with 23 * * Include some style to prevent FUOC
Jennifer Messerly 2013/10/17 02:04:35 Include some style to prevent flash of unstyled co
Siggi Cherem (dart-lang) 2013/10/17 02:37:40 Done.
24 * [CustomTag] and invoke the initialization method on it. 24 * * for each library in [libraries], register custom elements labeled with
25 * [CustomTag] and invoke the initialization method on it. If [libraries]
26 * is null, first find all libraries that need to be loaded by scanning for
27 * HTML imports in the main document.
25 * 28 *
26 * The initialization on each library is either a method named `main` or 29 * The initialization on each library is a top-level function and annotated with
27 * a top-level function and annotated with [initMethod]. 30 * [initMethod].
28 * 31 *
29 * The urls in [libraries] can be absolute or relative to [srcUrl]. 32 * The urls in [libraries] can be absolute or relative to
33 * `currentMirrorSystem().isolate.rootLibrary.uri`.
30 */ 34 */
31 void initPolymer(List<String> libraries, [String srcUrl]) { 35 void initPolymer([List<String> libraries]) {
32 runMicrotask(() { 36 runMicrotask(() {
33 // DOM events don't yet go through microtasks, so we catch those here. 37 // DOM events don't yet go through microtasks, so we catch those here.
34 new Timer.periodic(new Duration(milliseconds: 125), 38 new Timer.periodic(new Duration(milliseconds: 125),
35 (_) => performMicrotaskCheckpoint()); 39 (_) => performMicrotaskCheckpoint());
36 40
41 preventFuoc();
Jennifer Messerly 2013/10/17 02:04:35 Rename this "preventFlashOfUnstyledContent"? alte
Siggi Cherem (dart-lang) 2013/10/17 02:37:40 Done.
42
37 // TODO(jmesserly): mdv should use initMdv instead of mdv.initialize. 43 // TODO(jmesserly): mdv should use initMdv instead of mdv.initialize.
38 mdv.initialize(); 44 mdv.initialize();
39 document.register(PolymerDeclaration._TAG, PolymerDeclaration); 45 document.register(PolymerDeclaration._TAG, PolymerDeclaration);
40 46
41 for (var lib in libraries) { 47 if (libraries != null) {
42 _loadLibrary(lib, srcUrl); 48 _loadLibraries(libraries);
49 return;
43 } 50 }
44 51
45 Polymer._ready.complete(); 52 window.onLoad.listen((_) {
Jennifer Messerly 2013/10/17 02:04:35 do we need to worry about initPolymer being called
Siggi Cherem (dart-lang) 2013/10/17 02:37:40 fixed - I got rid of the onLoad event. This was ne
53 _loadLibraries(_discoverScripts(document, window.location.href));
54 });
55 });
56 }
46 57
47 // TODO(sigmund): move to boot.dart once it's ready. 58 void _loadLibraries(libraries) {
48 document.body.style.transition = 'opacity 0.3s'; 59 for (var lib in libraries) {
49 document.body.style.opacity = '1'; 60 _loadLibrary(lib);
50 }); 61 }
62 Polymer._ready.complete();
63 }
64
65 /**
66 * Walks the HTML import structure to discover all script tags that are
67 * implicitly loaded.
68 */
69 List<String> _discoverScripts(Document doc, String baseUri,
70 [Set<Document> seen, List<String> scripts]) {
71 if (seen == null) seen = new Set<Document>();
72 if (scripts == null) scripts = <String>[];
73 if (seen.contains(doc)) return scripts;
74 seen.add(doc);
75
76 var inlinedScriptCount = 0;
77 for (var node in doc.queryAll('script,link[rel="import"]')) {
78 if (node is LinkElement) {
79 _discoverScripts(node.import, node.href, seen, scripts);
80 } else if (node is ScriptElement && node.type == 'application/dart') {
81 var url = node.src;
82 if (url != '') {
83 // TODO(sigmund): consider either normalizing package: urls or add a
84 // warning to let users know about cannonicalization issues.
85 scripts.add(url);
86 } else {
87 // We generate a unique identifier for inlined scripts which we later
88 // translate to the unique identifiers used by Dartium. Dartium uses
89 // line/column number information which we can't compute here.
90 scripts.add('$baseUri:$inlinedScriptCount');
91 inlinedScriptCount++;
92 }
93 }
94 }
95 return scripts;
51 } 96 }
52 97
53 /** All libraries in the current isolate. */ 98 /** All libraries in the current isolate. */
54 final _libs = currentMirrorSystem().libraries; 99 final _libs = currentMirrorSystem().libraries;
55 100
101 final _rootUri = currentMirrorSystem().isolate.rootLibrary.uri;
Jennifer Messerly 2013/10/17 02:04:35 do we have a bug # about this?
Siggi Cherem (dart-lang) 2013/10/17 02:37:40 oops, yes, I removed the TODO when moving the code
102
103 /** Regex that matches urls used to represent inlined scripts. */
104 final RegExp _inlineScriptRegExp = new RegExp('\(.*\.html.*\):\([0-9]\+\)');
105
106 /**
107 * Map URLs fabricated by polymer to URLs fabricated by Dartium to represent
108 * inlined scripts. Polymer uses baseUri:script#, Dartium uses baseUri:line#
109 */
110 // TODO(sigmund): figure out if we can generate the same URL and expose it.
111 final Map<Uri, List<Uri>> _inlinedScriptMapping = () {
112 var map = {};
113 for (var uri in _libs.keys) {
114 var uriString = uri.toString();
115 var match = _inlineScriptRegExp.firstMatch(uriString);
116 if (match == null) continue;
117 var baseUri = Uri.parse(match.group(1));
118 if (map[baseUri] == null) map[baseUri] = [];
119 map[baseUri].add(uri);
120 }
121 return map;
122 }();
123
124 /** Returns a new Uri that replaces [path] in [uri]. */
125 Uri _replacePath(Uri uri, String path) {
126 return new Uri(scheme: uri.scheme, host: uri.host, port: uri.port,
127 path: path, query: uri.query, fragment: uri.fragment);
128 }
129
130 /** Returns the Uri in [href] without query parameters or fragments. */
131 String _baseUri(String href) {
132 var uri = Uri.parse(window.location.href);
133 var trimUri = new Uri(scheme: uri.scheme, host: uri.host,
134 port: uri.port, path: uri.path);
135 return trimUri.toString();
136 }
137
56 /** 138 /**
57 * Reads the library at [uriString] (which can be an absolute URI or a relative 139 * Reads the library at [uriString] (which can be an absolute URI or a relative
58 * URI from [srcUrl]), and: 140 * URI from the root library), and:
59 *
60 * * If present, invokes `main`.
61 * 141 *
62 * * If present, invokes any top-level and static functions marked 142 * * If present, invokes any top-level and static functions marked
63 * with the [initMethod] annotation (in the order they appear). 143 * with the [initMethod] annotation (in the order they appear).
64 * 144 *
65 * * Registers any [PolymerElement] that is marked with the [CustomTag] 145 * * Registers any [PolymerElement] that is marked with the [CustomTag]
66 * annotation. 146 * annotation.
67 */ 147 */
68 void _loadLibrary(String uriString, [String srcUrl]) { 148 void _loadLibrary(String uriString) {
69 var uri = Uri.parse(uriString); 149 var uri = _rootUri.resolve(uriString);
70 if (uri.scheme == '' && srcUrl != null) { 150 var lib;
71 uri = Uri.parse(path.normalize(path.join(path.dirname(srcUrl), uriString))); 151 var match = _inlineScriptRegExp.firstMatch(uriString);
152 if (match != null) {
153 var baseUri = Uri.parse(match.group(1));
154 var list = _inlinedScriptMapping[baseUri];
155 var pos = int.parse(match.group(2), onError: (_) => -1);
156 if (list != null && pos >= 0 && pos < list.length && list[pos] != null) {
157 lib = _libs[list[pos]];
158 }
159 } else {
160 lib = _libs[uri];
72 } 161 }
73 var lib = _libs[uri];
74 if (lib == null) { 162 if (lib == null) {
75 print('warning: $uri library not found'); 163 print('warning: $uri library not found');
76 return; 164 return;
77 } 165 }
78 166
79 // Invoke `main`, if present.
80 if (lib.functions[#main] != null) {
81 lib.invoke(#main, const []);
82 }
83
84 // Search top-level functions marked with @initMethod 167 // Search top-level functions marked with @initMethod
85 for (var f in lib.functions.values) { 168 for (var f in lib.functions.values) {
86 _maybeInvoke(lib, f); 169 _maybeInvoke(lib, f);
87 } 170 }
88 171
89 for (var c in lib.classes.values) { 172 for (var c in lib.classes.values) {
90 // Search for @CustomTag on classes 173 // Search for @CustomTag on classes
91 for (var m in c.metadata) { 174 for (var m in c.metadata) {
92 var meta = m.reflectee; 175 var meta = m.reflectee;
93 if (meta is CustomTag) { 176 if (meta is CustomTag) {
(...skipping 28 matching lines...) Expand all
122 print("warning: methods marked with @initMethod should take no " 205 print("warning: methods marked with @initMethod should take no "
123 "arguments, ${method.simpleName} expects some."); 206 "arguments, ${method.simpleName} expects some.");
124 return; 207 return;
125 } 208 }
126 obj.invoke(method.simpleName, const []); 209 obj.invoke(method.simpleName, const []);
127 } 210 }
128 211
129 class _InitMethodAnnotation { 212 class _InitMethodAnnotation {
130 const _InitMethodAnnotation(); 213 const _InitMethodAnnotation();
131 } 214 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698