-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
278 lines (199 loc) · 9.64 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* --------------------------------------------------------------------*/
/* GELD IST ZEIT */
/* Copyright 2019 by @groonworld */
/* Published under MIT License: https://opensource.org/licenses/MIT */
/* --------------------------------------------------------------------*/
/********************************************
* CALCULATOR Module *
*******************************************/
let numberCruncher = (function() {
const currency = '&euro';
const unit = ' Minuten'
let workdays = 5;
return {
salaryCalc: function(input) {
// 1. Check if user input is present and is a number
if (!isNaN(input.monatsgehalt) &&
!isNaN(input.wochenstunden) &&
input.monatsgehalt !== '' &&
input.wochenstunden !== '') {
const weeksPerMonth = (365.25 / 7) / 12;
return {
hrSalary: input.monatsgehalt / (input.wochenstunden * weeksPerMonth),
dSalary: (input.monatsgehalt / (input.wochenstunden * weeksPerMonth)) * (input.wochenstunden / workdays),
minSalary: input.monatsgehalt / (input.wochenstunden * weeksPerMonth) / 60,
quartSalary: input.monatsgehalt / (input.wochenstunden * weeksPerMonth) / 4
}
} else {
console.log('Function salaryCalc() received NaN input.')
}
},
coffeePrice: function(data) {
return Math.round(data.items.cappuccino / data.salary.minSalary) + unit;
},
customPrice: function(data) {
return Math.round(data.items.custom / data.salary.minSalary);
},
customConvert: function(price, dailyHrs) {
let fullMin = price / 60;
let minutes = price % 60;
let hours = Math.floor(fullMin % dailyHrs);
let days = Math.floor(fullMin / dailyHrs);
return {
days: days,
hours: hours,
minutes: minutes
}
}
}
})();
/********************************************
* UI CONTROLLER Module *
*******************************************/
let publish = (function() {
let currency = {
money: ' €',
};
return {
/******* Common functions ********/
toggleElement: function(elementID, action) { // 'action' must be either 'hide' or 'show'
let element = document.getElementById(elementID);
if (action === 'hide') {
element.style.display = "none";
} else if (action === 'show') {
element.style.display = "block";
} else {
console.log('ui.toggleElement() has received invalid argument of ' + action);
}
},
getInput: function() {
return {
monatsgehalt: Number(document.querySelector('.input_gehalt').value.replace(',','.')),
wochenstunden: Number(document.querySelector('.input_stunden').value.replace(',','.')),
}
},
update: function(salary, items, prices) { // Prints all the results to the user interface
document.getElementById('value_day').innerHTML = salary.dSalary.toFixed(2);
document.getElementById('value_hr').innerHTML = salary.hrSalary.toFixed(2);
//document.getElementById('value_quart').innerHTML = salary.quartSalary.toFixed(2);
document.getElementById('value_min').innerHTML = salary.minSalary.toFixed(2);
document.getElementById('cappuccino').textContent = prices.cappuccino;
},
getCustom: function() {
return Number(document.querySelector('.input_custom').value.replace(',','.'));
},
updateCustom: function(obj) {
// pad(); converts numbers to a string and padds numbers below 10 with a leading zero (3 --> '03').
let pad = function(number) {
return (number < 10) ? '0' + number.toString() : number.toString();
};
// Displays the resulting strings in the UI
document.getElementById('custom_days').textContent = pad(obj.days);
document.getElementById('custom_hours').textContent = pad(obj.hours);
document.getElementById('custom_minutes').textContent = pad(obj.minutes);
}
}
})();
/********************************************
* GLOBAL APP CONTROLLER Module *
*******************************************/
let controller = (function(calc, ui){
let dailyHrs; // See comment in calculate().
const data = {
workdays: 5,
input: {},
salary: {},
prices: {},
items: {
cappuccino: 5,
custom: -1,
}
};
function hasInput() {
// Calls either calculate() or custom(), depending on which fields have input.
let input_gehalt = document.querySelector('.input_gehalt').value !== ''; //
let input_stunden = document.getElementById('input_stunden').value !== ''; // TRUE if fields contain input
let input_custom = document.getElementById('input_custom').value !== ''; //
if ((input_gehalt || input_stunden) && !input_custom) {
calculate();
} else if (input_custom) {
custom();
} else {
console.log('Insufficient user input.')
};
}
function setUpEventListeners() {
document.getElementById('calculate_btn').addEventListener('click', calculate);
document.getElementById('btn_custom_calc').addEventListener('click', custom);
document.addEventListener('keypress', function(event) {
if (event.keyCode === 13 || event.which === 13){ // KeyCode 13 is the "Enter"-key
hasInput(); // Checks which fields have input and calls either calculate() or custom() accordingly.
}
});
}
function calculate() {
// 1. Get initial user input
data.input = ui.getInput();
data.input.dailyHrs = data.input.wochenstunden / data.workdays;
dailyHrs = data.input.dailyHrs; // Can't seem to base a calculation on a property within the same object as it is being created. Surely there must be a better way than this workaround.
// 2. Calculate salary values
data.salary = calc.salaryCalc(data.input);
// 3. Calculate value for cappuccino example
data.prices.cappuccino = calc.coffeePrice(data);
// 4. Update and display cappuccino example
ui.update(data.salary, data.items, data.prices);
ui.toggleElement('output_cappuccino', 'show');
// 5. Reveal the next section
ui.toggleElement('output_custom', 'show');
}
function custom() {
// 1. Get user input from "custom" field
data.items.custom = ui.getCustom();
// 2. Calculate custom price in minutes
data.prices.custom = calc.customPrice(data);
// 2a. Convert custom price to DD:HH:MM-format
data.prices.convertedCustom = calc.customConvert(data.prices.custom, data.input.dailyHrs);
// 3. Update and display converted custom price
ui.updateCustom(data.prices.convertedCustom);
// 4. Reveal the next section
ui.toggleElement('output_hours', 'show');
}
return {
init: function() {
// 1. Hide results section
ui.toggleElement('output_cappuccino', 'hide');
ui.toggleElement('output_custom', 'hide');
ui.toggleElement('output_hours', 'hide');
// 2. Setup Event Listeners
setUpEventListeners();
},
/*** FOR TESTING ONLY *****
logData: function() {
console.log(data);
}
/*** FOR TESTING ONLY ****/
}
})(numberCruncher, publish);
controller.init();
/***************************************************
* Some useless stuff: *
***************************************************/
/*
// This won't work. If the user de-selects the input field before pressing the 'enter' key, neither condition is met.
// A better idea is to check for contents (fieldContent ! = '' ) in all fields, and then select action based on that.
function activeElement() {
// Calls either calculate() or custom(), depending on which element is active
let input_gehalt = document.activeElement.tagName === 'input_gehalt';
let input_stunden = document.activeElement.tagName === 'input_stunden';
let input_custom = document.activeElement.tagName === 'input_custom';
console.log(input_gehalt);
console.log(input_stunden);
console.log(input_custom);
if (input_gehalt || input_stunden) {
return calculate;
} else if (input_custom) {
return custom;
} else {
console.log('No input element active.')
};
} */