-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·528 lines (492 loc) · 14.7 KB
/
app.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
const model = (() => {
const stampDutyInfo = [
{
fullCalculation: [
{
name: "single property",
percentRange: [
{
"less than £125k": 0
},
{
"£125k to £250k": 2
},
{
"£250k to £925k": 5
},
{
"£925k to £1.5m": 10
},
{
"rest over £1.5m": 12
}
],
range: [125000, 250000, 925000, 1500000]
},
{
name: "addional property",
percentRange: [
{
"less than £125k": 3
},
{
"£125k to £250k": 5
},
{
"£250k to £925k": 8
},
{
"£925k to £1.5m": 13
},
{
"rest over £1.5m": 15
}
],
range: [125000, 250000, 925000, 1500000]
},
{
name: "first time buyer",
percentRange: [
{
"less than £300k": 0
},
{
"£300k to £500k": 5
},
{
"£500k to £925k": 5
},
{
"£925k to £1.5m": 10
},
{
"rest over £1.5m": 12
}
],
range: [300000, 500000, 925000, 1500000]
}
],
currentAmount: null,
current: null,
tableTitles: ["Tax Band", "%", "Taxable Sum", "Tax"]
}
];
const data = stampDutyInfo[0];
return {
setCurrent: current => {
data.current = current;
},
getCurrent: () => {
return data.current;
},
setAmount: amount => {
data.currentAmount = amount;
},
getAmount: () => {
return data.currentAmount;
},
getData: () => {
return data.fullCalculation;
},
getTables: () => {
return data.tableTitles;
}
};
})();
const view = (() => {
const formatMoney = new Intl.NumberFormat("en-UK", {
style: "currency",
minimumFractionDigits: 0,
maximumFractionDigits: 0,
currency: "GBP"
});
let DOMStrings;
DOMStrings = {
active: "sd--active",
btns: "sd__btns",
btn: "sd__btn",
btnType: "sd__btn__type",
results: "sd__results",
resultsContainer: "sd__results__container",
close: "sd__close",
closeInside: "sd__close-inside",
calculation: "sd__calculation",
calculationInner: "sd__calculation__inner",
title: "sd__title",
sd: "sd",
SD__POPUP__PRESENT: "sd__popup__present",
icon: "sd__icon",
inner: "sd__inner",
row: "sd__row",
greyLight: "sd__grey-light",
flexOne: "sd__flex-one",
smFont: "sd__sm-font",
amount: "sd__amount",
bold: "sd__bold"
};
const createButtons = data => {
let buttons = ``;
for (let i = 0; i < data.length; i++) {
buttons += `
<button class="${DOMStrings.btn} ${DOMStrings.btnType}" data-name='${i +
1}'>${data[i].name}</button>
`;
}
return buttons;
};
return {
getCurrentDataKey: (name, i) => {
return Object.keys(name[i]);
},
getCurrentDataValue: (name, i) => {
return name[i][Object.keys(name[i])];
},
amountInRange: (amount, a, b, percent) => {
let amountToTax;
if (amount > a && amount <= b) {
amountToTax = amount - a;
} else {
if (percent === 0) {
amountToTax = 0;
} else if (amount < a) {
amountToTax = 0;
} else {
amountToTax = a;
}
}
return amountToTax;
},
totalTaxAmount: (amount, percent) => {
return (amount / 100) * percent;
},
displaySD: (data, amount) => {
let sd_icon = chrome.extension.getURL("./images/percentage.svg");
let SDhtml = `
<div class="${DOMStrings.sd} ${DOMStrings.active}" >
<img src="${sd_icon}" width="25px;" class="${
DOMStrings.icon
}" style="display:block">
<div class="${DOMStrings.inner}">
<div class="${DOMStrings.close}"><span class="${
DOMStrings.closeInside
}">×</span></div>
<p class="${DOMStrings.title}">Stamp duty calculator</p>
<p class="${DOMStrings.amount}">${formatMoney.format(amount)}</p>
<div class ="${DOMStrings.btns}">
${createButtons(data)}
</div>
<div class="${DOMStrings.results} ${DOMStrings.btnType}"></div>
</div>`;
return SDhtml;
},
currentDataType(data, current, name) {
return data[current - 1][name];
},
calcPercent(a, b) {
return (a / 100) * b;
},
calcPreviousPercentTotal(selectedData, percentRange, currentDataRange) {
// previous percent calc to pay
// pass the previous range of percent
const previousPercent =
percentRange[selectedData][Object.keys(percentRange[selectedData])];
// work out the range to calc percent on
const previousAmountToTax =
currentDataRange[selectedData] - currentDataRange[0];
// work out value
const previousPerCentCalc = this.calcPercent(
previousAmountToTax,
previousPercent
);
return previousPerCentCalc;
},
currentPercentPayment(amount, currentDataRange, selectedRange, percent) {
// get the current range percent calc
const amountToTax = amount - currentDataRange[selectedRange];
const perCentCalc = this.calcPercent(amountToTax, percent);
return perCentCalc;
},
totalCosts(currentDataRange, amount, percentRange) {
let currentPercentTotal = 0;
let percent;
for (let i = 0; i < currentDataRange.length; i++) {
if (amount > currentDataRange[i] && amount <= currentDataRange[i + 1]) {
percent = percentRange[i + 1][Object.keys(percentRange[i + 1])];
// previous percent calc to pay
currentPercentTotal += this.calcPreviousPercentTotal(
i,
percentRange,
currentDataRange
);
currentPercentTotal += this.currentPercentPayment(
amount,
currentDataRange,
i,
percent
);
} else if (amount <= currentDataRange[0]) {
percent = percentRange[0][Object.keys(percentRange[0])];
currentPercentTotal += calcPercent(amount, percent);
}
}
return {
currentPercentTotal,
percent
};
},
createTableRows(data, percentRange, currentDataRange, amount) {
// create the rows for each range
let tableRows = ``;
for (let i = 0; i < data[0].percentRange.length; i++) {
let amountInRange;
let totalTaxtAmount;
if (i === 0) {
amountInRange = formatMoney.format(
this.amountInRange(
amount,
currentDataRange[0],
currentDataRange[0],
this.getCurrentDataValue(percentRange, 0)
)
);
totalTaxtAmount = formatMoney.format(
this.totalTaxAmount(
this.amountInRange(
amount,
currentDataRange[0],
currentDataRange[0],
this.getCurrentDataValue(percentRange, 0)
),
this.getCurrentDataValue(percentRange, 0)
)
);
} else {
amountInRange = formatMoney.format(
this.amountInRange(
amount,
currentDataRange[i - 1],
currentDataRange[i],
this.getCurrentDataValue(percentRange, i)
)
);
totalTaxtAmount = formatMoney.format(
this.totalTaxAmount(
this.amountInRange(
amount,
currentDataRange[i - 1],
currentDataRange[i],
this.getCurrentDataValue(percentRange, i)
),
this.getCurrentDataValue(percentRange, i)
)
);
}
tableRows += `
<div class='${DOMStrings.row} ${DOMStrings.greyLight}'>
<div class="${DOMStrings.flexOne}">
${this.getCurrentDataKey(percentRange, i)}
</div>
<div class="${DOMStrings.flexOne}">
${this.getCurrentDataValue(percentRange, i)}
</div>
<div class="${DOMStrings.flexOne}">
${amountInRange}
</div>
<div class="${DOMStrings.flexOne}">
${totalTaxtAmount}
</div>
</div>
`;
}
return tableRows;
},
createStampDutyInfo(data, current, percent, amount, currentPercentTotal) {
// Create section once property type button is clicked
let currentDisplay = `
<div class="${DOMStrings.resultsContainer}">
<p>Type: ${data[current - 1].name}</p>
<p>Amount: ${formatMoney.format(amount)}</p>
<p>Percent:<span class="${
DOMStrings.smFont
}">(highest)</span> ${percent}%</p>
<p>Stamp duty cost: <span class="${
DOMStrings.bold
}">${formatMoney.format(currentPercentTotal)}</span></p>
</div>
<button class="${DOMStrings.calculation} ${
DOMStrings.btn
}" data-calculation="view">View calculation</button>
`;
return currentDisplay;
},
createTableTitles(tableTitles) {
// get table titles from data
let tableTitleNames = ``;
for (let i = 0; i < tableTitles.length; i++) {
tableTitleNames += `
<div class="${DOMStrings.flexOne}">${tableTitles[i]}</div>
`;
}
return tableTitleNames;
},
createTable(tableTitleNames, tableRows) {
// add above functions to create the table
let table = `
<div class="${DOMStrings.calculationInner} ${DOMStrings.smFont}">
<div class="${DOMStrings.row} ${DOMStrings.grey}">
${tableTitleNames}
</div>
${tableRows}
</div>
</div>
</div>`;
return table;
},
updateDisplay(data, current, amount, tableTitles) {
const currentDataRange = this.currentDataType(data, current, "range");
const percentRange = this.currentDataType(data, current, "percentRange");
let totalCostFnc = this.totalCosts(
currentDataRange,
amount,
percentRange
);
// return from inside totalCostFnc
let percent = totalCostFnc.percent;
let currentPercentTotal = totalCostFnc.currentPercentTotal;
let tableRows = this.createTableRows(
data,
percentRange,
currentDataRange,
amount
);
let currentDisplay = this.createStampDutyInfo(
data,
current,
percent,
amount,
currentPercentTotal
);
let tableTitleNames = this.createTableTitles(tableTitles);
let table = this.createTable(tableTitleNames, tableRows);
// Add the table to the bottom of stamp duty info
currentDisplay += table;
document.querySelector(
`.${DOMStrings.results}`
).innerHTML = currentDisplay;
},
getDOMStrings: () => {
return DOMStrings;
}
};
})();
const controller = ((m, v) => {
let setUpEvents = () => {
let data = m.getData();
let DOM = v.getDOMStrings();
// check if sd__active is there
document.addEventListener("dblclick", function(e) {
if (!document.body.classList.contains(`${DOM.SD__POPUP__PRESENT}`)) {
// get string
const target = e.target.innerHTML;
// remove all apart from numbers
let formattedTarget = target.replace(/[^0-9]/g, "");
const endNumber = parseFloat(formattedTarget);
if (Number.isInteger(endNumber)) {
const amount = endNumber;
m.setAmount(amount);
// create pop up html stamp duty
e.target.insertAdjacentHTML("beforeEnd", v.displaySD(data, amount));
document.body.classList.add(`${DOM.SD__POPUP__PRESENT}`);
}
}
}); // end dbl click
document.addEventListener(
"click",
function(event) {
// click property type calculation
if (containsClass(DOM.btnType)) {
propertyTypeClick(event);
}
// togglePopUp
togglePopUp(event);
// toggle calc inner
if (event.target.dataset.calculation === "view") {
toggleDisplay(DOM.calculationInner);
}
if (containsClass(DOM.icon)) {
// toggle inside container with app in
toggleDisplay(DOM.inner);
// toggle inital sd_icon popup
toggleDisplay(DOM.icon);
// toggle height
}
},
false
);
};
let DOM = v.getDOMStrings();
let data = m.getData();
const containsClass = className => {
return event.target.classList.contains(className);
};
const propertyTypeClick = event => {
// get the data 1,2,3 from btns
const current = event.target.dataset.name;
// set current
m.setCurrent(current);
m.getCurrent();
// get new amount
const currentAmount = getCurrentAmount();
const tableTitles = m.getTables();
// display results of stamp duty
v.updateDisplay(data, current, currentAmount, tableTitles);
};
const togglePopUp = event => {
const popup = document.querySelector(`.${DOM.active}`);
let targetElement = event.target; // clicked element
// search up the dom, if active class is present inside of
// the popup on click do nothing
// else clicking outside remove the div
if (
targetElement.classList.contains(`${DOM.close}`) ||
targetElement.classList.contains(`${DOM.closeInside}`)
) {
removeElements(document.querySelectorAll(`.${DOM.active}`));
document.body.classList.remove(`${DOM.SD__POPUP__PRESENT}`);
} else {
do {
if (targetElement == popup) {
// This is a click inside. Do nothing, just return.
return;
}
// Go up the DOM
targetElement = targetElement.parentNode;
} while (targetElement);
// This is a click outside.
// remove popup
// reset current data
document.body.classList.remove(`${DOM.SD__POPUP__PRESENT}`);
removeElements(document.querySelectorAll(`.${DOM.active}`));
}
};
const toggleDisplay = className => {
const targetClass = document.querySelector(`.${className}`);
let currentDisplay = targetClass.style.display;
if (currentDisplay === "block") {
document.querySelector(`.${className}`).style.display = "none";
} else {
document.querySelector(`.${className}`).style.display = "block";
}
};
const getCurrentAmount = () => {
return m.getAmount();
};
const removeElements = elms => elms.forEach(el => el.remove());
return {
init: () => {
setUpEvents();
console.log("stamp duty extension started");
}
};
})(model, view);
controller.init();