/**
* A Button component - 90% UX/COE Compliant<BR>
* <BR><BR><img src=../images/button.png width=70% style="border:1px lime dashed";>
* <BR><BR><a href="../html/button.html">DEMO</a>
*/
class Button extends HTMLElement {
constructor() {
console.group("Button.constructor")
super();
console.groupEnd();
};
/**
* Set observable values here. When Changed, attributeChangedCallback is invoked
* @observedAttributes
*/
static get observedAttributes() {
console.group("Button.observedAttributes");
this.observables = [];
console.log(this.observables);
console.groupEnd();
return this.observables;
};
/**
* This function is called when this is attached to DOM
* @connectedCallback.
*/
connectedCallback() {
console.group("Button.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();
this.innerHTML = "<button type='button' class='" + this.properties.class + "'>" + this.dom.content + "</button>";
if (!this.querySelector("button").classList.contains("btn-disabled")) {
this.addEventListener("click", function() {
self._onClick();
});
}
// WRAP UP AND ADD STATS
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("Button.disconnectedCallback")
/* CLEAN UP NOW */
console.groupEnd();
};
/**
* Called with .setAttribute(...) function call
* @attributeChangedCallback
*/
attributeChangedCallback(attr, oldval, newval) {
console.group("Button.attributeChangedCallback:", attr, oldval, newval);
this.properties = this.properties || [];
switch(attr)
{
case "class":
this.properties.class = newval;
break;
}
console.groupEnd();
};
/**
* Stores DOM elements of interest for future use
* @_fetchElements
*/
_fetchElements() {
console.group("Button._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} [class=btn btn-primary btn-lg] button class
*/
_fetchAttributes() {
console.group("Button._fetchAttributes");
this.properties = {
"cname" : "Button",
"author" : "Mel Heravi",
"version" : "1.0"
};
if (this.hasAttribute("class")) {
this.properties.class = this.getAttribute("class");
$(this).removeClass(this.getAttribute("class"));
console.log("class: ", this.properties.class);
}
console.groupEnd();
};
/**
* A sample callback usage function - see connectedCallback()
* @_onClick
*/
_onClick() {
console.group("Button._onClick:", this.id);
wc.publish(this, "wc-button", {
id: this.id,
action: "click"
});
console.groupEnd();
};
/**
* Destroy the instance object and artifacts
* @_destroy
*/
destroy() {
console.group("Message.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("Button._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("Button.test");
console.log("testing results will be printed here...");
console.groupEnd();
return true;
}
}
window.customElements.define('wc-button', Button);