Source: page.js

/**
 * A page components - <strike>UX/COE Compliant</strike>
 * <BR><BR><img src=../images/page.png width=100% style="border:1px lime dashed";> * <BR><BR><a href="../html/page.html">DEMO</a>
 */
class Page extends HTMLElement {
    constructor() {
        console.group("Page.constructor")
	
        super();

        console.groupEnd();
    };
    
    /**
     * Set observable values here. When Changed, attributeChangedCallback is invoked
     * @observedAttributes
     */
    static get observedAttributes() {
        console.group("Page.observedAttributes");

	this.observables = ["background", "width"];
	console.log(this.observables);

        console.groupEnd();
        return this.observables;
    };

    /**
     * This function is called when this is attached to DOM
     * @connectedCallback. 
     */
    connectedCallback() {
        console.group("Page.connectedCallback")
	
	let self = this;

	// MAKE SURE OUR COMPONENT HAS GLOBAL CLASS
	this.classList.add("wc");

	// FETCH ALL INTERESTING ELEMENTS
	this._fetchElements();

	// FETCH ALL ATTRIBUTES
	this._fetchAttributes();

	// WRAP UP AND ADD STATS
	this._finalize();

	// SHOW IT NOW (NO FLICKERS) 
	this.style.visibility = "visible";

        console.groupEnd();
    };

    /**
     * Invoked When component is removed. Usually with a .remove() function call
     * @disconnectedCallback
     */
    disconnectedCallback() {
        console.group("Page.disconnectedCallback")

	/* CLEAN UP NOW */

        console.groupEnd();
    };

    /**
     * Called with .setAttribute(...) function call
     * @attributeChangedCallback
     */
    attributeChangedCallback(attr, oldval, newval) {
        console.group("Page.attributeChangedCallback:", attr, oldval, newval);

	this.properties = this.properties || [];

	// EXAMPLE ONLY. replace with observables
	switch(attr)
	{
	    case "background":
	    this.properties.background = newval;
	    this.style.background = this.properties.background;
	    break;

	    case "width":
	    this.properties.width = newval;
	    this.style.width = this.properties.width;
	    break;
	}
	
        console.groupEnd();
    };

    /**
     * Stores DOM elements of interest for future use
     * @_fetchElements
     */
    _fetchElements() {
	console.group("Page._fetchElements");
	
	this.dom = this.dom || [];
	this.dom.content = this.innerHTML;

	console.groupEnd();
    };

    /**
     * Component attributes are _fetched and defaults are set if undefined
     * @_fetchAttributes
     * @param {string} [background=#FFF] page background
     * @param {string} [width=1280px] page width
     */
    _fetchAttributes() {
	console.group("Page._fetchAttributes");
	
	this.properties = {
	    "cname"	 : "Page",
	    "author"     : "Mel Heravi",
	    "version"    : "1.0",
	    "background" : "#FFF",
	    "width"      : "1280"
	};

	if (this.hasAttribute("background")) {
	    this.properties.background = this.getAttribute("background");
	    console.log("background: ", this.properties.background);
	}

	if (this.hasAttribute("width")) {
	    this.properties.width = this.getAttribute("width");
	    console.log("width: ", this.properties.width);
	}

	console.groupEnd();
    };

    /**
     * Destroy the instance object and artifacts
     * @_destroy
     */
    destroy() {
	console.group("Message.destroy:", this.id);

	// FREE POINTER
	delete this;

	// REMOVE ITEM FROM DOM
	this.parentNode.removeChild(this);

	console.groupEnd();
    };

    /**
     * SAVE DATA FOR ANALYTICS
     * @__finalize
     */
    _finalize() {
	console.group("Page._finalize:", this.id);

	this.classList.add("wc");

	// ADD ANALYTICS HERE
	wc.getStats(this, this.properties.cname, this.properties.version);
	
	console.groupEnd();
    };

    /**
     * FOR TESTING PURPOSES
     * @test
     */
    static test() {
	console.group("Page.test");

	console.log("testing results will be printed here...");

	console.groupEnd();
	return true;
    }
}

window.customElements.define('wc-page', Page);