-
Notifications
You must be signed in to change notification settings - Fork 0
/
EQ.js
124 lines (112 loc) · 3.75 KB
/
EQ.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
var persons = [];
$(document).ready(function(){
load();
parse();
if(persons == null)
persons = [];
var html = "<th>Event</th><th>Payee</th><th>Amount</th><th>"+persons.join("</th><th>")+"</th><th>"
html+="</th>";
$('#header').html(html);
$('#add').click(function(){addRow();})
$('#person').click(function(){addPerson();})
$('#clear').click(function(){clear();})
$('#analyse').click(function(){G.simplify();$('#matrix').html(matrix());})
})
function clear(){
$.localStorage.setItem("save",null);
$.localStorage.setItem("persons",JSON.stringify([]));
location.reload();
}
$(document).on('click','.a',function(){
$(this).toggleClass('active');
parse();
})
$(document).on('change','input',parse);
$(document).on('change','select',parse);
function addPerson(){
var name = $('#newguy').val();
persons.push(name);
parse();
location.reload();
}
function parse(){
G = new DebtGraph();
var rows = $('.event');
var R = [];
for(var i = 0;i<rows.length;i++){
console.log("A");
var row = $(rows[i]);
var payee = row.find('select').val();
var amount = parseFloat(row.find('input[type="number"]').val());
var event = row.find('input[type="text"]').val();
var users = [];
row.find('.active').each(function(x,e){users.push($(e).html())});
var payees = users.length;
for(var j = 0;j<users.length;j++){
if(users[j] != payee){
G.addDebt({
debtor : users[j],
creditor : payee,
amount : amount/(payees),
})
}
}
R.push({
event : event,
payee : payee,
amount : amount,
users : users,
})
}
console.log(R);
//G.simplify();
//$('#alldebts').html(allDebts())
save(R);
}
function save(R){
$.localStorage.setItem("save",JSON.stringify(R));
$.localStorage.setItem("persons",JSON.stringify(persons));
}
function load(){
var save = $.localStorage.getItem("save");
var ps = $.localStorage.getItem("persons");
persons = JSON.parse(ps);
if(save != null){
var R = JSON.parse(save);
if(R != null){
for(var i = 0;i<R.length;i++){
addRow(i);
$('#row'+i).find('input[type="text"]').val(R[i].event);
$('#row'+i).find('input[type="number"]').val(R[i].amount);
$('#row'+i).find('select').val(R[i].payee);
var a = $('#row'+i).find('.a');
for(var j = 0;j<a.length;j++){
if(R[i].users.indexOf($(a[j]).html())>=0){
$(a[j]).addClass('active');
}
}
}
return;
}
}
addRow(0);
}
function matrix(){
var html = "";
html+= "<h3>Who pays who</h3>";
html += '<table class="table">';
html+="<tr><th>Person A</th><th>Pays</th><th>Person B</th></tr>";
G.edges.forEach(x => html+="<tr><td>"+x.debtor.name+"</td><td>"+Math.round(100*x.amount)/100+"</td><td>"+x.creditor.name+"</td></tr>");
html+="</table>";
return html;
}
function allDebts(){
var html = '<table class="table">';
persons.forEach(x => html+="<tr><td>"+x+"</td><td>"+G.getDebt(x)+"</td></tr>")
html+="</table>";
return html;
}
function addRow(i){
html = '<tr id="row'+i+'" class="event"><td><input type="text" placeholder="event" style="width:100px" id="e"></td><td><select id="u"><option>'+persons.join("</option><option>")+'</option></select></td><td><input type="number" value="0" style="width:50px;"></td><td><div class="a">'+persons.join('</div></td><td><div class="a">')+'</div></td></tr>';
$('#addRow').before(html);
}