-
Notifications
You must be signed in to change notification settings - Fork 3
/
issuePane.js
592 lines (528 loc) · 22.6 KB
/
issuePane.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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/* Issue Tracker Pane
**
** This solid view allows a user to interact with an issue tracker, or individual issue,
** to change its state according to an ontology, comment on it, etc.
**
*/
import { create, login, ns, icons, rdf, tabs, table, utils, widgets } from 'solid-ui'
import { store, authn } from 'solid-logic'
import { board } from './board' // @@ will later be in solid-UI
import { renderIssue, renderIssueCard, getState, exposeOverlay } from './issue'
import { newTrackerButton } from './newTracker'
import { newIssueForm } from './newIssue'
import { trackerSettingsFormText } from './trackerSettingsForm.js'
// import { trackerInstancesFormText } from './trackerInstancesForm.js'
const $rdf = rdf
const kb = store
// const MY_TRACKERS_ICON = UI.icons.iconBase + 'noun_Document_998605.svg'
// const TRACKER_ICON = UI.icons.iconBase + 'noun_list_638112'
// const TASK_ICON = UI.icons.iconBase + 'noun_17020.svg'
const OVERFLOW_STYLE = 'position: fixed; z-index: 100; top: 1.51em; right: 2em; left: 2em; bottom:1.5em; border: 0.1em grey; overflow: scroll;'
export default {
icon: icons.iconBase + 'noun_122196.svg', // was: js/panes/issue/tbl-bug-22.png
// noun_list_638112 is a checklist document
// noun_Document_998605.svg is a stack of twpo checklists
// noun_97839.svg is a ladybug
// noun_122196.svg is a clipboard with a check list on it
// noun_17020.svg is a single check box
name: 'issue',
audience: [], // Anyone. was [ns.solid('PowerUser')]
// Does the subject deserve an issue pane?
label: function (subject, _context) {
const t = kb.findTypeURIs(subject)
if (
t['http://www.w3.org/2005/01/wf/flow#Task'] ||
kb.holds(subject, ns.wf('tracker'))
) { return 'issue' } // in case ontology not available
if (t['http://www.w3.org/2005/01/wf/flow#Tracker']) return 'tracker'
// Later: Person. For a list of things assigned to them,
// open bugs on projects they are developer on, etc
return null // No under other circumstances (while testing at least!)
},
mintClass: ns.wf('Tracker'),
mintNew: async function (context, options) {
/** Perform updates on more than one document @@ Move to rdflib!
*/
async function updateMany (deletions, insertions) {
const docs = deletions.concat(insertions).map(st => st.why)
const uniqueDocs = Array.from(new Set(docs))
const updates = uniqueDocs.map(doc =>
kb.updater.update(deletions.filter(st => st.why.sameTerm(doc)),
insertions.filter(st => st.why.sameTerm(doc))))
return Promise.all(updates)
}
const kb = context.session.store
let stateStore
if (options.newInstance) {
stateStore = kb.sym(options.newInstance.doc().uri + '_state.ttl')
} else {
options.newInstance = kb.sym(options.newBase + 'index.ttl#this')
stateStore = kb.sym(options.newBase + 'state.ttl')
}
const tracker = options.newInstance
const appDoc = tracker.doc()
const me = authn.currentUser()
if (me) {
kb.add(tracker, ns.dc('author'), me, appDoc)
}
kb.add(tracker, ns.rdf('type'), ns.wf('Tracker'), appDoc)
kb.add(tracker, ns.dc('created'), new Date(), appDoc)
// @@ to do --- adk user what sort of tracker they want
kb.add(tracker, ns.wf('issueClass'), ns.wf('Task'), appDoc) // @@ ask user
kb.add(tracker, ns.wf('initialState'), ns.wf('Open'), appDoc)
kb.add(tracker, ns.wf('stateStore'), stateStore, appDoc)
kb.add(tracker, ns.wf('assigneeClass'), ns.foaf('Person'), appDoc) // @@ set to people in the meeting?
kb.add(tracker, ns.wf('stateStore'), stateStore, stateStore) // Back Link
const ins = kb.statementsMatching(undefined, undefined, undefined, appDoc).concat(kb.statementsMatching(undefined, undefined, undefined, stateStore))
try {
await updateMany([], ins)
} catch (err) {
return widgets.complain(context, 'Error writing tracker configuration: ' + err)
}
/*
try {
await kb.updater.updateMany([], kb.statementsMatching(undefined, undefined, undefined, stateStore))
} catch (err) {
return widgets.complain(context, 'Error writing tracker state file: ' + err)
}
*/
const dom = context.dom
const div = options.div
const notice = div.appendChild(dom.createElement('div'))
notice.innerHTML = `<h4>Success</h4>
<p>Your <a href="${tracker.uri}">new tracker</a> has been made.
Use the settings tab to configure it.
</p>
`
// console.log('New tracker created ' + tracker)
// alert('New tracker created')
return options
},
render: function (subject, context) {
const dom = context.dom
const paneDiv = dom.createElement('div')
context.paneDiv = paneDiv
paneDiv.setAttribute('class', 'issuePane')
function complain (message) {
console.warn(message)
paneDiv.appendChild(widgets.errorMessageBlock(dom, message))
}
function complainIfBad (ok, message) {
if (!ok) {
complain(message)
}
}
/** Infer subclass from disjoint Union
**
** This is would not be needed if our quey language
** allowed is to query ardf Collection membership.
*/
async function fixSubClasses (kb, tracker) { // 20220228
async function checkOneSuperclass (klass) {
const collection = kb.any(klass, ns.owl('disjointUnionOf'), null, doc)
if (!collection) throw new Error(`Classification ${klass} has no disjointUnionOf`)
if (!collection.elements) throw new Error(`Classification ${klass} has no array`)
const needed = new Set(collection.elements.map(x => x.uri))
const existing = new Set(kb.each(null, ns.rdfs('subClassOf'), klass, doc).map(x => x.uri))
const superfluous = [...existing].filter(sub => !needed.has(sub))
const deleteActions = superfluous.map(sub => { return { action: 'delete', st: $rdf.st(kb.sym(sub), ns.rdfs('subClassOf'), klass, doc) } })
const missing = [...needed].filter(sub => !existing.has(sub))
const insertActions = missing.map(sub => { return { action: 'insert', st: $rdf.st(kb.sym(sub), ns.rdfs('subClassOf'), klass, doc) } })
return deleteActions.concat(insertActions)
}
const doc = tracker.doc()
const states = kb.any(tracker, ns.wf('issueClass'))
const cats = kb.each(tracker, ns.wf('issueCategory')).concat([states])
let damage = [] // to make totally functionaly need to deal with map over async.
for (const klass of cats) {
damage = damage.concat(await checkOneSuperclass(klass))
}
if (damage.length) {
const insertables = damage.filter(fix => fix.action === 'insert').map(fix => fix.st)
const deletables = damage.filter(fix => fix.action === 'delete').map(fix => fix.st)
// alert(`Internal error: s${damage} subclasses inconsistences!`)
console.log('Damage:', damage)
if (confirm(`Fix ${damage} inconsistent subclasses in tracker config?`)) {
await kb.updater.update(deletables, insertables)
}
}
}
/** /////////////////////////// Board
*/
function renderBoard (tracker, klass) {
const states = kb.any(tracker, ns.wf('issueClass'))
klass = klass || states // default to states
const doingStates = klass.sameTerm(states)
// These are states we will show by default: the open issues.
const stateArray = kb.any(klass, ns.owl('disjointUnionOf'))
if (!stateArray) {
return complain(`Configuration error: state ${states} does not have substates`)
}
let columnValues = stateArray.elements
if (doingStates && columnValues.length > 2 // and there are more than two
) { // strip out closed states
columnValues = columnValues.filter(state => kb.holds(state, ns.rdfs('subClassOf'), ns.wf('Open')) || state.sameTerm(ns.wf('Open')))
}
async function columnDropHandler (issue, newState) {
const currentState = getState(issue, klass)
const tracker = kb.the(issue, ns.wf('tracker'), null, issue.doc())
const stateStore = kb.any(tracker, ns.wf('stateStore'))
if (newState.sameTerm(currentState)) {
// alert('Same state ' + utils.label(currentState)) // @@ remove
return
}
try {
await kb.updater.update(
[$rdf.st(issue, ns.rdf('type'), currentState, stateStore)],
[$rdf.st(issue, ns.rdf('type'), newState, stateStore)])
} catch (err) {
widgets.complain(context, 'Unable to change issue state: ' + err)
}
boardDiv.refresh() // reorganize board to match the new reality
}
function isOpen (issue) {
const types = kb.findTypeURIs(issue)
return !!types[ns.wf('Open').uri]
}
const options = { columnDropHandler, filter: doingStates ? null : isOpen }
options.sortBy = ns.dct('created')
options.sortReverse = true
function localRenderIssueCard (issue) {
return renderIssueCard(issue, context)
}
// const columnValues = states // @@ optionally selected states would work
const boardDiv = board(dom, columnValues, localRenderIssueCard, options)
return boardDiv
}
/** ////////////// Table
*/
function tableRefreshButton (stateStore, tableDiv) {
const refreshButton = widgets.button(dom, icons.iconBase + 'noun_479395.svg',
'refresh table', async _event => {
try {
await kb.fetcher.load(stateStore, { force: true, clearPreviousData: true })
} catch (err) {
alert(err)
return
}
widgets.refreshTree(tableDiv)
})
return refreshButton
}
function renderTable (tracker) {
function newOptionalClause () {
const clause = new $rdf.IndexedFormula()
query.pat.optional.push(clause)
return clause
}
const states = kb.any(subject, ns.wf('issueClass'))
const cats = kb.each(tracker, ns.wf('issueCategory')) // zero or more
const vars = ['issue', 'state', 'created']
const query = new $rdf.Query(utils.label(subject))
for (let i = 0; i < cats.length; i++) {
vars.push('_cat_' + i)
}
const v = {} // The RDF variable objects for each variable name
vars.forEach(function (x) {
query.vars.push((v[x] = $rdf.variable(x)))
})
query.pat.add(v.issue, ns.wf('tracker'), tracker)
// query.pat.add(v['issue'], ns.dc('title'), v['title'])
query.pat.add(v.issue, ns.dct('created'), v.created)
query.pat.add(v.issue, ns.rdf('type'), v.state)
query.pat.add(v.state, ns.rdfs('subClassOf'), states)
query.pat.optional = []
for (let i = 0; i < cats.length; i++) {
const clause = newOptionalClause()
clause.add(v.issue, ns.rdf('type'), v['_cat_' + i])
clause.add(v['_cat_' + i], ns.rdfs('subClassOf'), cats[i])
}
const propertyList = kb.any(tracker, ns.wf('propertyList')) // List of extra properties
if (propertyList) {
const properties = propertyList.elements
for (let p = 0; p < properties.length; p++) {
const prop = properties[p]
let vname = '_prop_' + p
if (prop.uri.indexOf('#') >= 0) {
vname = prop.uri.split('#')[1]
}
const oneOpt = newOptionalClause()
query.vars.push((v[vname] = $rdf.variable(vname)))
oneOpt.add(v.issue, prop, v[vname])
}
}
const selectedStates = {}
const possible = kb.each(undefined, ns.rdfs('subClassOf'), states)
possible.forEach(function (s) {
if (
kb.holds(s, ns.rdfs('subClassOf'), ns.wf('Open')) ||
s.sameTerm(ns.wf('Open'))
) {
selectedStates[s.uri] = true
// console.log('on '+s.uri); // @@
}
})
function exposeThisOverlay (href) {
const subject = $rdf.sym(href)
exposeOverlay(subject, context)
}
const tableDiv = table(dom, {
query: query,
keyVariable: '?issue', // Charactersic of row
sortBy: '?created', // By default, sort by date
sortReverse: true, // most recent at the top
hints: {
'?issue': { linkFunction: exposeThisOverlay, label: 'Title' },
'?created': { cellFormat: 'shortDate' },
'?state': { initialSelection: selectedStates, label: 'Status' }
}
})
const stateStore = kb.any(subject, ns.wf('stateStore'))
tableDiv.appendChild(tableRefreshButton(stateStore, tableDiv))
return tableDiv
}
// Allow user to create new things within the folder
function renderCreationControl (refreshTarget) {
const creationDiv = dom.createElement('div')
const me = authn.currentUser()
const creationContext = {
// folder: subject,
div: creationDiv,
dom: dom,
noun: 'tracker',
statusArea: creationDiv,
me: me,
refreshTarget: refreshTarget
}
const issuePane = context.session.paneRegistry.byName('issue')
const relevantPanes = [issuePane]
create.newThingUI(creationContext, context, relevantPanes) // Have to pass panes down newUI
return creationDiv
}
function renderInstances (theClass) {
const instancesDiv = dom.createElement('div')
const context = { dom, div: instancesDiv, noun: 'tracker' }
login.registrationList(context, { public: true, private: true, type: theClass }).then(_context2 => {
instancesDiv.appendChild(renderCreationControl(instancesDiv))
/* // keep this code in case we need a form
const InstancesForm = ns.wf('TrackerInstancesForm')
const text = trackerInstancesFormText
$rdf.parse(text, kb, InstancesForm.doc().uri, 'text/turtle')
widgets.appendForm(dom, instancesDiv, {}, tracker, InstancesForm,
tracker.doc(), complainIfBad)
*/
})
return instancesDiv
}
function renderSettings (tracker) {
const settingsDiv = dom.createElement('div')
const states = kb.any(tracker, ns.wf('issueClass'))
const views = [tableView, states] // Possible default views
.concat(kb.each(tracker, ns.wf('issueCategory')))
const box = settingsDiv.appendChild(dom.createElement('div'))
const lhs = widgets.renderNameValuePair(dom, kb, box, null, 'Default view') // @@ use a predicate?
lhs.appendChild(widgets.makeSelectForOptions(dom, kb, tracker,
ns.wf('defaultView'),
views, {}, tracker.doc()))
// A registration control allows the to record this tracker in their type index
const context = { dom, div: settingsDiv, noun: 'tracker' }
login.registrationControl(context, tracker, ns.wf('Tracker')).then(_context2 => {
const settingsForm = ns.wf('TrackerSettingsForm')
const text = trackerSettingsFormText
$rdf.parse(text, kb, settingsForm.doc().uri, 'text/turtle')
widgets.appendForm(dom, settingsDiv, {}, tracker, settingsForm,
tracker.doc(), complainIfBad)
})
return settingsDiv
}
function renderTabsTableAndBoard () {
function renderMain (ele, object) {
ele.innerHTML = '' // Clear out "loading message"
if (object.sameTerm(boardView)) {
ele.appendChild(renderBoard(tracker))
} else if (object.sameTerm(tableView)) {
ele.appendChild(renderTable(tracker))
} else if (object.sameTerm(settingsView)) {
ele.appendChild(renderSettings(tracker))
} else if (object.sameTerm(instancesView)) {
ele.appendChild(renderInstances(ns.wf('Tracker')))
} else if ((kb.holds(tracker, ns.wf('issueCategory'), object)) ||
(kb.holds(tracker, ns.wf('issueClass'), object))) {
ele.appendChild(renderBoard(tracker, object))
} else {
throw new Error('Unexpected tab type: ' + object)
}
}
const states = kb.any(tracker, ns.wf('issueClass'))
const items = [instancesView, tableView, states]
.concat(kb.each(tracker, ns.wf('issueCategory')))
items.push(settingsView)
const selectedTab = kb.any(tracker, ns.wf('defaultView'), null, tracker.doc()) || tableView
const options = { renderMain, items, selectedTab }
// Add stuff to the ontologies which we believe but they don't say
const doc = instancesView.doc()
kb.add(instancesView, ns.rdfs('label'), 'My Trackers', doc) // @@ squatting on wf ns
kb.add(settingsView, ns.rdfs('label'), 'Settings', doc) // @@ squatting on wf ns
kb.add(states, ns.rdfs('label'), 'By State', doc) // @@ squatting on wf ns
const myTabs = tabs.tabWidget(options)
return myTabs
}
async function renderSingleIssue () {
tracker = kb.any(subject, ns.wf('tracker'))
if (!tracker) throw new Error('This issue ' + subject + 'has no tracker')
// Much data is in the tracker instance, so wait for the data from it
try {
const _xhrs = await context.session.store.fetcher.load(tracker.doc())
} catch (err) {
const msg = 'Failed to load tracker config ' + tracker.doc() + ': ' + err
return complain(msg)
}
const stateStore = kb.any(tracker, ns.wf('stateStore'))
if (!stateStore) {
return complain('Tracker has no state store: ' + tracker)
}
try {
await context.session.store.fetcher.load(subject)
} catch (err) {
return complain('Failed to load issue state ' + stateStore + ': ' + err)
}
paneDiv.appendChild(renderIssue(subject, context))
updater.addDownstreamChangeListener(stateStore, function () {
widgets.refreshTree(paneDiv)
}) // Live update
}
async function renderTracker () {
function showNewIssue (issue) {
widgets.refreshTree(paneDiv)
exposeOverlay(issue, context)
newIssueButton.disabled = false // https://stackoverflow.com/questions/41176582/enable-disable-a-button-in-pure-javascript
}
tracker = subject
try {
await fixSubClasses(kb, tracker)
} catch (err) {
console.log('@@@ Error fixing subclasses in config: ' + err)
}
const states = kb.any(subject, ns.wf('issueClass'))
if (!states) throw new Error('This tracker has no issueClass')
const stateStore = kb.any(subject, ns.wf('stateStore'))
if (!stateStore) throw new Error('This tracker has no stateStore')
// const me = await authn.currentUser()
const h = dom.createElement('h2')
h.setAttribute('style', 'font-size: 150%')
paneDiv.appendChild(h)
const classLabel = utils.label(states)
h.appendChild(dom.createTextNode(classLabel + ' list')) // Use class label @@I18n
// New Issue button
const newIssueButton = dom.createElement('button')
const container = dom.createElement('div')
newIssueButton.setAttribute('type', 'button')
newIssueButton.setAttribute('style', 'padding: 0.3em; font-size: 100%; margin: 0.5em;')
container.appendChild(newIssueButton)
paneDiv.appendChild(container)
const img = dom.createElement('img')
img.setAttribute('src', icons.iconBase + 'noun_19460_green.svg')
img.setAttribute('style', 'width: 1em; height: 1em; margin: 0.2em;')
newIssueButton.appendChild(img)
const span = dom.createElement('span')
span.innerHTML = 'New ' + classLabel
newIssueButton.appendChild(span)
newIssueButton.addEventListener(
'click',
function (_event) {
newIssueButton.disabled = true
container.appendChild(newIssueForm(dom, kb, tracker, null, showNewIssue))
},
false
)
// Table of issues - when we have the main issue list
// We also need the ontology loaded
//
context.session.store.fetcher
.load([stateStore])
.then(function (_xhrs) {
const tableDiv = renderTabsTableAndBoard(tracker)
// const tableDiv = renderTable(tracker) // was
paneDiv.appendChild(tableDiv)
if (tableDiv.refresh) {
// Refresh function
} else {
console.log('No table refresh function?!')
}
paneDiv.appendChild(newTrackerButton(subject, context))
updater.addDownstreamChangeListener(stateStore, tableDiv.refresh) // Live update
})
.catch(function (err) {
return console.log('Cannot load state store: ' + err)
})
// end of Tracker instance
} // render tracker
/* Render tabs with both views
*/
const boardView = ns.wf('BoardView')
const tableView = ns.wf('TableView')
const settingsView = ns.wf('SettingsView')
const instancesView = ns.wf('InstancesView')
const updater = kb.updater
const t = kb.findTypeURIs(subject)
let tracker
// Whatever we are rendering, lets load the ontology
const flowOntology = ns.wf('').doc()
if (!kb.holds(undefined, undefined, undefined, flowOntology)) {
// If not loaded already
$rdf.parse(require('./wf.js'), kb, flowOntology.uri, 'text/turtle') // Load ontology directly
}
const userInterfaceOntology = ns.ui('').doc()
if (!kb.holds(undefined, undefined, undefined, userInterfaceOntology)) {
// If not loaded already
$rdf.parse(require('./ui.js'), kb, userInterfaceOntology.uri, 'text/turtle') // Load ontology directly
}
// Render a single issue
if (
t['http://www.w3.org/2005/01/wf/flow#Task'] ||
kb.holds(subject, ns.wf('tracker'))
) {
renderSingleIssue().then(() => console.log('Single issue rendered'))
} else if (t['http://www.w3.org/2005/01/wf/flow#Tracker']) {
// Render a Tracker instance
renderTracker().then(() => console.log('Tracker rendered'))
} else {
console.log(
'Error: Issue pane: No evidence that ' +
subject +
' is either a bug or a tracker.'
)
}
let loginOutButton
const overlay = paneDiv.appendChild(dom.createElement('div'))
context.overlay = overlay
overlay.style = OVERFLOW_STYLE
overlay.style.visibility = 'hidden'
authn.checkUser().then(webId => {
if (webId) {
console.log('Web ID set already: ' + webId)
context.me = webId
// @@ enable things
return
}
loginOutButton = login.loginStatusBox(dom, webIdUri => {
authn.log
if (webIdUri) {
context.me = kb.sym(webIdUri)
console.log('Web ID set from login button: ' + webIdUri)
paneDiv.removeChild(loginOutButton)
// enable things
} else {
context.me = null
}
})
loginOutButton.setAttribute('style', 'margin: 0.5em 1em;')
paneDiv.appendChild(loginOutButton)
if (!context.statusArea) {
context.statusArea = paneDiv.appendChild(dom.createElement('div'))
}
})
return paneDiv
}
}
// ends