-
Notifications
You must be signed in to change notification settings - Fork 2
/
treeTable.js
110 lines (92 loc) · 2.89 KB
/
treeTable.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
/**
* Code for creating a tree table
*/
const treetable = require('./vendor/jquery-treetable/3.2.0/jquery.treetable')
const { updatePostings } = require('./postings')
const modalDetails = new Map()
function showModal(key) {
updatePostings(modalDetails.get(key), state.formatter, $('#modalPostings'), false);
$('#detailsModal').modal({
show: true
});
return false;
}
function makeTreeTable(amounts, node, formatter, sortByName, details) {
if (details) {
details.forEach((value, key) => {
modalDetails.set(key, value)
})
}
const html = []
html.push(`
<table id=${node.id + 'tree'}>
<thead>
<tr>
<th>Account</th>
<th>Amount</th>
${details ? '<th>Postings</th>' : ''}
</tr>
</thead>`)
console.log(amounts)
const keys = Array.from(amounts.keys())
if (sortByName) {
keys.sort()
} else {
keys.sort((l, r) => {
//sort by the amount of the paths
//of the same length
//eg expenses:home:rent
//and expenses:entertainment:movies
//should order by the values of
//expensse:home and expenses:entertainment
//as those are the buckets each is in
let lSplit = l.split(':')
let rSplit = r.split(':')
for(i = 0; i < Math.min(lSplit.length, rSplit.length); i++) {
lPrefix = lSplit.slice(0, i+1).join(':')
rPrefix = rSplit.slice(0, i+1).join(':')
if(lPrefix != rPrefix) {
let answer = amounts.get(rPrefix) - amounts.get(lPrefix)
if(answer != 0) {
return answer;
}
}
}
if(l > r) {
return 1;
} else {
return -1;
}
})
}
const ids = new Map();
let nextId = 1;
for (key of keys) {
if (!ids.has(key)) {
ids.set(key, nextId)
nextId++;
}
}
for (key of keys) {
value = amounts.get(key)
let parentAtt = ''
if (key.includes(':')) {
split = key.split(':')
split.pop()
parent = split.join(':')
parentId = ids.get(parent)
parentAtt = `data-tt-parent-id="${parentId}"`
}
html.push(`<tr data-tt-id="${ids.get(key)}" ${parentAtt}>`);
html.push(`<td>${escapeHtml(key)}</td>`)
html.push(`<td align="right">${formatter(value)}</td>`)
if (details) {
html.push(`<td align="right"><a href="javascript:showModal('${key}')">postings...</td>`)
}
html.push(`</tr>`)
}
html.push('</table>')
node.innerHTML = html.join('\n')
$("#" + node.id + 'tree').treetable({ expandable: true });
}
module.exports = { makeTreeTable, showModal }