Chromium Code Reviews| Index: lib/polymer_element.dart |
| diff --git a/lib/polymer_element.dart b/lib/polymer_element.dart |
| index 1fb2c3ec8792a95c2d553204a340294bc29196f6..8be4a670f9501f6e58090ff05138577261cca577 100644 |
| --- a/lib/polymer_element.dart |
| +++ b/lib/polymer_element.dart |
| @@ -8,6 +8,7 @@ import 'dart:async'; |
| import 'dart:html'; |
| import 'dart:mirrors'; |
| +import 'package:mdv/mdv.dart' as mdv; |
| import 'package:observe/observe.dart'; |
| import 'custom_element.dart'; |
| @@ -19,23 +20,8 @@ import 'src/utils_observe.dart' show toCamelCase, toHyphenedName; |
| * but it is designed to work with the `<element>` element and adds additional |
| * features. |
| */ |
| -// TODO(sigmund): change this to take the 'localName' (recent polymer change) |
| -void registerPolymerElement(Element elementElement, CustomElement create()) { |
| - // Creates the CustomElement and then publish attributes. |
| - createElement() { |
| - final CustomElement element = create(); |
| - // TODO(jmesserly): to simplify the DWC compiler, we always emit calls to |
| - // registerPolymerElement, regardless of the base class type. |
| - if (element is PolymerElement) { |
| - PolymerElement pElement = element; |
| - pElement._parseHostEvents(elementElement); |
| - pElement._parseLocalEvents(elementElement); |
| - pElement._publishAttributes(elementElement); |
| - } |
| - return element; |
| - } |
| - |
| - registerCustomElement(elementElement.attributes['name'], createElement); |
| +void registerPolymerElement(String localName, PolymerElement create()) { |
| + registerCustomElement(localName, () => create().._initialize(localName)); |
| } |
| /** |
| @@ -67,10 +53,45 @@ abstract class PolymerElement extends CustomElement with _EventsMixin { |
| // TODO(sigmund): delete. The next line is only added to avoid warnings from |
| // the analyzer (see http://dartbug.com/11672) |
| Element get host => super.host; |
| + |
| + /** |
| + * The declaration of this polymer-element, used to extract template contents |
| + * and other information. |
| + */ |
| + static Map<String, Element> _declarations = {}; |
| + static Element getDeclaration(String localName) { |
| + if (localName == null) return null; |
| + var element = _declarations[localName]; |
| + if (element == null) { |
| + element = document.query('polymer-element[name="$localName"]'); |
| + _declarations[localName] = element; |
| + } |
| + return element; |
| + } |
| + |
| Map<String, PathObserver> _publishedAttrs; |
| Map<String, StreamSubscription> _bindings; |
| + List<String> _localNames = []; |
|
Jennifer Messerly
2013/07/18 23:56:04
final?
Siggi Cherem (dart-lang)
2013/07/19 22:52:52
Done.
|
| + |
| + void _initialize(String localName) { |
| + if (localName == null) return; |
| - void _publishAttributes(Element elementElement) { |
| + var declaration = getDeclaration(localName); |
| + if (declaration == null) return; |
| + |
| + if (declaration.attributes['extends'] != null) { |
| + var base = declaration.attributes['extends']; |
| + // Skip normal tags, only initialize parent custom elements. |
| + if (base.contains('-')) _initialize(base); |
| + } |
| + |
| + _parseHostEvents(declaration); |
| + _parseLocalEvents(declaration); |
| + _publishAttributes(declaration); |
| + _localNames.add(localName); |
| + } |
| + |
| + void _publishAttributes(elementElement) { |
| _bindings = {}; |
| _publishedAttrs = {}; |
| @@ -93,14 +114,33 @@ abstract class PolymerElement extends CustomElement with _EventsMixin { |
| // var value = attributes[name]; |
| // if (value != null) propObserver.value = value; |
| // }); |
| - |
| + _initShadowRoot(); |
| _addHostListeners(); |
| } |
| - // TODO(sigmund): make this private once we create the shadow root directly |
| - // here in polymer element. |
| - void shadowRootReady(ShadowRoot root, String elementName) { |
| - _addInstanceListeners(root, elementName); |
| + void _initShadowRoot() { |
| + for (var localName in _localNames) { |
| + var declaration = getDeclaration(localName); |
| + var root = createShadowRoot(localName); |
| + _addInstanceListeners(root, localName); |
| + |
| + // TODO(jmesserly): warn if apply-author-styles was not set and we don't |
| + // have Shadow DOM support? In that case, styles won't have proper |
|
Jennifer Messerly
2013/07/18 23:56:04
I think this TODO is obsolete :)
Siggi Cherem (dart-lang)
2013/07/19 22:52:52
Done.
|
| + // encapsulation. |
| + if (root is ShadowRoot && |
|
Jennifer Messerly
2013/07/18 23:56:04
root is always ShadowRoot now, right?
Siggi Cherem (dart-lang)
2013/07/19 22:52:52
I think so. removed.
|
| + declaration.attributes['apply-author-styles'] != null) { |
|
Jennifer Messerly
2013/07/18 23:56:04
does polymer have apply-author-styles?
Siggi Cherem (dart-lang)
2013/07/19 22:52:52
turns out they have it as a property in the protot
|
| + root.applyAuthorStyles = true; |
| + } |
| + |
| + var templateNode = declaration.children.firstWhere( |
| + (n) => n.localName == 'template', orElse: () => null); |
| + if (templateNode == null) return; |
| + |
| + root.nodes.add(cloneTemplate(templateNode.content)); |
| + var syntax = templateNode.attributes['syntax']; |
|
Jennifer Messerly
2013/07/18 23:56:04
mind adding a TODO? we should treat syntax like Po
Siggi Cherem (dart-lang)
2013/07/19 22:52:52
Done.
|
| + mdv.bindModel(root, this, |
| + syntax != null ? TemplateElement.syntax[syntax] : null); |
| + } |
| } |
| void bind(String name, model, String path) { |