Source: announcements.js

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

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

	this.observables = [];

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

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

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

	// REPLACE CONTENT IF NECESSARY WITH NEW STUFF
	this.innerHTML = `
		<div class="clearfix">
		    <div class="wc-announcements-lhs">
			<div class="wc-announcements-header">
		            ${this.dom.header}
			</div>

			<div class="wc-announcements-pages">
			</div>
		    </div>

		    <div class="wc-announcements-rhs">
		        <div class="wc-announcements-rhs-inner">
		        </div>
		    </div>
		</div>`

	let pages = _(".wc-announcements-pages");
	let rhs = _(".wc-announcements-rhs-inner");

	for (var i=0; i<this.dom.body.length; i++) {
	    let body = this.dom.body[i]
	    body.setAttribute("id","body-" + i);

	    if (i == 0) {
		body.style.display = "block";
		rhs.innerHTML += "<a href='#' class='dot-active' onclick=" + self._onClick(i) + "><i class='fa fa-circle'></i></a>"
	    } else {
		body.style.display = "none";
		rhs.innerHTML += "<a href='#' class='dot' onclick=" + self._onClick(i) + "><i class='fa fa-circle-o'></i></a>"
	    }

	    pages.append(body);
	}

	let dots = __(".dot");

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

	/* CLEAN UP NOW */

        console.groupEnd();
    };

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

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

	let obs = Announcements.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("Announcements._fetchElements");
	
	this.dom = this.dom || [];
	this.dom.content = this.innerHTML;
	this.dom.header = this.querySelector(".wc-announcements-header").innerHTML;
	this.dom.body = this.querySelectorAll(".wc-announcements-body");

	console.groupEnd();
    };

    /**
     * Component attributes are _fetched and defaults are set if undefined
     * @_fetchAttributes
     * @param {string} [background=indianred] background color
     */
    _fetchAttributes() {
	console.group("Announcements._fetchAttributes");
	
	this.properties = {
	    "cname"	 : "Announcements",
	    "author"     : "Mel Heravi",
	    "version"    : "1.0"
	};
	
	// 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(which) {
	console.group("Announcements._onClick:", which);

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

	console.groupEnd();
    };

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

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

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

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

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

	console.groupEnd();
	return true;
    }
}

window.customElements.define('wc-announcements', Announcements);