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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 /// using the experimental bootstrap API, this would be invoked by an entry | 48 /// using the experimental bootstrap API, this would be invoked by an entry |
49 /// point that is automatically generated somehow. In particular, during | 49 /// point that is automatically generated somehow. In particular, during |
50 /// development, the entry point would be generated dynamically in `boot.js`. | 50 /// development, the entry point would be generated dynamically in `boot.js`. |
51 /// Similarly, pub-build would generate the entry point for deployment. | 51 /// Similarly, pub-build would generate the entry point for deployment. |
52 void startPolymer(List<Function> initializers, [bool deployMode = true]) { | 52 void startPolymer(List<Function> initializers, [bool deployMode = true]) { |
53 if (_startPolymerCalled) throw 'Initialization was already done.'; | 53 if (_startPolymerCalled) throw 'Initialization was already done.'; |
54 _startPolymerCalled = true; | 54 _startPolymerCalled = true; |
55 _hookJsPolymer(); | 55 _hookJsPolymer(); |
56 _deployMode = deployMode; | 56 _deployMode = deployMode; |
57 | 57 |
| 58 if (initializers == null) { |
| 59 throw 'Missing initialization of polymer elements. ' |
| 60 'Please check that the list of entry points in your pubspec.yaml ' |
| 61 'is correct. If you are using pub-serve, you may need to restart it.'; |
| 62 } |
| 63 |
| 64 Polymer.registerSync('d-auto-binding', AutoBindingElement, |
| 65 extendsTag: 'template'); |
| 66 |
58 for (var initializer in initializers) { | 67 for (var initializer in initializers) { |
59 initializer(); | 68 initializer(); |
60 } | 69 } |
61 } | 70 } |
62 | 71 |
63 /// Configures [initPolymer] making it optimized for deployment to the internet. | 72 /// Configures [initPolymer] making it optimized for deployment to the internet. |
64 /// With this setup the initializer list is supplied instead of searched for | 73 /// With this setup the initializer list is supplied instead of searched for |
65 /// at runtime. Additionally, after this method is called [initPolymer] omits | 74 /// at runtime. Additionally, after this method is called [initPolymer] omits |
66 /// the [Zone] that automatically invokes [Observable.dirtyCheck]. | 75 /// the [Zone] that automatically invokes [Observable.dirtyCheck]. |
67 void configureForDeployment(List<Function> initializers) { | 76 void configureForDeployment(List<Function> initializers) { |
68 loader.initializers = initializers; | 77 loader.initializers = initializers; |
69 loader.deployMode = true; | 78 loader.deployMode = true; |
70 } | 79 } |
71 | 80 |
72 /// To ensure Dart can interoperate with polymer-element registered by | 81 /// To ensure Dart can interoperate with polymer-element registered by |
73 /// polymer.js, we need to be able to execute Dart code if we are registering | 82 /// polymer.js, we need to be able to execute Dart code if we are registering |
74 /// a Dart class for that element. We trigger Dart logic by patching | 83 /// a Dart class for that element. We trigger Dart logic by patching |
75 /// polymer-element's register function and: | 84 /// polymer-element's register function and: |
76 /// | 85 /// |
77 /// * if it has a Dart class, run PolymerDeclaration's register. | 86 /// * if it has a Dart class, run PolymerDeclaration's register. |
78 /// * otherwise it is a JS prototype, run polymer-element's normal register. | 87 /// * otherwise it is a JS prototype, run polymer-element's normal register. |
79 void _hookJsPolymer() { | 88 void _hookJsPolymer() { |
| 89 // Note: platform.js is not used directly here, but we check that it is loaded |
| 90 // to provide a good error message in Dartium if people forgot to include it. |
| 91 // Otherwise, polymer.js below will fail with a hard to understand error |
| 92 // message. |
| 93 var platform = js.context['Platform']; |
| 94 if (platform == null) { |
| 95 throw new StateError('platform.js, dart_support.js must be loaded at' |
| 96 ' the top of your application, before any other scripts or HTML' |
| 97 ' imports that use polymer. Putting these two script tags at the top of' |
| 98 ' your <head> element should address this issue:' |
| 99 ' <script src="packages/web_components/platform.js"></script> and ' |
| 100 ' <script src="packages/web_components/dart_support.js"></script>.'); |
| 101 } |
80 var polymerJs = js.context['Polymer']; | 102 var polymerJs = js.context['Polymer']; |
81 if (polymerJs == null) { | 103 if (polymerJs == null) { |
82 throw new StateError('polymer.js must be loaded before polymer.dart, please' | 104 throw new StateError('polymer.js must be loaded before polymer.dart, please' |
83 ' add <link rel="import" href="packages/polymer/polymer.html"> to your' | 105 ' add <link rel="import" href="packages/polymer/polymer.html"> to your' |
84 ' <head> before any Dart scripts. Alternatively you can get a different' | 106 ' <head> before any Dart scripts. Alternatively you can get a different' |
85 ' version of polymer.js by following the instructions at' | 107 ' version of polymer.js by following the instructions at' |
86 ' http://www.polymer-project.org; if you do that be sure to include' | 108 ' http://www.polymer-project.org.'); |
87 ' the platform polyfills.'); | |
88 } | 109 } |
89 | 110 |
90 // TODO(jmesserly): dart:js appears to not callback in the correct zone: | 111 // TODO(jmesserly): dart:js appears to not callback in the correct zone: |
91 // https://code.google.com/p/dart/issues/detail?id=17301 | 112 // https://code.google.com/p/dart/issues/detail?id=17301 |
92 var zone = Zone.current; | 113 var zone = Zone.current; |
93 | 114 |
94 polymerJs.callMethod('whenPolymerReady', | 115 polymerJs.callMethod('whenPolymerReady', |
95 [zone.bindCallback(() => Polymer._ready.complete())]); | 116 [zone.bindCallback(() => Polymer._onReady.complete())]); |
96 | 117 |
97 var polyElem = document.createElement('polymer-element'); | 118 JsFunction originalRegister = _polymerElementProto['register']; |
98 var proto = new JsObject.fromBrowserObject(polyElem)['__proto__']; | |
99 if (proto is Node) proto = new JsObject.fromBrowserObject(proto); | |
100 | |
101 JsFunction originalRegister = proto['register']; | |
102 if (originalRegister == null) { | 119 if (originalRegister == null) { |
103 throw new StateError('polymer.js must expose "register" function on ' | 120 throw new StateError('polymer.js must expose "register" function on ' |
104 'polymer-element to enable polymer.dart to interoperate.'); | 121 'polymer-element to enable polymer.dart to interoperate.'); |
105 } | 122 } |
106 | 123 |
107 registerDart(jsElem, String name, String extendee) { | 124 registerDart(jsElem, String name, String extendee) { |
108 // By the time we get here, we'll know for sure if it is a Dart object | 125 // By the time we get here, we'll know for sure if it is a Dart object |
109 // or not, because polymer-element will wait for us to notify that | 126 // or not, because polymer-element will wait for us to notify that |
110 // the @CustomTag was found. | 127 // the @CustomTag was found. |
111 final type = _getRegisteredType(name); | 128 final type = _getRegisteredType(name); |
112 if (type != null) { | 129 if (type != null) { |
113 final extendsDecl = _getDeclaration(extendee); | 130 final extendsDecl = _getDeclaration(extendee); |
114 return zone.run(() => | 131 return zone.run(() => |
115 new PolymerDeclaration(jsElem, name, type, extendsDecl).register()); | 132 new PolymerDeclaration(jsElem, name, type, extendsDecl).register()); |
116 } | 133 } |
117 // It's a JavaScript polymer element, fall back to the original register. | 134 // It's a JavaScript polymer element, fall back to the original register. |
118 return originalRegister.apply([name, extendee], thisArg: jsElem); | 135 return originalRegister.apply([name, extendee], thisArg: jsElem); |
119 } | 136 } |
120 | 137 |
121 proto['register'] = new JsFunction.withThis(registerDart); | 138 _polymerElementProto['register'] = new JsFunction.withThis(registerDart); |
122 } | 139 } |
| 140 |
| 141 // Note: we cache this so we can use it later to look up 'init'. |
| 142 // See registerSync. |
| 143 JsObject _polymerElementProto = () { |
| 144 var polyElem = document.createElement('polymer-element'); |
| 145 var proto = new JsObject.fromBrowserObject(polyElem)['__proto__']; |
| 146 if (proto is Node) proto = new JsObject.fromBrowserObject(proto); |
| 147 return proto; |
| 148 }(); |
OLD | NEW |