Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added translations and multi currency support #1

Merged
merged 1 commit into from May 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions MMM-cryptocurrency.css
@@ -1,3 +1,9 @@
.currency {
color: white;
}
.mmm-cryptocurrency > tr {
padding-bottom: 5px;
}
.mmm-cryptocurrency > tr > td, .mmm-cryptocurrency > tr > th {
padding-left: 20px;
}
185 changes: 130 additions & 55 deletions MMM-cryptocurrency.js
@@ -1,58 +1,133 @@
Module.register("MMM-cryptocurrency", {
result: {},
defaults: {
currency: 'bitcoin',
conversion: 'USD'
},

start: function() {
this.getTicker();
this.scheduleUpdate();
},

getStyles: function() {
return ["MMM-cryptocurrency.css"];
},

getTicker: function () {
var currency = this.config.currency;
var conversion = this.config.conversion;
var url = 'https://api.coinmarketcap.com/v1/ticker/' + currency + '/?convert=' + conversion;
this.sendSocketNotification('get_ticker', url);
},

scheduleUpdate: function() {
var self = this;
// Refresh time should not be less than 5 minutes
var delay = 300000;
setInterval(function() {
self.getTicker();
}, delay);
},

getDom: function() {
var wrapper = document.createElement("ticker");
var data = this.result;
var textElement = document.createElement("span");
textElement.className = 'currency';
var text = this.config.currency + ': ';
var lastPrice = data.price_usd;
if (lastPrice) {
textElement.innerHTML = text;
wrapper.appendChild(textElement);
var priceElement = document.createElement("span");
priceElement.innerHTML = lastPrice + ' ' + this.config.conversion;
wrapper.appendChild(priceElement);
}
return wrapper;
},

socketNotificationReceived: function(notification, payload) {
if (notification === "got_result") {
// Get first element of the array
this.result = payload[0];
this.updateDom();
}
},
result: {},
defaults: {
currency: ['bitcoin', 'ethereum'],
conversion: 'USD',
displayLongNames: false
},

start: function () {
this.getTicker();
this.scheduleUpdate();
},

getStyles: function () {
return ["MMM-cryptocurrency.css"];
},

getTicker: function () {
var conversion = this.config.conversion;
var url = 'https://api.coinmarketcap.com/v1/ticker/?convert='+conversion+'&limit=10';
this.sendSocketNotification('get_ticker', url);
},

scheduleUpdate: function () {
var self = this;
// Refresh time should not be less than 5 minutes
var delay = 300000;
setInterval(function () {
self.getTicker();
}, delay);
},

getDom: function () {
var wrapper = document.createElement("table");
wrapper.className = 'small mmm-cryptocurrency';
var data = this.result;

var tableHead = document.createElement("tr");
tableHead.className = 'header-row';

// Using the MM Translate function to translate the strings
var tableHeadValues = [
this.translate("CURRENCY"),
this.translate('PRICE'),
this.translate('CHANGE')
];

for (var thCounter = 0; thCounter < tableHeadValues.length; thCounter++) {
var tableHeadSetup = document.createElement("th");
tableHeadSetup.innerHTML = tableHeadValues[thCounter];

tableHead.appendChild(tableHeadSetup);
}

wrapper.appendChild(tableHead);


for (var trCounter = 0; trCounter < data.length; trCounter++) {

var oneCurrency = data[trCounter];

var trWrapper = document.createElement("tr");

if (this.config.displayLongNames) {
var name = oneCurrency.name;
} else {
name = oneCurrency.symbol;
}

var rightCurrencyFormat = this.config.conversion.toLowerCase();

// rounding the price and adds the currency string
var formattedPrice = Math.round(oneCurrency['price_'+rightCurrencyFormat])+' '+this.config.conversion;

var tdValues = [
name,
formattedPrice,
oneCurrency.percent_change_24h+'%'
];

for (var c = 0; c < tdValues.length; c++) {
var tdWrapper = document.createElement("td");
tdWrapper.innerHTML = tdValues[c];
trWrapper.appendChild(tdWrapper);
}

wrapper.appendChild(trWrapper);
}

return wrapper;
},

socketNotificationReceived: function (notification, payload) {
if (notification === "got_result") {

this.result = this.getWantedCurrencys(this.config.currency, payload);
this.updateDom();
}
},

getWantedCurrencys: function (wantedCurrencys, apiResult) {

// get wanted currencys from config and loop trough them

var filteredCurrencys = [];
// loop trough whole api result
for (var i = 0; i < apiResult.length; i++){
var singleCurrency = apiResult[i];
// loop trough currency's specified in config
for (var c = 0; c < wantedCurrencys.length; c++){
if(singleCurrency.id == wantedCurrencys[c]){
// add them to our wanted list
filteredCurrencys.push(singleCurrency)
}
}
}

return filteredCurrencys;

},

/**
* Load translations files
* @returns {{en: string, de: string}}
*/
getTranslations: function () {
return {
en: "translations/en.json",
de: "translations/de.json"
};
},

});
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -21,7 +21,7 @@ Here is an example of an entry in `config.js`
module: "MMM-cryptocurrency",
position: "top_right",
config: {
currency: 'ethereum',
currency: ['ethereum', 'bitcoin'],
conversion: 'USD'
}
}
Expand Down
5 changes: 5 additions & 0 deletions translations/de.json
@@ -0,0 +1,5 @@
{
"CURRENCY": "Währung",
"PRICE": "Preis",
"CHANGE": "% Änderung (24h)"
}
5 changes: 5 additions & 0 deletions translations/en.json
@@ -0,0 +1,5 @@
{
"CURRENCY": "Currency",
"PRICE": "Price",
"CHANGE": "% Change (24h)"
}