forked from miohtama/bitcoin-prices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitcoinprices.js
413 lines (331 loc) · 12.9 KB
/
bitcoinprices.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/**
* bitcoinprices.js
*
* Display human-friendly bitcoin prices, both desktop and mobile.
*
* Copyright 2013 Mikko Ohtamaa http://opensourcehacker.com
*
* Licensed under MIT license.
*/
/* global define */
// UMD boilerplate
// https://github.com/umdjs/umd/blob/master/returnExports.js
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jQuery'], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.
// jQuery(window) is jQuery 2.1+
module.exports = factory(require('jquery/dist/jquery')(window));
} else {
// Browser globals (root is window)
root.bitcoinprices = factory(root.jQuery);
}
}(this, function (jQuery) {
"use strict";
// Store jQuery locally, so we can override it
// with an external option
var $ = jQuery;
// Defaults
var defaultConfig = {
// Where we get bitcoinaverage data
// or null if we run headless (not in browser)
url: "https://api.bitcoinaverage.com/ticker/all",
// Which of bitcoinaverages value we use to present prices
marketRateVariable: "24h_avg",
// Which currencies are in shown to the user
currencies: ["BTC", "USD", "EUR", "CNY"],
// Special currency symbol artwork
symbols: {},
// CSS selector Which prices we make clickable on the page
// If null defaults to [data-btc-price] (attribute match)
clickablePriceSelector: null,
// Which currency we show user by the default if
// no currency is selected
defaultCurrency: "BTC",
// How the user is able to interact with the prices
ux : {
// Make everything with data-btc-price HTML attribute clickable
clickPrices : true,
// Build Bootstrap dropdown menu for currency switching
menu : true,
// Allow user to cycle through currency choices in Currency: USD quick menu
clickableCurrencySymbol: true
},
// Price source data attribute
priceAttribute: "data-btc-price",
// Price source currency
priceOrignalCurrency: "BTC"
};
return {
/** Store exchange rate data as returned by bitcoinaverages.com */
data : null,
/** Our configuration options */
config : null,
/**
* Update market rate data from the server using JSON AJAX request.
*
* Assumes the server sets proper cache headers, so we are not bombing
* the server.
*/
loadData : function () {
var self = this;
$.getJSON(self.config.url, function(resp) {
self.data = resp;
$(document).trigger("marketdataavailable");
}).error(function() {
throw new Error("Could not load exchage rate data from:" + self.config.url);
});
},
/**
* Convert between BTC and fiat currecy.
*
* @param {Number} amount Currency amount to convert
* @param {String} source Three-letter currency code
* @param {String} target Three-letter currency code
* @return {Number} Amount in other currency
*/
convert : function(amount, source, target) {
var inverse;
if(!$.isNumeric(amount)) {
throw new Error("Amount must be numeric");
}
if(!source || !target) {
throw new Error("You need to give both source and target currency:" + source + " " + target);
}
// No conversion
if(source == "BTC" && target == "BTC") {
return amount;
}
if(!(source == "BTC" || target == "BTC")) {
// Convert through BTC
return this.convert(this.convert(amount, source, "BTC"), "BTC", target);
}
if(source == "BTC") {
inverse = true;
// http://stackoverflow.com/a/16201730/315168
target = [source, source = target][0];
} else {
inverse = false;
}
if(!this.data) {
throw new Error("Exchange rate data not available");
}
var currencyData = this.data[source];
if(!currencyData) {
throw new Error("We do not have market data for currency: " + source);
}
var rate = currencyData[this.config.marketRateVariable];
if(!rate) {
throw new Error("Cannot parse bitcoinaverage data for " + source + " " + this.config.url);
}
if(inverse) {
return amount*rate;
} else {
return amount/rate;
}
},
/**
* Format a price for a currency.
*
* Fills in currency symbols we have configured.
*
* @param {Number} amount
* @param {String} currency Three letter currency code
* @param {Boolean} symbol Add currency symbol
* @return {String} HTML snippet
*/
formatPrice : function (amount, currency, symbol) {
var decimals;
if(currency == "BTC") {
decimals = 8;
} else {
decimals = 2;
}
var formatted = amount.toFixed(decimals);
if(symbol) {
formatted += " " + this.getCurrencySymbol(currency);
}
return formatted;
},
/**
* Get HTML for a currency symbol
* @param {String} currency Three-letter currency code
*/
getCurrencySymbol : function(currency) {
var symbols = this.config.symbols || {};
var symbol = this.config.symbols[currency] || currency;
return symbol;
},
/**
* Assume we have market data available.
*
* Update the prices to reflect the current state of selected
* currency and market price.
*/
updatePrices : function() {
var self = this;
var currentCurrency = this.getActiveCurrency();
var config = this.config;
// Find all elements which declare themselves to present BTC prices
$("[" + config.priceAttribute + "]").each(function() {
var elem = $(this);
var btcPrice = elem.attr(config.priceAttribute);
try {
btcPrice = parseFloat(btcPrice, 10);
} catch(e) {
// On invalid price keep going forward
// silently ignoring this
return;
}
var priceSymbol = elem.attr("data-price-symbol") != "off";
var inCurrentCurrency = self.convert(btcPrice, config.priceOrignalCurrency, currentCurrency);
elem.html(self.formatPrice(inCurrentCurrency, currentCurrency, priceSymbol));
});
},
/**
* Update currency symbols on the page which are not directly associated with a price.
*/
updateCurrencySymbols : function() {
$(".current-currency-symbol").html(this.getCurrencySymbol(this.getActiveCurrency()));
},
/**
* Get the currency selected by the user.
*/
getActiveCurrency : function() {
return window.localStorage["bitcoinprices.currency"] || this.config.defaultCurrency || "BTC";
},
/**
* If we have an active currency which is not provided by current data return to BTC;
*/
resetCurrency : function() {
var currency = this.getActiveCurrency();
var idx = $.inArray(currency, this.config.currencies);
if(idx < 0) {
window.localStorage["bitcoinprices.currency"] = "BTC";
}
},
/**
* Loop available currencies, select next one.
*
* @return {String} user-selected next three-letter currency code
*/
toggleNextActiveCurrency : function() {
var currency = this.getActiveCurrency();
var idx = $.inArray(currency, this.config.currencies);
if(idx < 0) {
idx = 0;
}
idx = (++idx) % this.config.currencies.length;
currency = window.localStorage["bitcoinprices.currency"] = this.config.currencies[idx];
return currency;
},
/**
* User changes the default currency through clicking a price.
*/
installClicker : function() {
var self = this;
function onclick(e) {
e.preventDefault();
self.toggleNextActiveCurrency();
$(document).trigger("activecurrencychange");
}
// We have now market data available,
// decoreate elements so the user knows there is interaction
var clickablePriceSelector = this.config.clickablePriceSelector || "[" + this.config.priceAttribute + "]";
$(clickablePriceSelector).addClass("clickable-price");
if(this.config.ux.clickableCurrencySymbol) {
$(".current-currency-symbol").addClass("clickable-price");
}
$(".clickable-price").click(onclick);
},
/**
* Populate Bootstrap dropdown menu "currency-dropdown" with available currency choices.
*
* Automatically toggle the currently activated currency.
*/
installCurrencyMenu : function() {
var self = this;
var menu = $(".currency-dropdown");
function updateCurrencyInMenu(currency) {
var symbol = self.getCurrencySymbol(currency);
menu.find(".currency-symbol").html(symbol);
menu.find("li[data-currency]").removeClass("active");
menu.find("li[data-currency=" + currency + "]").addClass("active");
}
function buildMenu() {
$.each(self.config.currencies, function() {
var symbol = self.getCurrencySymbol(this);
var template = [
"<li class='currency-menu-entry' data-currency='",
this,
"'><a role='menuitem'>",
symbol,
"</a></li>"
];
var html = template.join("");
menu.find("ul").append(html);
});
}
buildMenu();
$(document).on("activecurrencychange", function() {
var active = self.getActiveCurrency();
updateCurrencyInMenu(active);
});
menu.on("click", ".currency-menu-entry", function(e) {
var currency = $(this).attr("data-currency");
window.localStorage["bitcoinprices.currency"] = currency;
$(document).trigger("activecurrencychange");
});
// Initialize the currency from what the user had on the last page load
var active = this.getActiveCurrency();
updateCurrencyInMenu(active);
},
/**
* Make prices clickable and tooltippable.
*
* Assume we have market data available.
*/
installUX : function() {
var self = this;
if(self.config.ux.clickPrices) {
this.installClicker();
}
if(self.config.ux.menu) {
this.installCurrencyMenu();
}
// Whenever some UX element updates the active currency then refresh the page
$(document).bind("activecurrencychange", function() {
self.updatePrices();
self.updateCurrencySymbols();
});
},
/**
* Call to initialize the detault bitcoinprices UI.
*/
init : function(_config) {
if(!_config) {
throw new Error("You must give config object");
}
var self = this;
// Allow jQuery override
// (solves many problems with require() jQuery includes)
if(_config.jQuery) {
$ = _config.jQuery;
}
this.config = $.extend({}, defaultConfig, _config);
if(this.config.url) {
// Chec we are not running headless testing mode
$(document).bind("marketdataavailable", function() {
self.updatePrices();
self.updateCurrencySymbols();
self.installUX();
});
this.loadData();
}
}
};
}));