forked from ddymko/binaryconverter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
150 lines (133 loc) · 5.32 KB
/
main.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
(function() {
document.addEventListener('DOMContentLoaded', function() {
var modifier;
var numb, bina, octa, hexa;
var dec = document.getElementById('dec');
var bin = document.getElementById('bin');
var oct = document.getElementById('oct');
var hex = document.getElementById('hex');
// Validates the inputs
dec.addEventListener('keydown', function(e) {
if (!validateDec(e.keyCode) && !validateBindings(e)) e.preventDefault();
});
// Converts the values, clears input boxes otherwise
dec.addEventListener('keyup', function(e) {
if (isEmpty(this.value))
clear();
else if (!allowTabbing(e)) {
convert(this.value);
bin.value = bina;
oct.value = octa;
hex.value = hexa;
}
});
// Validates the inputs
bin.addEventListener('keydown', function(e) {
if (!validateBin(e.keyCode) && !validateBindings(e)) e.preventDefault();
});
// Converts the values, clears input boxes otherwise
bin.addEventListener('keyup', function(e) {
if (isEmpty(this.value))
clear();
else if (!allowTabbing(e)) {
convert(this.value);
dec.value = numb;
oct.value = octa;
hex.value = hexa;
}
});
// Validates the inputs
oct.addEventListener('keydown', function(e) {
if (!validateOct(e.keyCode) && !validateBindings(e)) e.preventDefault();
});
// Converts the values, clears input boxes otherwise
oct.addEventListener('keyup', function(e) {
if (isEmpty(this.value))
clear();
else if (!allowTabbing(e)) {
convert(this.value);
bin.value = bina;
dec.value = numb;
hex.value = hexa;
}
});
// Validates the inputs
hex.addEventListener('keydown', function(e) {
if (!validateHex(e.keyCode) && !validateBindings(e)) e.preventDefault();
});
// Converts the values, clears input boxes otherwise
hex.addEventListener('keyup', function(e) {
if (isEmpty(this.value))
clear();
else if (!allowTabbing(e)) {
convert(parseInt(this.value, 16));
bin.value = bina;
dec.value = numb;
oct.value = octa;
}
});
// Power the conversions to the respective format
function convert(val) {
numb = new Number(val);
bina = numb.toString(2);
octa = numb.toString(8);
hexa = numb.toString(16).toUpperCase();
}
// Checks if the value is empty
function isEmpty(value) {
return value === '';
}
// Clears out the text boxes when the user deletes an entry
function clear() {
dec.value = '';
bin.value = '';
hex.value = '';
oct.value = '';
}
// Validates the inputs for decimal boxes
function validateDec(keyCode) {
return ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 96 && keyCode <= 105)) ? true : false;
}
// Validates the inputs for binary boxes
function validateBin(keyCode) {
return (keyCode === 48 || keyCode === 49 || keyCode === 96 || keyCode === 97) ? true : false;
}
// Validates the inputs for octal boxes
function validateOct(keyCode) {
return ((keyCode >= 48 && keyCode <= 55) || (keyCode >= 96 && keyCode <= 103)) ? true : false;
}
// Validates the inputs for hexadecimal boxes
function validateHex(keyCode) {
return ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 96 && keyCode <= 105) || (keyCode >= 65 && keyCode <= 70)) ? true : false;
}
// Validates when various bindings are used for ease of use, outlined below
function validateBindings(ev) {
if (ev.keyCode === 8) // Backspace key
return true;
if (ev.keyCode === 9) // Tab key
return true;
if (ev.keyCode === 46) // Delete key
return true;
if (ev.keyCode === 65 && ev.metaKey === true) // Cmd + A
return true;
if (ev.keyCode === 65 && ev.ctrlKey === true) // Ctrl + A
return true;
if (ev.keyCode === 67 && ev.metaKey === true) // Cmd + C
return true;
if (ev.keyCode === 67 && ev.ctrlKey === true) // Ctrl + C
return true;
if (ev.keyCode === 86 && ev.metaKey === true) // Cmd + V
return true;
if (ev.keyCode === 86 && ev.ctrlKey === true) // Ctrl + V
return true;
if (ev.keyCode >= 37 && ev.keyCode <= 40) // Arrow keys
return true;
}
// Allows for the use to tab and shift tab through the results
function allowTabbing(ev) {
console.log(ev.keyCode + ', Shift:' + ev.shiftKey);
if (ev.keyCode === 9 || ev.shiftKey && ev.keyCode === 9 || ev.keyCode === 16)
return true;
}
});
})();