-
Notifications
You must be signed in to change notification settings - Fork 1
/
send-btc.js.in
125 lines (111 loc) · 4.34 KB
/
send-btc.js.in
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
var sendBTC = {
fiatRates: {
'USD': null,
'GBP': null,
'EUR': null,
'RUB': null,
'CNY': null,
'KRW': null,
'JPY': null,
},
refreshQRCodeDelay: 400,
fiatSymbol: function(fiat) {
return '<span class="fa fa-' + fiat.toLowerCase() + '"></span>';
},
refreshQRCode: function(uri) {
delete this.refreshQRCodeID;
var qrdiv = $('#send-btc-modal-qrcode');
qrdiv.empty();
qrdiv.qrcode(uri);
},
refreshURI: function(delayQRCode) {
var uri = 'bitcoin:' + this.address + '?amount=' + encodeURIComponent($('#send-btc-amount').val()) + '&label=' + encodeURIComponent($('#send-btc-label').val());
$('#send-btc-modal-form').attr('action', uri);
if (this.refreshQRCodeID)
clearTimeout(this.refreshQRCodeID);
if (delayQRCode) {
var me = this;
this.refreshQRCodeID = setTimeout(function() { me.refreshQRCode(uri); }, me.refreshQRCodeDelay);
} else {
delete this.refreshQRCodeID;
this.refreshQRCode(uri);
}
},
refreshBTC: function() {
var rate = this.fiatRates[this.fiat];
if (rate) {
$('#send-btc-amount').val(($('#send-btc-fiat-amount').val() / rate).toFixed(8));
this.refreshURI(true);
}
},
refreshFiat: function() {
var rate = this.fiatRates[this.fiat];
if (rate)
$('#send-btc-fiat-amount').val(($('#send-btc-amount').val() * rate).toFixed(2));
},
setFiat: function(fiat) {
if (!(fiat in this.fiatRates))
throw 'Invalid currency "' + fiat + '"';
this.fiat = fiat;
$('#send-btc-modal-fiat-symbol').html(this.fiatSymbol(fiat));
this.refreshFiat();
},
updateExchange: function() {
var me = this;
$.getJSON('https://www.willl.party/ticker', function(data, textStatus) {
var options = $('#send-btc-modal-fiat-options');
options.empty();
var gotSome = false;
for (fiat in me.fiatRates) {
if (fiat in data) {
gotSome = true;
me.fiatRates[fiat] = data[fiat].last;
options.append(
'<li><a href="#" onclick="sendBTC.setFiat(\'' + fiat + '\');">' +
me.fiatSymbol(fiat) + ' ' + fiat +
' <span class="text-muted">' + me.fiatRates[fiat] + '</span></a></li>'
);
}
}
if (!gotSome)
options.append('<li class="dropdown-header">No market data</li>');
options.append(
'<li role="separator" class="divider"></li>' +
'<li><a href="#" onclick="sendBTC.updateExchange();">' +
'<span class="fa fa-refresh"></span> Refresh</a></li>'
);
me.refreshFiat();
});
},
insertModal: function() {
$('#send-btc-modal').remove();
$('html').prepend('<span id="send-btc-modal"></span>');
$('#send-btc-modal').addClass('modal fade');
$('#send-btc-modal').attr('tabindex', '-1').attr('role', 'dialog').attr('aria-labelledby', 'send-btc-modal-title');
$('#send-btc-modal').html('@MODAL_HTML@');
},
init: function() {
this.insertModal();
sendBTC.updateExchange();
$('#send-btc-modal').on('show.bs.modal', function(event) {
var trigger = $(event.relatedTarget);
var address = trigger.data('address') || '';
var label = trigger.data('label') || '';
var amount = trigger.data('amount') || '0.0';
var fiat = trigger.data('fiat') || 'USD';
if (!$.isNumeric(amount) || amount < 0)
throw 'Invalid amount "' + amount + '"';
if (!(fiat in sendBTC.fiatRates))
throw 'Invalid currency "' + fiat + '"';
sendBTC.address = address;
sendBTC.label = label;
$('#send-btc-address').text(address);
$('#send-btc-address').attr('href', 'https://blockchain.info/address/' + address);
$('#send-btc-label').val(label);
$('#send-btc-amount').val(amount);
sendBTC.setFiat(fiat);
sendBTC.refreshURI();
});
},
};
sendBTC.init();