-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-BTC-Xapo.js
131 lines (110 loc) · 3.64 KB
/
MMM-BTC-Xapo.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
Xapo Bitcoin Price
====================================
Developer : Zulkifli Mohamed (putera)
E-mail : mr.putera@gmail.com
*/
'use strict';
Module.register("MMM-BTC-Xapo",
{
result: {},
defaults:
{
currency: 'MYR',
refreshTime: 5,
language: "ms-my",
animationSpeed: 2500
},
getStyles: function() {
return [this.file('css/MMM-BTC-Xapo.css')];
},
getTranslations: function() {
return {
"ms-my": "translations/ms-my.json",
"en": "translations/en.json"
};
},
start: function() {
this.getPrice();
this.scheduleUpdate();
},
getPrice: function() {
this.sendSocketNotification('GET_PRICE', this.config.currency.toUpperCase());
},
scheduleUpdate: function(delay) {
var nextLoad = this.config.refreshTime * 60 * 1000;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
setInterval(function() {
self.getPrice();
}, nextLoad);
},
getDom: function() {
var data = this.result;
var currency = this.config.currency;
var lastPriceBuy = data.buy;
var lastPriceSell = data.sell;
var w = document.createElement("div");
if (lastPriceBuy && lastPriceSell)
{
if (typeof lastPriceBuy != "undefined") {
lastPriceBuy = currency + ' ' + lastPriceBuy.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else {
lastPriceBuy = '-';
}
if (typeof lastPriceSell != "undefined") {
lastPriceSell = currency + ' ' + lastPriceSell.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else {
lastPriceSell = '-';
}
// Title
var elt = document.createElement('div');
elt.className = 'normal dimmed';
elt.innerHTML = this.translate("XAPO_TITLE");
// Buy
var elbuy = document.createElement('div');
elbuy.style.width = '50%';
elbuy.style.float = 'left';
var elb = document.createElement('div');
elb.className = 'small dimmed';
elb.innerHTML = this.translate("BUY") + ':';
var elbp = document.createElement('div');
elbp.className = 'medium bright';
elbp.innerHTML = lastPriceBuy;
elbuy.appendChild(elb);
elbuy.appendChild(elbp);
// Sell
var elsell = document.createElement('div');
elsell.style.width = '50%';
elsell.style.float = 'right';
var els = document.createElement('div');
els.className = 'small dimmed buy-title';
els.innerHTML = this.translate("SELL") + ':';
var elsp = document.createElement('div');
elsp.className = 'medium bright';
elsp.innerHTML = lastPriceSell;
elsell.appendChild(els);
elsell.appendChild(elsp);
w.appendChild(elt);
w.appendChild(elbuy);
w.appendChild(elsell);
}
else
{
var el = document.createElement("div");
el.className = 'small dimmed';
el.innerHTML = this.translate("LOADING");
w.appendChild(el);
}
return w;
},
socketNotificationReceived: function(notification, payload) {
if (notification === "PRICE_RESULT") {
var self = this;
this.result = payload;
this.updateDom(self.config.animationSpeed);
}
},
});