-
Notifications
You must be signed in to change notification settings - Fork 16
/
stocks.js
113 lines (82 loc) · 2.91 KB
/
stocks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict';
Module.register("stocks", {
result: [],
// Default module config.
defaults: {
stocks: 'MSFT,AAPL,GOOG,INTC',
updateInterval: 60000
},
getStyles: function() {
return ["stocks.css"];
},
start: function() {
this.getStocks();
this.scheduleUpdate();
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("marquee");
wrapper.className = 'medium bright';
var count = 0;
var _this = this;
if (this.result.length > 0){
this.result.forEach(function(stock) {
var symbolElement = document.createElement("span");
var symbol = stock.symbol;
var lastPrice = stock.latestPrice;
var changePercentage = stock.changePercent;
var changeValue = stock.change;
var lastClosePrice = stock.close;
var lastPriceFix = stock.latestPrice;
symbolElement.innerHTML = symbol + ' ';
wrapper.appendChild(symbolElement);
var priceElement = document.createElement("span");
priceElement.innerHTML = lastPrice;
var changeElement = document.createElement("span");
if (changePercentage > 0)
changeElement.className = "up";
else
changeElement.className = "down";
var change = Math.abs(changeValue, -2);
changeElement.innerHTML = " " + change;
var divider = document.createElement("span");
if (count < _this.result.length - 1)
divider.innerHTML = ' • ';
wrapper.appendChild(priceElement);
wrapper.appendChild(changeElement);
wrapper.appendChild(divider);
count++;
});
}
return wrapper;
},
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
setInterval(function() {
self.getStocks();
}, nextLoad);
},
roundValue: function(value) {
return Math.round(value*100)/100;
},
getStocks: function () {
var url = "https://api.iextrading.com/1.0/stock/" //aapl/quote";
var requestUrls = [];
var stocksArray = this.config.stocks.split(',');
stocksArray.forEach(function(stock) {
var requestUrl = url + stock + "/quote";
requestUrls.push(requestUrl);
});
this.sendSocketNotification('GET_STOCKS_MULTI', requestUrls);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "STOCKS_RESULT") {
this.result = payload;
this.updateDom(self.config.fadeSpeed);
}
},
});