-
Notifications
You must be signed in to change notification settings - Fork 5
/
bitquote.js
107 lines (101 loc) · 4.24 KB
/
bitquote.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
var bitQuotes = [];
function initialize(options) {
var options = $.extend({
"fiat": "USD",
"fiatSymbol": "$",
"container": "bitquote",
"showBidAsk": true,
"href": "https://bitcoinaverage.com/",
"autoUpdate": true,
"updateInterval": 60000,
"autoResize": true
}, options);
bitQuotes.push(options);
var container = '#' + options.container;
$.get("https://api.bitcoinaverage.com/ticker/" + options.fiat, function (data) {
createDOM(options, function () {
$(container + " .bitquote-price").html(options.fiatSymbol + data.last);
if (options.showBidAsk) {
if (options.fiatSymbol.length <= 2) {
$(container + " .bitquote-bid").html("Bid: " + options.fiatSymbol + data.bid);
$(container + " .bitquote-ask").html("Ask: " + options.fiatSymbol + data.ask);
}
else {
$(container + " .bitquote-bid").html("Bid: " + data.bid);
$(container + " .bitquote-ask").html("Ask: " + data.ask);
}
}
if (options.autoResize)
adjustWidth(options, options.fiatSymbol + data.bid);
});
});
$(document).ready(function () {
if (options.href) {
$(container).on('click', function (e) {
window.open(options.href, '_blank');
});
}
});
setInterval(function () {
updateQuotes(bitQuotes);
}, options.updateInterval);
}
function adjustWidth(options, price) {
var container = '#' + options.container;
var mainpriceLength = price.replace(/[\. ]+/g, "").length;
if (options.fiatSymbol.length >= 2) {
var subpriceLength = price.replace(/[\. ]+/g, "").length - options.fiatSymbol.replace(/[\. ]+/g, "").length;
}
else {
var subpriceLength = price.replace(/[\. ]+/g, "").length;
}
$(document).ready(function () {
var containerWidth = $(container).width();
if (options.showBidAsk) {
if (mainpriceLength <= 6)
mainpriceLength = 7;
$(container + ' .bitquote-price').css('font-size', Math.floor(containerWidth / mainpriceLength));
$(container + ' .bitquote-bid').css('font-size', Math.floor(containerWidth / (subpriceLength + 12)));
$(container + ' .bitquote-ask').css('font-size', Math.floor(containerWidth / (subpriceLength + 12)));
}
else {
$(container + ' .bitquote-price').css('font-size', Math.floor(containerWidth / mainpriceLength));
}
});
}
function createDOM(options, callback) {
var container = '#' + options.container;
if (options.showBidAsk)
var baseHTML = '<div class="bitquote-logo"><img src="https://en.bitcoin.it/w/images/en/2/29/BC_Logo_.png" /></div><div class="bitquote-price"></div><div class="askbidParent"><div class="bitquote-ask"></div><div class="bitquote-bid"></div></div><div class="clearboth"></div>'
else
var baseHTML = '<div class="bitquote-logo"><img src="https://en.bitcoin.it/w/images/en/2/29/BC_Logo_.png" /></div><div class="bitquote-price"></div><div class="clearboth"></div>'
$(container).append(baseHTML);
return callback();
}
function updateQuotes(bitOptions) {
$.each(bitOptions, function (i, options) {
var container = '#' + options.container;
if (options.autoUpdate) {
$.get("https://api.bitcoinaverage.com/ticker/" + options.fiat, function (data) {
if ($(container + " .bitquote-price").text() != options.fiatSymbol + data.last)
$(container + " .bitquote-price").fadeOut(600, function () {
$(this).text(options.fiatSymbol + data.last).fadeIn(600);
})
if ($(container + " .bitquote-bid").text() != "Bid: " + options.fiatSymbol + data.bid && options.showBidAsk)
$(container + " .bitquote-bid").fadeOut(600, function () {
if (options.fiatSymbol.length >= 2)
$(this).text("Bid: " + data.bid).fadeIn(600);
else
$(this).text("Bid: " + options.fiatSymbol + data.bid).fadeIn(600);
});
if ($(container + " .bitquote-ask").text() != "Ask: " + options.fiatSymbol + data.ask && options.showBidAsk)
$(container + " .bitquote-ask").fadeOut(600, function () {
if (options.fiatSymbol.length >= 2)
$(this).text("Ask: " + data.ask).fadeIn(600);
else
$(this).text("Ask: " + options.fiatSymbol + data.ask).fadeIn(600);
});
});
}
})
}