forked from SolidOS/solid-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautocompleteBar.ts
More file actions
144 lines (123 loc) · 4.67 KB
/
Copy pathautocompleteBar.ts
File metadata and controls
144 lines (123 loc) · 4.67 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
// The Control with decorations
// import { ns, widgets, store } from 'solid-ui'
// import { ns, widgets, store, icons } from '../../../index'
/* eslint-disable no-console */
import * as ns from '../../../ns'
import { icons } from '../../../iconBase'
import { store } from '../../../logic'
import * as widgets from '../../../widgets'
import { renderAutoComplete, AutocompleteDecoration } from './autocompletePicker' // dbpediaParameters
import { NamedNode } from 'rdflib'
// import { wikidataParameters } from './publicData'
const WEBID_NOUN = 'Solid ID'
const GREEN_PLUS = icons.iconBase + 'noun_34653_green.svg'
const SEARCH_ICON = icons.iconBase + 'noun_Search_875351.svg'
const EDIT_ICON = icons.iconBase + 'noun_253504.svg'
export async function renderAutocompleteControl (dom:HTMLDocument,
person:NamedNode,
barOptions,
acOptions,
addOneIdAndRefresh): Promise<HTMLElement> {
async function autoCompleteDone (object, name) {
if (barOptions.permanent) { // remember to set this in publicid panel
setVisible(editButton, true)
setVisible(acceptButton, false)
setVisible(cancelButton, false)
} else {
console.log('temporary - removed decoratiion')
removeDecorated()
}
return addOneIdAndRefresh(object, name)
}
async function greenButtonHandler (_event) {
const webid = await widgets.askName(dom, store, creationArea, ns.vcard('url'), undefined, WEBID_NOUN)
if (!webid) {
return // cancelled by user
}
return addOneIdAndRefresh(person, webid)
}
function cancelButtonHandler (_event) {
removeDecorated()
if (barOptions.permanent) {
displayAutocomplete()
}
}
function removeDecorated () {
if (decoratedAutocomplete) {
creationArea.removeChild(decoratedAutocomplete)
decoratedAutocomplete = undefined
}
}
async function displayAutocomplete () {
decoratedAutocomplete = dom.createElement('div') as HTMLElement
decoratedAutocomplete.setAttribute('style', 'display: flex; flex-flow: wrap;')
decoratedAutocomplete.appendChild(await renderAutoComplete(dom, acOptions, decoration, autoCompleteDone))
decoratedAutocomplete.appendChild(acceptButton)
decoratedAutocomplete.appendChild(cancelButton)
creationArea.appendChild(decoratedAutocomplete)
}
async function searchButtonHandler (_event) {
if (decoratedAutocomplete) {
creationArea.removeChild(decoratedAutocomplete)
decoratedAutocomplete = undefined
} else {
displayAutocomplete()
}
}
async function droppedURIHandler (uris) {
for (const webid of uris) { // normally one but can be more than one
await addOneIdAndRefresh(person, webid)
}
}
// const queryParams = barOptions.queryParameters || wikidataParameters
const acceptButton = widgets.continueButton(dom)
const cancelButton = widgets.cancelButton(dom, removeDecorated) // @@ not in edit case only in temporary case
let editButton
let editing = true
function setVisible (element:HTMLElement, visible:boolean) {
element.style.visibility = visible ? 'visible' : 'collapse'
}
function syncEditingStatus () {
if (editing) {
setVisible(editButton, false)
setVisible(acceptButton, true)
setVisible(cancelButton, true)
} else {
setVisible(editButton, true)
setVisible(acceptButton, false)
setVisible(cancelButton, false)
}
}
const decoration:AutocompleteDecoration = {
acceptButton, cancelButton, editButton
}
let decoratedAutocomplete = undefined as HTMLElement | undefined
// const { dom } = dataBrowserContext
// barOptions = barOptions || {}
const creationArea = dom.createElement('div')
creationArea.setAttribute('style', 'display: flex; flex-flow: wrap;')
if (barOptions.editable) {
// creationArea.appendChild(await renderAutoComplete(dom, barOptions, autoCompleteDone)) wait for searchButton
creationArea.style.width = '100%'
if (barOptions.manualURIEntry) {
const plus = creationArea.appendChild(widgets.button(dom, GREEN_PLUS, barOptions.idNoun, greenButtonHandler))
widgets.makeDropTarget(plus, droppedURIHandler, undefined)
}
if (barOptions.dbLookup && !acOptions.currentObject && !barOptions.permanent) {
creationArea.appendChild(widgets.button(dom, SEARCH_ICON, barOptions.idNoun, searchButtonHandler))
}
if (barOptions.permanent && barOptions.editable) {
editButton = widgets.button(dom, EDIT_ICON, 'Edit', _event => {
editing = !editing
syncEditingStatus()
})
creationArea.appendChild(editButton)
}
}
if (barOptions.permanent || acOptions.currentObject) {
displayAutocomplete()
}
syncEditingStatus()
return creationArea
} // renderAutocompleteControl
// ends