-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
157 lines (132 loc) · 3.78 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
const rate = 7.53450;
const registerFormat = new RegExp('^0+[0-9]{0,9}[.]?[0-9]{0,2}$');
const registerFullFormat = new RegExp('^0+[0-9]{0,9}[.][0-9]{2}$');
const sibling = {
'ceur': 'chrk',
'chrk': 'ceur',
'ueur': 'uhrk',
'uhrk': 'ueur',
};
const nextInput = {
'ceur': 'ueur',
'chrk': 'uhrk',
'ueur': 'ceur',
'uhrk': 'chrk',
};
let register;
let activeInput;
function init() {
initOnInputClickSelectInput();
initOnButtonClickSelectKey();
initOnEuroClickAddValue();
initOnGlobalKeyPressTypeInput();
}
function resetRegister() { register = '0'; }
function initOnInputClickSelectInput() {
let elements = document.getElementsByTagName('input');
for (let each of elements) {
each.addEventListener('click', clickThisInput);
each.addEventListener('focus', focusThisInput);
}
}
function initOnButtonClickSelectKey() {
let elements = document.getElementsByClassName('button');
for (let each of elements) {
each.addEventListener('click', selectKey);
}
}
function initOnEuroClickAddValue() {
let elements = document.getElementsByTagName('img');
for (let each of elements) {
each.addEventListener('click', addValue);
}
}
function initOnGlobalKeyPressTypeInput() {
document.addEventListener('keypress',
e => typeInput(String.fromCharCode(e.which).toUpperCase())
);
}
function selectInput(inputId) {
activeInput = inputId;
resetRegister();
let element = document.getElementById(activeInput);
element.focus();
}
let lastSelectedInput = null;
window.addEventListener('load', function () {
lastSelectedInput = document.activeElement;
selectInput('ceur');
});
function focusThisInput(self) {
if (lastSelectedInput === document.activeElement) return;
lastSelectedInput = document.activeElement;
clickThisInput(self);
}
function clickThisInput(self) {
selectInput(self.target.id);
}
function selectKey(self) {
let key = self.target.childNodes[0].nodeValue;
typeInput(key);
}
function addValue(self) {
selectSiblingIfHrk();
value = parseFloat(self.target.dataset.value);
input = document.getElementById(activeInput);
input.value = (parseFloat(input.value) + value).toFixed(2);
resetRegister(); // register = input.value;
input.focus();
updateSibling();
}
function selectSiblingIfHrk() {
if (/^[cu]hrk$/.test(activeInput)) {
selectInput(sibling[activeInput]);
flash(activeInput);
}
}
function flash(id) {
// https://stackoverflow.com/a/63561659
let element = document.getElementById(id);
element.classList.remove('flash');
void element.offsetWidth;
element.classList.add('flash');
}
function selectNextInput() {
selectInput(nextInput[activeInput]);
}
function typeInput(key) {
input = document.getElementById(activeInput);
if (key === 'C') { resetRegister(); key = ''; }
if (key === ',') { key = '.'; }
if (key === '.' && register.slice(-1) === '.') { key = ''; }
if (registerFormat.test(register + key)) {
register += key;
} else if (registerFormat.test(register.slice(0, -1) + key)) {
register = register.slice(0, -1) + key;
}
input.value = parseFloat(register).toFixed(2);
input.focus();
updateSibling();
if(registerFullFormat.test(register)) selectNextInput();
}
function updateSibling() {
input = document.getElementById(activeInput);
v = input.value;
s = document.getElementById(sibling[activeInput]);
r = /eur$/.test(activeInput) ? rate : 1/rate;
s.value = (v * r).toFixed(2);
updatePovrat();
}
function updatePovrat() {
updatePEur();
updatePHrk();
}
function updatePEur() { diff('ueur', 'ceur', 'peur'); }
function updatePHrk() { diff('uhrk', 'chrk', 'phrk'); }
function diff(uid, cid, pid) {
let num = document.getElementById(uid).value - document.getElementById(cid).value;
if (num < 0) { num = 0; }
num = num.toFixed(2);
document.getElementById(pid).value = num;
}
init();