/**
* Access Component - used by "Portal Dashboard"<BR>
* <BR><BR><img src=../images/access.png width=60%>
* <BR><BR><a href="../html/access.html">DEMO</a>
*/
class Access extends HTMLElement {
constructor() {
console.group("Access.constructor")
super();
console.groupEnd();
};
/**
* Set observable values here. When Changed, attributeChangedCallback is invoked
* @observedAttributes
*/
static get observedAttributes() {
console.group("Access.observedAttributes");
this.observables = ["background"];
console.groupEnd();
return this.observables;
};
/**
* This function is called when this is attached to DOM
* @connectedCallback.
*/
connectedCallback() {
console.group("Access.connectedCallback")
let self = this;
// GET PROPERTIES AND INTERESTING ELEMENTS
this._initialize();
// REPLACE CONTENT IF NECESSARY WITH NEW STUFF
this.innerHTML = `
<div class="wc-access-lhs">
<i class='fa fa-${this.properties.icon} fa-2x'></i>
</div>
<div class="wc-access-rhs">
<div class="wc-access-header">
<div class="wc-access-header-inner">
${this.dom.header.innerHTML}
</div>
<div class="wc-access-body">
${this.dom.body.innerHTML}
</div>
</div>
`
// IF IT IS HOVERABLE ADD HOVER EFFECT AND PUBLISH CLICK EVENTS
if (this.properties.hoverable == "true") {
this.classList.add("hoverable");
this.addEventListener("click", function() {
self._onClick();
});
}
// 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("Access.disconnectedCallback")
/* CLEAN UP NOW */
console.groupEnd();
};
/**
* Called with .setAttribute(...) function call
* @attributeChangedCallback
*/
attributeChangedCallback(attr, oldval, newval) {
console.group("Access.attributeChangedCallback:", attr, oldval, newval);
this.properties = this.properties || [];
let obs = Access.observedAttributes;
for (let i = 0; i < obs.length; i++) {
this.properties[obs[i]] = newval;
console.log(obs[i] + ": " + this.properties.background);
// YOUR CODE FOR CHANGES GO HERE
}
console.groupEnd();
};
/**
* Stores DOM elements of interest for future use
* @_fetchElements
*/
_fetchElements() {
console.group("Access._fetchElements");
this.dom = this.dom || [];
this.dom.content = this.innerHTML;
this.dom.header = this.querySelector(".wc-access-header");
this.dom.body = this.querySelector(".wc-access-body");
console.groupEnd();
};
/**
* Component attributes are _fetched and defaults are set if undefined
* @_fetchAttributes
* @param {string} [icon=key] icon to be displayed on left hand side
*/
_fetchAttributes() {
console.group("Access._fetchAttributes");
this.properties = {
"cname" : "Access",
"author" : "Mel Heravi",
"version" : "1.0",
"icon" : "key",
"hoverable" : "true"
};
// 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() {
console.group("Access._onClick:", this.id);
wc.publish(this, "wc-access", {
action: "click",
id: this.id
});
console.groupEnd();
};
/**
* Destroy the instance object and artifacts
* @_destroy
*/
destroy() {
console.group("Access.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("Access._initialize:", this.id);
// FETCH ALL INTERESTING ELEMENTS
this._fetchElements();
// FETCH ALL ATTRIBUTES
this._fetchAttributes();
console.groupEnd();
};
/**
* SAVE DATA FOR ANALYTICS
* @__finalize
*/
_finalize() {
console.group("Access._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("Access.test");
console.log("testing results will be printed here...");
console.groupEnd();
return true;
}
}
window.customElements.define('wc-access', Access);