Elliott Sprehn d3fbe58e94 Add shadowRootReady callback for SkyElement.
This is called right after stamping the template into the ShadowRoot.
This is a useful place to querySelector/getElementById for elements
inside the ShadowRoot.

R=eseidel@chromium.org

Review URL: https://codereview.chromium.org/817053002
2014-12-19 14:01:49 -08:00

79 lines
1.6 KiB
Plaintext

<!--
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-->
<import src="TemplateBinding.sky" />
<script>
var templates = new Map();
class SkyElement extends HTMLElement {
static register() {
var wrapper = document.currentScript.parentNode;
if (wrapper.localName !== 'sky-element')
throw new Error('No <sky-element>.');
var tagName = wrapper.getAttribute("name");
if (!tagName)
throw new Error('<sky-element> must have a name.');
var template = wrapper.querySelector('template');
if (template)
templates.set(tagName, template);
return document.registerElement(tagName, {
prototype: this.prototype,
});
}
created() {
// override
}
attached() {
// override
}
dettached() {
// override
}
attributeChanged(attrName, oldValue, newValue) {
// override
}
shadowRootReady() {
// override
}
createdCallback() {
this.created();
}
attachedCallback() {
if (!this.shadowRoot) {
var template = templates.get(this.localName);
if (template) {
var shadow = this.ensureShadowRoot();
shadow.appendChild(template.createInstance(this));
this.shadowRootReady();
}
}
this.attached();
}
dettachedCallback() {
this.dettached();
}
attributeChangedCallback(attrName, oldValue, newValue) {
// reserved for canonical behavior
this.attributeChanged(attrName, oldValue, newValue);
}
};
module.exports = SkyElement;
</script>