/**
* Ticker Component<BR>
* <BR><BR><img src=/tk/lib/components/w/img/ticker.png width=30% style="border:1px lime dashed;padding:20px">
* <BR><BR><a href="/tk/lib/components/w/html/ticker.html">DEMO</a>
*/
class Ticker extends HTMLElement {
constructor() {
wc.group("Ticker.constructor")
super();
wc.groupEnd();
};
/**
* Set observable values here. When Changed, attributeChangedCallback is invoked
* @observedAttributes
*/
static get observedAttributes() {
wc.group("Ticker.observedAttributes");
this.observables = [];
wc.groupEnd();
return this.observables;
};
/**
* This function is called when this is attached to DOM
* @connectedCallback.
*/
connectedCallback() {
wc.group("Ticker.connectedCallback")
let self = this;
// GET PROPERTIES AND INTERESTING ELEMENTS
this._initialize();
// REPLACE CONTENT FROM TEMPLATE
this.innerHTML = "<wc-ticker-container class=bg-dark></wc-ticker-container>"
let s = this.properties.symbols;
//alert(s.split(","));
// SAVE THESE FOR setInterval
this.symbols = this.properties.symbols.split(",");
this.interval = this.properties.interval;
for (var i in this.symbols) {
$("wc-ticker-container").append(`<span class="${this.symbols[i]} m-2">${this.symbols[i]}</span>`);
}
this._update();
//setInterval(this._update, this.interval * 1000);
// ADD STATS AND OTHER FINAL STUFF
this._finalize();
wc.groupEnd();
};
/**
* Publish all events
* @private
* @_publish
*/
_publish() {
wc.group("Ticker.publish");
this.addEventListener("click", e => {
wc.publish("wc-ticker", {
time: new Date().getTime(),
action: "click",
id: this.id,
uparam: this.properties.uparam
});
});
wc.groupEnd();
}
/**
* Called with .setAttribute(...) function call
* @attributeChangedCallback
*/
attributeChangedCallback(attr, oldval, newval) {
wc.group("Ticker.attributeChangedCallback:", attr, oldval, newval);
this.properties = this.properties || [];
let obs = Ticker.observedAttributes;
for (let i = 0; i < obs.length; i++) {
if (newval) {
this.properties[obs[i]] = newval;
}
}
// YOUR CODE FOR CHANGES GO HERE (MAYBE NULL FIRST TIME THROUGH)
try {
switch(attr)
{
case "header":
break;
default:
break;
}
} catch(e) {
wc.warn(e.name + ' > ' + e.message);
}
wc.groupEnd();
};
/**
* Stores DOM elements of interest for future use
* @private
* @_fetchElements
*/
_fetchElements() {
wc.group("Ticker._fetchElements");
this.dom = this.dom || [];
this.dom.content = this.innerHTML;
wc.groupEnd();
};
/**
* Component attributes are _fetched and defaults are set if undefined
* @private
* @_fetchAttributes
* @param {string} [modal=true] mode for our ticker
*/
_fetchAttributes() {
wc.group("Ticker._fetchAttributes");
this.properties = {
cname : "ticker",
author : "Mel Heravi",
version : "1.0",
};
// SAVE WIDGET SPECIFIC PROPERTIES
this.propertiesW = [];
// SAVE ALL OTHER PROPERTIES
let attrs = wc.getAttributes(this)
for (var key in attrs) {
let attr = this.getAttribute(key) || this.properties.key;
this.properties[key] = this.getAttribute(key);
this.propertiesW[key] = this.getAttribute(key);
wc.log(key + ": " + attrs[key]);
}
// SET ALL INITIAL ATTRIBUTES
for (var key in this.properties) {
switch(key)
{
case "header":
break;
default:
break;
}
}
wc.log("ATTRIBUTES: ", this.properties);
wc.groupEnd();
};
/**
* Destroy the instance object and artifacts
* @destroy
*/
destroy() {
wc.group("Ticker.destroy:", this.id);
// FREE ALL MEMORY
// you should delete all created objects here
// FREE POINTER
delete this;
// REMOVE ITEM FROM DOM
this.parentNode.removeChild(this);
wc.groupEnd();
};
/**
* configure the instance object and artifacts
* @configure
*/
configure(options) {
wc.group("Ticker.configure:", JSON.stringify(options));
// PROCESS ALL OPTIONS HERE
wc.groupEnd();
};
/**
* SAVE DATA FOR ANALYTICS
* @private
* @_initialize
*/
_initialize() {
wc.group("Ticker._initialize:", this.id);
// FETCH ALL INTERESTING ELEMENTS
this._fetchElements();
// FETCH ALL ATTRIBUTES
this._fetchAttributes();
wc.groupEnd();
};
/**
* SAVE DATA FOR ANALYTICS
* @private
* @_finalize
*/
_finalize() {
wc.group("Ticker._finalize:", this.id);
this.classList.add("wc");
// ADD ANALYTICS HERE
wc.setStats(this, this.properties.cname, this.properties.version);
// SHOW IT NOW (NO FLICKERS)
this.style.visibility = "visible";
wc.groupEnd();
};
/**
* Invoked When component is removed. Usually with a .remove() function call
* @disconnectedCallback
*/
disconnectedCallback() {
wc.group("Ticker.disconnectedCallback")
/* CLEAN UP NOW */
wc.groupEnd();
};
/**
* Invoked When component is removed. Usually with a .remove() function call
* @disconnectedCallback
*/
_update() {
wc.group("Ticker.update")
var sym = this.properties.symbols;
var url = `https://financialmodelingprep.com/api/v3/stock/real-time-price-full/${sym}`;
$.getJSON(url, function(data) {
for (var i = 0; i < data.length; i++) {
let con = $(`wc-ticker-container .${data[i].symbol}`);
if (data[i].change > 0) {
con.html(`<span style="color:#ccc">${data[i].symbol}</span> ${data[i].price} <i class='fa fa-caret-up text-success'></i>`);
} else if (data[i].change < 0) {
con.html(`<span style="color:#ccc">${data[i].symbol}</span> ${data[i].price} <i class='fa fa-caret-down text-danger'></i>`);
} else {
con.html(`<span style="color:#ccc" class="mr-2">${data[i].symbol}</span> ${data[i].price}`);
}
}
});
wc.timeout(() => {
this._update();
}, this.interval*1000);
wc.groupEnd();
};
}
window.customElements.define('wc-ticker', Ticker);