-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathtable.js
More file actions
1753 lines (1316 loc) · 46 KB
/
table.js
File metadata and controls
1753 lines (1316 loc) · 46 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
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Table Widget: Format an array of RDF statements as an HTML table.
//
// This can operate in one of three modes: when the class of object is given
// or when the source document from whuch data is taken is given,
// or if a prepared query object is given.
// (In principle it could operate with neither class nor document
// given but typically
// there would be too much data.)
// When the tableClass is not given, it looks for common classes in the data,
// and gives the user the option.
//
// 2008 Written, Ilaria Liccardi as the tableViewPane.js
// 2014 Core table widget moved into common/table.js - timbl
//
var UI = {
icons: require('./iconBase.js'),
log: require('./log'),
ns: require('./ns'),
store: require('./store'),
utils: require('./utils'),
widgets: require('./widgets')
}
// UI.widgets.renderTableViewPane
module.exports = function renderTableViewPane (doc, options) {
var sourceDocument = options.sourceDocument
var tableClass = options.tableClass
var givenQuery = options.query
var RDFS_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
var RDFS_LITERAL = 'http://www.w3.org/2000/01/rdf-schema#Literal'
var ns = UI.ns
var kb = UI.store
var rowsLookup = {} // Persistent mapping of subject URI to dom TR
// Predicates that are never made into columns:
var FORBIDDEN_COLUMNS = {
'http://www.w3.org/2002/07/owl#sameAs': true,
'http://www.w3.org/1999/02/22-rdf-syntax-ns#type': true
}
// Number types defined in the XML schema:
var XSD_NUMBER_TYPES = {
'http://www.w3.org/2001/XMLSchema#decimal': true,
'http://www.w3.org/2001/XMLSchema#float': true,
'http://www.w3.org/2001/XMLSchema#double': true,
'http://www.w3.org/2001/XMLSchema#integer': true,
'http://www.w3.org/2001/XMLSchema#nonNegativeInteger': true,
'http://www.w3.org/2001/XMLSchema#positiveInteger': true,
'http://www.w3.org/2001/XMLSchema#nonPositiveInteger': true,
'http://www.w3.org/2001/XMLSchema#negativeInteger': true,
'http://www.w3.org/2001/XMLSchema#long': true,
'http://www.w3.org/2001/XMLSchema#int': true,
'http://www.w3.org/2001/XMLSchema#short': true,
'http://www.w3.org/2001/XMLSchema#byte': true,
'http://www.w3.org/2001/XMLSchema#unsignedLong': true,
'http://www.w3.org/2001/XMLSchema#unsignedInt': true,
'http://www.w3.org/2001/XMLSchema#unsignedShort': true,
'http://www.w3.org/2001/XMLSchema#unsignedByte': true
}
var XSD_DATE_TYPES = {
'http://www.w3.org/2001/XMLSchema#dateTime': true,
'http://www.w3.org/2001/XMLSchema#date': true
}
// Classes that indicate an image:
var IMAGE_TYPES = {
'http://xmlns.com/foaf/0.1/Image': true,
'http://purl.org/dc/terms/Image': true
}
// Name of the column used as a "key" value to look up the row.
// This is necessary because in the normal view, the columns are
// all "optional" values, meaning that we will get a result set
// for every individual value that is found. The row key acts
// as an anchor that can be used to combine this information
// back into the same row.
var keyVariable = options.keyVariable || '?_row'
// Use queries to render the table, currently experimental:
var USE_QUERIES = true
var subjectIdCounter = 0
var allType, types
var typeSelectorDiv, addColumnDiv
// The last SPARQL query used:
var lastQuery = null
var mostCommonType = null
var resultDiv = doc.createElement('div')
resultDiv.className = 'tableViewPane'
resultDiv.appendChild(generateControlBar()) // sets typeSelectorDiv
var tableDiv = doc.createElement('div')
resultDiv.appendChild(tableDiv)
// Save a refresh function for use by caller
resultDiv.refresh = function () {
runQuery(table.query, table.logicalRows, table.columns, table)
// updateTable(givenQuery, mostCommonType) // This could be a lot more incremental and efficient
}
// A specifically asked-for query
if (givenQuery) {
var table = renderTableForQuery(givenQuery)
// lastQuery = givenQuery
tableDiv.appendChild(table)
} else {
// Find the most common type and select it by default
var s = calculateTable()
allType = s[0]; types = s[1]
if (!tableClass) typeSelectorDiv.appendChild(
generateTypeSelector(allType, types))
mostCommonType = getMostCommonType(types)
if (mostCommonType != null) {
buildFilteredTable(mostCommonType)
} else {
buildFilteredTable(allType)
}
}
return resultDiv
// /////////////////////////////////////////////////////////////////
function closeDialog (dialog) {
dialog.parentNode.removeChild(dialog)
}
function createActionButton (label, callback) {
var button = doc.createElement('input')
button.setAttribute('type', 'submit')
button.setAttribute('value', label)
button.addEventListener('click', callback, false)
return button
}
function createSparqlWindow () {
var dialog = doc.createElement('div')
dialog.setAttribute('class', 'sparqlDialog')
var title = doc.createElement('h3')
title.appendChild(doc.createTextNode('Edit SPARQL query'))
var inputbox = doc.createElement('textarea')
inputbox.value = queryToSPARQL(lastQuery)
dialog.appendChild(title)
dialog.appendChild(inputbox)
dialog.appendChild(createActionButton('Query', function () {
var query = SPARQLToQuery(inputbox.value)
updateTable(query)
closeDialog(dialog)
}))
dialog.appendChild(createActionButton('Close', function () {
closeDialog(dialog)
}))
return dialog
}
function sparqlButtonPressed () {
var dialog = createSparqlWindow()
resultDiv.appendChild(dialog)
}
function generateSparqlButton () {
var image = doc.createElement('img')
image.setAttribute('class', 'sparqlButton')
image.setAttribute('src', tabulator.iconPrefix + 'icons/1pt5a.gif')
image.setAttribute('alt', 'Edit SPARQL query')
image.addEventListener('click', sparqlButtonPressed, false)
return image
}
// Generate the control bar displayed at the top of the screen.
function generateControlBar () {
var result = doc.createElement('table')
result.setAttribute('class', 'toolbar')
var tr = doc.createElement('tr')
/* @@ Add in later -- not debugged yet
var sparqlButtonDiv = doc.createElement("td")
sparqlButtonDiv.appendChild(generateSparqlButton())
tr.appendChild(sparqlButtonDiv)
*/
typeSelectorDiv = doc.createElement('td')
tr.appendChild(typeSelectorDiv)
addColumnDiv = doc.createElement('td')
tr.appendChild(addColumnDiv)
result.appendChild(tr)
return result
}
// Add the SELECT details to the query being built.
function addSelectToQuery (query, type) {
var selectedColumns = type.getColumns()
for (var i = 0; i < selectedColumns.length; ++i) {
// TODO: autogenerate nicer names for variables
// variables have to be unambiguous
var variable = kb.variable('_col' + i)
query.vars.push(variable)
selectedColumns[i].setVariable(variable)
}
}
// Add WHERE details to the query being built.
function addWhereToQuery (query, rowVar, type) {
var queryType = type.type
if (queryType == null) {
queryType = kb.variable('_any')
}
// _row a type
query.pat.add(rowVar,
UI.ns.rdf('type'),
queryType)
}
// Generate OPTIONAL column selectors.
function addColumnsToQuery (query, rowVar, type) {
var selectedColumns = type.getColumns()
for (var i = 0; i < selectedColumns.length; ++i) {
var column = selectedColumns[i]
var formula = kb.formula()
formula.add(rowVar,
column.predicate,
column.getVariable())
query.pat.optional.push(formula)
}
}
// Generate a query object from the currently-selected type
// object.
function generateQuery (type) {
var query = new tabulator.rdf.Query()
var rowVar = kb.variable(keyVariable.slice(1)); // don't pass '?'
addSelectToQuery(query, type)
addWhereToQuery(query, rowVar, type)
addColumnsToQuery(query, rowVar, type)
return query
}
// Build the contents of the tableDiv element, filtered according
// to the specified type.
function buildFilteredTable (type) {
// Generate "add column" cell.
clearElement(addColumnDiv)
addColumnDiv.appendChild(generateColumnAddDropdown(type))
var query = generateQuery(type)
updateTable(query, type)
}
function updateTable (query, type) {
// Stop the previous query from doing any updates.
if (lastQuery != null) {
lastQuery.running = false
}
// Render the HTML table.
var htmlTable = renderTableForQuery(query, type)
// Clear the tableDiv element, and replace with the new table.
clearElement(tableDiv)
tableDiv.appendChild(htmlTable)
// Save the query for the edit dialog.
lastQuery = query
}
// Remove all subelements of the specified element.
function clearElement (element) {
while (element.childNodes.length > 0) {
element.removeChild(element.childNodes[0])
}
}
// A SubjectType is created for each rdf:type discovered.
function SubjectType (type) {
this.type = type
this.columns = null
this.allColumns = null
this.useCount = 0
// Get a list of all columns used by this type.
this.getAllColumns = function () {
return this.allColumns
}
// Get a list of the current columns used by this type
// (subset of allColumns)
this.getColumns = function () {
// The first time through, get a list of all the columns
// and select only the six most popular columns.
if (this.columns == null) {
var allColumns = this.getAllColumns()
this.columns = allColumns.slice(0, 7)
}
return this.columns
}
// Get a list of unused columns
this.getUnusedColumns = function () {
var allColumns = this.getAllColumns()
var columns = this.getColumns()
var result = []
for (var i = 0; i < allColumns.length; ++i) {
if (columns.indexOf(allColumns[i]) == -1) {
result.push(allColumns[i])
}
}
return result
}
this.addColumn = function (column) {
this.columns.push(column)
}
this.removeColumn = function (column) {
this.columns = this.columns.filter(function (x) {
return x != column
})
}
this.getLabel = function () {
return UI.utils.label(this.type)
}
this.addUse = function () {
this.useCount += 1
}
}
// Class representing a column in the table.
function Column () {
this.useCount = 0
// Have we checked any values for this column yet?
this.checkedAnyValues = false
// If the range is unknown, but we just get literals in this
// column, then we can generate a literal selector.
this.possiblyLiteral = true
// If the range is unknown, but we just get literals and they
// match the regular expression for numbers, we can generate
// a number selector.
this.possiblyNumber = true
// We accumulate classes which things in the column must be a member of
this.constraints = []
// Check values as they are read. If we don't know what the
// range is, we might be able to infer that it is a literal
// if all of the values are literals. Similarly, we might
// be able to determine if the literal values are actually
// numbers (using regexps).
this.checkValue = function (term) {
var termType = term.termType
if (this.possiblyLiteral && termType != 'Literal' && termType != 'NamedNode') {
this.possiblyNumber = false
this.possiblyLiteral = false
} else if (this.possiblyNumber) {
if (termType != 'Literal') {
this.possiblyNumber = false
} else {
var literalValue = term.value
if (!literalValue.match(/^\-?\d+(\.\d*)?$/)) {
this.possiblyNumber = false
}
}
}
this.checkedAnyValues = true
}
this.getVariable = function () {
return this.variable
}
this.setVariable = function (variable) {
this.variable = variable
}
this.getKey = function () {
return this.variable.toString()
}
this.addUse = function () {
this.useCount += 1
}
this.getLabel = function () {
if (this.predicate != null) {
if (this.predicate.sameTerm(ns.rdf('type')) && this.superClass) {
return UI.utils.label(this.superClass)
}
return UI.utils.label(this.predicate)
} else if (this.variable != null) {
return this.variable.toString()
} else {
return 'unlabeled column?'
}
}
this.setPredicate = function (predicate, inverse, other) {
if (inverse) { // variable is in the subject pos
this.inverse = predicate
this.constraints = this.constraints.concat(
kb.each(predicate, UI.ns.rdfs('domain')))
if (predicate.sameTerm(ns.rdfs('subClassOf')) && (other.termType == 'NamedNode')) {
this.superClass = other
this.alternatives = kb.each(undefined, ns.rdfs('subClassOf'), other)
}
} else { // variable is the object
this.predicate = predicate
this.constraints = this.constraints.concat(kb.each(predicate, UI.ns.rdfs('range')))
}
}
this.getConstraints = function () {
return this.constraints
}
this.filterFunction = function () {
return true
}
this.sortKey = function () {
return this.getLabel().toLowerCase()
}
this.isImageColumn = function () {
for (var i = 0; i < this.constraints.length; i++)
if (this.constraints[i].uri in IMAGE_TYPES) return true
return false
}
}
// Convert an object to an array.
function objectToArray (obj, filter) {
var result = []
for (var property in obj) { // @@@ have to guard against methods
var value = obj[property]
if (!filter || filter(property, value)) {
result.push(value)
}
}
return result
}
// Get the list of valid columns from the columns object.
function getColumnsList (columns) {
return objectToArray(columns)
}
// Generate an <option> in a drop-down list.
function optionElement (label, value) {
var result = doc.createElement('option')
result.setAttribute('value', value)
result.appendChild(doc.createTextNode(label))
return result
}
// Generate drop-down list box for choosing type of data displayed
function generateTypeSelector (allType, types) {
var resultDiv = doc.createElement('div')
resultDiv.appendChild(doc.createTextNode('Select type: '))
var dropdown = doc.createElement('select')
dropdown.appendChild(optionElement('All types', 'null'))
for (var uri in types) {
dropdown.appendChild(optionElement(types[uri].getLabel(), uri))
}
dropdown.addEventListener('click', function () {
var type
if (dropdown.value == 'null') {
type = allType
} else {
type = types[dropdown.value]
}
typeSelectorChanged(type)
}, false)
resultDiv.appendChild(dropdown)
return resultDiv
}
// Callback invoked when the type selector drop-down list is changed.
function typeSelectorChanged (selectedType) {
buildFilteredTable(selectedType)
}
// Build drop-down list to add a new column
function generateColumnAddDropdown (type) {
var resultDiv = doc.createElement('div')
var unusedColumns = type.getUnusedColumns()
unusedColumns.sort(function (a, b) {
var aLabel = a.sortKey()
var bLabel = b.sortKey()
return (aLabel > bLabel) - (aLabel < bLabel)
})
// If there are no unused columns, the div is empty.
if (unusedColumns.length > 0) {
resultDiv.appendChild(doc.createTextNode('Add column: '))
// Build dropdown list of unused columns.
var dropdown = doc.createElement('select')
dropdown.appendChild(optionElement('', '-1'))
for (var i = 0; i < unusedColumns.length; ++i) {
var column = unusedColumns[i]
dropdown.appendChild(optionElement(column.getLabel(), '' + i))
}
resultDiv.appendChild(dropdown)
// Invoke callback when the dropdown is changed, to add
// the column and reload the table.
dropdown.addEventListener('click', function () {
var columnIndex = new Number(dropdown.value)
if (columnIndex >= 0) {
type.addColumn(unusedColumns[columnIndex])
buildFilteredTable(type)
}
}, false)
}
return resultDiv
}
// Find the column for a given predicate, creating a new column object
// if necessary.
function getColumnForPredicate (columns, predicate) {
var column
if (predicate.uri in columns) {
column = columns[predicate.uri]
} else {
column = new Column()
column.setPredicate(predicate)
columns[predicate.uri] = column
}
return column
}
// Find a type by its URI, creating a new SubjectType object if
// necessary.
function getTypeForObject (types, type) {
var subjectType
if (type.uri in types) {
subjectType = types[type.uri]
} else {
subjectType = new SubjectType(type)
types[type.uri] = subjectType
}
return subjectType
}
// Discover types and subjects for search.
function discoverTypes () {
// rdf:type properties of subjects, indexed by URI for the type.
var types = {}
// Get a list of statements that match: ? rdfs:type ?
// From this we can get a list of subjects and types.
var subjectList = kb.statementsMatching(undefined,
UI.ns.rdf('type'),
tableClass, // can be undefined OR
sourceDocument) // can be undefined
// Subjects for later lookup. This is a mapping of type URIs to
// lists of subjects (it is necessary to record the type of
// a subject).
var subjects = {}
for (var i = 0; i < subjectList.length; ++i) {
var type = subjectList[i].object
if (type.termType != 'NamedNode') { // @@ no bnodes?
continue
}
var typeObj = getTypeForObject(types, type)
if (!(type.uri in subjects)) {
subjects[type.uri] = []
}
subjects[type.uri].push(subjectList[i].subject)
typeObj.addUse()
}
return [ subjects, types ]
}
// Get columns for the given subject.
function getSubjectProperties (subject, columns) {
// Get a list of properties of this subject.
var properties = kb.statementsMatching(subject,
undefined,
undefined,
sourceDocument)
var result = {}
for (var j = 0; j < properties.length; ++j) {
var predicate = properties[j].predicate
if (predicate.uri in FORBIDDEN_COLUMNS) {
continue
}
// Find/create a column for this predicate.
var column = getColumnForPredicate(columns, predicate)
column.checkValue(properties[j].object)
result[predicate.uri] = column
}
return result
}
// Identify the columns associated with a type.
function identifyColumnsForType (type, subjects) {
var allColumns = {}
// Process each subject of this type to build up the
// column list.
for (var i = 0; i < subjects.length; ++i) {
var columns = getSubjectProperties(subjects[i], allColumns)
for (var predicateUri in columns) {
var column = columns[predicateUri]
column.addUse()
}
}
// Generate the columns list
var allColumnsList = objectToArray(allColumns)
sortColumns(allColumnsList)
type.allColumns = allColumnsList
}
// Build table information from parsing RDF statements.
function calculateTable () {
// Find the types that we will display in the dropdown
// list box, and associated objects of those types.
var subjects, types
var s = discoverTypes()
subjects = s[0]; types = s[1]; // no [ ] on LHS
for (var typeUrl in subjects) {
var subjectList = subjects[typeUrl]
var type = types[typeUrl]
identifyColumnsForType(type, subjectList)
}
// TODO: Special type that captures all rows.
// Combine columns from all types
var allType = new SubjectType(null)
return [ allType, objectToArray(types) ]
}
// Sort the list of columns by the most common columns.
function sortColumns (columns) {
function sortFunction (a, b) {
return (a.useCount < b.useCount) - (a.useCount > b.useCount)
}
columns.sort(sortFunction)
}
// Create the delete button for a column.
function renderColumnDeleteButton (type, column) {
var button = doc.createElement('a')
button.appendChild(doc.createTextNode('[x]'))
button.addEventListener('click', function () {
type.removeColumn(column)
buildFilteredTable(type)
}, false)
return button
}
// Render the table header for the HTML table.
function renderTableHeader (columns, type) {
var tr = doc.createElement('tr')
/* Empty header for link column */
var linkTd = doc.createElement('th')
tr.appendChild(linkTd)
/*
var labelTd = doc.createElement("th")
labelTd.appendChild(doc.createTextNode("*label*"))
tr.appendChild(labelTd)
*/
for (var i = 0; i < columns.length; ++i) {
var th = doc.createElement('th')
var column = columns[i]
th.appendChild(doc.createTextNode(column.getLabel()))
// We can only add a delete button if we are using the
// proper interface and have a type to delete from:
if (type != null) {
th.appendChild(renderColumnDeleteButton(type, column))
}
tr.appendChild(th)
}
return tr
}
// Sort the rows in the rendered table by data from a specific
// column, using the provided sort function to compare values.
function applyColumnSort (rows, column, sortFunction, reverse) {
var columnKey = column.getKey()
// Sort the rows array.
rows.sort(function (row1, row2) {
var row1Value = null, row2Value = null
if (columnKey in row1.values) {
row1Value = row1.values[columnKey][0]
}
if (columnKey in row2.values) {
row2Value = row2.values[columnKey][0]
}
var result = sortFunction(row1Value, row2Value)
if (reverse) {
return -result
} else {
return result
}
})
// Remove all rows from the table:
if (rows.length) {
var parentTable = rows[0]._htmlRow.parentNode
for (var i = 0; i < rows.length; ++i) {
parentTable.removeChild(rows[i]._htmlRow)
}
// Add back the rows in the new sorted order:
for (var i = 0; i < rows.length; ++i) {
parentTable.appendChild(rows[i]._htmlRow)
}
}
}
// Filter the list of rows based on the selectors for the
// columns.
function applyColumnFiltersToRow (row, columns) {
var rowDisplayed = true
// Check the filter functions for every column.
// The row should only be displayed if the filter functions
// for all of the columns return true.
for (var c = 0; c < columns.length; ++c) {
var column = columns[c]
var columnKey = column.getKey()
var columnValue = null
if (columnKey in row.values) {
columnValue = row.values[columnKey][0]
}
if (!column.filterFunction(columnValue)) {
rowDisplayed = false
break
}
}
// Show or hide the HTML row according to the result
// from the filter function.
var htmlRow = row._htmlRow
if (rowDisplayed) {
htmlRow.style.display = ''
} else {
htmlRow.style.display = 'none'
}
}
// Filter the list of rows based on the selectors for the
// columns.
function applyColumnFilters (rows, columns) {
// Apply filterFunction to each row.
for (var r = 0; r < rows.length; ++r) {
var row = rows[r]
applyColumnFiltersToRow(row, columns)
}
}
// /////////////////////////////////// Literal column handling
// Sort by literal value
function literalSort (rows, column, reverse) {
function literalToString (colValue) {
if (colValue != null) {
if (colValue.termType == 'Literal') {
return colValue.value.toLowerCase()
} else if (colValue.termType == 'NamedNode') {
return UI.utils.label(colValue).toLowerCase()
}
return colValue.value.toLowerCase()
} else {
return ''
}
}
function literalCompare (value1, value2) {
var strValue1 = literalToString(value1)
var strValue2 = literalToString(value2)
if (strValue1 < strValue2) {
return -1
} else if (strValue1 > strValue2) {
return 1
} else {
return 0
}
}
applyColumnSort(rows, column, literalCompare, reverse)
}
// Generates a selector for an RDF literal column.
function renderLiteralSelector (rows, columns, column) {
var result = doc.createElement('div')
var textBox = doc.createElement('input')
textBox.setAttribute('type', 'text')
textBox.style.width = '70%'
result.appendChild(textBox)
var sort1 = doc.createElement('span')
sort1.appendChild(doc.createTextNode('\u25BC'))
sort1.addEventListener('click', function () {
literalSort(rows, column, false)
}, false)
result.appendChild(sort1)
var sort2 = doc.createElement('span')
sort2.appendChild(doc.createTextNode('\u25B2'))
sort2.addEventListener('click', function () {
literalSort(rows, column, true)
}, false)
result.appendChild(sort2)
var substring = null
// Filter the table to show only rows that have a particular
// substring in the specified column.
column.filterFunction = function (colValue) {
if (substring == null) {
return true
} else if (colValue == null) {