forked from SolidOS/solid-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms.js
More file actions
1734 lines (1637 loc) · 49.7 KB
/
Copy pathforms.js
File metadata and controls
1734 lines (1637 loc) · 49.7 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
/* F O R M S
*
* A Vanilla Dom implementation of the form language
*/
/* global alert */
import { fieldParams } from './forms/fieldParams'
import { field, mostSpecificClassURI, fieldFunction } from './forms/fieldFunction'
import * as debug from '../debug'
import { basicField } from './forms/basic'
module.exports = {}
var forms = {}
forms.field = field // Form field functions by URI of field type.
var UI = {
icons: require('../iconBase'),
log: require('../log'),
ns: require('../ns'),
store: require('../store'),
style: require('../style'),
widgets: forms
}
const $rdf = require('rdflib')
const error = require('./error')
const buttons = require('./buttons')
const ns = require('../ns')
const utils = require('../utils')
const checkMarkCharacter = '\u2713'
const cancelCharacter = '\u2715'
const dashCharacter = '-'
// ///////////////////////////////////////////////////////////////////////
/* Form Field implementations
**
*/
/** Group of different fields
**
** One type of form field is an ordered Group of other fields.
** A Form is actually just the same as a group.
**
** @param {Document} dom The HTML Document object aka Document Object Model
** @param {Element?} container If present, the created widget will be appended to this
** @param {Map} already A hash table of (form, subject) kept to prevent recursive forms looping
** @param {Node} subject The thing about which the form displays/edits data
** @param {Node} form The form or field to be rendered
** @param {Node} store The web document in which the data is
** @param {function(ok, errorMessage)} callbackFunction Called when data is changed?
**
** @returns {Element} The HTML widget created
*/
forms.field[ns.ui('Form').uri] = forms.field[
ns.ui('Group').uri
] = function (dom, container, already, subject, form, store, callbackFunction) {
const kb = UI.store
var box = dom.createElement('div')
box.setAttribute('style', `padding-left: 2em; border: 0.05em solid ${UI.style.formBorderColor};`) // Indent a group
const ui = UI.ns.ui
if (container) container.appendChild(box)
// Prevent loops
var key = subject.toNT() + '|' + form.toNT()
if (already[key]) {
// been there done that
box.appendChild(dom.createTextNode('Group: see above ' + key))
var plist = [$rdf.st(subject, ns.owl('sameAs'), subject)] // @@ need prev subject
dom.outlineManager.appendPropertyTRs(box, plist)
return box
}
// box.appendChild(dom.createTextNode('Group: first time, key: '+key))
var already2 = {}
for (var x in already) already2[x] = 1
already2[key] = 1
var parts = kb.any(form, ui('parts'))
var p2
if (parts) {
p2 = parts.elements
} else {
parts = kb.each(form, ui('part')) // Warning: unordered
p2 = forms.sortBySequence(parts)
}
if (!parts) {
box.appendChild(error.errorMessageBlock(dom, 'No parts to form! '))
return dom
}
var eles = []
var original = []
for (var i = 0; i < p2.length; i++) {
var field = p2[i]
var t = mostSpecificClassURI(field) // Field type
if (t === ui('Options').uri) {
var dep = kb.any(field, ui('dependingOn'))
if (dep && kb.any(subject, dep)) original[i] = kb.any(subject, dep).toNT()
}
var fn = fieldFunction(dom, field)
var itemChanged = function (ok, body) {
if (ok) {
for (var j = 0; j < p2.length; j++) {
// This is really messy.
var field = p2[j]
var t = mostSpecificClassURI(field) // Field type
if (t === ui('Options').uri) {
var dep = kb.any(field, ui('dependingOn'))
var newOne = fn(
dom,
box,
already,
subject,
field,
store,
callbackFunction
)
box.removeChild(newOne)
box.insertBefore(newOne, eles[j])
box.removeChild(eles[j])
original[j] = kb.any(subject, dep).toNT()
eles[j] = newOne
}
}
}
callbackFunction(ok, body)
}
eles.push(fn(dom, box, already2, subject, field, store, itemChanged))
}
return box
}
/** Options field: Select one or more cases
**
** @param {Document} dom The HTML Document object aka Document Object Model
** @param {Element?} container If present, the created widget will be appended to this
** @param {Map} already A hash table of (form, subject) kept to prevent recursive forms looping
** @param {Node} subject The thing about which the form displays/edits data
** @param {Node} form The form or field to be rendered
** @param {Node} store The web document in which the data is
** @param {function(ok, errorMessage)} callbackFunction Called when data is changed?
**
** @returns {Element} The HTML widget created
*/
forms.field[ns.ui('Options').uri] = function (
dom,
container,
already,
subject,
form,
store,
callbackFunction
) {
const kb = UI.store
var box = dom.createElement('div')
// box.setAttribute('style', 'padding-left: 2em; border: 0.05em dotted purple;') // Indent Options
const ui = UI.ns.ui
if (container) container.appendChild(box)
var dependingOn = kb.any(form, ui('dependingOn'))
if (!dependingOn) {
dependingOn = ns.rdf('type')
} // @@ default to type (do we want defaults?)
var cases = kb.each(form, ui('case'))
if (!cases) {
box.appendChild(error.errorMessageBlock(dom, 'No cases to Options form. '))
}
var values
if (dependingOn.sameTerm(ns.rdf('type'))) {
values = kb.findTypeURIs(subject)
} else {
var value = kb.any(subject, dependingOn)
if (value === undefined) {
box.appendChild(
error.errorMessageBlock(
dom,
"Can't select subform as no value of: " + dependingOn
)
)
} else {
values = {}
values[value.uri] = true
}
}
// @@ Add box.refresh() to sync fields with values
for (var i = 0; i < cases.length; i++) {
var c = cases[i]
var tests = kb.each(c, ui('for')) // There can be multiple 'for'
for (var j = 0; j < tests.length; j++) {
if (values[tests[j].uri]) {
var field = kb.the(c, ui('use'))
if (!field) {
box.appendChild(
error.errorMessageBlock(
dom,
'No "use" part for case in form ' + form
)
)
return box
} else {
forms.appendForm(
dom,
box,
already,
subject,
field,
store,
callbackFunction
)
}
break
}
}
}
return box
}
/** Multiple field: zero or more similar subFields
**
** @param {Document} dom The HTML Document object aka Document Object Model
** @param {Element?} container If present, the created widget will be appended to this
** @param {Map} already A hash table of (form, subject) kept to prevent recursive forms looping
** @param {Node} subject The thing about which the form displays/edits data
** @param {Node} form The form or field to be rendered
** @param {Node} store The web document in which the data is
** @param {function(ok, errorMessage)} callbackFunction Called when data is changed?
**
** @returns {Element} The HTML widget created
*/
forms.field[ns.ui('Multiple').uri] = function (
dom,
container,
already,
subject,
form,
store,
callbackFunction
) {
/** Diagnostic function
*/
function debugString (values) {
return values.map(x => x.toString().slice(-7)).join(', ')
}
/** Add an item to the local quadstore not the UI or the web
*
* @param {Node} object The RDF object to be represented by this item.
*/
async function addItem (object) {
if (!object) object = forms.newThing(store) // by default just add new nodes
if (ordered) {
createListIfNecessary() // Sets list and unsavedList
list.elements.push(object)
await saveListThenRefresh()
} else {
const toBeInserted = [$rdf.st(subject, property, object, store)]
try {
await kb.updater.update([], toBeInserted)
} catch (err) {
const msg = 'Error adding to unordered multiple: ' + err
box.appendChild(error.errorMessageBlock(dom, msg))
debug.error(msg)
}
refresh() // 20191213
}
}
/** Make a dom representation for an item
* @param {Event} anyEvent if used as an event handler
* @param {Node} object The RDF object to be represented by this item.
*/
function renderItem (object) {
async function deleteThisItem () {
if (ordered) {
debug.log('pre delete: ' + debugString(list.elements))
for (let i = 0; i < list.elements.length; i++) {
if (list.elements[i].sameTerm(object)) {
list.elements.splice(i, 1)
await saveListThenRefresh()
return
}
}
} else {
// unordered
if (kb.holds(subject, property, object)) {
var del = [$rdf.st(subject, property, object, store)]
kb.updater.update(del, [], function (uri, ok, message) {
if (ok) {
body.removeChild(subField)
} else {
body.appendChild(
error.errorMessageBlock(
dom,
'Multiple: delete failed: ' + message
)
)
}
})
}
}
}
/** Move the object up or down in the ordered list
* @param {Event} anyEvent if used as an event handler
* @param {Boolean} upwards Move this up (true) or down (false).
*/
async function moveThisItem (event, upwards) {
// @@ possibly, allow shift+click to do move to top or bottom?
debug.log('pre move: ' + debugString(list.elements))
for (var i = 0; i < list.elements.length; i++) {
// Find object in array
if (list.elements[i].sameTerm(object)) {
break
}
}
if (i === list.elements.length) {
alert('list move: not found element for ' + object)
}
if (upwards) {
if (i === 0) {
alert('@@ boop - already at top -temp message') // @@ make boop sound
return
}
list.elements.splice(i - 1, 2, list.elements[i], list.elements[i - 1])
} else {
// downwards
if (i === list.elements.length - 1) {
alert('@@ boop - already at bottom -temp message') // @@ make boop sound
return
}
list.elements.splice(i, 2, list.elements[i + 1], list.elements[i])
}
await saveListThenRefresh()
}
/* A subField has been filled in
*
* One possibility is to not actually make the link to the thing until
* this callback happens to avoid widow links
*/
function itemDone (uri, ok, message) {
debug.log(`Item ${uri} done callback for item ${object.uri.slice(-7)}`)
if (!ok) { // when does this happen? errors typically deal with upstream
debug.error(' Item done callback: Error: ' + message)
} else {
linkDone(uri, ok, message)
}
}
var linkDone = function (uri, ok, message) {
return callbackFunction(ok, message)
}
// if (!object) object = forms.newThing(store)
UI.log.debug('Multiple: render object: ' + object)
// var tr = box.insertBefore(dom.createElement('tr'), tail)
// var ins = []
// var del = []
var fn = fieldFunction(dom, element)
var subField = fn(dom, null, already, object, element, store, itemDone) // p2 was: body. moving to not passing that
subField.subject = object // Keep a back pointer between the DOM array and the RDF objects
// delete button and move buttons
if (kb.updater.editable(store.uri)) {
buttons.deleteButtonWithCheck(dom, subField, utils.label(property),
deleteThisItem)
if (ordered) {
subField.appendChild(
buttons.button(
dom, UI.icons.iconBase + 'noun_1369237.svg', 'Move Up',
async event => moveThisItem(event, true))
)
subField.appendChild(
buttons.button(
dom, UI.icons.iconBase + 'noun_1369241.svg', 'Move Down',
async event => moveThisItem(event, false))
)
}
}
return subField // unused
} // renderItem
/// ///////// Body of form field implementation
var plusIconURI = UI.icons.iconBase + 'noun_19460_green.svg' // white plus in green circle
const kb = UI.store
kb.updater = kb.updater || new $rdf.UpdateManager(kb)
var box = dom.createElement('table')
// We don't indent multiple as it is a sort of a prefix of the next field and has contents of one.
// box.setAttribute('style', 'padding-left: 2em; border: 0.05em solid green;') // Indent a multiple
const ui = UI.ns.ui
if (container) container.appendChild(box)
const orderedNode = kb.any(form, ui('ordered'))
const ordered = orderedNode ? $rdf.Node.toJS(orderedNode) : false
var property = kb.any(form, ui('property'))
const reverse = kb.anyJS(form, ui('reverse'))
if (!property) {
box.appendChild(
error.errorMessageBlock(dom, 'No property to multiple: ' + form)
) // used for arcs in the data
return box
}
var min = kb.any(form, ui('min')) // This is the minimum number -- default 0
min = min ? 0 + min.value : 0
// var max = kb.any(form, ui('max')) // This is the minimum number
// max = max ? max.value : 99999999
var element = kb.any(form, ui('part')) // This is the form to use for each one
if (!element) {
box.appendChild(
error.errorMessageBlock(dom, 'No part to multiple: ' + form)
)
return box
}
var body = box.appendChild(dom.createElement('tr')) // 20191207
var list // The RDF collection which keeps the ordered version or null
var values // Initial values - always an array. Even when no list yet.
values = reverse ? kb.any(null, property, subject) : kb.any(subject, property)
if (ordered) {
list = reverse ? kb.any(null, property, subject) : kb.any(subject, property)
if (list) {
values = list.elements
} else {
values = []
}
} else {
values = reverse ? kb.each(null, property, subject) : kb.each(subject, property)
list = null
}
// Add control on the bottom for adding more items
if (kb.updater.editable(store.uri)) {
var tail = box.appendChild(dom.createElement('tr'))
tail.style.padding = '0.5em'
var img = tail.appendChild(dom.createElement('img'))
img.setAttribute('src', plusIconURI) // plus sign
img.setAttribute('style', 'margin: 0.2em; width: 1.5em; height:1.5em')
img.title = 'Click to add one or more ' + utils.predicateLabel(property, reverse)
var prompt = tail.appendChild(dom.createElement('span'))
prompt.textContent =
(values.length === 0 ? 'Add one or more ' : 'Add more ') +
utils.label(property)
tail.addEventListener('click', async _eventNotUsed => {
await addItem()
}, true)
}
function createListIfNecessary () {
if (!list) {
list = new $rdf.Collection()
if (reverse) {
kb.add(list, property, subject, store)
} else {
kb.add(subject, property, list, store)
}
}
}
async function saveListThenRefresh () {
debug.log('save list: ' + debugString(list.elements)) // 20191214
createListIfNecessary()
try {
await kb.fetcher.putBack(store)
} catch (err) {
box.appendChild(
error.errorMessageBlock(dom, 'Error trying to put back a list: ' + err)
)
return
}
refresh()
}
function refresh () {
let vals
if (ordered) {
const li = reverse ? kb.the(null, property, subject) : kb.the(subject, property)
vals = li ? li.elements : []
} else {
vals = reverse ? kb.each(null, property, subject) : kb.each(subject, property)
vals.sort() // achieve consistency on each refresh
}
utils.syncTableToArrayReOrdered(body, vals, renderItem)
}
body.refresh = refresh // Allow live update
refresh()
async function asyncStuff () {
var extra = min - values.length
if (extra > 0) {
for (var j = 0; j < extra; j++) {
debug.log('Adding extra: min ' + min)
await addItem() // Add blanks if less than minimum
}
await saveListThenRefresh()
}
// if (unsavedList) {
// await saveListThenRefresh() // async
// }
}
asyncStuff().then(
() => { debug.log(' Multiple render: async stuff ok') },
(err) => { debug.error(' Multiple render: async stuff fails. #### ', err) }
) // async
return box
} // Multiple
/* Text field
**
*/
// For possible date popups see e.g. http://www.dynamicdrive.com/dynamicindex7/jasoncalendar.htm
// or use HTML5: http://www.w3.org/TR/2011/WD-html-markup-20110113/input.date.html
//
forms.fieldParams = fieldParams
forms.field[ns.ui('PhoneField').uri] = basicField
forms.field[ns.ui('EmailField').uri] = basicField
forms.field[ns.ui('ColorField').uri] = basicField
forms.field[ns.ui('DateField').uri] = basicField
forms.field[ns.ui('DateTimeField').uri] = basicField
forms.field[ns.ui('TimeField').uri] = basicField
forms.field[ns.ui('NumericField').uri] = basicField
forms.field[ns.ui('IntegerField').uri] = basicField
forms.field[ns.ui('DecimalField').uri] = basicField
forms.field[ns.ui('FloatField').uri] = basicField
forms.field[ns.ui('TextField').uri] = basicField
forms.field[ns.ui('SingleLineTextField').uri] = basicField
forms.field[ns.ui('NamedNodeURIField').uri] = basicField
/* Multiline Text field
**
*/
forms.field[ns.ui('MultiLineTextField').uri] = function (
dom,
container,
already,
subject,
form,
store,
callbackFunction
) {
const ui = UI.ns.ui
const kb = UI.store
var property = kb.any(form, ui('property'))
if (!property) {
return error.errorMessageBlock(dom, 'No property to text field: ' + form)
}
const box = dom.createElement('div')
box.appendChild(forms.fieldLabel(dom, property, form))
store = forms.fieldStore(subject, property, store)
var field = forms.makeDescription(
dom,
kb,
subject,
property,
store,
callbackFunction
)
// box.appendChild(dom.createTextNode('<-@@ subj:'+subject+', prop:'+property))
box.appendChild(field)
if (container) container.appendChild(box)
return box
}
/* Boolean field and Tri-state version (true/false/null)
**
** @@ todo: remove tristate param
*/
function booleanField (
dom,
container,
already,
subject,
form,
store,
callbackFunction,
tristate
) {
const ui = UI.ns.ui
const kb = UI.store
var property = kb.any(form, ui('property'))
if (!property) {
const errorBlock = error.errorMessageBlock(
dom,
'No property to boolean field: ' + form
)
if (container) container.appendChild(errorBlock)
return errorBlock
}
var lab = kb.any(form, ui('label'))
if (!lab) lab = utils.label(property, true) // Init capital
store = forms.fieldStore(subject, property, store)
var state = kb.any(subject, property)
if (state === undefined) {
state = false
} // @@ sure we want that -- or three-state?
// UI.log.debug('store is '+store)
var ins = $rdf.st(subject, property, true, store)
var del = $rdf.st(subject, property, false, store)
var box = buildCheckboxForm(dom, kb, lab, del, ins, form, store, tristate)
if (container) container.appendChild(box)
return box
}
forms.field[ns.ui('BooleanField').uri] = function (
dom,
container,
already,
subject,
form,
store,
callbackFunction
) {
return booleanField(
dom,
container,
already,
subject,
form,
store,
callbackFunction,
false
)
}
forms.field[ns.ui('TristateField').uri] = function (
dom,
container,
already,
subject,
form,
store,
callbackFunction
) {
return booleanField(
dom,
container,
already,
subject,
form,
store,
callbackFunction,
true
)
}
/* Classifier field
**
** Nested categories
**
** @@ To do: If a classification changes, then change any dependent Options fields.
*/
forms.field[ns.ui('Classifier').uri] = function (
dom,
container,
already,
subject,
form,
store,
callbackFunction
) {
const kb = UI.store
const ui = UI.ns.ui
var category = kb.any(form, ui('category'))
if (!category) {
return error.errorMessageBlock(dom, 'No category for classifier: ' + form)
}
UI.log.debug('Classifier: store=' + store)
var checkOptions = function (ok, body) {
if (!ok) return callbackFunction(ok, body)
/*
var parent = kb.any(undefined, ui('part'), form)
if (!parent) return callbackFunction(ok, body)
var kids = kb.each(parent, ui('part')); // @@@@@@@@@ Garbage
kids = kids.filter(function(k){return kb.any(k, ns.rdf('type'), ui('Options'))})
if (kids.length) UI.log.debug('Yes, found related options: '+kids[0])
*/
return callbackFunction(ok, body)
}
var box = forms.makeSelectForNestedCategory(
dom,
kb,
subject,
category,
store,
checkOptions
)
if (container) container.appendChild(box)
return box
}
/** Choice field
**
** Not nested. Generates a link to something from a given class.
** Optional subform for the thing selected.
** Alternative implementatons caould be:
** -- pop-up menu (as here)
** -- radio buttons
** -- auto-complete typing
**
** Todo: Deal with multiple. Maybe merge with multiple code.
*/
forms.field[ns.ui('Choice').uri] = function (
dom,
container,
already,
subject,
form,
store,
callbackFunction
) {
var ns = UI.ns
const ui = UI.ns.ui
const kb = UI.store
var multiple = false
var p
var box = dom.createElement('tr')
if (container) container.appendChild(box)
var lhs = dom.createElement('td')
box.appendChild(lhs)
var rhs = dom.createElement('td')
box.appendChild(rhs)
var property = kb.any(form, ui('property'))
if (!property) {
return error.errorMessageBlock(dom, 'No property for Choice: ' + form)
}
lhs.appendChild(forms.fieldLabel(dom, property, form))
var from = kb.any(form, ui('from'))
if (!from) {
return error.errorMessageBlock(dom, "No 'from' for Choice: " + form)
}
var subForm = kb.any(form, ui('use')) // Optional
var follow = kb.anyJS(form, ui('follow')) // data doc moves to new subject?
var possible = []
var possibleProperties
var np = '--' + utils.label(property) + '-?'
var opts = { multiple: multiple, nullLabel: np, disambiguate: false }
possible = kb.each(undefined, ns.rdf('type'), from)
for (var x in kb.findMembersNT(from)) {
possible.push(kb.fromNT(x))
// box.appendChild(dom.createTextNode("RDFS: adding "+x))
} // Use rdfs
// UI.log.debug("%%% Choice field: possible.length 1 = "+possible.length)
if (from.sameTerm(ns.rdfs('Class'))) {
for (p in buttons.allClassURIs()) possible.push(kb.sym(p))
// UI.log.debug("%%% Choice field: possible.length 2 = "+possible.length)
} else if (from.sameTerm(ns.rdf('Property'))) {
possibleProperties = buttons.propertyTriage(kb)
for (p in possibleProperties.op) possible.push(kb.fromNT(p))
for (p in possibleProperties.dp) possible.push(kb.fromNT(p))
opts.disambiguate = true // This is a big class, and the labels won't be enough.
} else if (from.sameTerm(ns.owl('ObjectProperty'))) {
possibleProperties = buttons.propertyTriage(kb)
for (p in possibleProperties.op) possible.push(kb.fromNT(p))
opts.disambiguate = true
} else if (from.sameTerm(ns.owl('DatatypeProperty'))) {
possibleProperties = buttons.propertyTriage(kb)
for (p in possibleProperties.dp) possible.push(kb.fromNT(p))
opts.disambiguate = true
}
var object = kb.any(subject, property)
function addSubForm () {
object = kb.any(subject, property)
fieldFunction(dom, subForm)(
dom,
rhs,
already,
object,
subForm,
follow ? object.doc() : store,
callbackFunction
)
}
var possible2 = forms.sortByLabel(possible)
if (kb.any(form, ui('canMintNew'))) {
opts.mint = '* New *' // @@ could be better
opts.subForm = subForm
}
var selector = forms.makeSelectForOptions(
dom,
kb,
subject,
property,
possible2,
opts,
store,
callbackFunction
)
rhs.appendChild(selector)
if (object && subForm) addSubForm()
return box
}
// Documentation - non-interactive fields
//
forms.field[ns.ui('Comment').uri] = forms.field[
ns.ui('Heading').uri
] = function (
dom,
container,
already,
subject,
form,
_store,
_callbackFunction
) {
const ui = UI.ns.ui
const kb = UI.store
var contents = kb.any(form, ui('contents'))
if (!contents) contents = 'Error: No contents in comment field.'
var uri = mostSpecificClassURI(form)
var params = forms.fieldParams[uri]
if (params === undefined) {
params = {}
} // non-bottom field types can do this
var box = dom.createElement('div')
if (container) container.appendChild(box)
var p = box.appendChild(dom.createElement(params.element))
p.textContent = contents
var style = kb.anyValue(form, ui('style')) || params.style || ''
if (style) p.setAttribute('style', style)
return box
}
// A button for editing a form (in place, at the moment)
//
// When editing forms, make it yellow, when editing thr form form, pink
// Help people understand how many levels down they are.
//
forms.editFormButton = function (
dom,
container,
form,
store,
callbackFunction
) {
var b = dom.createElement('button')
b.setAttribute('type', 'button')
b.innerHTML = 'Edit ' + utils.label(ns.ui('Form'))
b.addEventListener(
'click',
function (_e) {
var ff = forms.appendForm(
dom,
container,
{},
form,
ns.ui('FormForm'),
store,
callbackFunction
)
ff.setAttribute(
'style',
ns.ui('FormForm').sameTerm(form)
? 'background-color: #fee;'
: 'background-color: #ffffe7;'
)
b.parentNode.removeChild(b)
},
true
)
return b
}
forms.appendForm = function (
dom,
container,
already,
subject,
form,
store,
itemDone
) {
return fieldFunction(dom, form)(
dom,
container,
already,
subject,
form,
store,
itemDone
)
}
/** Find list of properties for class
//
// Three possible sources: Those mentioned in schemas, which exludes many
// those which occur in the data we already have, and those predicates we
// have come across anywhere and which are not explicitly excluded from
// being used with this class.
*/
forms.propertiesForClass = function (kb, c) {
var ns = UI.ns
var explicit = kb.each(undefined, ns.rdf('range'), c)
;[
ns.rdfs('comment'),
ns.dc('title'), // Generic things
ns.foaf('name'),
ns.foaf('homepage')
].map(function (x) {
explicit.push(x)
})
var members = kb.each(undefined, ns.rdf('type'), c)
if (members.length > 60) members = members.slice(0, 60) // Array supports slice?
var used = {}
for (var i = 0; i < (members.length > 60 ? 60 : members.length); i++) {
kb.statementsMatching(members[i], undefined, undefined).map(function (st) {
used[st.predicate.uri] = true
})
}
explicit.map(function (p) {
used[p.uri] = true
})
var result = []
for (var uri in used) {
result.push(kb.sym(uri))
}
return result
}
/** Find the closest class
* @param kb The store
* @param cla - the URI of the class
* @param prop
*/
forms.findClosest = function findClosest (kb, cla, prop) {
var agenda = [kb.sym(cla)] // ordered - this is breadth first search
while (agenda.length > 0) {
var c = agenda.shift() // first
// if (c.uri && (c.uri == ns.owl('Thing').uri || c.uri == ns.rdf('Resource').uri )) continue
var lists = kb.each(c, prop)
UI.log.debug('Lists for ' + c + ', ' + prop + ': ' + lists.length)
if (lists.length !== 0) return lists
var supers = kb.each(c, ns.rdfs('subClassOf'))
for (var i = 0; i < supers.length; i++) {
agenda.push(supers[i])
UI.log.debug('findClosest: add super: ' + supers[i])
}
}
return []
}
// Which forms apply to a given existing subject?
forms.formsFor = function (subject) {
var ns = UI.ns
const kb = UI.store
UI.log.debug('formsFor: subject=' + subject)
var t = kb.findTypeURIs(subject)
var t1
for (t1 in t) {
UI.log.debug(' type: ' + t1)
}
var bottom = kb.bottomTypeURIs(t) // most specific
var candidates = []
for (var b in bottom) {
// Find the most specific
UI.log.debug('candidatesFor: trying bottom type =' + b)
candidates = candidates.concat(
forms.findClosest(kb, b, ns.ui('creationForm'))
)
candidates = candidates.concat(
forms.findClosest(kb, b, ns.ui('annotationForm'))
)
}
return candidates
}
forms.sortBySequence = function (list) {
var p2 = list.map(function (p) {
var k = UI.store.any(p, ns.ui('sequence'))
return [k || 9999, p]
})
p2.sort(function (a, b) {
return a[0] - b[0]
})
return p2.map(function (pair) {
return pair[1]
})
}
forms.sortByLabel = function (list) {