-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.js
77 lines (61 loc) · 2.25 KB
/
logic.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
// array of buttons
var button = document.querySelectorAll('button');
// for display Box
var input = document.querySelector('input');
// addOrReplace used to check if we need to add new value to input or Replace last value from input
var addOrReplace = true;
// hold used for asynchronous calling
var hold;
// replace all variables to its original position
var is_busy;
// on long press we will add number to our input box
var delay = 1000;
// keep a track of index in text array
var index = -1;
// to check if new event happened or old one
var click = null;
for (var i = 0, len = button.length; i < len; ++i) {
// onmousedown event
button[i].onmousedown = function (e) {
var text = this.getAttribute('data-text').split(""),
number = this.getAttribute('data-number');
addOrReplace = true;
// clear old stored value
clearTimeout(is_busy);
// to check if we are on the same button or new one
if (click !== e.target) {
addOrReplace = false;
}
/* to check if we reached at the last index of array or not
and if we clicked new button then we set our index to zero again
*/
if (index >= text.length - 1 || click !== e.target) {
index = 0;
click = e.target;
} else {
index = index + 1;
}
// if long press happens then we call setTimeout web api to add number in input Box
hold = setTimeout(function () {
if (number) {
input.value = input.value.slice(0, -1) + number;
}
}, delay);
/* with the help of busy variable we can decide weather
to add new character at last or replace the last one
*/
input.value = addOrReplace ? input.value.slice(0, -1) + text[index] : input.value + text[index];
};
button[i].onmouseup = function (e) {
// clear all the values after user removed the mouse from the button
clearTimeout(hold);
addOrReplace = true;
is_busy = setTimeout(function () {
index = -1;
addOrReplace = false;
e.target = null;
}, delay);
input.focus();
input.selectionStart = input.selectionEnd = input.value.length;
};
}