Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 <!-- | 1 <!-- |
| 2 // Copyright 2014 The Chromium Authors. All rights reserved. | 2 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 // Use of this source code is governed by a BSD-style license that can be | 3 // Use of this source code is governed by a BSD-style license that can be |
| 4 // found in the LICENSE file. | 4 // found in the LICENSE file. |
| 5 --> | 5 --> |
| 6 <import src="TemplateBinding.sky" /> | 6 <import src="TemplateBinding.sky" /> |
| 7 <script> | 7 <script> |
| 8 var Base = { | 8 var templates = new Map(); |
| 9 __proto__: HTMLElement.prototype, | |
| 10 | 9 |
| 11 register: function() { | 10 function createPrototype(proto, definition) { |
| 12 // |this| is prototype | 11 var result = Object.create(proto); |
| 13 var template = document.currentScript.previousElementSibling; | 12 var names = Object.getOwnPropertyNames(definition); |
| 14 if (template && template.localName == 'template') | 13 for (var i = 0; i < names.length; ++i) { |
| 15 this.template_ = template; | 14 var descriptor = Object.getOwnPropertyDescriptor(definition, names[i]); |
| 15 Object.defineProperty(result, names[i], descriptor); | |
| 16 } | |
| 17 return result; | |
| 18 } | |
| 19 | |
| 20 var BasePrototype = createPrototype(HTMLElement.prototype, { | |
| 21 | |
| 22 createdCallback: function() { | |
| 23 this.created(); | |
| 16 }, | 24 }, |
| 17 | 25 |
| 18 createdCallback: function() { | 26 created: function() { |
| 27 // override | |
| 19 }, | 28 }, |
| 20 | 29 |
| 21 attachedCallback: function() { | 30 attachedCallback: function() { |
| 22 var shadow = this.createShadowRoot(); | 31 if (!this.shadowRoot) { |
| 23 shadow.appendChild(this.template_.createInstance(this)); | 32 var template = templates.get(this.localName); |
| 33 if (template) { | |
| 34 var shadow = this.createShadowRoot(); | |
| 35 shadow.appendChild(template.createInstance(this)); | |
| 36 } | |
| 37 } | |
| 24 this.attached(); | 38 this.attached(); |
| 25 }, | 39 }, |
| 26 | 40 |
| 27 attached: function() { | 41 attached: function() { |
| 28 // override | 42 // override |
| 29 }, | 43 }, |
| 30 | 44 |
| 31 dettachedCallback: function() { | 45 dettachedCallback: function() { |
| 32 this.dettached(); | 46 this.dettached(); |
| 33 }, | 47 }, |
| 34 | 48 |
| 35 dettached: function() { | 49 dettached: function() { |
| 36 // override | 50 // override |
| 37 }, | 51 }, |
| 38 | 52 |
| 39 attributeChangedCallback: function(attrName, oldValue, newValue) { | 53 attributeChangedCallback: function(attrName, oldValue, newValue) { |
| 40 // reserved for canonical behavior | 54 // reserved for canonical behavior |
| 41 this.attributeChanged(attrName, oldValue, newValue); | 55 this.attributeChanged(attrName, oldValue, newValue); |
| 42 }, | 56 }, |
| 43 | 57 |
| 44 attributeChanged: function(attrName, oldValue, newValue) { | 58 attributeChanged: function(attrName, oldValue, newValue) { |
| 45 // override | 59 // override |
| 46 } | 60 } |
| 47 }; | 61 }); |
| 48 | 62 |
| 49 function SkyElement(prototype) { | 63 function SkyElement(prototype) { |
| 50 prototype.__proto__ = Base; | 64 var template = document.currentScript.previousElementSibling; |
| 51 document.registerElement(prototype.name, { prototype: prototype }); | 65 if (template && template.localName === 'template') |
| 52 prototype.register(); | 66 templates.set(prototype.name, template); |
|
rafaelw
2014/11/12 12:00:53
Nit: You want weak semantics here, so I'd use a Sy
| |
| 67 | |
| 68 document.registerElement(prototype.name, { | |
| 69 prototype: createPrototype(BasePrototype, prototype), | |
| 70 }); | |
| 53 }; | 71 }; |
| 54 | 72 |
| 55 module.exports = SkyElement; | 73 module.exports = SkyElement; |
| 56 </script> | 74 </script> |
| OLD | NEW |