-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathbuttons.ts
More file actions
1522 lines (1398 loc) · 48.8 KB
/
buttons.ts
File metadata and controls
1522 lines (1398 loc) · 48.8 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
/* Buttons
*/
import { IndexedFormula, NamedNode, st, sym, uri, Util } from 'rdflib'
import { icons } from '../iconBase'
import ns from '../ns'
import { style } from '../style'
import * as debug from '../debug'
import { info } from '../log'
import { uploadFiles, makeDraggable, makeDropTarget } from './dragAndDrop'
import { store } from 'solid-logic'
import * as utils from '../utils'
import newPersonIconDataURI from '../newperson'
import '../v2/components/actions/button'
import { errorMessageBlock } from './error'
import { addClickListenerToElement, createImageDiv, wrapDivInATR } from './widgetHelpers'
import { linkIcon, createLinkForURI } from './buttons/iconLinks'
import { lucideIcons } from '../icons/lucide'
/**
* UI Widgets such as buttons
* @packageDocumentation
*/
/* global alert */
const { iconBase } = icons
const cancelIconURI = lucideIcons.x // lucide x (was noun_1180156.svg)
const checkIconURI = lucideIcons.check // lucide check (was noun_1180158.svg)
export type StatusAreaContext = {
statusArea?: HTMLElement
div?: HTMLElement
dom?: HTMLDocument
}
export type ButtonType = 'Primary' | 'Secondary'
export type ButtonWidgetOptions = {
buttonColor?: ButtonType,
needsBorder?: boolean
}
export type RenderAsDivOptions = {
image?: HTMLImageElement,
title?: string,
deleteFunction?: () => void,
link?: boolean,
noun?: string,
draggable?: boolean,
clickable?: boolean,
onClickFunction?: () => void,
wrapInATR?: boolean
}
export type AttachmentSupportingInfo = string | HTMLElement | null | undefined
export type RenderSupportingInfo = (target: NamedNode, dom: HTMLDocument) => AttachmentSupportingInfo
export type AttachmentInlineInfo = string | HTMLElement | null | undefined
export type RenderNameSuffix = (target: NamedNode, dom: HTMLDocument) => AttachmentInlineInfo
function getStatusArea (context?: StatusAreaContext) {
let box = (context && context.statusArea) || (context && context.div) || null
if (box) return box
let dom = context && context.dom
if (!dom && typeof document !== 'undefined') {
dom = document
}
if (dom) {
const body = dom.getElementsByTagName('body')[0]
box = dom.createElement('div')
body.insertBefore((box as unknown as HTMLElement), body.firstElementChild)
if (context) {
context.statusArea = (box as unknown as HTMLElement)
}
return box
}
return null
}
/**
* Display an error message block
*/
export function complain (context?: StatusAreaContext, err?: string) {
if (!err) return // only if error
const ele = getStatusArea(context)
debug.log('Complaint: ' + err)
if (ele) ele.appendChild(errorMessageBlock((context && context.dom) || document, err))
else alert(err)
}
/**
* Remove all the children of an HTML element
*/
export function clearElement (ele: HTMLElement) {
while (ele.firstChild) {
ele.removeChild(ele.firstChild)
}
return ele
}
/**
* To figure out the log URI from the full URI used to invoke the reasoner
*/
export function extractLogURI (fullURI) {
const logPos = fullURI.search(/logFile=/)
const rulPos = fullURI.search(/&rulesFile=/)
return fullURI.substring(logPos + 8, rulPos)
}
/**
* By default, converts e.g. '2020-02-19T19:35:28.557Z' to '19:35'
* if today is 19 Feb 2020, and to 'Feb 19' if not.
* @@@ TODO This needs to be changed to local time
* @param noTime Return a string like 'Feb 19' even if it's today.
*/
export function shortDate (str?: string, noTime?: boolean): string {
if (!str) return '???'
const month = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
]
try {
const nowZ = new Date().toISOString()
// var nowZ = $rdf.term(now).value
// var n = now.getTimezoneOffset() // Minutes
if (str.slice(0, 10) === nowZ.slice(0, 10) && !noTime) {
return str.slice(11, 16)
}
if (str.slice(0, 4) === nowZ.slice(0, 4)) {
return (
month[parseInt(str.slice(5, 7), 10) - 1] +
' ' +
parseInt(str.slice(8, 10), 10)
)
}
return str.slice(0, 10)
} catch (e) {
return 'shortdate:' + e
}
}
/**
* Format a date and time
* @param date for instance `new Date()`
* @param format for instance '{FullYear}-{Month}-{Date}T{Hours}:{Minutes}:{Seconds}.{Milliseconds}'
* @returns for instance '2000-01-15T23:14:23.002'
*/
export function formatDateTime (date: Date, format: string): string {
return format
.split('{')
.map(function (s) {
const k = s.split('}')[0]
const width = { Milliseconds: 3, FullYear: 4 }
const d = { Month: 1 }
return s
? ('000' + (date['get' + k]() + (d[k] || 0))).slice(-(width[k] || 2)) + s.split('}')[1]
: ''
})
.join('')
}
/**
* Get a string representation of the current time
* @returns for instance '2000-01-15T23:14:23.002'
*/
export function timestamp (): string {
return formatDateTime(
new Date(),
'{FullYear}-{Month}-{Date}T{Hours}:{Minutes}:{Seconds}.{Milliseconds}'
)
}
/**
* Get a short string representation of the current time
* @returns for instance '23:14:23.002'
*/
export function shortTime (): string {
return formatDateTime(
new Date(),
'{Hours}:{Minutes}:{Seconds}.{Milliseconds}'
)
}
// ///////////////////// Handy UX widgets
/**
* Sets the best name we have and looks up a better one
*/
export function setName (element: HTMLElement, x: NamedNode) {
const kb = store
const findName = function (x) {
const name =
kb.any(x, ns.vcard('fn')) ||
kb.any(x, ns.foaf('name')) ||
kb.any(x, ns.vcard('organization-name'))
return name ? name.value : null
}
const name = x.sameTerm(ns.foaf('Agent')) ? 'Everyone' : findName(x)
element.textContent = name || utils.label(x)
if (!name && x.uri) {
if (!kb.fetcher) {
throw new Error('kb has no fetcher')
}
// Note this is only a fetch, not a lookUP of all sameAs etc
kb.fetcher.nowOrWhenFetched(x.doc(), undefined, function (_ok) {
element.textContent = findName(x) || utils.label(x) // had: (ok ? '' : '? ') +
})
}
}
/**
* Set of suitable images
* See also [[findImage]]
* @param x The thing for which we want to find an image
* @param kb The RDF store to look in
* @returns It goes looking for triples in `kb`,
* `(subject: x), (predicate: see list below) (object: image-url)`
* to find any image linked from the thing with one of the following
* predicates (in order):
* * ns.sioc('avatar')
* * ns.foaf('img')
* * ns.vcard('logo')
* * ns.vcard('hasPhoto')
* * ns.vcard('photo')
* * ns.foaf('depiction')
*/
export function imagesOf (x: NamedNode | null, kb: IndexedFormula): any[] {
return kb
.each(x, ns.sioc('avatar'))
.concat(kb.each(x, ns.foaf('img')))
.concat(kb.each(x, ns.vcard('logo')))
.concat(kb.each(x, ns.vcard('hasPhoto')))
.concat(kb.each(x, ns.vcard('photo')))
.concat(kb.each(x, ns.foaf('depiction')))
}
/**
* Best logo or avatar or photo etc to represent someone or some group etc
*/
export const iconForClass = {
// Map from CURIE → icon. May be either an iconBase-relative filename (legacy)
// or a full URL (e.g. data: URI from lucide). The setImage() helper below
// detects "data:" prefixes and skips the iconBase join.
'solid:AppProviderClass': lucideIcons.appWindow, // was noun_144.svg
'solid:AppProvider': lucideIcons.appWindow, // was noun_15177.svg
'solid:Pod': lucideIcons.database, // was noun_Cabinet_1434380.svg
'vcard:Group': lucideIcons.users, // was noun_339237.svg (the filled-group icon)
'vcard:Organization': lucideIcons.building2, // was noun_143899.svg
// TEMP HACK: Use locally bundled icon; switch to solid-assets icon mapping soon.
'vcard:Individual': newPersonIconDataURI,
'schema:Person': newPersonIconDataURI,
'foaf:Person': newPersonIconDataURI,
'foaf:Agent': lucideIcons.globe, // was noun_98053.svg
'acl:AuthenticatedAgent': lucideIcons.userCheck, // was noun_99101.svg
'prov:SoftwareAgent': lucideIcons.bot, // was noun_Robot_849764.svg
'vcard:AddressBook': lucideIcons.bookUser, // was noun_15695.svg
'trip:Trip': lucideIcons.route, // was noun_581629.svg
'meeting:LongChat': lucideIcons.messagesSquare, // the stacked-chat icon (was noun_1689339.svg)
'meeting:Meeting': lucideIcons.usersRound, // was noun_66617.svg
'meeting:Project': lucideIcons.briefcase, // was noun_1036577.svg
'ui:Form': lucideIcons.formInput, // was noun_122196.svg
'rdfs:Class': 'class-rectangle.svg', // For RDF developers — left as legacy
'rdf:Property': 'property-diamond.svg',
'owl:Ontology': 'noun_classification_1479198.svg',
'wf:Tracker': lucideIcons.formInput, // was noun_122196.svg
'wf:Task': 'noun_17020_gray-tick.svg',
'wf:Open': 'noun_17020_sans-tick.svg',
'wf:Closed': 'noun_17020.svg'
}
/**
* Returns the origin of the URI of a NamedNode
*/
function tempSite (x: NamedNode) {
// use only while one in rdflib fails with origins 2019
const str = x.uri.split('#')[0]
const p = str.indexOf('//')
if (p < 0) throw new Error('This URI does not have a web site part (origin)')
const q = str.indexOf('/', p + 2)
if (q < 0) {
// no third slash?
return str.slice(0) + '/' // Add slash to a bare origin
} else {
return str.slice(0, q + 1)
}
}
/**
* Find an image for this thing as a class
*/
export function findImageFromURI (x: NamedNode | string): string | null {
const iconDir = iconBase
// Special cases from URI scheme:
if (typeof x !== 'string' && x.uri) {
if (
x.uri.split('/').length === 4 &&
!x.uri.split('/')[1] &&
!x.uri.split('/')[3]
) {
return lucideIcons.appWindow // App (origin); was noun_15177.svg
}
// Non-HTTP URI types imply types
if (x.uri.startsWith('message:') || x.uri.startsWith('mid:')) {
// message: is apple bug-- should be mid:
return lucideIcons.mail // was noun_480183.svg
}
if (x.uri.startsWith('mailto:')) {
return lucideIcons.mail // mailto target; was noun_567486.svg (mailbox)
}
// For HTTP(s) documents, we could look at the MIME type if we know it.
if (x.uri.startsWith('https:') && x.uri.indexOf('#') < 0) {
return tempSite(x) + 'favicon.ico' // was x.site().uri + ...
// Todo: make the document icon a fallback for if the favicon does not exist
// todo: pick up a possible favicon for the web page itself from a link
// was: return iconDir + 'noun_681601.svg' // document - under solid assumptions
}
return null
}
return iconDir + 'noun_10636_grey.svg' // Grey Circle - some thing
}
/**
* Find something we have as explicit image data for the thing
* See also [[imagesOf]]
* @param thing The thing for which we want to find an image
* @returns The URL of a globe icon if thing equals `ns.foaf('Agent')`
* or `ns.rdf('Resource')`. Otherwise, it goes looking for
* triples in `store`,
* `(subject: thing), (predicate: see list below) (object: image-url)`
* to find any image linked from the thing with one of the following
* predicates (in order):
* * ns.sioc('avatar')
* * ns.foaf('img')
* * ns.vcard('logo')
* * ns.vcard('hasPhoto')
* * ns.vcard('photo')
* * ns.foaf('depiction')
*/
export function findImage (thing: NamedNode): string {
const kb = store
if (thing.sameTerm(ns.foaf('Agent')) || thing.sameTerm(ns.rdf('Resource'))) {
return lucideIcons.globe // was noun_98053.svg
}
const image =
kb.any(thing, ns.sioc('avatar')) ||
kb.any(thing, ns.foaf('img')) ||
kb.any(thing, ns.vcard('logo')) ||
kb.any(thing, ns.vcard('hasPhoto')) ||
kb.any(thing, ns.vcard('photo')) ||
kb.any(thing, ns.foaf('depiction'))
return image ? (image as any).uri : null as any
}
/**
* Do the best you can with the data available
*
* @return {Boolean} Are we happy with this icon?
* Sets src AND STYLE of the image.
*/
function trySetImage (element, thing, iconForClassMap) {
const kb = store
const explitImage = findImage(thing)
if (explitImage) {
element.setAttribute('src', explitImage)
return true
}
// This is one of the classes we know about - the class itself?
const typeIcon = iconForClassMap[thing.uri]
if (typeIcon) {
element.setAttribute('src', typeIcon)
element.style = style.classIconStyle
// element.style.border = '0.1em solid green;'
// element.style.backgroundColor = '#eeffee' // pale green
return true
}
const schemeIcon = findImageFromURI(thing)
if (schemeIcon) {
element.setAttribute('src', schemeIcon)
return true // happy with this -- don't look it up
}
// Do we have a generic icon for something in any class its in?
const types = kb.findTypeURIs(thing)
for (const typeURI in types) {
if (iconForClassMap[typeURI]) {
element.setAttribute('src', iconForClassMap[typeURI])
return false // maybe we can do better
}
}
element.setAttribute('src', lucideIcons.circle) // lucide circle (was noun_10636_grey.svg)
return false // we can do better
}
/**
* ToDo: Also add icons for *properties* like home, work, email, range, domain, comment,
*/
export function setImage (element: HTMLElement, thing: NamedNode) { // 20191230a
const kb = store
const iconForClassMap = {}
for (const k in iconForClass) {
const pref = k.split(':')[0]
const id = k.split(':')[1]
const theClass = ns[pref](id)
const iconRef = iconForClass[k]
/* Temporary hack for a new design icon */
if (iconRef.startsWith('data:')) {
iconForClassMap[theClass.uri] = iconRef
} else {
iconForClassMap[theClass.uri] = uri.join(iconRef, iconBase)
}
}
const happy = trySetImage(element, thing, iconForClassMap)
if (!happy && thing.uri) {
if (!kb.fetcher) {
throw new Error('kb has no fetcher')
}
kb.fetcher.nowOrWhenFetched(thing.doc(), undefined, (ok) => {
if (ok) {
trySetImage(element, thing, iconForClassMap)
}
})
}
}
// If a web page, then a favicon, with a fallback to ???
// See, e.g., http://stackoverflow.com/questions/980855/inputting-a-default-image
export function faviconOrDefault (dom: HTMLDocument, x: NamedNode) {
const image = dom.createElement('img')
;(image as any).style = style.iconStyle
const isOrigin = function (x) {
if (!x.uri) return false
const parts = x.uri.split('/')
return parts.length === 3 || (parts.length === 4 && parts[3] === '')
}
image.setAttribute(
'src',
isOrigin(x) ? lucideIcons.appWindow : lucideIcons.formInput // App vs document; were noun_15177.svg / noun_681601.svg
)
if (x.uri && x.uri.startsWith('https:') && x.uri.indexOf('#') < 0) {
const res = dom.createElement('object') // favico with a fallback of a default image if no favicon
res.setAttribute('data', tempSite(x) + 'favicon.ico')
res.setAttribute('type', 'image/x-icon')
res.appendChild(image) // fallback
return res
} else {
setImage(image, x)
return image
}
}
/* Two-option dialog pop-up
*/
function renderDeleteConfirmPopup (dom, refererenceElement, prompt, deleteFunction) {
function removePopup () {
refererenceElement.parentElement.removeChild(refererenceElement)
}
function removePopupAndDoDeletion () {
removePopup()
deleteFunction()
}
const popup = dom.createElement('div')
popup.style = style.confirmPopupStyle
popup.style.position = 'absolute'
popup.style.top = '-1em' // try leaving original button clear
popup.style.display = 'grid'
popup.style.gridTemplateColumns = 'auto auto'
const affirm = dom.createElement('div')
affirm.style.gridColumn = '1/2'
affirm.style.gridRow = '1' // @@ sigh; TS. could pass number in fact
const cancel = dom.createElement('div')
cancel.style.gridColumn = '1/2'
cancel.style.gridRow = '2'
const xButton = cancelButton(dom, removePopup)
popup.appendChild(xButton)
xButton.style.gridColumn = '1'
xButton.style.gridRow = '2'
const cancelPrompt = popup.appendChild(dom.createElement('button'))
cancelPrompt.style = style.buttonStyle
cancelPrompt.style.gridRow = '2'
cancelPrompt.style.gridColumn = '2'
cancelPrompt.textContent = 'Cancel' // @@ I18n
const affirmIcon = button(dom, lucideIcons.trash2, 'Delete it') // lucide trash-2 (was noun_925021.svg)
popup.appendChild(affirmIcon)
affirmIcon.style.gridRow = '1'
affirmIcon.style.gridColumn = '1'
const sureButtonElt = popup.appendChild(dom.createElement('button'))
sureButtonElt.style = style.buttonStyle
sureButtonElt.style.gridRow = '1'
sureButtonElt.style.gridColumn = '2'
sureButtonElt.textContent = prompt
popup.appendChild(sureButtonElt)
affirmIcon.addEventListener('click', removePopupAndDoDeletion)
sureButtonElt.addEventListener('click', removePopupAndDoDeletion)
// xButton.addEventListener('click', removePopup)
cancelPrompt.addEventListener('click', removePopup)
return popup
}
/**
* Delete button with a check you really mean it
* @@ Supress check if command key held down?
*/
export function deleteButtonWithCheck (
dom: HTMLDocument,
container: HTMLElement,
noun: string,
deleteFunction: () => any
) {
function createPopup () {
const refererenceElement = dom.createElement('div')
container.insertBefore(refererenceElement, deleteButton)
refererenceElement.style.position = 'relative' // Needed as reference for popup
refererenceElement.appendChild(renderDeleteConfirmPopup(dom, refererenceElement, prompt, deleteFunction))
}
const minusIconURI = lucideIcons.circleMinus // lucide circle-minus (was noun_2188_red.svg)
const deleteButton = dom.createElement('img')
deleteButton.setAttribute('src', minusIconURI)
deleteButton.setAttribute('style', style.smallButtonStyle) // @@tsc - would set deleteButton.style
deleteButton.style.float = 'right' // Historically this has alwaus floated right
const prompt = 'Remove this ' + noun
deleteButton.title = prompt
// @@ In an ideal world, make use of hover an accessibility option
deleteButton.classList.add('hoverControlHide')
deleteButton.addEventListener('click', createPopup)
container.classList.add('hoverControl')
container.appendChild(deleteButton)
deleteButton.setAttribute('data-testid', 'deleteButtonWithCheck')
return deleteButton // or button div? caller may change size of image
}
/* Make a button
*
* @param dom - the DOM document object
* @Param iconURI - the URI of the icon to use (if any)
* @param text - the tooltip text or possibly button contents text
* @param handler <function> - A handler to called when button is clicked
*
* @returns <dDomElement> - the button
*/
export function button (dom: HTMLDocument, iconURI: string | undefined, text: string,
handler?: (_event: any) => void,
options: ButtonWidgetOptions = { buttonColor: 'Primary', needsBorder: false }) {
if (iconURI) {
// Icon buttons stay as plain <button> elements (icon variant is handled elsewhere).
const button = dom.createElement('button')
button.setAttribute('type', 'button')
const img = button.appendChild(dom.createElement('img'))
img.setAttribute('src', iconURI)
img.setAttribute('style', 'width: 2em; height: 2em;') // trial and error. 2em disappears
img.title = text
button.setAttribute('style', style.buttonStyle)
if (handler) {
button.addEventListener('click', handler, false)
}
return button
}
// Text buttons now use the <solid-ui-button> design-system component.
// buttonColor 'Primary' (the default) -> variant="primary", 'Secondary' -> variant="secondary".
const button = dom.createElement('solid-ui-button')
button.setAttribute('type', 'button')
button.setAttribute(
'variant',
options.buttonColor === 'Secondary' ? 'secondary' : 'primary'
)
button.textContent = text.toLocaleUpperCase()
if (handler) {
button.addEventListener('click', handler, false)
}
return button
}
/* Make a cancel button
*
* @param dom - the DOM document object
* @param handler <function> - A handler to called when button is clicked
*
* @returns <dDomElement> - the button
*/
export function cancelButton (dom: HTMLDocument, handler: (_event?: any) => void) {
const b = button(dom, cancelIconURI, 'Cancel', handler)
if (b.firstChild) { // sigh for tsc
(b.firstChild as HTMLElement).style.opacity = '0.3' // Black X is too harsh: current language is grey X
}
return b
}
/* Make a continue button
*
* @param dom - the DOM document object
* @param handler <function> - A handler to called when button is clicked
*
* @returns <dDomElement> - the button
*/
export function continueButton (dom: HTMLDocument, handler: (_event: any) => void) {
return button(dom, checkIconURI, 'Continue', handler)
}
/* Grab a name for a new thing
*
* Form to get the name of a new thing before we create it
* @params theClass Misspelt to avoid clashing with the JavaScript keyword
* @returns: a promise of (a name or null if cancelled)
*/
export function askName (
dom: HTMLDocument,
kb: IndexedFormula,
container: HTMLDivElement,
predicate?: NamedNode,
theClass?: NamedNode,
noun?: string) {
return new Promise(function (resolve, _reject) {
const form = dom.createElement('div') // form is broken as HTML behaviour can resurface on js error
// classLabel = utils.label(ns.vcard('Individual'))
predicate = predicate || ns.foaf('name') // eg 'name' in user's language
noun = noun || (theClass ? utils.label(theClass) : ' ') // eg 'folder' in users's language
const prompt = noun + ' ' + utils.label(predicate) + ': '
form.appendChild(dom.createElement('p')).textContent = prompt
const namefield = dom.createElement('input')
namefield.setAttribute('type', 'text')
namefield.setAttribute('size', '100')
namefield.setAttribute('maxLength', '2048') // No arbitrary limits
namefield.setAttribute('style', style.textInputStyle)
namefield.select() // focus next user input
form.appendChild(namefield)
container.appendChild(form)
// namefield.focus()
function gotName () {
((form as HTMLElement).parentNode as HTMLElement).removeChild(form)
resolve(namefield.value.trim())
}
namefield.addEventListener('keyup', function (e) {
if (e.keyCode === 13) {
gotName()
}
}, false)
form.appendChild(dom.createElement('br'))
form.appendChild(cancelButton(dom, function (_event) {
((form as HTMLElement).parentNode as HTMLElement).removeChild(form)
resolve(null)
}))
form.appendChild(continueButton(dom, function (_event) {
gotName()
}))
namefield.focus()
}) // Promise
}
/**
* A TR to represent a draggable person, etc in a list
*
* pred is unused param at the moment
*/
export const personTR = renderAsRow // The legacy name is used in a lot of places
export function renderAsRow (dom: HTMLDocument, pred: NamedNode, obj: NamedNode, options: any): HTMLTableRowElement {
const tr = dom.createElement('tr')
options = options || {}
// tr.predObj = [pred.uri, obj.uri] moved to acl-control
const td1 = tr.appendChild(dom.createElement('td'))
const td2 = tr.appendChild(dom.createElement('td'))
const td3 = tr.appendChild(dom.createElement('td'))
// const image = td1.appendChild(dom.createElement('img'))
const image = options.image || faviconOrDefault(dom, obj)
td1.setAttribute('style', 'vertical-align: middle; width:2.5em; padding:0.5em; height: 2.5em;')
td2.setAttribute('style', 'vertical-align: middle; text-align:left;')
td3.setAttribute('style', 'vertical-align: middle; width:2em; padding:0.5em; height: 4em;')
td1.appendChild(image)
const nameLine = td2.appendChild(dom.createElement('div'))
const nameText = nameLine.appendChild(dom.createElement('span'))
if (options.title) {
nameText.textContent = options.title
} else {
setName(nameText, obj) // This is async
}
if (typeof options.renderNameSuffix === 'function') {
const inlineInfo = options.renderNameSuffix(obj, dom)
if (inlineInfo) {
const nameSuffix = nameLine.appendChild(dom.createElement('span'))
nameSuffix.setAttribute('style', 'margin-left: 0.4em; opacity: 0.8;')
if (typeof inlineInfo === 'string') {
nameSuffix.textContent = inlineInfo
} else {
nameSuffix.appendChild(inlineInfo)
}
}
}
if (typeof options.renderSupportingInfo === 'function') {
const supportingInfo = options.renderSupportingInfo(obj, dom)
if (supportingInfo) {
const supportingLine = td2.appendChild(dom.createElement('div'))
supportingLine.setAttribute('style', 'font-size: 90%; opacity: 0.8;')
if (typeof supportingInfo === 'string') {
supportingLine.textContent = supportingInfo
} else {
supportingLine.appendChild(supportingInfo)
}
}
}
if (options.deleteFunction) {
deleteButtonWithCheck(dom, td3, options.noun || 'one', options.deleteFunction)
}
if (obj.uri) {
// blank nodes need not apply
if (options.link !== false) {
const anchor = td3.appendChild(linkIcon(dom, obj))
anchor.classList.add('HoverControlHide')
td3.appendChild(dom.createElement('br'))
}
if (options.draggable !== false) {
// default is on
image.setAttribute('draggable', 'false') // Stop the image being dragged instead - just the TR
makeDraggable(tr, obj)
}
}
;(tr as any).subject = obj
return tr
}
/* A helper function for renderAsDiv
* creates the NameDiv for the person
* Note: could not move it to the helper file because they call exported functions
* from buttons
* @internal exporting this only for unit tests
*/
export function createNameDiv (dom: HTMLDocument, div: HTMLDivElement, title: string | undefined, obj: NamedNode) {
const nameDiv = div.appendChild(dom.createElement('div'))
if (title) {
nameDiv.textContent = title
} else {
setName(nameDiv, obj) // This is async
}
}
/* A helper function for renderAsDiv
* creates the linkDiv for the person
* Note: could not move it to the helper file because they call exported functions
* from buttons
* @internal exporting this only for unit tests
*/
export function createLinkDiv (dom: HTMLDocument, div: HTMLDivElement, obj: NamedNode, options: RenderAsDivOptions) {
const linkDiv = div.appendChild(dom.createElement('div'))
linkDiv.setAttribute('style', style.linkDivStyle)
if (options.deleteFunction) {
deleteButtonWithCheck(dom, linkDiv, options.noun || 'one', options.deleteFunction)
}
if (obj.uri) {
// blank nodes need not apply
if (options.link !== false) {
createLinkForURI(dom, linkDiv, obj)
}
makeDraggable(div, obj)
}
}
/**
* A Div to represent a draggable person, etc in a list
* configurable to add an onClick listener
*/
export function renderAsDiv (dom: HTMLDocument, obj: NamedNode, options: RenderAsDivOptions): HTMLElement {
const div = dom.createElement('div')
div.setAttribute('style', style.renderAsDivStyle)
options = options || {}
const image = options.image || faviconOrDefault(dom, obj)
createImageDiv(dom, div, image)
createNameDiv(dom, div, options.title, obj)
createLinkDiv(dom, div, obj, options)
if (options.clickable && options.onClickFunction) {
addClickListenerToElement(div, options.onClickFunction)
}
// to be compatible with the SolidOS table layout
if (options.wrapInATR) {
const tr = wrapDivInATR(dom, div, obj)
return tr
}
return div
}
/**
* Refresh a DOM tree recursively
*/
export function refreshTree (root: any): void {
if (root.refresh) {
root.refresh()
return
}
for (let i = 0; i < root.children.length; i++) {
refreshTree(root.children[i])
}
}
/**
* Options argument for [[attachmentList]] function
*/
/* The following options renderSupportingInfo and renderNameSuffix
were generated by AI Model: GPT-5.3-Codex */
/* Prompt: Add two options one renderSupportingInfo to allow the caller to add some additional information
to be displayed below the name. Also add renderNameSuffix to allow the caller to add pronouns or other suffixes to the name. */
export type attachmentListOptions = {
doc?: NamedNode
modify?: boolean
promptIcon?: string
predicate?: NamedNode
uploadFolder?: NamedNode
noun?: string
renderSupportingInfo?: RenderSupportingInfo
renderNameSuffix?: RenderNameSuffix
}
/**
* Component that displays a list of resources, for instance
* the attachments of a message, or the various documents related
* to a meeting.
* Accepts dropping URLs onto it to add attachments to it.
*/
export function attachmentList (dom: HTMLDocument, subject: NamedNode, div: HTMLElement, options: attachmentListOptions = {}) {
// options = options || {}
const docsWaitingForRowRefresh = new Set<string>()
const hasAsyncEnrichedRowOptions = !!(options.renderSupportingInfo || options.renderNameSuffix)
const deleteAttachment = function (target) {
if (!kb.updater) {
throw new Error('kb has no updater')
}
kb.updater.update(st(subject, predicate, target, doc) as any, [], function (
uri,
ok,
errorBody,
_xhr
) {
if (ok) {
refresh()
} else {
complain(undefined, 'Error deleting one: ' + errorBody)
}
})
}
function createNewRow (target) {
const theTarget = target
const opt: any = { noun }
opt.renderSupportingInfo = options.renderSupportingInfo
opt.renderNameSuffix = options.renderNameSuffix
if (hasAsyncEnrichedRowOptions && target?.uri && kb.fetcher) {
const targetDoc = target.doc()
const requestState = targetDoc?.uri ? kb.fetcher.requested?.[targetDoc.uri] : undefined
const shouldWaitForFetch = requestState !== 'done' && requestState !== 'failed'
if (targetDoc?.uri && shouldWaitForFetch && !docsWaitingForRowRefresh.has(targetDoc.uri)) {
docsWaitingForRowRefresh.add(targetDoc.uri)
// Root fix: these row options can depend on async profile data, so rerender once fetch completes.
kb.fetcher.nowOrWhenFetched(targetDoc, undefined, () => {
docsWaitingForRowRefresh.delete(targetDoc.uri)
refresh()
})
}
}
if (modify) {
opt.deleteFunction = function () {
deleteAttachment(theTarget)
}
}
return personTR(dom, predicate, target, opt)
}
const refresh = function () {
const things = kb.each(subject, predicate)
things.sort()
utils.syncTableToArray(
attachmentTable,
things,
createNewRow,
hasAsyncEnrichedRowOptions
? function (row, thing) {
// When row content depends on async profile data, recreate matched rows on refresh.
const replacement = createNewRow(thing)
return replacement
}
: undefined
)
}
function droppedURIHandler (uris) {
const ins: any = []
uris.forEach(function (u) {
const target = sym(u) // Attachment needs text label to disinguish I think not icon.
debug.log('Dropped on attachemnt ' + u) // icon was: iconBase + 'noun_25830.svg'
ins.push(st(subject, predicate, target, doc))
})
if (!kb.updater) {
throw new Error('kb has no updater')
}
kb.updater.update([], ins, function (uri, ok, errorBody, _xhr) {
if (ok) {
refresh()
} else {
complain(undefined, 'Error adding one: ' + errorBody)
}
})
}
function droppedFileHandler (files) {
uploadFiles(
kb.fetcher,
files,
options.uploadFolder?.uri, // Files
options.uploadFolder?.uri, // Pictures
function (theFile, destURI) {
const ins = [st(subject, predicate, kb.sym(destURI), doc)]
if (!kb.updater) {
throw new Error('kb has no updater')
}
kb.updater.update([], ins, function (uri, ok, errorBody, _xhr) {
if (ok) {
refresh()
} else {
complain(undefined, 'Error adding link to uploaded file: ' + errorBody)
}
})
}
)
}
const doc = options.doc || subject.doc()
if (options.modify === undefined) options.modify = true
const modify = options.modify
const promptIcon: string = options.promptIcon || (lucideIcons.target as string) // lucide target (was noun_748003.svg)
// const promptIcon = options.promptIcon || (iconBase + 'noun_25830.svg') // paperclip
const predicate = options.predicate || ns.wf('attachment')
const noun = options.noun || 'attachment'
const kb = store
const attachmentOuter = div.appendChild(dom.createElement('table'))
attachmentOuter.setAttribute('style', 'margin-top: 1em; margin-bottom: 1em;')
const attachmentOne = attachmentOuter.appendChild(dom.createElement('tr'))
const attachmentLeft = attachmentOne.appendChild(dom.createElement('td'))
const attachmentRight = attachmentOne.appendChild(dom.createElement('td'))
const attachmentTable = attachmentRight.appendChild(dom.createElement('table'))
attachmentTable.appendChild(dom.createElement('tr')) // attachmentTableTop
;(attachmentOuter as any).refresh = refresh // Participate in downstream changes
// ;(attachmentTable as any).refresh = refresh <- outer should be best?
refresh()
if (modify) {
// const buttonStyle = 'width; 2em; height: 2em; margin: 0.5em; padding: 0.1em;'
const paperclip = button(dom, promptIcon, 'Drop attachments here')
// paperclip.style = buttonStyle // @@ needed? default has white background
attachmentLeft.appendChild(paperclip)
const fhandler = options.uploadFolder ? droppedFileHandler : null
makeDropTarget(paperclip, droppedURIHandler, fhandler) // beware missing the wire of the paparclip!
const paperclipImage = paperclip.querySelector('img')
if (paperclipImage) {