/**
* A ALert component - <strike>UX/COE Compliant</strike><BR>
* <BR><BR><img src=../images/alert.png width=100% style="border:1px lime dashed";>
* <BR><BR><a href="../html/alert.html">DEMO</a>
*/
class Alert extends HTMLElement {
constructor() {
console.group("Alert.constructor")
super();
console.groupEnd();
};
/**
* Set observable values here. When Changed, attributeChangedCallback is invoked
* @observedAttributes
*/
static get observedAttributes() {
console.group("Alert.observedAttributes");
this.observables = ["message"];
console.log(this.observables);
console.groupEnd();
return this.observables;
};
/**
* This function is called when this is attached to DOM
* @connectedCallback.
*/
connectedCallback() {
console.group("Alert.connectedCallback")
let self = this;
// FETCH ALL INTERESTING ELEMENTS
this._fetchElements();
// FETCH ALL ATTRIBUTES
this._fetchAttributes();
// REPLACE CONTENT IF NECESSARY WITH NEW STUFF
this.innerHTML = `
<wc-alert-inner>
<wc-alert-inner-lhs>
<wc-alert-icon>
<i class='fa fa-warning'></i><span>ALERT:</span>
</wc-alert-icon>
<wc-alert-message>
${this.dom.content}
</wc-alert-message>
</wc-alert-inner-lhs>
<wc-alert-inner-rhs>
<a href='#'>
CLOSE
<span class='closeit'><i class='fa fa-times'></i></span>
</a>
</wc-alert-inner-rhs>
</wc-alert-inner>`;
let closer = this.querySelector("wc-alert-inner-rhs")
closer.addEventListener("click", function() {
self._onClick();
});
if (this.classList.contains("thin")) {
let lhs = this.querySelector("wc-alert-inner-lhs");
let rhs = this.querySelector("wc-alert-inner-rhs");
lhs.style.marginLeft = "48px";
rhs.style.marginRight = "48px";
}
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("Alert.disconnectedCallback")
/* CLEAN UP NOW */
console.groupEnd();
};
/**
* Called with .setAttribute(...) function call
* @attributeChangedCallback
*/
attributeChangedCallback(attr, oldval, newval) {
console.group("Alert.attributeChangedCallback:", attr, oldval, newval);
this.properties = this.properties || [];
// EXAMPLE ONLY. replace with observables
switch(attr)
{
case "message":
this.properties.message = newval;
this.querySelector("wc-alert-message").innerHTML = this.properties.message;
break;
}
console.groupEnd();
};
/**
* Stores DOM elements of interest for future use
* @_fetchElements
*/
_fetchElements() {
console.group("Alert._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} [message=NOTSET] message text
*/
_fetchAttributes() {
console.group("Alert._fetchAttributes");
this.properties = {
"cname" : "Alert",
"author" : "Mel Heravi",
"version" : "1.0",
"message" : "NOTSET"
};
// FETCH MESSAGE
if (this.hasAttribute("message")) {
this.properties.message = this.getAttribute("message");
console.log("message: ", this.properties.message);
}
console.groupEnd();
};
/**
* A sample callback usage function - see connectedCallback()
* @_onClick
*/
_onClick() {
console.group("Button._onClick:", this.id);
this.addEventListener("click", function() {
this.style.display = "none";
});
wc.publish(this, "wc-alert", {
id: this.id,
action: "closed"
});
console.groupEnd();
};
/**
* Destroy the instance object and artifacts
* @_destroy
*/
destroy() {
console.group("Alert.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("Alert._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("Alert.test");
console.log("testing results will be printed here...");
console.groupEnd();
return true;
}
}
window.customElements.define('wc-alert', Alert);