-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
262 lines (230 loc) · 9.5 KB
/
script.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
$(function () {
/**
* The maximum number of points in each series of the chart.
*/
const maxPointsPerSeries = 30;
/**
* The maximum number of coins that can be selected for inclusion in the chart.
*/
const maxSelected = 5;
/**
* The ID of the interval that updates the chart.
*/
let intervalId = 0;
const chart = Highcharts.chart('chart', {
chart: {
type: 'spline',
animation: Highcharts.svg,
marginRight: 10
},
time: {
useUTC: false
},
title: {
text: 'Exchange Rates'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150
},
yAxis: {
type: 'logarithmic',
title: {
text: 'USD'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
headerFormat: '<b>{series.name}</b><br/>',
pointFormat: '{point.x:%Y-%m-%d %H:%M:%S}<br/>${point.y:.4f}'
},
legend: {
align: 'center',
verticalAlign: 'top',
borderWidth: 1
},
});
$('#cards').empty();
$('#home-tab').on('show.bs.tab', function () {
$('#search').show();
});
$('#home-tab').on('hide.bs.tab', function () {
$('#search').hide();
});
$('#reports-tab').on('show.bs.tab', async function (e) {
const fsyms = $('.card:has(:checkbox:checked) .card-title')
.map(function () {
return $(this).text().toUpperCase();
})
.get();
for (let i = 0; i < fsyms.length; i++) {
chart.addSeries({
id: fsyms[i],
name: fsyms[i],
});
}
if (fsyms.length > 0) {
intervalId = setInterval(async function () {
const prices = await fetch(`https://min-api.cryptocompare.com/data/pricemulti?fsyms=${fsyms.join(',')}&tsyms=usd`)
.then(res => res.json());
const now = Date.now();
for (let i = 0; i < fsyms.length; i++) {
const series = chart.get(fsyms[i]);
if (series) {
series.addPoint([now, prices[fsyms[i]]['USD']], true, series.data.length >= maxPointsPerSeries);
}
}
}, 2000);
}
});
$('#reports-tab').on('hide.bs.tab', function () {
if (intervalId) {
clearInterval(intervalId);
intervalId = 0;
}
while (chart.series.length > 0) {
chart.series[0].remove();
}
});
$('#search').on('input', function ({ target }) {
const substr = $(target).val().toLowerCase();
$('#cards').children().hide();
$('#cards').children().filter(function () {
return $(this).text().toLowerCase().includes(substr);
}).show();
});
$('#too-many-coins').on('show.bs.modal', function () {
$('#selected-coins').empty();
$('#cards .card:has(:checkbox:checked)').each(function () {
const [, id] = $(':checkbox', this).attr('id').match(/select-(.*)/);
const symbol = $('.card-title', this).text();
const inputGroup = $(`
<div class="input-group w-auto mx-2 my-2">
<div class="input-group-prepend">
<div class="input-group-text">
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="selected-${id}" checked>
<label for="selected-${id}" class="custom-control-label"></label>
</div>
</div>
</div>
<div class="input-group-append">
<label for="selected-${id}" class="input-group-text text-uppercase">${symbol}</label>
</div>
</div>`);
$(':checkbox', inputGroup).change(function () {
$(`#select-${id}`).prop('checked', $(this).prop('checked'));
$('#too-many-coins form :submit').prop('disabled', $('#cards :checkbox:checked').length >= maxSelected);
});
$('#selected-coins').append(inputGroup);
});
});
/**
* The id of the coin that triggered the modal.
*/
let triggerId = '';
$('#too-many-coins').on('hide.bs.modal', function () {
if (!$(`#select-${triggerId}`).prop('checked')) {
const checkboxes = $('#selected-coins :checkbox');
checkboxes.prop('checked', true);
checkboxes.change();
}
});
$('#too-many-coins form').submit(function (e) {
e.preventDefault();
$(`#select-${triggerId}`).prop('checked', true);
$('#too-many-coins').modal('hide');
});
function showModal(checkbox) {
[, triggerId] = checkbox.id.match(/select-(.*)/);
const card = $(checkbox).closest('.card');
const symbol = $('.card-title', card).text();
const tooMany = $('#too-many-coins');
$('.modal-title', tooMany).text(`You cannot select more than ${maxSelected} coins. To select ${symbol.toUpperCase()}, first de-select one or more of the following:`);
$('form :submit', tooMany).text(`Select ${symbol.toUpperCase()}`);
$('form :submit', tooMany).prop('disabled', true);
setTimeout(tooMany.modal.bind(tooMany, 'show'), 0);
}
$.getJSON('https://api.coingecko.com/api/v3/coins').done(function (coins) {
$('#cards').empty();
$('#cards').append(coins.slice(0, 100).map(({ id, name, symbol }) => $(`
<div class="card col-sm-6 col-md-4 col-lg-3 col-xl-2">
<label for="select-${id}" class="card-body">
<h5 class="card-title text-uppercase">${symbol}</h5>
<div class="custom-control custom-switch" style="position: absolute; top: 22px; right: 25px;">
<input type="checkbox" class="custom-control-input" id="select-${id}">
<label class="custom-control-label" for="select-${id}"></label>
</div>
<p>${name}</p>
<button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#more-info-${id}">More Info</button>
<div class="collapse mt-4 more-info" id="more-info-${id}">
<div class="card border-primary p-4" style="border-radius: 200em 200em 0 0;">
<img class="card-img-top img-thumbnail rounded-circle border-dark" src="img/dollar.gif" />
<div class="card-body d-flex flex-column align-items-center">
<div class="spinner-grow"></div>
<div class="spinner-grow"></div>
<div class="spinner-grow"></div>
</div>
</div>
</div>
</label>
</div>`)));
$('#cards :checkbox').click(function (e) {
if ($('#cards :checkbox:checked').length > maxSelected) {
e.preventDefault();
showModal(e.target);
}
});
$('#cards [data-toggle="collapse"]').click(function (e) {
// This is necessary to prevent the coin getting selected/de-selected when pressing "More Info" on iPad.
e.preventDefault();
});
$('#cards .more-info').on('shown.bs.collapse', function ({ target }) {
const [, id] = target.id.match(/more-info-(.*)/);
getMoreInfo(id).done(function ({
image: {
large: imageUrl
},
market_data: {
current_price: {
usd,
eur,
ils
}
}
}) {
const cardImg = $('.card-img-top', target);
const cardBody = $('.card-body', target);
cardImg.attr('src', imageUrl);
cardBody.empty();
cardBody.append(`<h5 class="card-text">$${usd}</h5>`);
cardBody.append(`<h5 class="card-text">€${eur}</h5>`);
cardBody.append(`<h5 class="card-text">₪${ils}</h5>`);
});
});
$('#cards .more-info').on('hidden.bs.collapse', function ({ target }) {
const cardBody = $('.card-body', target);
cardBody.empty();
cardBody.append('<div class="spinner-grow"></div>');
cardBody.append('<div class="spinner-grow"></div>');
cardBody.append('<div class="spinner-grow"></div>');
});
const moreInfoCache = {};
function getMoreInfo(id) {
if (moreInfoCache[id] && Date.now() - moreInfoCache[id].time < 120000) {
const d = $.Deferred();
d.resolve(moreInfoCache[id]);
return d.promise();
} else {
return $.getJSON(`https://api.coingecko.com/api/v3/coins/${id}?tickers=false&community_data=false&developer_data=false`)
.done(function (coinInfo) {
moreInfoCache[id] = { time: Date.now(), ...coinInfo };
});
}
}
});
});