forked from SolidOS/solid-panes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperiod.js
More file actions
336 lines (297 loc) · 11.4 KB
/
period.js
File metadata and controls
336 lines (297 loc) · 11.4 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* Financial Period Pane
**
** This outline pane allows a user to interact with a period
** downloaded from a bank statement, annotting it with classes and comments,
** trips, etc
*/
var UI = require('solid-ui')
var panes = require('pane-registry')
module.exports = {
icon: UI.icons.iconBase + 'noun_142708.svg',
name: 'period',
// Does the subject deserve this pane?
label: function (subject) {
var kb = UI.store
var t = kb.findTypeURIs(subject)
if (t['http://www.w3.org/2000/10/swap/pim/qif#Period']) return 'period'
return null // No under other circumstances (while testing at least!)
},
render: function (subject, dom) {
var kb = UI.store
var ns = UI.ns
var div = dom.createElement('div')
div.setAttribute('class', 'periodPane')
var mention = function mention (message, style) {
if (!style) style = 'color: grey;'
var pre = dom.createElement('pre')
pre.setAttribute('style', style)
div.appendChild(pre)
pre.appendChild(dom.createTextNode(message))
}
var happy = function happy (message) {
return mention('✓ ' + message, 'color: #010; background-color: #efe')
}
var complain = function complain (message) {
return mention(message, 'color: #100; background-color: #fee')
}
/*
var rerender = function (div) {
var parent = div.parentNode
var div2 = thisPane.render(subject, dom)
parent.replaceChild(div2, div)
}
*/
var renderPeriod = function () {
var dtstart = kb.any(subject, ns.cal('dtstart'))
if (dtstart === undefined) {
complain('(Error: There is no start date known for this period <' +
subject.uri + '>,\n -- every period needs one.)')
};
var dtend = kb.any(subject, ns.cal('dtend'))
if (dtend === undefined) {
complain('(Error: There is no end date known for this period <' +
subject.uri + '>,\n -- every period needs one.)')
};
// var store = kb.any(subject, UI.ns.qu('annotationStore')) || null
var predicateURIsDone = {}
var donePredicate = function (pred) { predicateURIsDone[pred.uri] = true }
donePredicate(ns.rdf('type'))
var inPeriod = function (date) {
return !!(date && date >= dtstart && date < dtend)
}
var d2 = function (n) {
var s = '' + n
if (s.indexOf('.') >= 0) {
return s.split('.')[0] + '.' + (s.split('.')[1] + '00').slice(0, 2)
}
return s + '.00'
}
var transactionInPeriod = function (x) {
return inPeriod(kb.any(x, ns.qu('date')))
}
var oderByDate = function (x, y) {
let dx = UI.store.any(x, ns.qu('date'))
let dy = UI.store.any(y, ns.qu('date'))
if (dx !== undefined && dy !== undefined) {
if (dx.value < dy.value) return -1
if (dx.value > dy.value) return 1
}
if (x.uri < y.uri) return -1 // Arbitrary but repeatable
if (x.uri > y.uri) return 1
return 0
}
/*
var setPaneStyle = function (account) {
var mystyle = 'padding: 0.5em 1.5em 1em 1.5em; '
if (account) {
var backgroundColor = kb.any(account, UI.ns.ui('backgroundColor'))
if (backgroundColor) {
mystyle += 'background-color: ' + backgroundColor.value + '; '
}
}
div.setAttribute('style', mystyle)
}
// setPaneStyle();
*/
var h2 = div.appendChild(dom.createElement('h2'))
h2.textContent = 'Period ' + dtstart.value.slice(0, 10) + ' - ' + dtend.value.slice(0, 10)
var insertedPane = function (dom, subject, paneName) {
var p = panes.byName(paneName)
var d = p.render(subject, dom)
d.setAttribute('style', 'border: 0.1em solid green;')
return d
}
var expandAfterRow = function (dom, row, subject, paneName, solo) {
var siblings = row.parentNode.children
if (solo) {
for (var j = siblings.length - 1; j >= 0; j--) {
if (siblings[j].expanded) {
siblings[j].parentNode.removeChild(siblings[j].expanded)
siblings[j].expanded = false
}
}
}
var tr = dom.createElement('tr')
var td = tr.appendChild(dom.createElement('td'))
td.setAttribute('style', 'width: 98%; padding: 1em; border: 0.1em solid grey;')
var cols = row.children.length
if (row.nextSibling) {
row.parentNode.insertBefore(tr, row.nextSibling)
} else {
row.parentNode.appendChild(tr)
}
row.expanded = tr
td.setAttribute('colspan', '' + cols)
td.appendChild(insertedPane(dom, subject, paneName))
}
var expandAfterRowOrCollapse = function (dom, row, subject, paneName, solo) {
if (row.expanded) {
row.parentNode.removeChild(row.expanded)
row.expanded = false
} else {
expandAfterRow(dom, row, subject, paneName, solo)
}
}
var transactionTable = function (dom, list) {
var table = dom.createElement('table')
table.setAttribute('style', 'margin-left: 100; font-size: 9pt; width: 85%;')
var transactionRow = function (dom, x) {
var tr = dom.createElement('tr')
var setTRStyle = function (tr, account) {
// var mystyle = "padding: 0.5em 1.5em 1em 1.5em; ";
var mystyle = 'margin-left: 8em; padding-left: 5em;'
if (account) {
var backgroundColor = kb.any(account, UI.ns.ui('backgroundColor'))
if (backgroundColor) {
mystyle += 'background-color: ' + backgroundColor.value + '; '
}
}
tr.setAttribute('style', mystyle)
}
var account = kb.any(x, ns.qu('toAccount'))
setTRStyle(tr, account)
var c0 = tr.appendChild(dom.createElement('td'))
var date = kb.any(x, ns.qu('date'))
c0.textContent = date ? date.value.slice(0, 10) : '???'
c0.setAttribute('style', 'width: 7em;')
var c1 = tr.appendChild(dom.createElement('td'))
c1.setAttribute('style', 'width: 36em;')
var payee = kb.any(x, ns.qu('payee'))
c1.textContent = payee ? payee.value : '???'
var a1 = c1.appendChild(dom.createElement('a'))
a1.textContent = ' ➜'
a1.setAttribute('href', x.uri)
var c3 = tr.appendChild(dom.createElement('td'))
var amount = kb.any(x, ns.qu('in_USD'))
c3.textContent = amount ? d2(amount.value) : '???'
c3.setAttribute('style', 'width: 6em; text-align: right; ') // @@ decimal alignment?
tr.addEventListener('click', function (e) { // solo unless shift key
expandAfterRowOrCollapse(dom, tr, x, 'transaction', !e.shiftKey)
}, false)
return tr
}
var list2 = list.filter(transactionInPeriod)
list2.sort(oderByDate)
for (var i = 0; i < list2.length; i++) {
table.appendChild(transactionRow(dom, list2[i]))
}
return table
}
// List unclassified transactions
var dummies = {
'http://www.w3.org/2000/10/swap/pim/qif#Transaction': true, // (we knew)
'http://www.w3.org/2000/10/swap/pim/qif#Unclassified': true, // pseudo classifications we may phase out
'http://www.w3.org/2000/10/swap/pim/qif#UnclassifiedOutgoing': true,
'http://www.w3.org/2000/10/swap/pim/qif#UnclassifiedIncome': true
}
var xURIs = kb.findMemberURIs(ns.qu('Transaction'))
var unclassifiedIn = []
var unclassifiedOut = []
var usd, z
for (var y in xURIs) { // For each thing which can be inferred to be a transaction
if (xURIs.hasOwnProperty(y)) {
z = kb.sym(y)
let tt = kb.each(z, ns.rdf('type')) // What EXPLICIT definitions
var classified = false
for (let j = 0; j < tt.length; j++) {
let t = tt[j]
if (dummies[t.uri] === undefined) {
classified = true
}
};
if (!classified) {
usd = kb.any(z, ns.qu('in_USD'))
if (usd === undefined) {
usd = kb.any(z, ns.qu('amount'))
}
if (usd && ('' + usd.value).indexOf('-') >= 0) {
unclassifiedOut.push(kb.sym(y))
} else {
unclassifiedIn.push(kb.sym(y))
}
}
}
}
var tab, count
if (unclassifiedIn.length) {
tab = transactionTable(dom, unclassifiedIn)
count = tab.children.length
div.appendChild(dom.createElement('h3')).textContent = 'Unclassified Income' +
(count < 4 ? '' : ' (' + count + ')')
div.appendChild(tab)
} else {
happy('No unclassified income')
}
if (unclassifiedOut.length) {
tab = transactionTable(dom, unclassifiedOut)
count = tab.children.length
div.appendChild(dom.createElement('h3')).textContent = 'Unclassified Outgoings' +
(count < 4 ? '' : ' (' + count + ')')
div.appendChild(tab)
} else {
happy('No unclassified outgoings ')
}
// /////////////// Check some categories of transaction for having given fields
var catSymbol = function (catTail) {
var cats = kb.findSubClassesNT(ns.qu('Transaction'))
for (var cat in cats) {
if (cats.hasOwnProperty(cat)) {
if (cat.slice(1, -1).split('#')[1] === catTail) {
return kb.sym(cat.slice(1, -1))
}
};
};
return null
}
var checkCatHasField = function (catTail, pred) {
var cat = catSymbol(catTail)
var tab
var guilty = []
var count = 0
if (!cat) {
complain('Error: No category correspnding to ' + catTail)
return null
}
var list = kb.each(undefined, ns.rdf('type'), cat)
for (var i = 0; i < list.length; i++) {
if (!kb.any(list[i], pred)) {
guilty.push(list[i])
}
}
if (guilty.length) {
tab = transactionTable(dom, guilty)
count = tab.children.length
div.appendChild(dom.createElement('h3')).textContent = UI.utils.label(cat) +
' with no ' + UI.utils.label(pred) +
(count < 4 ? '' : ' (' + count + ')')
div.appendChild(tab)
}
return count
}
// Load dynamically as properties of period
if (checkCatHasField('Reimbursables', ns.trip('trip')) === 0) {
happy('Reimbursables all have trips')
}
if (checkCatHasField('Other_Inc_Speaking', ns.trip('trip')) === 0) {
happy('Speaking income all has trips')
}
// end of render period instance
} // renderPeriod
// //////////////////////////////////////////////////////////////////////////////
// var me = UI.authn.currentUser()
// Render a single Period
// This works only if enough metadata about the properties can drive the RDFS
// (or actual type statements whichtypically are NOT there on)
var t = kb.findTypeURIs(subject)
if (t['http://www.w3.org/2000/10/swap/pim/qif#Period']) {
let needed = kb.each(subject, ns.rdfs('seeAlso'))
console.log('Loading before render: ' + needed.length)
kb.fetcher.load(needed).then(function (responses) {
renderPeriod()
})
}
// if (!me) complain("You do not have your Web Id set. Set your Web ID to make changes.");
return div
}
}
// ends