Source: notification.js

/**
 * A Notification component - <strike>UX/COE Compliant</strike>
 * <BR><font color=green>success</font>, <font color=steelblue>information</font>, <font color=orange>warning</font>, <font color=red>errors</font> 
 * <BR><BR><img src=../images/notification.png width=100% style="border:1px lime dashed";>
 * <BR><BR><a href="../html/notification.html">DEMO</a>
 */
class Notification extends HTMLElement {
    constructor() {
        console.group("Notification.constructor")
	
        super();

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

	let observables = ["width", "icon"];
	console.log(observables);

        console.groupEnd();
        return observables;
    };

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

	if (!this.dom.ctrl) {
	    // THERE ARE NO CONTROLS. CREATE A CLOSE LINK
	    let tmp = document.createElement("wc-notification-close");
	    this.dom.header.appendChild(tmp);
    
	    this.dom.close = this.querySelector("wc-notification-close");

	    this.dom.close.addEventListener("click", function() {
		self._onClick();
	    });
	} else {
	    // IF THERE IS A btn-close CLASS ON LINKS/BUTTONS
	    let close = this.querySelector(".btn-close");
	    
	    if (close) {
		close.addEventListener("click", function() {
		    self.destroy();
		});
	    }
	}

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

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

        console.groupEnd();
    };

    /**
     * This function is called when this is removed from DOM
     * @disconnectedCallback. 
     */
    disconnectedCallback() {
        console.group("Notification.disconnectedCallback")

	// CLEANUP NOW

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

	/* CLEAN UP NOW */

        console.groupEnd();
    };

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

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

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

	    case "icon":
	    this.properties.icon = newval;

	    let h = this.querySelector("wc-notification-header");
	    let c = h.innerHTML;
	    
	    h.innerHTML = "<i class='fa fa-" + this.properties.icon + "'></i>&nbsp;" + c;
	    console.log(">>>>>>", h.innerHTML);

	    this.style.icon = this.properties.icon;
	    break;
	}
	
        console.groupEnd();
    };

    /**
     * Stores DOM elements of interest for future use
     * @_fetchElements
     */
    _fetchElements() {
	console.group("Notification._fetchElements");
	
	this.dom = this.dom || [];
	this.dom.content = this.innerHTML;
	this.dom.ctrl    = this.querySelector("wc-notification-controls");
	this.dom.header  = this.querySelector("wc-notification-header");

	console.groupEnd();
    };

    /**
     * Component attributes are _fetched and defaults are set if undefined
     * @_fetchAttributes
     * @param {string} author component owner
     * @param {string} version Latest version of this component
     * @param {string} [width:100%] notification width
     * @param {string} [icon:""] icon to be used by notification
     */
    _fetchAttributes() {
	console.group("Notification._fetchAttributes");
	
	this.properties = {
	    "cname"   : "Notification",
	    "author"  : "Mel Heravi",
	    "version" : "1.0",
	    "width"   : "100%",
	    "icon"    : ""
	};

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

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

	console.groupEnd();
    };

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

	if ($('.modal').hasClass('in')) {
	    $(".modal").modal("hide");
	    $(".modal").data('bs.modal', null); // destroy the modal;
	} else {
	    this.destroy();
	}

	console.groupEnd();
    };

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

	delete this;

	this.parentNode.removeChild(this);

	console.groupEnd();
    };

    /**
     * SAVE DATA FOR ANALYTICS
     * @__finalize
     */
    _finalize() {
	console.group("Notification._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("Notification.test");

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

	console.groupEnd();
	return true;
    }

}

window.customElements.define('wc-notification', Notification);