Source: sidebar.js

/**
 * A SIDEBAR COMPONENT<BR>
 * <BR><BR><img src=../images/sidebar.png width=100% style="border:1px lime dashed";>
 * <BR><BR><a href="../html/sidebar.html">DEMO</a>
 */
class Sidebar extends HTMLElement {
    constructor() {
        console.group("Sidebar.constructor")
	
        super();

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

	this.observables = ["lhs","rhs","offset"];
	console.log(this.observables);

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

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

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

	// FETCH ALL ATTRIBUTES
	this._fetchAttributes();

	// REPLACE CONTENT IF NECESSARY WITH NEW STUFF
	this.innerHTML = `
		<div class='container'>
		    <div class='row'>
			${this.dom.content}
		    </div>
		</div>`

	this.lhs = this.querySelector('wc-sidebar-lhs')
	this.rhs = this.querySelector('wc-sidebar-rhs')

	this.lhs.classList.add(this.properties.lhs);
	this.rhs.classList.add(this.properties.rhs);
	
	window.addEventListener("resize", function() {
	    self._onResize();
	});

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

	/* CLEAN UP NOW */

        console.groupEnd();
    };

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

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

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

	    case "rhs":
	    this.properties.rhs = newval;
	    break;

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

    /**
     * Stores DOM elements of interest for future use
     * @_fetchElements
     */
    _fetchElements() {
	console.group("Sidebar._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} [lhs=col-md-3] class for left hand side
     * @param {string} [rhs=col-md-9] class for right hand side
     * @param {string} [offset=0] bottom margin for sidebar
     */
    _fetchAttributes() {
	console.group("Sidebar._fetchAttributes");
	
	this.properties = {
	    "cname"	 : "Sidebar",
	    "author"     : "Mel Heravi",
	    "version"    : "1.0",
	    "lhs"	 : "col-md-3",
	    "rhs"	 : "col-md-9",
	    "offset"	 : "0"
	};
	
	if (this.hasAttribute("lhs")) {
	    this.properties.lhs = this.getAttribute("lhs");
	    console.log("lhs: ", this.properties.lhs);
	}

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

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

	console.groupEnd();
    };

    /**
     * Destroy the instance object and artifacts
     * @_destroy
     */
    destroy() {
	console.group("Sidebar.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("Sidebar._finalize:", this.id);

	this.classList.add("wc");

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

    /**
     * A sample callback usage function - see connectedCallback()
     * @_onClick
     */
    _onResize() {
	console.group("Panel._onResize");

	let html = document.documentElement;
	
	let height = html.clientHeight;

	this.lhs.style.height = (height - this.properties.offset) + "px";
	this.rhs.style.height = (height - this.properties.offset) + "px";

	console.groupEnd();
    };

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

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

	console.groupEnd();
	return true;
    }
}

window.customElements.define('wc-sidebar', Sidebar);