Source: profile.js

/**
 * Profile Component - used by "Portal Dashboard"
 * <BR><BR><img src=../images/profile.png width=40%>
 * <BR><BR><a href="../html/profile.html">DEMO</a>
 */
class Profile extends HTMLElement {
    constructor() {
        console.group("Profile.constructor")
	
        super();

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

	this.observables = [];

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

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

	// GET PROPERTIES AND INTERESTING ELEMENTS
	this._initialize();

	// REPLACE CONTENT IF NECESSARY WITH NEW STUFF
	this.innerHTML = `
		    <div class="wc-profile-lhs">
			<div class="wc-profile-header">
			   <div>
				${this.properties.name}
			   </div>
			</div>
			<div class="wc-profile-body">
			    <table>
				<tbody>
				    <tr>
					<td>Phone:</td>
					<td>${this.properties.phone}</td>
				    </tr>
				    <tr>
					<td>Email:</td>
					<td><a href=mailto:${this.properties.email}>${this.properties.email}</a></td>
				    </tr>
				    <tr>
					<td colspan="2"><a href="#" class="profile-link" id="${this.id}-email">Update Email ID</a></td>
				    </tr>
				</tbody>
			    </table>
			</div>
		    </div>
		    <div class="wc-profile-rhs">
			<i class="fa fa-lock fa-2x"></i>
		        <div style=line-height:18px;margin-top:10px;>
			    <a href="#" class="profile-link" id="${this.id}-password">Change your<BR>password online!</a>
		        </div>
		    </div>`

	let links = this.querySelectorAll(".profile-link");

	for (var i=0; i<links.length; i++) {
	    let link = links[i]

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

	// ADD STATS AND OTHER FINAL STUFF
	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("Profile.disconnectedCallback")

	/* CLEAN UP NOW */

        console.groupEnd();
    };

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

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

	let obs = Profile.observedAttributes;

	for (let i = 0; i < obs.length; i++) {
	    this.properties[obs[i]] = newval;
	    // YOUR CODE FOR CHANGES GO HERE 
	}
	
        console.groupEnd();
    };

    /**
     * Stores DOM elements of interest for future use
     * @_fetchElements
     */
    _fetchElements() {
	console.group("Profile._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=NOTSET] user name
     * @param {string} [phone=NOTSET] user phone number
     * @param {string} [email=NOTSET] user email
     */
    _fetchAttributes() {
	console.group("Profile._fetchAttributes");
	
	this.properties = {
	    "cname"	 : "Profile",
	    "author"     : "Mel Heravi",
	    "version"    : "1.0",
	    "name"       : "NOTSET",
	    "phone"      : "NOTSET",
	    "email"      : "NOTSET"
	};
	
	// SAVE WIDGET SPECIFIC PROPERTIES
	this.propertiesW = [];

	// SAVE ALL OTHER PROPERTIES
	let attrs = wc.getAttributes(this)
	
 	for (var key in attrs) {
	    this.properties[key]  = this.getAttribute(key);
	    this.propertiesW[key] = this.getAttribute(key);
	    console.log(key + ": " + attrs[key]);
	}

	console.log("attributes: ", this.properties);

	// PROCESS ALL PROPERTIES (example below);
	// this.style.background = this.properties.background;

	console.groupEnd();
    };

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

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

	console.groupEnd();
    };

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

	// FREE POINTER
	delete this;

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

	console.groupEnd();
    };

    /**
     * SAVE DATA FOR ANALYTICS
     * @__initialize
     */
    _initialize() {
	console.group("Profile._initialize:", this.id);

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

	// FETCH ALL ATTRIBUTES
	this._fetchAttributes();
	
	console.groupEnd();
    };

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

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

	console.groupEnd();
	return true;
    }
}

window.customElements.define('wc-profile', Profile);