/**
* A Dropdown component - 90% UX/COE Compliant
* <BR><BR><img src=../images/dropdown.png width=30% style="border:1px lime dashed";>
* <BR><BR><a href="../html/dropdown.html">DEMO</a>
*/
class Dropdown extends HTMLElement {
constructor() {
console.group("Dropdown.constructor")
super();
console.groupEnd();
};
/**
* Set observable values here. When Changed, attributeChangedCallback is invoked
* @observedAttributes
*/
static get observedAttributes() {
console.group("Dropdown.observedAttributes");
this.observables = ["title", "width"];
console.log(this.observables);
console.groupEnd();
return this.observables;
};
/**
* This function is called when this is attached to DOM
* @connectedCallback.
*/
connectedCallback() {
console.group("Dropdown.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.classList.add("dropdown");
let str = "";
str +=
"<button class='btn btn-default dropdown-toggle' type='button' id='" +
this.id + "-container' data-toggle='dropdown' aria-haspopup='true' aria-expanded='true'>" +
this.properties.title + " <span class='fa fa-angle-down'></span></button>" + this.dom.content;
// REFRESH DOM NOW
this.innerHTML = str;
this.querySelector(".dropdown-toggle").style.width = this.properties.width;
let tmp = this.querySelector("ul");
tmp.classList.add("dropdown-menu");
tmp.setAttribute("aria-labelledby", this.id + "container");
let items = this.querySelectorAll("ul li a");
// ADD CLICK CALLBACK. USER CAN IGNORE IF THE HAVE LINKS SETUP ALREAD
for (let i=0; i<items.length; i++) {
let item = items[i];
item.addEventListener("click", function() {
self._onClick(item);
});
}
$("#" + this.id + " .dropdown-menu").on('click', 'li a', function(){
$(this).parent().parent().siblings(".btn:first-child").html($(this).text() + "<span class='fa fa-angle-down'></span>");
$(this).parent().parent().siblings(".btn:first-child").val($(this).text());
});
// 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("Dropdown.disconnectedCallback")
/* CLEAN UP NOW */
console.groupEnd();
};
/**
* Called with .setAttribute(...) function call
* @attributeChangedCallback
*/
attributeChangedCallback(attr, oldval, newval) {
console.group("Dropdown.attributeChangedCallback:", attr, oldval, newval);
this.properties = this.properties || [];
// EXAMPLE ONLY. replace with observables
switch(attr)
{
case "title":
this.properties.title = newval;
this.style.title = this.properties.title;
break;
case "width":
this.properties.width = newval;
this.style.width = this.properties.width;
break;
}
console.groupEnd();
};
/**
* Stores DOM elements of interest for future use
* @_fetchElements
*/
_fetchElements() {
console.group("Dropdown._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} [width=auto] width of dropdown button
*/
_fetchAttributes() {
console.group("Dropdown._fetchAttributes");
this.properties = {
"cname" : "Dropdown",
"author" : "Mel Heravi",
"version" : "1.0",
"title" : "NOT SET",
"width" : "auto"
};
if (this.hasAttribute("title")) {
this.properties.title = this.getAttribute("title");
console.log("title: ", this.properties.title);
}
if (this.hasAttribute("width")) {
this.properties.width = this.getAttribute("width");
console.log("width: ", this.properties.width);
}
console.groupEnd();
};
/**
* A sample callback usage function - see connectedCallback()
* @_onClick
* @param {object} item link that was clicked on
*/
_onClick(item) {
console.group("Dropdown._onClick:", item);
//PUBLISH TO THE EVENT
wc.publish(this, "wc-tabs", {
item: item,
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("Dropdown._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("Dropdown.test");
console.log("testing results will be printed here...");
console.groupEnd();
return true;
}
}
window.customElements.define('wc-dropdown', Dropdown);