Source: textarea.js

/**
 * Textarea Component
 * <BR><BR><img src=../images/textarea.png width=60% style="border:1px lime dashed";>
 * <BR><BR><a href="../html/textarea.html">DEMO</a>
 */
class Textarea extends HTMLElement {
    constructor() {
        console.group("Textarea.constructor")
	
        super();

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

	this.observables = ["defaultWidth"];

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

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

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

	if (typeof this.properties.label === "undefined") {
	    this.label = "";
	} else {
	    this.label = `<label class="control-label" type="label" for="${this.id}" id=${this.id}-label">${this.properties.label}</label>`
	}

	if (this.properties.defaultWidth) {
	    this.classList.add("default");
	} else {
	    this.classList.add("stretch");
	}

	// REPLACE CONTENT IF NECESSARY WITH NEW STUFF
	this.innerHTML = `
		<div class="form-group">
		    <div class="form-element form-lhs">
		        ${this.label}
		    </div>

		    <div class="form-element form-rhs">
		        <textarea class="form-control ${this.properties.class}" id="${this.id}">${this.properties.value}</textarea>
		    </div>

		    <div class="help-block with-errors"></div>
		  </div>`

	// TRANSFER ALL ATTRIBUTES NOW (below is an example)
	let widget = this.querySelector("textarea");
	let label  = this.querySelector("label");

 	for (var key in this.propertiesW) {
	    this.removeAttribute(key);
	    if (key != "class") {
		widget.setAttribute(key, this.properties[key]);
	    }
	}	

	// IF WIDTH IS SET
	if (this.properties.width) {
	    $(this).find("textarea").css({minWidth:this.properties.width, maxWidth:this.properties.width});
	}

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

	/* CLEAN UP NOW */

        console.groupEnd();
    };

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

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

	let obs = Textarea.observedAttributes;

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

    /**
     * Stores DOM elements of interest for future use
     * @_fetchElements
     */
    _fetchElements() {
	console.group("Textarea._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} [background=indianred] background color
     */
    _fetchAttributes() {
	console.group("Textarea._fetchAttributes");
	
	this.properties = {
	    "cname"	 : "Textarea",
	    "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();
    };

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

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

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

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

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

	console.groupEnd();
	return true;
    }
}

window.customElements.define('wc-textarea', Textarea);