/
dataContentPane.js
265 lines (236 loc) · 11.2 KB
/
dataContentPane.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
/* Data content Pane
**
** This pane shows the content of a particular RDF resource
** or at least the RDF semantics we attribute to that resource.
*/
// To do: - Only take data from one graph
// - Only do forwards not backward?
// - Expand automatically all the way down
// - original source view? Use ffox view source
tabulator.panes.dataContentPane = {
icon: tabulator.Icon.src.icon_dataContents,
name: 'dataContents',
label: function(subject) {
if('http://www.w3.org/2007/ont/link#ProtocolEvent' in tabulator.kb.findTypeURIs(subject)) return null;
var n = tabulator.kb.statementsMatching(
undefined, undefined, undefined, subject).length;
if (n == 0) return null;
return "Data ("+n+")";
},
/*
shouldGetFocus: function(subject) {
return tabulator.kb.whether(subject, tabulator.ns.rdf('type'), tabulator.ns.link('RDFDocument'));
},
*/
statementsAsTables: function statementsAsTables(sts, myDocument, initialRoots) {
var rep = myDocument.createElement('table');
var sz = tabulator.rdf.Serializer( tabulator.kb );
var res = sz.rootSubjects(sts);
var roots = res.roots;
var subjects = res.subjects;
var loopBreakers = res.loopBreakers;
for (var x in loopBreakers) dump('\tdataContentPane: loopbreaker:'+x+'\n')
var outline = tabulator.outline;
var doneBnodes = {}; // For preventing looping
var referencedBnodes = {}; // Bnodes which need to be named alas
// The property tree for a single subject or anonymos node
function propertyTree(subject) {
// print('Proprty tree for '+subject);
var rep = myDocument.createElement('table')
var lastPred = null;
var sts = subjects[sz.toStr(subject)]; // relevant statements
if (!sts) { // No statements in tree
rep.appendChild(myDocument.createTextNode('...')); // just empty bnode as object
return rep;
}
sts.sort();
var same =0;
var td_p; // The cell which holds the predicate
for (var i=0; i<sts.length; i++) {
var st = sts[i];
var tr = myDocument.createElement('tr');
if (st.predicate.uri != lastPred) {
if (lastPred && same > 1) td_p.setAttribute("rowspan", ''+same)
td_p = myDocument.createElement('td');
td_p.setAttribute('class', 'pred');
var anchor = myDocument.createElement('a')
anchor.setAttribute('href', st.predicate.uri)
anchor.addEventListener('click', tabulator.panes.utils.openHrefInOutlineMode, true);
anchor.appendChild(myDocument.createTextNode(tabulator.Util.predicateLabelForXML(st.predicate)));
td_p.appendChild(anchor);
tr.appendChild(td_p);
lastPred = st.predicate.uri;
same = 0;
}
same++;
var td_o = myDocument.createElement('td');
td_o.appendChild(objectTree(st.object));
tr.appendChild(td_o);
rep.appendChild(tr);
}
if (lastPred && same > 1) td_p.setAttribute("rowspan", ''+same)
return rep;
}
// Convert a set of statements into a nested tree of tables
function objectTree(obj) {
var res;
switch(obj.termType) {
case 'NamedNode':
var anchor = myDocument.createElement('a')
anchor.setAttribute('href', obj.uri)
anchor.addEventListener('click', tabulator.panes.utils.openHrefInOutlineMode, true);
anchor.appendChild(myDocument.createTextNode(tabulator.Util.label(obj)));
return anchor;
case 'Literal':
if (!obj.datatype || !obj.datatype.uri) {
res = myDocument.createElement('div');
res.setAttribute('style', 'white-space: pre-wrap;');
res.textContent = obj.value;
return res
} else if (obj.datatype.uri == 'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral') {
res = myDocument.createElement('div');
res.setAttribute('class', 'embeddedXHTML');
res.innerHTML = obj.value; // Try that @@@ beware embedded dangerous code
return res;
};
return myDocument.createTextNode(obj.value); // placeholder - could be smarter,
case 'BlankNode':
if (obj.toNT() in doneBnodes) { // Break infinite recursion
referencedBnodes[(obj.toNT())] = true;
var anchor = myDocument.createElement('a')
anchor.setAttribute('href', '#'+obj.toNT().slice(2))
anchor.setAttribute('class','bnodeRef')
anchor.textContent = '*'+obj.toNT().slice(3);
return anchor;
}
doneBnodes[obj.toNT()] = true; // Flag to prevent infinite recusruion in propertyTree
var newTable = propertyTree(obj);
doneBnodes[obj.toNT()] = newTable; // Track where we mentioned it first
if (tabulator.Util.ancestor(newTable, 'TABLE') && tabulator.Util.ancestor(newTable, 'TABLE').style.backgroundColor=='white') {
newTable.style.backgroundColor='#eee'
} else {
newTable.style.backgroundColor='white'
}
return newTable;
case 'collection':
var res = myDocument.createElement('table')
res.setAttribute('class', 'collectionAsTables')
for (var i=0; i<obj.elements.length; i++) {
var tr = myDocument.createElement('tr');
res.appendChild(tr);
tr.appendChild(objectTree(obj.elements[i]));
}
return res;
case 'formula':
var res = tabulator.panes.dataContentPane.statementsAsTables(obj.statements, myDocument);
res.setAttribute('class', 'nestedFormula')
return res;
case 'Variable':
var res = myDocument.createTextNode('?' + obj.uri);
return res;
}
throw "Unhandled node type: "+obj.termType
}
// roots.sort();
if (initialRoots) {
roots = initialRoots.concat(roots.filter(function(x){
for (var i=0; i<initialRoots.length; i++) { // Max 2
if (x.sameTerm(initialRoots[i])) return false;
}
return true;
}));
}
for (var i=0; i<roots.length; i++) {
var tr = myDocument.createElement('tr')
rep.appendChild(tr);
var td_s = myDocument.createElement('td')
tr.appendChild(td_s);
var td_tree = myDocument.createElement('td')
tr.appendChild(td_tree);
var root = roots[i];
if (root.termType == 'BlankNode') {
td_s.appendChild(myDocument.createTextNode(tabulator.Util.label(root))); // Don't recurse!
}
else {
td_s.appendChild(objectTree(root)); // won't have tree
}
td_tree.appendChild(propertyTree(root));
}
for (var bNT in referencedBnodes) { // Add number to refer to
var table = doneBnodes[bNT];
var tr = myDocument.createElement('tr');
var anchor = myDocument.createElement('a')
anchor.setAttribute('id', bNT.slice(2))
anchor.setAttribute('class','bnodeDef')
anchor.textContent = bNT.slice(3)+')';
table.insertBefore(anchor, table.firstChild);
}
return rep;
}, // statementsAsTables
// View the data in a file in user-friendly way
render: function(subject, myDocument) {
var kb = tabulator.kb;
var div = myDocument.createElement("div")
div.setAttribute('class', 'dataContentPane');
// Because of smushing etc, this will not be a copy of the original source
// We could instead either fetch and re-parse the source,
// or we could keep all the pre-smushed triples.
var sts = kb.statementsMatching(undefined, undefined, undefined, subject); // @@ slow with current store!
if (1) {
initialRoots = []; // Ordering: start with stuf fabout this doc
if (kb.holds(subject, undefined, undefined, subject)) initialRoots.push(subject);
// Then about the primary topic of the document if any
var ps = kb.any(subject, tabulator.ns.foaf('primaryTopic'), undefined, subject);
if (ps) initialRoots.push(ps);
div.appendChild(tabulator.panes.dataContentPane.statementsAsTables(
sts, myDocument, initialRoots));
} else { // An outline mode openable rendering .. might be better
var sz = tabulator.rdf.Serializer( tabulator.kb );
var res = sz.rootSubjects(sts);
var roots = res.roots;
var p = {};
// p.icon = dataContentPane.icon
p.render = function(s2) {
var div = myDocument.createElement('div')
div.setAttribute('class', 'withinDocumentPane')
var plist = kb.statementsMatching(s2, undefined, undefined, subject)
appendPropertyTRs(div, plist, false, function(pred, inverse) {return true;})
return div
}
for (var i=0; i<roots.length; i++) {
var tr = myDocument.createElement("TR");
root = roots[i];
tr.style.verticalAlign="top";
var td = thisOutline.outline_objectTD(root, undefined, tr)
tr.appendChild(td)
div.appendChild(tr);
outline_expand(td, root, {'pane': p});
}
}
return div
}
};
tabulator.panes.register(tabulator.panes.dataContentPane, false);
/* Pane within Document data content view
**
** This outline pane contains docuemnts from a specific source document only.
** It is a pane used recursively within an outer dataContentPane. (above)
*/
/* Not used in fact??
tabulator.panes.register({
icon: Icon.src.icon_withinDocumentPane, // should not show
label: function(subject) { return 'doc contents';},
filter: function(pred, inverse) {
return true; // show all
},
render: function(subject, source) {
var div = myDocument.createElement('div')
div.setAttribute('class', 'withinDocumentPane')
var plist = kb.statementsMatching(subject, undefined, undefined, source)
tabulator.outline.appendPropertyTRs(div, plist, false,
function(pred, inverse) {return true;});
return div ;
}
}, true);
*/
//ends