-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
executable file
·259 lines (232 loc) · 9.65 KB
/
index.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<style>
div.container{margin-top: 15px;}
</style>
</head>
<body>
<!-- Contenedor principal -->
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">Pay calculator</div>
<div class="panel-body">
<!-- Botón de ayuda -->
<div id="help">
<button id="btnAyuda" type="button" class="btn btn-default" title="Instrucciones">
<span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span>Help
</button>
</div>
<!-- Panel con el texto de la ayuda -->
<div id="instructions" class="col-md-6 col-md-offset-3" style="display:none;">
<ul class="list-group">
<li class="list-group-item disabled">Usage Instructions:</li>
<li class="list-group-item">1. In the <b>'Name'</b> field, enter the name of the member.</li>
<li class="list-group-item">2. In the <b>'Amount Paid'</b> field, enter the total amount they paid.</li>
<li class="list-group-item">3. If you need to add more members, click the <b>'Add Member'</b> button.</li>
<li class="list-group-item">4. When you are done, click the <b>'Calculate'</b> button.</li>
<li class="list-group-item">5. You can see the results at the bottom.</li>
</ul>
</div>
<!-- Etiquetas de Nombre y Monto -->
<div class="col-md-5 col-md-offset-3">
<div class="col-md-6 col-sm-5 col-xs-7">
<label class="control-label" for='miembro_0'>Name</label>
</div>
<div class="col-md-6">
<label class="control-label" for='monto_0'>Amount Paid</label>
</div>
</div>
<!-- Campos de texto -->
<div class="col-md-6 col-md-offset-3">
<div id="fields">
<div class="form-group form-inline" id="li_0" data-id="0">
<div class="input-group">
<input class="form-control" list="compipas" id="miembro_0" placeholder="Name" />
<span class="input-group-addon"> $</span>
<input class="form-control" type="number" id="monto_0" min="0" max="1000000" value="0" />
</div>
</div>
<div class="form-group form-inline" id="li_list" data-id="1">
<div class="input-group">
<input class="form-control" list="compipas" id="miembro_1" placeholder="Name"/>
<span class="input-group-addon"> $</span>
<input class="form-control" type="number" id="monto_1" min="0" max="1000000" value="0" />
</div>
</div>
</div>
</div>
<div class="col-md-6 col-md-offset-3">
<div id="totales">
<div class="form-group form-inline">
<div class="input-group">
<input class="form-control" type="text" value="Total" readonly="readonly" />
<span class="input-group-addon"> $</span>
<input class="form-control" type="number" id="total" value="0" readonly="readonly" />
</div>
</div>
<div class="form-group form-inline">
<div class="input-group">
<input class="form-control" type="text" value="Average" readonly="readonly" />
<span class="input-group-addon"> $</span>
<input class="form-control" type="number" id="promedio" value="0" readonly="readonly" />
</div>
</div>
</div>
</div>
<!-- Botones 'Añadir compipa' y 'Calcular' -->
<div class="col-md-6 col-md-offset-3">
<div class="form-group">
<div id='last'>
<input class="btn btn-default" type="button" id="add" value="Add member" />
</div>
</div>
<div class="form-group">
<input class="btn btn-primary" type="button" id="calcular" value="Calculate" />
</div>
</div>
</div>
</div>
</div>
<!-- Panel de resultados -->
<div class="container" id="panel-resultados" style="display:none;">
<div class="panel panel-primary">
<div class="panel-heading">Results</div>
<div class="panel-body">
<div class="col-md-6 col-md-offset-3" id="resultado">
</div>
</div>
</div>
</div>
<script>
// Función obtener promedio
const obtenerPromedio = () => {
let total = 0;
let validCount = 0;
document.querySelectorAll('[id^="li_"]').forEach(li => {
const id = li.dataset.id;
const nombre = document.getElementById(`miembro_${id}`).value.trim();
const monto = parseInt(document.getElementById(`monto_${id}`).value) || 0;
if (nombre !== '') {
total += monto;
validCount++;
}
});
document.getElementById('total').value = total;
document.getElementById('promedio').value = validCount ? total / validCount : 0;
document.getElementById('panel-resultados').style.display = 'none';
}
// Función eliminar
const eliminar = (element) => {
element.remove();
obtenerPromedio();
calcular();
}
// Función calcular
const calcular = () => {
const miembro = [];
const matriz = [];
const membersCount = document.querySelectorAll('[id^="li_"]').length;
document.getElementById('resultado').innerHTML = '';
document.querySelectorAll('[id^="li_"]').forEach((li, i) => {
const id = li.dataset.id;
const nombre = document.getElementById(`miembro_${id}`).value.trim();
if (nombre !== '') {
miembro[i] = {
nombre: nombre,
monto: parseInt(document.getElementById(`monto_${id}`).value) || 0
};
}
});
const promedio = parseInt(document.getElementById('promedio').value);
miembro.forEach((miembroX, x) => {
if (miembroX.monto !== promedio) {
if (miembroX.monto < promedio) {
miembroX.monto -= promedio;
matriz[x] = {};
miembro.forEach((miembroY, y) => {
if (miembroY !== miembroX && miembroY.monto > promedio & miembroX.monto < 0) {
const amount = Math.min(miembroY.monto - promedio, -miembroX.monto);
matriz[x][y] = amount;
miembroX.monto += amount;
miembroY.monto -= amount;
}
});
}
}
});
matriz.forEach((deudas, x) => {
if (Object.keys(deudas).length > 0) {
const listGroup = document.createElement('div');
listGroup.className = 'list-group';
const detail = document.createElement('a');
detail.name = 'detail';
detail.id = x;
detail.className = 'list-group-item active';
detail.textContent = `${miembro[x].nombre} owes:`;
listGroup.appendChild(detail);
Object.entries(deudas).forEach(([y, amount]) => {
const detailItem = document.createElement('a');
detailItem.name = `detail_${x}`;
detailItem.className = 'list-group-item';
detailItem.textContent = `${miembro[y].nombre}: $${Math.round(amount)}`;
listGroup.appendChild(detailItem);
});
document.getElementById('resultado').appendChild(listGroup);
}
});
document.getElementById('panel-resultados').style.display = 'block';
}
// Variable global members, va contando el número de elementos creados, aunque se eliminen
let members = 1;
// Botón de ayuda
document.getElementById('btnAyuda').addEventListener('click', () => {
const instructions = document.getElementById('instructions');
if (instructions.style.display === 'none' || instructions.style.display === '') {
instructions.style.display = 'block';
} else {
instructions.style.display = 'none';
}
});
// Añadir un nuevo elemento
document.getElementById('add').addEventListener('click', () => {
members++;
const newLi = document.getElementById('li_0').cloneNode(true);
newLi.dataset.id = members;
document.getElementById('fields').appendChild(newLi);
const newMiembro = newLi.querySelector('[id^="miembro_"]');
newMiembro.id = `miembro_${members}`;
newMiembro.value = '';
const newMonto = newLi.querySelector('[id^="monto_"]');
newMonto.id = `monto_${members}`;
newMonto.value = 0;
const eliminarBtn = document.createElement('input');
eliminarBtn.type = 'button';
eliminarBtn.className = 'btn btn-default eliminar';
eliminarBtn.dataset.id = members;
eliminarBtn.value = 'Remove';
newLi.appendChild(eliminarBtn);
eliminarBtn.addEventListener('click', () => {
eliminar(newLi);
});
});
// Cuando hay un cambio en fields, se recalcula el total y el promedio
document.getElementById('fields').addEventListener('input', (event) => {
obtenerPromedio();
});
// Al hacer click en el botón 'Calcular'
document.getElementById('calcular').addEventListener('click', calcular);
// Plegar o desplegar los resultados de un compipa
document.getElementById('resultado').addEventListener('click', (event) => {
if (event.target.name === 'detail') {
const id = event.target.id;
document.querySelectorAll(`a[name="detail_${id}"]`).forEach(element => {
element.classList.toggle('hidden');
});
}
});
</script>
</body>
</html>