Source: icon.js

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

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

	this.observables = ["name", "size"];
	console.log(this.observables);

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

    /**
     * This function is called when this is attached to DOM
     * @connectedCallback. 
     */
    connectedCallback() {
        console.group("Icon.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();

	this.innerHTML = `<i class='fa fa-${this.properties.name} ${this.properties.size} fa-fw'></i>`;

	this.addEventListener("click", function() {
	    self._onClick();
	});

	// 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("Icon.disconnectedCallback")

	/* CLEAN UP NOW */

        console.groupEnd();
    };

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

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

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

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

    /**
     * Stores DOM elements of interest for future use
     * @_fetchElements
     */
    _fetchElements() {
	console.group("Icon._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} name name of the icon
     * @param {string} [size=sm] size of the icon
     */
    _fetchAttributes() {
	console.group("Icon._fetchAttributes");
	
	this.properties = {
	    "cname"      : "Icon",
	    "author"     : "Mel Heravi",
	    "version"    : "1.0",
	    "name"       : "",
	    "size"       : "sm"
	};

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

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

	console.groupEnd();
    };

    /**
     * A sample callback usage function - see connectedCallback()
     * @_onClick
     */
    _onClick() {
	console.group("Icon._onClick:", this.id);

	wc.publish(this, "wc-icon", {
	    action: "click",
	    id: this.id
	});

	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("Icon._finalize:", this.id);

	this.classList.add("wc");

	this.properties = {
	    "cname"     : "Icon",
	    "author"    : "Mel Heravi",
	    "version"   : "1.0"
	};

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

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

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

	console.groupEnd();
	return true;
    }
}

window.customElements.define('wc-icon', Icon);