-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
216 lines (186 loc) · 5.8 KB
/
script.js
File metadata and controls
216 lines (186 loc) · 5.8 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const AmountInput = document.querySelector("[data-user-input");
const TipsBtn = document.querySelectorAll("[data-tip]");
const CustomTips = document.querySelector("[data-custom-tip]");
const People = document.querySelector("[data-no-of-person]");
const Errors = document.querySelector("[data-error-message]");
const PeopleSvg = document.querySelector("[data-svg]");
const NoPeopleDiv = document.querySelector(".people-inputs");
const TipPerson = document.querySelector("[data-tip-per-person]");
const TotalPerson = document.querySelector("[data-total-per-person]");
const ResetBtn = document.querySelector("[data-reset]");
let billValue = "";
let TipValue = "";
let NumberPeople = "";
// User Amount Input
AmountInput.addEventListener("input", Billvalue);
// Tips Button
TipsBtn.forEach((btn) => {
btn.addEventListener("click", Handle);
});
// Custom User Tip
CustomTips.addEventListener("input", CustomTipValue);
// Number of People Input
People.addEventListener("input", NumberofPeople);
// Reset Button
ResetBtn.addEventListener("click", Reset);
// Below Function is used to validate the User AmountInput.
function ValidateAmount(p) {
var regExp = /^[0-9]*\.?[0-9]*$/; //A Global Search for numbers that are not from 0 to 9;
return p.match(regExp);
}
function ValidateValue(x) {
var RegExp = /^[0-9]/; //A Global Search for numbers that are not from 0 to 9; means no character.
return x.match(RegExp);
}
// Function For AmountInput
function Billvalue() {
// Replaces the user entered ',' into '.'
if (AmountInput.value.includes(",")) {
AmountInput.value = AmountInput.value.replace(",", ".");
}
// Prevents user to enter character in AmountInput
if (ValidateAmount(AmountInput.value)) {
console.log("valid");
} else {
console.log("Invalid");
AmountInput.value = AmountInput.value.substring(
0,
AmountInput.value.length - 1
); //Basically extracting all the strings or character entered by user in AmountInput.
}
if (AmountInput.value == 0) return;
billValue = parseFloat(AmountInput.value);
calculateTip();
resetOpa();
// console.log(billValue);
}
// Function For Tips Button
function Handle(event) {
TipsBtn.forEach((btn) => {
// Removing the Active class from button
btn.classList.remove("btn-active");
// Setting the active class in Button
if (event.target.value == btn.value) {
btn.classList.add("btn-active");
// console.log(event.target.value);
// console.log(btn.value);
TipValue = parseFloat(btn.value);
}
CustomTips.value = "";
calculateTip();
resetOpa();
});
}
// Function For Tips Button
function CustomTipValue() {
if (ValidateValue(CustomTips.value)) {
console.log("Tip Valid");
} else {
console.log("Tip Invalid");
CustomTips.value = CustomTips.value.substring(
0,
CustomTips.value.length - 1
);
}
// Removing the Active class from tips Button
TipsBtn.forEach((btn) => {
btn.classList.remove("btn-active");
});
TipValue = parseFloat(CustomTips.value / 100);
calculateTip();
resetOpa();
// console.log(TipValue);
}
// Function For Tips Button
function NumberofPeople() {
// Validting the number of people
if (ValidateValue(People.value)) {
console.log("No People Valid");
} else {
console.log("No People Invalid");
People.value = People.value.substring(0, People.value.length - 1);
}
NumberPeople = parseFloat(People.value);
if (NumberPeople == 0 || NumberPeople < 1 || NumberPeople == NaN) {
Errors.style.display = "block";
PeopleSvg.classList.add("error-svg");
NoPeopleDiv.setAttribute("data-error", "true");
} else {
Errors.style.display = "none";
PeopleSvg.classList.remove("error-svg");
NoPeopleDiv.setAttribute("data-error", "false");
}
calculateTip();
resetOpa();
// console.log(People.value);
}
// Function For Calculating the tip
function calculateTip() {
let TipAmount = (billValue * TipValue) / NumberPeople;
let TotalAmount = billValue + billValue * TipValue;
TipPerson.innerHTML = "$" + TipAmount.toFixed(2);
TotalPerson.innerHTML = "$" + TotalAmount.toFixed(2);
}
// Function For Reset Button Active State
function resetOpa() {
if (AmountInput.value != 0 && People.value != 0) {
ResetBtn.style.opacity = "1";
} else {
ResetBtn.style.opacity = "0.25";
}
}
// Function For Reset (INCOMPLETE IT IS.)
function Reset() {
if (AmountInput.value != 0 && People.value != 0) {
AmountInput.value = "";
Billvalue();
People.value = "";
NumberofPeople();
TipsBtn[0].click();
TipPerson.innerHTML = "$0.00";
TotalPerson.innerHTML = "$0.00";
ResetBtn.style.opacity = "0.25";
} else {
return;
}
}
// For Dark Mode Theme Toggler
let DarkMode = localStorage.getItem("darkMode");
const ThemeBtn = document.querySelector("[data-theme]");
const EnableDarkMode = () => {
ThemeBtn.innerHTML = "Dark";
document.body.classList.add("Darkmode");
localStorage.setItem("darkMode", "enabled");
};
const DisableDarkMode = () => {
ThemeBtn.innerHTML = "Light";
document.body.classList.remove("Darkmode");
localStorage.setItem("darkMode", "disabled");
};
if (DarkMode == "enabled") {
EnableDarkMode();
}
ThemeBtn.addEventListener("click", () => {
DarkMode = localStorage.getItem("darkMode");
if (DarkMode == "enabled") {
DisableDarkMode();
} else {
EnableDarkMode();
}
});
// Button Ripple Effect
const RippleBtn = document.querySelectorAll(".ripple");
RippleBtn.forEach((ripple) => {
ripple.addEventListener("click", function (e) {
console.log("Ripple Effect is here");
let x = e.clientX - e.target.offsetLeft;
let y = e.clientY - e.target.offsetTop;
let ripples = document.createElement("span");
ripples.style.left = x + "px";
ripples.style.top = y + "px";
this.appendChild(ripples);
setTimeout(() => {
ripples.remove();
}, 10000);
});
});