/**
* A NAVIGATION COMPONENT <BR>
* <BR><BR><img src=../images/nav.png width=50% style="border:1px lime dashed";>
* <BR><BR><a href="../html/nav.html">DEMO</a>
*/
class Nav extends HTMLElement {
constructor() {
console.group("Nav.constructor")
super();
console.groupEnd();
};
/**
* Set observable values here. When Changed, attributeChangedCallback is invoked
* @observedAttributes
*/
static get observedAttributes() {
console.group("Nav.observedAttributes");
this.observables = ["config", "direction"];
console.log(this.observables);
console.groupEnd();
return this.observables;
};
/**
* This function is called when this is attached to DOM
* @connectedCallback.
*/
connectedCallback() {
console.group("Nav.connectedCallback")
let self = this;
// FETCH ALL INTERESTING ELEMENTS
this._fetchElements();
// FETCH ALL ATTRIBUTES
this._fetchAttributes();
if (this.properties.config) {
// IF WE HAVE A CONFIG PROPERTY
let url = location.protocol + "//" + location.host + this.properties.config;
$.getJSON(url, function(data) {
let str = "<ul class='list-group'>";
for (var i = 0; i < data.length; i++) {
str += "<a href='" + data[i].url + "' id='" + data[i].id + "' class='list-group-item " + self.properties.direction + "'>" + data[i].text + "</a>";
}
str += "</ul>";
self.innerHTML = str;
let links = self.querySelectorAll(".list-group-item");
for (i=0; i < links.length; i++) {
let link = links[i];
links[i].addEventListener("click", function() {
self._onClick(link);
});
}
});
} else {
// IF WE HAVE A INLINE LINKS
let links = this.querySelectorAll("wc-nav-links a");
let str = "<ul class='list-group'>";
for (let i = 0; i < links.length; i++) {
let link = links[i];
link.classList.add(self.properties.direction);
link.classList.add("list-group-item");
str += link.outerHTML;
link.addEventListener("click", function() {
self._onClick(link);
});
}
str += "</ul>";
}
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("Nav.disconnectedCallback")
/* CLEAN UP NOW */
console.groupEnd();
};
/**
* Called with .setAttribute(...) function call
* @attributeChangedCallback
*/
attributeChangedCallback(attr, oldval, newval) {
console.group("Nav.attributeChangedCallback:", attr, oldval, newval);
this.properties = this.properties || [];
switch(attr)
{
case "config":
this.properties.config = newval;
break;
case "direction":
this.properties.direction = newval;
break;
}
console.groupEnd();
};
/**
* Stores DOM elements of interest for future use
* @_fetchElements
*/
_fetchElements() {
console.group("Nav._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} config JSON config file
* @param {string} [direction=vertical] direction of items
*/
_fetchAttributes() {
console.group("Nav._fetchAttributes");
this.properties = {
"cname" : "Nav",
"author" : "Mel Heravi",
"version" : "1.0",
"config" : null,
"direction" : "vertical",
};
if (this.hasAttribute("config")) {
this.properties.config = this.getAttribute("config");
console.log("config: ", this.properties.config);
}
if (this.hasAttribute("direction")) {
this.properties.direction = this.getAttribute("direction");
console.log("direction: ", this.properties.direction);
}
console.groupEnd();
};
/**
* A sample callback usage function - see connectedCallback()
* @_onClick
*/
_onClick(link) {
console.group("Nav._onClick:", link);
// REMOVE ALL ACTIVES
$("#" + this.id + " .list-group-item").removeClass("active");
// MAKE CLICKED LINK ACTIVE
link.classList.add("active");
wc.publish(this, "wc-nav", {
action: "click",
nav: this.id,
id: link.id
});
console.groupEnd();
};
/**
* Destroy the instance object and artifacts
* @_destroy
*/
destroy() {
console.group("Nav.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("Nav._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("Nav.test");
console.log("testing results will be printed here...");
console.groupEnd();
return true;
};
/**
* FETCH JSON FILE
* @_getJSON
*/
_getJSON(path,callback) {
console.group("nav._getJSON");
var url = location.protocol + "//" + location.host + "/" + path;
var xhr = new XMLHttpRequest();
xhr.open("get", url, true);
xhr.responseType = "json";
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
callback(null, xhr.response);
} else {
callback(status);
}
};
xhr.send();
console.groupEnd();
return true;
};
}
window.customElements.define('wc-nav', Nav);