-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript2.js
126 lines (106 loc) · 4.98 KB
/
script2.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
let from ='RUB';
let to = 'USD';
let amount = 1;
let display = document.querySelector('#inputR');
let direction = 'left';
// парсим курс с сервиса
async function getRate(from, to) {
console.log('from ',from);
console.log('to :',to);
loadingStart();
try {
const response = await fetch(`https://api.ratesapi.io/api/latest?base=${from}&symbols=${to}`);
const data = await response.json();
await setTimeout( function() { loadingEnd() }, 1000);
return data.rates[to];
}
catch (err) { console.log(err) }
}
// конверторно-иверторный мега калькулятор
async function converter (from='RUB', to='USD', amount=1, direction) {
const rate = await getRate(from, to);
const result = (rate<0)? amount / rate : amount * rate;
console.log(rate);
console.log(amount);
console.log(result);
render(result, direction, from, to, rate);
}
// кидаем инфу в HTML
const render = (result, direction, from, to, rate) => {
console.log(direction);
console.log(from);
console.log(to);
if (direction === 'left') {
document.querySelector('.detailsL' ).innerText = `1 ${from} = ${rate} ${to}`;
document.querySelector('.detailsR' ).innerText = `1 ${to} = ${(1/rate).toFixed(4)} ${from}`;
}
else if (direction === 'right') {
document.querySelector('.detailsL' ).innerText = `1 ${to} = ${rate} ${from}`;
document.querySelector('.detailsR' ).innerText = `1 ${from} = ${1/rate.toFixed(4)} ${to}`;
}
display.value = result.toFixed(2)
// document.querySelector('input.right').value = result.toFixed(2)
// document.querySelector('input.left').value = result.toFixed(2)
}
// подсвечиваем кнопки
const buttonStyle = (direction, target) => {
document.querySelectorAll(`button.${direction}`)
.forEach(x => x.classList.remove('selected'));
target.classList.add('selected');
}
function validate(data) {
console.log(data);
const pattern = /[^0-9]/
const newData = data.replace(pattern, '') ;
console.log(newData);
return newData;
}
// запрос-о-накопитель, распределитель, слушатель
function handler(event, type) {
// console.log(event.target);
const direction = (event.target.classList.contains('left')) ? 'left' : 'right';
if (event.target.tagName === 'INPUT' && event.target.classList.contains('left')) { amount = event.target.value; display = document.querySelector('input.right');}
if (event.target.tagName === 'INPUT' && event.target.classList.contains('right')){ amount = event.target.value; display =document.querySelector('input.left') ; }
if (event.target.tagName === 'SELECT') { from = event.target.options[event.target.selectedIndex].value; }
if (event.target.tagName === 'BUTTON') { direction === 'left' ? from = event.target.value : to = event.target.value; buttonStyle(direction, event.target); }
converter(from, to, amount, direction);
}
function loadingStart() {
document.querySelector('.loader').style.display = 'block';
document.querySelector('.loaderMessage').innerText = 'Loading';
setTimeout( function() { document.querySelector('.loaderMessage').innerText = 'Loading.'; }, 250 )
setTimeout( function() { document.querySelector('.loaderMessage').innerText = 'Loading..'; }, 500 )
setTimeout( function() { document.querySelector('.loaderMessage').innerText = 'Loading...'; }, 750 )
}
function loadingEnd() { document.querySelector('.loader').style.display = 'none'; }
const reverse = () => {
const valueL = document.querySelector('input.left').value;
const valueR = document.querySelector('input.right').value;
// const buttonR = document.querySelector('.left');
// const buttonL = document.querySelector('input.right');
console.log(document.querySelector('button.left ').value);
const temp = from;
from = to;
to = temp;
// document.querySelectorAll(`button.left`).forEach(x => { if ( x.value !== from ) x.classList.toggle('selected') });
const tempoL = document.querySelectorAll(`button.left.selected`);
console.log(tempoL);
const tempoR = document.querySelectorAll(`button.right.selected`);
console.log(tempoR);
let tempContainer = tempoL.innerHTML;
tempoL.innerHTML = tempoR.innerHTML;
tempoR.innerHTML = tempContainer;
tempContainer = tempoL.value;
tempoL.value = tempoR.value;
tempoR.value = tempContainer;
// document.querySelectorAll(`button.${direction}`).forEach(x => x.classList.remove('selected'));
// target.classList.add('selected');
// target.classList.add('selected');
document.querySelector('input.left').value = valueR;
document.querySelector('input.right').value = valueL;
}
document.querySelectorAll('button').forEach( x => x.addEventListener('click', handler));
document.querySelectorAll('select').forEach( x => x.addEventListener('change', handler));
document.querySelectorAll('input.cur').forEach( x => x.addEventListener('input', handler));
document.querySelector('.reverse').addEventListener('click', reverse);
document.querySelector('input.right').value = converter('RUB','USD',1,'left')