forked from SolidOS/solid-panes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtripPane.js
More file actions
176 lines (150 loc) · 5.81 KB
/
tripPane.js
File metadata and controls
176 lines (150 loc) · 5.81 KB
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
/* Trip Pane
**
** This pane deals with trips themselves and also
** will look at transactions organized by trip.
**
** This outline pane allows a user to interact with a transaction
** downloaded from a bank statement, annotting it with classes and comments,
** trips, etc
*/
import * as UI from 'solid-ui-jss'
import * as $rdf from 'rdflib'
const ns = UI.ns
export default {
icon: UI.icons.iconBase + 'noun_62007.svg',
name: 'travel expenses',
audience: [ns.solid('PowerUser')],
// Does the subject deserve this pane?
label: function (subject, context) {
const kb = context.session.store
const t = kb.findTypeURIs(subject)
// if (t['http://www.w3.org/2000/10/swap/pim/qif#Transaction']) return "$$";
// if(kb.any(subject, UI.ns.qu('amount'))) return "$$$"; // In case schema not picked up
if (UI.ns.qu('Transaction') in kb.findSuperClassesNT(subject)) {
return 'by Trip'
}
if (t['http://www.w3.org/ns/pim/trip#Trip']) return 'Trip $'
return null // No under other circumstances (while testing at least!)
},
render: function (subject, context) {
const myDocument = context.dom
const kb = context.store
const ns = UI.ns
const CAL = $rdf.Namespace('http://www.w3.org/2002/12/cal/ical#')
const TRIP = $rdf.Namespace('http://www.w3.org/ns/pim/trip#')
const div = myDocument.createElement('div')
div.setAttribute('class', 'transactionPane')
div.innerHTML = '<h2>Trip transactions</h2>'
const complain = function complain (message, style) {
if (style === undefined) style = 'color: grey'
const pre = myDocument.createElement('pre')
pre.setAttribute('style', style)
div.appendChild(pre)
pre.appendChild(myDocument.createTextNode(message))
}
// //////////////////////////////////////////////////////////////////////////////
//
// Body of trip pane
const t = kb.findTypeURIs(subject)
// var me = authn.currentUser()
// Function: Render a single trip
const renderTrip = function renderTrip (subject, thisDiv) {
const query = new $rdf.Query(UI.utils.label(subject))
const vars = ['date', 'transaction', 'comment', 'type', 'in_USD']
const v = {}
vars.forEach(function (x) {
query.vars.push((v[x] = $rdf.variable(x)))
}) // Only used by UI
query.pat.add(v.transaction, TRIP('trip'), subject)
const opt = kb.formula()
opt.add(v.transaction, ns.rdf('type'), v.type) // Issue: this will get stored supertypes too
query.pat.optional.push(opt)
query.pat.add(v.transaction, UI.ns.qu('date'), v.date)
opt.add(v.transaction, ns.rdfs('comment'), v.comment)
query.pat.optional.push(opt)
// opt = kb.formula();
query.pat.add(v.transaction, UI.ns.qu('in_USD'), v.in_USD)
// query.pat.optional.push(opt);
const calculations = function () {
const total = {}
const trans = kb.each(undefined, TRIP('trip'), subject)
// complain("@@ Number of transactions in this trip: " + trans.length);
trans.forEach(function (t) {
let ty = kb.the(t, ns.rdf('type'))
// complain(" -- one trans: "+t.uri + ' -> '+kb.any(t, UI.ns.qu('in_USD')));
if (!ty) ty = UI.ns.qu('ErrorNoType')
if (ty && ty.uri) {
const tyuri = ty.uri
if (!total[tyuri]) total[tyuri] = 0.0
const lit = kb.any(t, UI.ns.qu('in_USD'))
if (!lit) {
complain(' @@ No amount in USD: ' + lit + ' for ' + t)
}
if (lit) {
total[tyuri] = total[tyuri] + parseFloat(lit.value)
// complain(' Trans type ='+ty+'; in_USD "' + lit
// +'; total[tyuri] = '+total[tyuri]+';')
}
}
})
let str = ''
let types = 0
let grandTotal = 0.0
for (const uri in total) {
str += UI.utils.label(kb.sym(uri)) + ': ' + total[uri] + '; '
types++
grandTotal += total[uri]
}
complain('Totals of ' + trans.length + ' transactions: ' + str, '') // @@@@@ yucky -- need 2 col table
if (types > 1) {
complain('Overall net: ' + grandTotal, 'text-treatment: bold;')
}
}
const tableDiv = UI.table(myDocument, {
query,
onDone: calculations
})
thisDiv.appendChild(tableDiv)
}
// Render the set of trips which have transactions in this class
if (UI.ns.qu('Transaction') in kb.findSuperClassesNT(subject)) {
const ts = kb.each(undefined, ns.rdf('type'), subject)
const triples = []
const index = []
for (let i = 0; i < ts.length; i++) {
const trans = ts[i]
const trip = kb.any(trans, TRIP('trip'))
if (!trip) {
triples.push(trans)
} else {
if (!(trans in index)) index[trip] = { total: 0, transactions: [] }
const usd = kb.any(trans, UI.ns.qu('in_USD'))
if (usd) index[trip].total += usd
const date = kb.any(trans, UI.ns.qu('date'))
index[trip.toNT()].transactions.push([date, trans])
}
}
/* var byDate = function(a,b) {
return new Date(kb.any(a, CAL('dtstart'))) -
new Date(kb.any(b, CAL('dtstart')));
}
*/
const list = []
for (const h1 in index) {
const t1 = kb.fromNT(h1)
list.push([kb.any(t1, CAL('dtstart')), t1])
}
list.sort()
for (let j = 0; j < list.length; j++) {
const t2 = list[j][1]
renderTrip(t2, div)
}
// Render a single trip
} else if (t['http://www.w3.org/ns/pim/trip#Trip']) {
renderTrip(subject, div)
}
// if (!me) complain("You do not have your Web Id set. Set your Web ID to make changes.");
return div
}
}
// ends