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 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 /// 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 | 14 /// automatically when loading the library of a custom element. |
15 * automatically when loading the library of a custom element. | |
16 */ | |
17 const initMethod = const _InitMethodAnnotation(); | 15 const initMethod = const _InitMethodAnnotation(); |
18 | 16 |
19 /** | 17 /// Initializes a polymer application as follows: |
20 * Initializes a polymer application as follows: | 18 /// * set up up polling for observable changes |
21 * * set up up polling for observable changes | 19 /// * initialize Model-Driven Views |
22 * * initialize Model-Driven Views | 20 /// * Include some style to prevent flash of unstyled content (FOUC) |
23 * * Include some style to prevent flash of unstyled content (FOUC) | 21 /// * for each library included transitively from HTML and HTML imports, |
24 * * for each library included transitively from HTML and HTML imports, | 22 /// register custom elements declared there (labeled with [CustomTag]) and |
25 * register custom elements declared there (labeled with [CustomTag]) and | 23 /// invoke the initialization method on it (top-level functions annotated with |
26 * invoke the initialization method on it (top-level functions annotated with | 24 /// [initMethod]). |
27 * [initMethod]). | |
28 */ | |
29 Zone initPolymer() { | 25 Zone initPolymer() { |
30 // We use this pattern, and not the inline lazy initialization pattern, so we | 26 // We use this pattern, and not the inline lazy initialization pattern, so we |
31 // can help dart2js detect that _discoverInitializers can be tree-shaken for | 27 // can help dart2js detect that _discoverInitializers can be tree-shaken for |
32 // deployment (and hence all uses of dart:mirrors from this loading logic). | 28 // deployment (and hence all uses of dart:mirrors from this loading logic). |
33 // TODO(sigmund): fix polymer's transformers so they can replace initPolymer | 29 // TODO(sigmund): fix polymer's transformers so they can replace initPolymer |
34 // by initPolymerOptimized. | 30 // by initPolymerOptimized. |
35 if (_initializers == null) _initializers = _discoverInitializers(); | 31 if (_initializers == null) _initializers = _discoverInitializers(); |
36 | 32 |
37 // In deployment mode, we rely on change notifiers instead of dirty checking. | 33 // In deployment mode, we rely on change notifiers instead of dirty checking. |
38 if (!_deployMode) { | 34 if (!_deployMode) { |
39 return dirtyCheckZone()..run(initPolymerOptimized); | 35 return dirtyCheckZone()..run(initPolymerOptimized); |
40 } | 36 } |
41 | 37 |
42 return initPolymerOptimized(); | 38 return initPolymerOptimized(); |
43 } | 39 } |
44 | 40 |
45 /** | 41 /// Same as [initPolymer], but runs the version that is optimized for deployment |
46 * Same as [initPolymer], but runs the version that is optimized for deployment | 42 /// to the internet. The biggest difference is it omits the [Zone] that |
47 * to the internet. The biggest difference is it omits the [Zone] that | 43 /// automatically invokes [Observable.dirtyCheck], and the list of initializers |
48 * automatically invokes [Observable.dirtyCheck], and the list of initializers | 44 /// must be supplied instead of being dynamically searched for at runtime using |
49 * must be supplied instead of being dynamically searched for at runtime using | 45 /// mirrors. |
50 * mirrors. | |
51 */ | |
52 Zone initPolymerOptimized() { | 46 Zone initPolymerOptimized() { |
53 // TODO(sigmund): refactor this so we can replace it by codegen. | 47 // TODO(sigmund): refactor this so we can replace it by codegen. |
54 smoke.useMirrors(); | 48 smoke.useMirrors(); |
55 // TODO(jmesserly): there is some code in src/declaration/polymer-element.js, | 49 // TODO(jmesserly): there is some code in src/declaration/polymer-element.js, |
56 // git version 37eea00e13b9f86ab21c85a955585e8e4237e3d2, right before | 50 // git version 37eea00e13b9f86ab21c85a955585e8e4237e3d2, right before |
57 // it registers polymer-element, which uses Platform.deliverDeclarations to | 51 // it registers polymer-element, which uses Platform.deliverDeclarations to |
58 // coordinate with HTML Imports. I don't think we need it so skipping. | 52 // coordinate with HTML Imports. I don't think we need it so skipping. |
59 document.register(PolymerDeclaration._TAG, PolymerDeclaration); | 53 document.register(PolymerDeclaration._TAG, PolymerDeclaration); |
60 | 54 |
61 for (var initializer in _initializers) { | 55 for (var initializer in _initializers) { |
62 initializer(); | 56 initializer(); |
63 } | 57 } |
64 | 58 |
65 // Run this after user code so they can add to Polymer.veiledElements | 59 // Run this after user code so they can add to Polymer.veiledElements |
66 _preventFlashOfUnstyledContent(); | 60 _preventFlashOfUnstyledContent(); |
67 | 61 |
68 customElementsReady.then((_) => Polymer._ready.complete()); | 62 customElementsReady.then((_) => Polymer._ready.complete()); |
69 return Zone.current; | 63 return Zone.current; |
70 } | 64 } |
71 | 65 |
72 /** | 66 /// Configures [initPolymer] making it optimized for deployment to the internet. |
73 * Configures [initPolymer] making it optimized for deployment to the internet. | 67 /// With this setup the initializer list is supplied instead of searched for |
74 * With this setup the initializer list is supplied instead of being dynamically | 68 /// at runtime. Additionally, after this method is called [initPolymer] omits |
75 * searched for at runtime. Additionally, after this method is called, | 69 /// the [Zone] that automatically invokes [Observable.dirtyCheck]. |
76 * [initPolymer] omits the [Zone] that automatically invokes | |
77 * [Observable.dirtyCheck]. | |
78 */ | |
79 void configureForDeployment(List<Function> initializers) { | 70 void configureForDeployment(List<Function> initializers) { |
80 _initializers = initializers; | 71 _initializers = initializers; |
81 _deployMode = true; | 72 _deployMode = true; |
82 } | 73 } |
83 | 74 |
84 /** | 75 /// List of initializers that by default will be executed when calling |
85 * List of initializers that by default will be executed when calling | 76 /// initPolymer. If null, initPolymer will compute the list of initializers by |
86 * initPolymer. If null, initPolymer will compute the list of initializers by | 77 /// crawling HTML imports, searchfing for script tags, and including an |
87 * crawling HTML imports, searchfing for script tags, and including an | 78 /// initializer for each type tagged with a [CustomTag] annotation and for each |
88 * initializer for each type tagged with a [CustomTag] annotation and for each | 79 /// top-level method annotated with [initMethod]. The value of this field is |
89 * top-level method annotated with [initMethod]. The value of this field is | 80 /// assigned programatically by the code generated from the polymer deploy |
90 * assigned programatically by the code generated from the polymer deploy | 81 /// scripts. |
91 * scripts. | |
92 */ | |
93 List<Function> _initializers; | 82 List<Function> _initializers; |
94 | 83 |
95 /** True if we're in deployment mode. */ | 84 /// True if we're in deployment mode. |
96 bool _deployMode = false; | 85 bool _deployMode = false; |
97 | 86 |
98 List<Function> _discoverInitializers() { | 87 List<Function> _discoverInitializers() { |
99 var initializers = []; | 88 var initializers = []; |
100 var librariesToLoad = _discoverScripts(document, window.location.href); | 89 var librariesToLoad = _discoverScripts(document, window.location.href); |
101 for (var lib in librariesToLoad) { | 90 for (var lib in librariesToLoad) { |
102 try { | 91 try { |
103 _loadLibrary(lib, initializers); | 92 _loadLibrary(lib, initializers); |
104 } catch (e, s) { | 93 } catch (e, s) { |
105 // Deliver errors async, so if a single library fails it doesn't prevent | 94 // Deliver errors async, so if a single library fails it doesn't prevent |
106 // other things from loading. | 95 // other things from loading. |
107 new Completer().completeError(e, s); | 96 new Completer().completeError(e, s); |
108 } | 97 } |
109 } | 98 } |
110 return initializers; | 99 return initializers; |
111 } | 100 } |
112 | 101 |
113 /** | 102 /// Walks the HTML import structure to discover all script tags that are |
114 * Walks the HTML import structure to discover all script tags that are | 103 /// implicitly loaded. This code is only used in Dartium and should only be |
115 * implicitly loaded. This code is only used in Dartium and should only be | 104 /// called after all HTML imports are resolved. Polymer ensures this by asking |
116 * called after all HTML imports are resolved. Polymer ensures this by asking | 105 /// users to put their Dart script tags after all HTML imports (this is checked |
117 * users to put their Dart script tags after all HTML imports (this is checked | 106 /// by the linter, and Dartium will otherwise show an error message). |
118 * by the linter, and Dartium will otherwise show an error message). | |
119 */ | |
120 List<String> _discoverScripts(Document doc, String baseUri, | 107 List<String> _discoverScripts(Document doc, String baseUri, |
121 [Set<Document> seen, List<String> scripts]) { | 108 [Set<Document> seen, List<String> scripts]) { |
122 if (seen == null) seen = new Set<Document>(); | 109 if (seen == null) seen = new Set<Document>(); |
123 if (scripts == null) scripts = <String>[]; | 110 if (scripts == null) scripts = <String>[]; |
124 if (doc == null) { | 111 if (doc == null) { |
125 print('warning: $baseUri not found.'); | 112 print('warning: $baseUri not found.'); |
126 return scripts; | 113 return scripts; |
127 } | 114 } |
128 if (seen.contains(doc)) return scripts; | 115 if (seen.contains(doc)) return scripts; |
129 seen.add(doc); | 116 seen.add(doc); |
130 | 117 |
131 bool scriptSeen = false; | 118 bool scriptSeen = false; |
132 for (var node in doc.querySelectorAll('script,link[rel="import"]')) { | 119 for (var node in doc.querySelectorAll('script,link[rel="import"]')) { |
133 if (node is LinkElement) { | 120 if (node is LinkElement) { |
134 _discoverScripts(node.import, node.href, seen, scripts); | 121 _discoverScripts(node.import, node.href, seen, scripts); |
135 } else if (node is ScriptElement && node.type == 'application/dart') { | 122 } else if (node is ScriptElement && node.type == 'application/dart') { |
136 if (!scriptSeen) { | 123 if (!scriptSeen) { |
137 var url = node.src; | 124 var url = node.src; |
138 scripts.add(url == '' ? baseUri : url); | 125 scripts.add(url == '' ? baseUri : url); |
139 scriptSeen = true; | 126 scriptSeen = true; |
140 } else { | 127 } else { |
141 print('warning: more than one Dart script tag in $baseUri. Dartium ' | 128 print('warning: more than one Dart script tag in $baseUri. Dartium ' |
142 'currently only allows a single Dart script tag per document.'); | 129 'currently only allows a single Dart script tag per document.'); |
143 } | 130 } |
144 } | 131 } |
145 } | 132 } |
146 return scripts; | 133 return scripts; |
147 } | 134 } |
148 | 135 |
149 /** All libraries in the current isolate. */ | 136 /// All libraries in the current isolate. |
150 final _libs = currentMirrorSystem().libraries; | 137 final _libs = currentMirrorSystem().libraries; |
151 | 138 |
152 // TODO(sigmund): explore other (cheaper) ways to resolve URIs relative to the | 139 // TODO(sigmund): explore other (cheaper) ways to resolve URIs relative to the |
153 // root library (see dartbug.com/12612) | 140 // root library (see dartbug.com/12612) |
154 final _rootUri = currentMirrorSystem().isolate.rootLibrary.uri; | 141 final _rootUri = currentMirrorSystem().isolate.rootLibrary.uri; |
155 | 142 |
156 final Logger _loaderLog = new Logger('polymer.loader'); | 143 final Logger _loaderLog = new Logger('polymer.loader'); |
157 | 144 |
158 bool _isHttpStylePackageUrl(Uri uri) { | 145 bool _isHttpStylePackageUrl(Uri uri) { |
159 var uriPath = uri.path; | 146 var uriPath = uri.path; |
160 return uri.scheme == _rootUri.scheme && | 147 return uri.scheme == _rootUri.scheme && |
161 // Don't process cross-domain uris. | 148 // Don't process cross-domain uris. |
162 uri.authority == _rootUri.authority && | 149 uri.authority == _rootUri.authority && |
163 uriPath.endsWith('.dart') && | 150 uriPath.endsWith('.dart') && |
164 (uriPath.contains('/packages/') || uriPath.startsWith('packages/')); | 151 (uriPath.contains('/packages/') || uriPath.startsWith('packages/')); |
165 } | 152 } |
166 | 153 |
167 /** | 154 /// Reads the library at [uriString] (which can be an absolute URI or a relative |
168 * Reads the library at [uriString] (which can be an absolute URI or a relative | 155 /// URI from the root library), and: |
169 * URI from the root library), and: | 156 /// |
170 * | 157 /// * If present, invokes any top-level and static functions marked |
171 * * If present, invokes any top-level and static functions marked | 158 /// with the [initMethod] annotation (in the order they appear). |
172 * with the [initMethod] annotation (in the order they appear). | 159 /// |
173 * | 160 /// * Registers any [PolymerElement] that is marked with the [CustomTag] |
174 * * Registers any [PolymerElement] that is marked with the [CustomTag] | 161 /// annotation. |
175 * annotation. | |
176 */ | |
177 void _loadLibrary(String uriString, List<Function> initializers) { | 162 void _loadLibrary(String uriString, List<Function> initializers) { |
178 var uri = _rootUri.resolve(uriString); | 163 var uri = _rootUri.resolve(uriString); |
179 var lib = _libs[uri]; | 164 var lib = _libs[uri]; |
180 if (_isHttpStylePackageUrl(uri)) { | 165 if (_isHttpStylePackageUrl(uri)) { |
181 // Use package: urls if available. This rule here is more permissive than | 166 // Use package: urls if available. This rule here is more permissive than |
182 // how we translate urls in polymer-build, but we expect Dartium to limit | 167 // how we translate urls in polymer-build, but we expect Dartium to limit |
183 // the cases where there are differences. The polymer-build issues an error | 168 // the cases where there are differences. The polymer-build issues an error |
184 // when using packages/ inside lib without properly stepping out all the way | 169 // when using packages/ inside lib without properly stepping out all the way |
185 // to the packages folder. If users don't create symlinks in the source | 170 // to the packages folder. If users don't create symlinks in the source |
186 // tree, then Dartium will also complain because it won't find the file seen | 171 // tree, then Dartium will also complain because it won't find the file seen |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 print("warning: methods marked with @initMethod should take no " | 225 print("warning: methods marked with @initMethod should take no " |
241 "arguments, ${method.simpleName} expects some."); | 226 "arguments, ${method.simpleName} expects some."); |
242 return; | 227 return; |
243 } | 228 } |
244 initializers.add(() => obj.invoke(method.simpleName, const [])); | 229 initializers.add(() => obj.invoke(method.simpleName, const [])); |
245 } | 230 } |
246 | 231 |
247 class _InitMethodAnnotation { | 232 class _InitMethodAnnotation { |
248 const _InitMethodAnnotation(); | 233 const _InitMethodAnnotation(); |
249 } | 234 } |
OLD | NEW |