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 templates = new Map(); | 8 var templates = new Map(); |
9 | 9 |
10 function createPrototype(proto, definition) { | 10 class SkyElement extends HTMLElement { |
11 var result = Object.create(proto); | 11 |
12 var names = Object.getOwnPropertyNames(definition); | 12 static register() { |
13 for (var i = 0; i < names.length; ++i) { | 13 var wrapper = document.currentScript.parentNode; |
14 var descriptor = Object.getOwnPropertyDescriptor(definition, names[i]); | 14 |
15 Object.defineProperty(result, names[i], descriptor); | 15 if (wrapper.localName !== 'sky-element') |
| 16 throw new Error('No <sky-element>.'); |
| 17 |
| 18 var tagName = wrapper.getAttribute("name"); |
| 19 if (!tagName) |
| 20 throw new Error('<sky-element> must have a name.'); |
| 21 |
| 22 var template = wrapper.querySelector('template'); |
| 23 if (template) |
| 24 templates.set(tagName, template); |
| 25 |
| 26 return document.registerElement(tagName, { |
| 27 prototype: this.prototype, |
| 28 }); |
16 } | 29 } |
17 return result; | |
18 } | |
19 | 30 |
20 var BasePrototype = createPrototype(HTMLElement.prototype, { | 31 created() { |
| 32 // override |
| 33 } |
21 | 34 |
22 createdCallback: function() { | 35 attached() { |
| 36 // override |
| 37 } |
| 38 |
| 39 dettached() { |
| 40 // override |
| 41 } |
| 42 |
| 43 attributeChanged(attrName, oldValue, newValue) { |
| 44 // override |
| 45 } |
| 46 |
| 47 createdCallback() { |
23 this.created(); | 48 this.created(); |
24 }, | 49 } |
25 | 50 |
26 created: function() { | 51 attachedCallback() { |
27 // override | |
28 }, | |
29 | |
30 attachedCallback: function() { | |
31 if (!this.shadowRoot) { | 52 if (!this.shadowRoot) { |
32 var template = templates.get(this.localName); | 53 var template = templates.get(this.localName); |
33 if (template) { | 54 if (template) { |
34 var shadow = this.ensureShadowRoot(); | 55 var shadow = this.ensureShadowRoot(); |
35 shadow.appendChild(template.createInstance(this)); | 56 shadow.appendChild(template.createInstance(this)); |
36 } | 57 } |
37 } | 58 } |
38 this.attached(); | 59 this.attached(); |
39 }, | 60 } |
40 | 61 |
41 attached: function() { | 62 dettachedCallback() { |
42 // override | 63 this.dettached(); |
43 }, | 64 } |
44 | 65 |
45 dettachedCallback: function() { | 66 attributeChangedCallback(attrName, oldValue, newValue) { |
46 this.dettached(); | |
47 }, | |
48 | |
49 dettached: function() { | |
50 // override | |
51 }, | |
52 | |
53 attributeChangedCallback: function(attrName, oldValue, newValue) { | |
54 // reserved for canonical behavior | 67 // reserved for canonical behavior |
55 this.attributeChanged(attrName, oldValue, newValue); | 68 this.attributeChanged(attrName, oldValue, newValue); |
56 }, | |
57 | |
58 attributeChanged: function(attrName, oldValue, newValue) { | |
59 // override | |
60 } | 69 } |
61 }); | |
62 | |
63 function SkyElement(prototype) { | |
64 var template = document.currentScript.previousElementSibling; | |
65 if (template && template.localName === 'template') | |
66 templates.set(prototype.name, template); | |
67 | |
68 document.registerElement(prototype.name, { | |
69 prototype: createPrototype(BasePrototype, prototype), | |
70 }); | |
71 }; | 70 }; |
72 | 71 |
73 module.exports = SkyElement; | 72 module.exports = SkyElement; |
74 </script> | 73 </script> |
OLD | NEW |