Source: tabs.js

/**
 * A Tab container implementation - UX/COE Compliant
 * <BR><BR><img src=../images/tabs.png width=60% style="border:1px lime dashed";>
 * <BR><BR><a href="../html/tabs.html">DEMO</a>
 */
class Tabs extends HTMLElement {
    constructor() {
        console.group("Tabs.constructor")
	
        super();

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

	let observables = ["active"];
	console.log(observables);

        console.groupEnd();
        return observables;
    };

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

	// ADD OUR GLOBAL CLASS
	this.classList.add("wc");

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

	// FETCH ALL ATTRIBUTES
	this._fetchAttributes();

	// ADD THE CONTENT
	this.innerHTML = "<wc-tabs-header><ul></ul></wc-tabs-header>" + this.dom.content;

	this._init();

	this.style.width  = this.properties.width;
	this.style.height = this.properties.height;

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

	// CLEANUP

        console.groupEnd();
    };

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

	/* CLEAN UP NOW */

        console.groupEnd();
    };

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

	switch(attr)
	{
	    case "xxx":
	    this.properties.xxx = newval;
	    break;
	}
	
        console.groupEnd();
    };

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

	console.groupEnd();
    };

    /**
     * Component attributes are fetched and defaults are set if not provided
     * @_fetchAttributes
     * @param {string} author component owner
     * @param {string} version Latest version of this component
     * @param {string} [width=100%] panel width
     * @param {string} [height=100%] body height
     * @param {integer} [active=0] index of active tab
     */
    _fetchAttributes() {
	console.group("Tabs._fetchAttributes");
	
	this.properties = {
	    "cname"   : "Tabs",
	    "author"  : "Mel Heravi",
	    "version" : "1.0",
	    "active"  : 0,
	    "width"   : "100%",
	    "height"  : "100%"
	};

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

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

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

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

	console.groupEnd();
    };

    /**
     * BUILDS OUT TABS
     * @_init
     */
    _init() {
        console.group("Tabs._init")

	this.properties.active = this.getAttribute("active");
	this.dom.tabs = this.querySelectorAll("wc-tab");

	let ul = this.querySelector("wc-tabs-header ul");

	for (let i=0; i<this.dom.tabs.length; i++) {
	    if (i == this.properties.active) {
		ul.innerHTML += "<li class='active'>" + this.dom.tabs[i].title;
		this.dom.tabs[i].style.display = "block";

		// FAT ARROWS ARE NOT SUPPORTED
		let self = this;
		let indx = i;
	    } else {
		ul.innerHTML += "<li>" + this.dom.tabs[i].title;
	    }
	}

	let self = this;
	this.dom.lis = this.querySelectorAll("wc-tabs-header ul li");

	for (let i=0; i<this.dom.lis.length; i++) {
	    let li = this.dom.lis[i];

	    li.addEventListener("click", function(e) {
		self._onclick(i);
	    });
	}
	
        console.groupEnd();
    };

    /**
     * A sample callback usage function - see connectedCallback()
     * @_onclick
     * @param {integer} which index of tab that was clicked
     */
    _onclick(which) {
	console.group("Tabs._onclick:", which);

	for (let i=0; i<this.dom.lis.length; i++) {
	    if (i == which) {
		this.dom.lis[i].classList.add("active");
		this.dom.tabs[i].style.display = "block";
		this.properties.active = i;
	    } else {
		this.dom.lis[i].classList.remove("active");
		this.dom.tabs[i].style.display = "none";
	    }
	}
	
	//PUBLISH TO THE EVENT
	wc.publish(this, "wc-tabs", {
	    action: "click",
	    id: which
	});

	console.groupEnd();
    };

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

	delete this;

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

	console.groupEnd();
    };

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

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

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

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

	console.groupEnd();
	return true;
    }

}

window.customElements.define('wc-tabs', Tabs);