-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMealSplitCalculator.html
240 lines (228 loc) · 8.28 KB
/
MealSplitCalculator.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Meal Split Calculator</title>
<link rel="stylesheet" href="styles.css"/>
<!-- Icon from https://icons8.com/icon/84779/estimate with -->
<!-- background=#395B64 (addRowButton background) and color=#333 (body background) -->
<link rel="icon" type="image/x-icon" href="icons8-estimate-96.png">
<script src="jquery-3.6.3.min.js"></script>
<script>
var rowCounter = 1;
$(document).ready(function() {
$("#total").on("input", () => calculate());
$("#addButton").click(() => addRow());
$("#copyLinkButton").click(() => generateAndCopyUrl());
var loadedRows = loadFromQueryParams();
// If we didn't load any rows from query params, then just start off with a blank row
if (!loadedRows) {
addRow();
}
calculate();
});
function loadFromQueryParams() {
var params = (new URL(document.location)).searchParams;
total = params.get('total');
if (total) {
$("#total").val(total);
}
rows = params.getAll('row');
if (rows && rows.length > 0) {
rows.forEach((row) => {
[name, cost] = splitRowParam(row);
[nameId, costId] = addRow();
$("#" + nameId).val(name);
$("#" + costId).val(cost);
});
return true;
}
return false;
}
function splitRowParam(row) {
// We use last index in case the name has '|' in it (though that'd be weird)
const lastIndex = row.lastIndexOf('|');
// If there's no delimiter, then either the name or the cost is missing. We'll assume it's
// the cost and try to parse it as such; but if that doesn't work, we'll assume it's the
// name instead.
if (lastIndex == -1) {
const cost = parseFloat(row);
console.log(cost);
if (isNaN(cost)) {
return [row, ""]
}
return ["", cost]
}
// Otherwise, we can just parse it normally as "<name>|<cost>"
const name = row.slice(0, lastIndex);
const cost = parseFloat(row.slice(lastIndex + 1));
return [name, cost]
}
function generateAndCopyUrl() {
var url = new URL(document.location);
total = getTotal();
if (total != 0) {
url.searchParams.append("total", total);
}
// Iterate over each row in the table body
$("#mainTable tbody tr").each(function() {
const nameCell = $(this).find("td").eq(0);
const costCell = $(this).find("td").eq(1);
const nameInput = nameCell.find("input");
var name = "";
if (nameInput.val() != "") {
name = nameInput.val();
}
const costInput = costCell.find("input");
var cost = 0;
if (costInput.val() != "") {
cost = costInput.val();
}
url.searchParams.append("row", name + "|" + cost);
});
navigator.clipboard.writeText(url.toString());
<!-- Icon from https://icons8.com/icon/21740/double-tick -->
<!-- background=#395B64 (addRowButton background) and color=#E5E5CB (body color) -->
$("#copyLinkButton img").attr('src',"icons8-double-tick-100.png")
setTimeout(() => {
$("#copyLinkButton img").attr('src',"icons8-link-100.png");
}, 1000);
}
function addRow() {
// each id should be unique for valid HTML
const nameId = "name" + rowCounter;
const costId = "cost" + rowCounter;
const removeRowButtonId = "removeRowButton" + rowCounter;
rowCounter = rowCounter + 1;
const rowContent = `
<tr>
<td nowrap><input type="text" id="${nameId}" name="${nameId}" class="inputTextField"></td>
<td nowrap>$ <input type="number" id="${costId}" name="${costId}" step="0.01" class="inputTextField"></td>
<td nowrap></td>
<td nowrap></td>
<td nowrap><button id="${removeRowButtonId}" class="removeRowButton">X</button></td>
</>tr>
`
$("#mainTable tbody").append(rowContent);
$("#" + costId).on("input", () => calculate());
$("#" + removeRowButtonId).click(() => removeRow($("#" + removeRowButtonId)));
return [nameId, costId]
}
function calculate() {
subtotal = calculateSubtotal();
[totalPercentage, totalToPay] = calculatePercentageAndToPay(subtotal);
// Update Totals row
$("#mainTable tfoot tr").each(function() {
const costCell = $(this).find("th").eq(1);
costCell.text("$ " + round(subtotal));
const percentageCell = $(this).find("th").eq(2);
percentageCell.text(totalPercentage + "%");
const toPayCell = $(this).find("th").eq(3);
toPayCell.text("$ " + totalToPay);
});
const extras = round(getTotal() - subtotal);
$("#extras").text("Taxes, Fees, Discounts, Tips, etc: $ " + extras);
}
function calculateSubtotal() {
var newSubtotal = 0.0;
// Iterate over each row in the table body
$("#mainTable tbody tr").each(function() {
const costCell = $(this).find("td").eq(1);
const costInput = costCell.find("input");
if (costInput.val() != "") {
newSubtotal = newSubtotal + parseFloat(costInput.val());
}
});
return newSubtotal;
}
function getTotal() {
total = $("#total").val();
if (total == "") {
total = 0;
}
return total;
}
function calculatePercentageAndToPay(subtotal) {
total = getTotal();
totalToPay = 0;
totalPercentage = 0;
// Iterate over each row in the table body
$("#mainTable tbody tr").each(function() {
const costCell = $(this).find("td").eq(1);
const percentageCell = $(this).find("td").eq(2);
const toPayCell = $(this).find("td").eq(3);
const costInput = costCell.find("input");
var cost = 0;
if (costInput.val() != "") {
cost = parseFloat(costInput.val());
}
percentage = 0;
if (subtotal != 0) {
var percentage = cost / subtotal;
}
roundedPercentage = round(percentage * 100);
totalPercentage = round(totalPercentage + roundedPercentage);
percentageCell.text(roundedPercentage + "%");
toPay = round(percentage * total);
totalToPay = round(totalToPay + toPay);
toPayCell.text("$ " + toPay);
});
return [totalPercentage, totalToPay];
}
function removeRow(button) {
button.closest('tr').remove();
calculate();
}
function round(num) {
return parseFloat((Math.round(num * 100) / 100).toFixed(2));
}
</script>
</head>
<body>
<h1>Meal Split Calculator</h1>
<label for="total"><b>Overall Total: $ </b></label><input type="number" id="total" name="total" step="0.01" class="inputTextField">
<br>
<br>
<table id="mainTable">
<thead>
<tr>
<th>Name</th>
<th>Cost</th>
<th>Percentage</th>
<th>To Pay</th>
<th></th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
<tr>
<th>Totals</th>
<th align="left"></th>
<th align="left"></th>
<th align="left"></th>
<th></th>
</tr>
</tfoot>
</table>
<p id="extras"></p>
<button id="addButton" class="addRowButton">
<!-- Icon from https://icons8.com/icon/11394/add-row -->
<!-- background=#395B64 (addRowButton background) and color=#E5E5CB (body color) -->
<img src="icons8-add-row-100.png" title="Add Row" class="addRowButtonImage">
</button>
<br>
<br>
<button id="copyLinkButton" class="addRowButton">
<!-- Icon from https://icons8.com/icon/7867/link -->
<!-- background=#395B64 (addRowButton background) and color=#E5E5CB (body color) -->
<img src="icons8-link-100.png" title="Copy Link" class="addRowButtonImage">
</button>
<br>
<br>
<a href="../MealSplitCalculator" target="_blank">README</a>
<br>
<br>
</body>
</html>