-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeFilter.js
More file actions
101 lines (91 loc) · 3.17 KB
/
timeFilter.js
File metadata and controls
101 lines (91 loc) · 3.17 KB
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
var timeFilter = (function() {
var global = {};
global.submitInput = function(ele, clockType, amPm) {
let minutes = 0, hours = 0;
if (ele.value === "") {
clockulous.noTimeTravel();
return false;}
if (!ele.value.match(/[:]/)) { // in the event they left out the ":"
if (ele.value.length<3) { hours = parseInt(ele.value) }
else {
hours = parseInt(ele.value.slice(0, -2));
minutes = parseInt(ele.value.slice(-2)); }
} else if(ele.value.match(/:/)) { // in the event they got a ":"
splitStr = ele.value.split(":");
hours = parseInt(splitStr[0]);
minutes = parseInt(splitStr[1]);
if (isNaN(hours)) hours = 0;
if (isNaN(minutes)) minutes = 0;
}
if (clockType && amPm === "PM") hours += 12;
return hours*60*60+minutes*60;
}
global.filter = function(evt, ele, clockType) {
let key;
key = evt.key || evt.which;
index = ele.getAttribute('data-index');
if (key === "Enter") { clockulous.submitTimeFilter(ele, index) }
/////// Exclude non number characteres. I avoided a [^] match to leave operational keys enabled. //////
if (key.length === 1) {
if(key.match(/[A-Za-z\s\.\\\+\*\?\^\$\[\]\{\}\(\)\|\/\-&~!@#%`="><_',;]|Decimal|Multiply|Add|Divide|Subtract|Seperator/)) evt.preventDefault();
}
/////// Prevents users from typing ":" twice, or from typing 3 digits of minutes ///////
if (ele.value.match(/:/)) {
if(key.match(/:/)) evt.preventDefault();
if(ele.value.match(/[0-9]:[0-9]{2}/)) {
if (key.match(/[0-9]/)) {
evt.preventDefault();
ele.value = ele.value.match(/[0-9]/g).join("") + key;
}
}
}
/////// First Number and Second Number Conditions ////////
if (ele.value.length === 0) { // First Keypress
if ( clockType ? key.match(/[2-9]/) : key.match(/[3-9]/) ) { // transfer 2-9 or 3-9 to 1 hour place
evt.preventDefault();
ele.value = "0" + key + ":";
} else if (key.match(/:/)) { // 00:XX
evt.preventDefault();
ele.value = "00" + key;
}
} else if (ele.value.length === 1) { // Second Keypress
if ( clockType ) { // 12 hour clock
if (key.match(/[3-5]/)) { // 1:X transfer 3-5 to 10 minute place
evt.preventDefault();
ele.value += ":" + key;
} else if (key.match(/[6-9]/)) { // 1:X transfer 6-9 to 1 minute place
evt.preventDefault();
ele.value += ":0" + key;
}
} else if ( !clockType && ele.value === "2" ) { // 24 hour clock
if (key.match(/5/)) { // 2:X transfer 5 to 10 minute place
evt.preventDefault();
ele.value += ":" + key;
} else if (key.match(/[6-9]/)) {
evt.preventDefault();
ele.value += ":0" + key;
}
}
}
/////// Prevents a Number greating than 5 following a ":" /////
else if (ele.value.match(/:$/)) {
if (key.match(/[6-9]/)) {
evt.preventDefault();
ele.value += "0" + key;
}
}
////// If a user types 4 numbers without a ":" then adds ":" for them
if (ele.value.match(/[0-9]{3}/)) {
if (key.match(/[0-9]/)) {
evt.preventDefault();
ele.value = ele.value.slice(0,2) + ":" + ele.value.slice(2,3) + key
}
}
}
global.postFilter = function() {
filtered = this.value.match(/\d|:/g);
if (filtered) { this.value = filtered.join('') }
else { this.value = '' };
}
return global;
})();