brighter-trading/static/Alerts.js

64 lines
1.7 KiB
JavaScript

class Alert{
constructor(alert_type, source, state) {
// The type of alert.
this.type = alert_type;
// The source of the alert.
this.source = source;
// Other info in the alert.
this.state = state;
// The alert messages.
this.msg = 'Signal state change: ' + this.source + ' = ' + this.state;
}
alert_source(){
return this.source;
}
alert_type(){
return this.type;
}
alert_state(){
return this.state;
}
alert_msg(){
return this.msg;
}
}
class Alerts {
constructor(target_id) {
// The list of alert messages.
this.alerts = [];
// The html element id that displays the alert messages.
this.target_id = target_id;
// The html element that displays the alert messages.
this.target = null;
}
publish_alerts(alert_type, data){
if (alert_type == 'signal_changes'){
// If the alert_type is signal changes then data will
// contain a list of objects with format: { name: str, state: bool }
console.log('publishing alerts')
for(let sig in data){
console.log('publishing single alert');
this.alerts.push( new Alert('signal', sig, data[sig]) );
}
this.update_html();
}
}
update_html(){
let alerts ='';
for (let index in this.alerts){
let alert = this.alerts[index].alert_msg();
alerts += '<span>' + alert + '</span><br>';
}
this.target.innerHTML = alerts;
}
set_target(){
// This is called after the html document has been parsed.
this.target = document.getElementById(this.target_id);
}
}