forked from SolidOS/solid-panes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattachPane.js
More file actions
433 lines (389 loc) · 15.6 KB
/
Copy pathattachPane.js
File metadata and controls
433 lines (389 loc) · 15.6 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
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
/* Attachment Pane
**
** - Attach a document to a thing
** - View attachments
** - Look at all unattached Supporting Documents.
** - Drag a document onto the pane to attach it @@
**
**
*/
var UI = require('solid-ui')
module.exports = {
icon: UI.icons.iconBase + 'noun_25830.svg', // noun_25830
name: 'attachments',
// Does the subject deserve an attachments pane?
//
// In this case we will render any thing which is in any subclass of
// certain classes, or also the certain classes themselves, as a
// triage tool for correlating many attachees with attachments.
// We also offer the pane for anything of any class which just has an attachment already.
//
label: function (subject) {
var kb = UI.store
var t = kb.findTypeURIs(subject)
var QU = $rdf.Namespace('http://www.w3.org/2000/10/swap/pim/qif#')
var WF = $rdf.Namespace('http://www.w3.org/2005/01/wf/flow#')
if (t['http://www.w3.org/ns/pim/trip#Trip'] || // If in any subclass
subject.uri == 'http://www.w3.org/ns/pim/trip#Trip' ||
t['http://www.w3.org/2005/01/wf/flow#Task'] ||
t['http://www.w3.org/2000/10/swap/pim/qif#Transaction'] ||
// subject.uri == 'http://www.w3.org/2000/10/swap/pim/qif#Transaction' ||
QU('Transaction') in kb.findSuperClassesNT(subject) ||
kb.holds(subject, WF('attachment'))) return 'attachments'
return null
},
render: function (subject, dom) {
var kb = UI.store
var ns = UI.ns
var WF = $rdf.Namespace('http://www.w3.org/2005/01/wf/flow#')
var CAL = $rdf.Namespace('http://www.w3.org/2002/12/cal/ical#')
var DC = $rdf.Namespace('http://purl.org/dc/elements/1.1/')
var DCT = $rdf.Namespace('http://purl.org/dc/terms/')
var TRIP = $rdf.Namespace('http://www.w3.org/ns/pim/trip#')
var QU = $rdf.Namespace('http://www.w3.org/2000/10/swap/pim/qif#')
// ////////////////////////////////////////////////////////////////////////////
var setModifiedDate = function (subj, kb, doc) {
var deletions = kb.statementsMatching(subject, DCT('modified'))
var deletions = deletions.concat(kb.statementsMatching(subject, WF('modifiedBy')))
var insertions = [ $rdf.st(subject, DCT('modified'), new Date(), doc) ]
if (me) insertions.push($rdf.st(subject, WF('modifiedBy'), me, doc))
sparqlService.update(deletions, insertions, function (uri, ok, body) {})
}
var complain = function complain (message) {
var pre = dom.createElement('pre')
pre.setAttribute('style', 'background-color: pink')
div.appendChild(pre)
pre.appendChild(dom.createTextNode(message))
}
var thisPane = this
var rerender = function (div) {
var parent = div.parentNode
var div2 = thisPane.render(subject, dom)
parent.replaceChild(div2, div)
}
// Where can we write about this thing?
//
// Returns term for document or null
var findStore = function (kb, subject) {
if (kb.updater.editable(subject.doc(), kb)) return subject.doc()
var store = kb.any(subject.doc(), QU('annotationStore'))
return store
}
var div = dom.createElement('div')
var esc = UI.utils.escapeForXML
div.setAttribute('class', 'attachPane')
div.innerHTML = '<h1>' + esc(UI.utils.label(subject, true)) +
' attachments</h1>' //
var predicate = WF('attachment')
var range = QU('SupportingDocument')
var subjects
var multi
var options = {}
var currentMode = 0; // 0 -> Show all; 1 -> Attached; 2 -> unattached
var currentSubject = null, currentObject = null
var currentSubjectItem = null, currentObjectItem = null
var objectType = QU('SupportingDocument')
// Find all members of the class which we know about
// and sort them by an appropriate property. @@ Move to library
//
var getSortKeySimple = function (c) {
var sortBy = kb.sym({
'http://www.w3.org/2005/01/wf/flow#Task': 'http://purl.org/dc/elements/1.1/created',
'http://www.w3.org/ns/pim/trip#Trip': // @@ put this into the ontologies
'http://www.w3.org/2002/12/cal/ical#dtstart',
'http://www.w3.org/2000/10/swap/pim/qif#Transaction': 'http://www.w3.org/2000/10/swap/pim/qif#date',
'http://www.w3.org/2000/10/swap/pim/qif#SupportingDocument': 'http://purl.org/dc/elements/1.1/date'}[subject.uri])
if (!sortBy) {
sortBy = kb.any(subject, UI.ns.ui('sortBy'))
}
return sortBy
}
var getSortKey = function (c) {
var k = getSortKeySimple(c.uri)
if (k) return k
var sup = kb.findSuperClassesNT(c)
for (var cl in sup) { // note unordered -- could be closest first
k = getSortKeySimple(kb.fromNT(cl).uri)
if (k) return k
}
return undefined // failure
}
var getMembersAndSort = function (subject) {
var sortBy = getSortKey(subject)
var u, x, key, uriHash = kb.findMemberURIs(subject)
var pairs = [], subjects = []
for (u in uriHash) if (uriHash.hasOwnProperty(u)) {
x = kb.sym(u)
if (sortBy) {
key = kb.any(x, sortBy)
if (!key) {
// complain("No key "+key+" sortby "+sortBy+" for "+x)
key = '8888-12-31'
} else {
key = key.value
}
} else {
complain('No sortby ' + sortBy + ' for ' + x)
key = '9999-12-31'
}
// key = (sortBy && kb.any(x, sortBy)) || kb.literal("9999-12-31") // Undated appear future
// if (!key) complain("Sort: '"+key+"' No "+sortBy+" for "+x) // Assume just not in this year
pairs.push([key, x])
}
pairs.sort()
pairs.reverse(); // @@ Descending order .. made a toggle?
for (var i = 0; i < pairs.length; i++) {
subjects.push(pairs[i][1])
}
return subjects
}
// Set up a triage of many class members against documents or just one
if (subject.uri == 'http://www.w3.org/ns/pim/trip#Trip' ||
QU('Transaction') in kb.findSuperClassesNT(subject)
// subject.uri == 'http://www.w3.org/2000/10/swap/pim/qif#Transaction'
) {
multi = true
subjects = getMembersAndSort(subject)
} else {
currentSubject = subject
currentMode = 1; // Show attached only.
subjects = [ subject ]
multi = false
}
// var store = findStore(kb, subject)
// if (!store) complain("There is no annotation store for: "+subject.uri)
// var objects = kb.each(undefined, ns.rdf('type'), range)
var objects = getMembersAndSort(range)
if (!objects) complain('objects:' + objects.length)
var deselectObject = function () {
currentObject = null
preview.innerHTML = ''
}
var showFiltered = function (mode) {
var filtered = (mode == 0) ? objects :
(mode == 1) ? (currentSubject === null
? objects.filter(function (y) {return !!kb.holds(undefined, predicate, y)})
: objects.filter(function (y) {return !!kb.holds(currentSubject, predicate, y)}))
: objects.filter(function (y) {return kb.each(undefined, predicate, y).length == 0})
UI.widgets.selectorPanelRefresh(objectList,
dom, kb, objectType, predicate, true, filtered, options, showObject, linkClicked)
if (filtered.length == 1) {
currentObject = filtered[0]
showObject(currentObject, null, true); // @@ (Sure?) if only one select it.
} else {
deselectObject()
}
}
var setAttachment = function (x, y, value, refresh) {
if (kb.holds(x, predicate, y) == value) return
var verb = value ? 'attach' : 'detach'
// complain("Info: starting to "+verb+" " + y.uri + " to "+x.uri+ ":\n")
var linkDone3 = function (uri, ok, body) {
if (ok) {
// complain("Success "+verb+" "+y.uri+" to "+x.uri+ ":\n"+ body)
refresh()
} else {
complain('Error: Unable to ' + verb + ' ' + y.uri + ' to ' + x.uri + ':\n' + body)
}
}
var store = findStore(kb, x)
if (!store) {
complain('There is no annotation store for: ' + x.uri)
} else {
var sts = [$rdf.st(x, predicate, y, store)]
if (value) {
kb.updater.update([], sts, linkDone3)
} else {
kb.updater.update(sts, [], linkDone3)
}
}
}
var linkClicked = function (x, event, inverse, refresh) {
var s, o
if (inverse) { // Objectlist
if (!currentSubject) {
complain('No subject for the link has been selected')
return
} else {
s = currentSubject
o = x
}
} else { // Subjectlist
if (!currentObject) {
complain('No object for the link has been selected')
return
} else {
s = x
o = currentObject
}
}
setAttachment(s, o, !kb.holds(s, predicate, o), refresh); // @@ toggle
}
// When you click on a subject, filter the objects connected to the subject in Mode 1
var showSubject = function (x, event, selected) {
if (selected) {
currentSubject = x
} else {
currentSubject = null
if (currentMode === 1) deselectObject()
} // If all are displayed, refresh would just annoy:
if (currentMode !== 0) showFiltered(currentMode) // Refresh the objects
}
if (multi) {
var subjectList = UI.widgets.selectorPanel(dom, kb, subject,
predicate, false, subjects, options, showSubject, linkClicked)
subjectList.setAttribute('style',
'background-color: white; width: 25em; height: 100%; padding: 0 em; overflow:scroll; float:left')
div.appendChild(subjectList)
}
var showObject = function (x, event, selected) {
if (!selected) {
deselectObject()
preview.innerHTML = '' // Clean out what is there
// complain("Show object "+x.uri)
return
}
currentObject = x
try {
/*
*/
var dispalyable = function (kb, x) {
var cts = kb.fetcher.getHeader(x, 'content-type')
if (cts) {
var displayables = [ 'text/html', 'image/png', 'application/pdf']
for (var j = 0; j < cts.length; j++) {
for (var k = 0; k < displayables.length; k++) {
if (cts[j].indexOf(displayables[k]) >= 0) {
return true
}
}
}
}
return false
}
preview.innerHTML = 'Loading ....'
if (x.uri) kb.fetcher.nowOrWhenFetched(x.uri, undefined, function (ok, body) {
if (!ok) {
preview.textContent = 'Error loading ' + x.uri + ': ' + body
return
}
var outliner = UI.panes.getOutliner(dom)
var display = outliner.propertyTable(x); // ,table, pane
preview.innerHTML = ''
preview.appendChild(display)
})
/*
if (dispalyable(kb, x) || x.uri.slice(-4) == ".pdf" || x.uri.slice(-4) == ".png" || x.uri.slice(-5) == ".html" ||
x.uri.slice(-5) == ".jpeg") { // @@@@@@ MAJOR KLUDGE! use metadata after HEAD
preview.innerHTML = '<iframe height="100%" width="100%"src="'
+ x.uri + '">' + x.uri + '</iframe>'
} else {
}
*/
} catch(e) {
preview.innerHTML = '<span style="background-color: pink;">' + 'Error:' + e + '</span>'; // @@ enc
}
}
div.setAttribute('style', 'background-color: white; width:40cm; height:20cm;')
var headerButtons = function (dom, labels, intial, callback) {
var head = dom.createElement('table')
var current = intial
head.setAttribute('style', 'float: left; width: 30em; padding: 0.5em; height: 1.5em; background-color: #ddd; color: #444; font-weight: bold')
var tr = dom.createElement('tr')
var style0 = 'border-radius: 0.6em; text-align: center;'
var style1 = style0 + 'background-color: #ccc; color: black;'
head.appendChild(tr)
var setStyles = function () {
for (i = 0; i < labels.length; i++) {
buttons[i].setAttribute('style', i == current ? style1 : style0)
}
}
var i, b, buttons = []
for (i = 0; i < labels.length; i++) {
b = buttons[i] = dom.createElement('td')
b.textContent = labels[i]
tr.appendChild(buttons[i])
var listen = function (b, i) {
b.addEventListener('click', function (e) {
current = i
setStyles()
callback(i)
})
}
listen(b, i)
}
setStyles()
return head
}
var setMode = function (mode) {
if (mode !== currentMode) {
currentMode = mode
deselectObject()
showFiltered(mode)
}
}
var wrapper = dom.createElement('div')
wrapper.setAttribute('style', ' width: 30em; height: 100%; padding: 0em; float:left;')
// wrapper.appendChild(head)
div.appendChild(wrapper)
wrapper.appendChild(headerButtons(dom, [ 'all', 'attached', 'not attached',], currentMode, setMode))
var objectList = UI.widgets.selectorPanel(dom, kb, objectType, predicate, true, objects, options, showObject, linkClicked)
objectList.setAttribute('style',
'background-color: #ffe; width: 30em; height: 100%; padding: 0em; overflow:scroll;'); // float:left
wrapper.appendChild(objectList)
// objectList.insertBefore(head, objectList.firstChild)
var preview = dom.createElement('div')
preview.setAttribute('style', /*background-color: black; */ 'padding: 0em; margin: 0; height: 100%; overflow:scroll;')
div.appendChild(preview)
showFiltered(currentMode)
if (subjects.length > 0 && multi) {
var stores = {}
for (var k = 0; k < subjects.length; k++) {
var store = findStore(kb, subjects[k])
if (store) stores[store.uri] = subjects[k]
// if (!store) complain("No store for "+subjects[k].uri)
}
for (var storeURI in stores) {
// var store = findStore(kb,subjects[subjectList.length-1])
var store = kb.sym(storeURI)
var mintBox = dom.createElement('div')
mintBox.setAttribute('style', 'clear: left; width: 20em; margin-top:2em; background-color:#ccc; border-radius: 1em; padding: 1em; font-weight: bold;')
mintBox.textContent = '+ New ' + UI.utils.label(subject)
if (true) { // Only if > 1 store in set?
mintBox.textContent += ' in ' + UI.utils.label(store)
var storeLab = dom.createElement('span')
storeLab.setAttribute('style', 'font-weight: normal; font-size: 80%; color: #777;')
storeLab.textContent = storeURI
mintBox.appendChild(dom.createElement('br'))
mintBox.appendChild(storeLab)
}
/*
var mintButton = dom.createElement('img')
mintBox.appendChild(mintButton)
mintButton.setAttribute('src', ...); @@ Invokes master handler
*/
mintBox.addEventListener('click', function (event) {
var thisForm = UI.widgets.promptForNew(
dom, kb, subject, predicate, subject, null, store,
function (ok, body) {
if (!ok) {
// callback(ok, body); // @@ if ok, need some form of refresh of the select for the new thing
} else {
// Refresh @@
}
})
try {
div.insertBefore(thisForm, mintBox.nextSibling) // Sigh no insertAfter
} catch(e) {
div.appendChild(thisForm)
}
var newObject = thisForm.AJAR_subject
}, false)
div.appendChild(mintBox)
}
}
// if (!me) complain("(You do not have your Web Id set. Set your Web ID to make changes.)")
return div
}
} // pane object
// ends