| 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); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 * [CustomTag] and invoke the initialization method on it. If [libraries] | 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 | 26 * is null, first find all libraries that need to be loaded by scanning for |
| 27 * HTML imports in the main document. | 27 * HTML imports in the main document. |
| 28 * | 28 * |
| 29 * The initialization on each library is a top-level function and annotated with | 29 * The initialization on each library is a top-level function and annotated with |
| 30 * [initMethod]. | 30 * [initMethod]. |
| 31 * | 31 * |
| 32 * The urls in [libraries] can be absolute or relative to | 32 * The urls in [libraries] can be absolute or relative to |
| 33 * `currentMirrorSystem().isolate.rootLibrary.uri`. | 33 * `currentMirrorSystem().isolate.rootLibrary.uri`. |
| 34 */ | 34 */ |
| 35 void initPolymer([List<String> libraries]) { | 35 void initPolymer() { |
| 36 // Note: we synchronously load all libraries because the script invoking | 36 if (_useDirtyChecking) { |
| 37 // this is run after all HTML imports are resolved. | 37 dirtyCheckZone().run(_initPolymerOptimized); |
| 38 if (libraries == null) { | 38 } else { |
| 39 libraries = _discoverScripts(document, window.location.href); | 39 _initPolymerOptimized(); |
| 40 } | 40 } |
| 41 dirtyCheckZone().run(() => initPolymerOptimized(libraries)); | |
| 42 } | 41 } |
| 43 | 42 |
| 44 /** | 43 /** |
| 45 * Same as [initPolymer], but runs the version that is optimized for deployment | 44 * Same as [initPolymer], but runs the version that is optimized for deployment |
| 46 * to the internet. The biggest difference is it omits the [Zone] that | 45 * to the internet. The biggest difference is it omits the [Zone] that |
| 47 * automatically invokes [Observable.dirtyCheck], and the list of libraries must | 46 * automatically invokes [Observable.dirtyCheck], and the list of libraries must |
| 48 * be supplied instead of being dynamically searched for at runtime. | 47 * be supplied instead of being dynamically searched for at runtime. |
| 49 */ | 48 */ |
| 50 // TODO(jmesserly): change the Polymer build step to call this directly. | 49 // TODO(jmesserly): change the Polymer build step to call this directly. |
| 51 void initPolymerOptimized(List<String> libraries) { | 50 void _initPolymerOptimized() { |
| 52 preventFlashOfUnstyledContent(); | 51 preventFlashOfUnstyledContent(); |
| 53 | 52 |
| 54 // TODO(jmesserly): mdv should use initMdv instead of mdv.initialize. | 53 // TODO(jmesserly): mdv should use initMdv instead of mdv.initialize. |
| 55 mdv.initialize(); | 54 mdv.initialize(); |
| 56 document.register(PolymerDeclaration._TAG, PolymerDeclaration); | 55 document.register(PolymerDeclaration._TAG, PolymerDeclaration); |
| 57 | 56 |
| 58 _loadLibraries(libraries); | 57 _loadLibraries(); |
| 59 } | 58 } |
| 60 | 59 |
| 61 void _loadLibraries(libraries) { | 60 /** |
| 62 for (var lib in libraries) { | 61 * Configures [initPolymer] making it optimized for deployment to the internet. |
| 62 * With this setup the list of libraries to initialize is supplied instead of |
| 63 * being dynamically searched for at runtime. Additionally, after this method is |
| 64 * called, [initPolymer] omits the [Zone] that automatically invokes |
| 65 * [Observable.dirtyCheck]. |
| 66 */ |
| 67 void configureForDeployment(List<String> libraries) { |
| 68 _librariesToLoad = libraries; |
| 69 _useDirtyChecking = false; |
| 70 } |
| 71 |
| 72 /** |
| 73 * Libraries that will be initialized. For each library, the intialization |
| 74 * registers any type tagged with a [CustomTag] annotation and calls any |
| 75 * top-level method annotated with [initMethod]. The value of this field is |
| 76 * assigned programatically by the code generated from the polymer deploy |
| 77 * scripts. During development, the libraries are inferred by crawling HTML |
| 78 * imports and searching for script tags. |
| 79 */ |
| 80 List<String> _librariesToLoad = |
| 81 _discoverScripts(document, window.location.href); |
| 82 bool _useDirtyChecking = true; |
| 83 |
| 84 void _loadLibraries() { |
| 85 for (var lib in _librariesToLoad) { |
| 63 try { | 86 try { |
| 64 _loadLibrary(lib); | 87 _loadLibrary(lib); |
| 65 } catch (e, s) { | 88 } catch (e, s) { |
| 66 // Deliver errors async, so if a single library fails it doesn't prevent | 89 // Deliver errors async, so if a single library fails it doesn't prevent |
| 67 // other things from loading. | 90 // other things from loading. |
| 68 new Completer().completeError(e, s); | 91 new Completer().completeError(e, s); |
| 69 } | 92 } |
| 70 } | 93 } |
| 71 | 94 |
| 72 customElementsReady.then((_) => Polymer._ready.complete()); | 95 customElementsReady.then((_) => Polymer._ready.complete()); |
| 73 } | 96 } |
| 74 | 97 |
| 75 /** | 98 /** |
| 76 * Walks the HTML import structure to discover all script tags that are | 99 * Walks the HTML import structure to discover all script tags that are |
| 77 * implicitly loaded. | 100 * implicitly loaded. This code is only used in Dartium and should only be |
| 101 * called after all HTML imports are resolved. Polymer ensures this by asking |
| 102 * users to put their Dart script tags after all HTML imports (this is checked |
| 103 * by the linter, and Dartium will otherwise show an error message). |
| 78 */ | 104 */ |
| 79 List<String> _discoverScripts(Document doc, String baseUri, | 105 List<String> _discoverScripts(Document doc, String baseUri, |
| 80 [Set<Document> seen, List<String> scripts]) { | 106 [Set<Document> seen, List<String> scripts]) { |
| 81 if (seen == null) seen = new Set<Document>(); | 107 if (seen == null) seen = new Set<Document>(); |
| 82 if (scripts == null) scripts = <String>[]; | 108 if (scripts == null) scripts = <String>[]; |
| 83 if (seen.contains(doc)) return scripts; | 109 if (seen.contains(doc)) return scripts; |
| 84 seen.add(doc); | 110 seen.add(doc); |
| 85 | 111 |
| 86 var inlinedScriptCount = 0; | 112 var inlinedScriptCount = 0; |
| 87 for (var node in doc.queryAll('script,link[rel="import"]')) { | 113 for (var node in doc.queryAll('script,link[rel="import"]')) { |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 print("warning: methods marked with @initMethod should take no " | 243 print("warning: methods marked with @initMethod should take no " |
| 218 "arguments, ${method.simpleName} expects some."); | 244 "arguments, ${method.simpleName} expects some."); |
| 219 return; | 245 return; |
| 220 } | 246 } |
| 221 obj.invoke(method.simpleName, const []); | 247 obj.invoke(method.simpleName, const []); |
| 222 } | 248 } |
| 223 | 249 |
| 224 class _InitMethodAnnotation { | 250 class _InitMethodAnnotation { |
| 225 const _InitMethodAnnotation(); | 251 const _InitMethodAnnotation(); |
| 226 } | 252 } |
| OLD | NEW |