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
146 lines (123 loc) · 5.11 KB
/
Copy pathautocompleteBar.ts
File metadata and controls
146 lines (123 loc) · 5.11 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
/* The Autocomplete Control with decorations
This control has the buttons which control the state between editing, viewing, searching, accepting
and so on. See the state diagram in the documentation. The AUtocomplete Picker does the main work.
*/
import * as ns from '../../../ns'
import { icons } from '../../../iconBase'
import { store } from 'solid-logic'
import * as widgets from '../../../widgets'
import * as utils from '../../../utils'
import { renderAutoComplete, AutocompleteDecoration, setVisible } from './autocompletePicker' // dbpediaParameters
import { NamedNode } from 'rdflib'
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'
// const DELETE_ICON = icons.iconBase + 'noun_2188_red.svg'
export async function renderAutocompleteControl (dom:HTMLDocument,
person:NamedNode,
barOptions,
acOptions,
addOneIdAndRefresh,
deleteOne): Promise<HTMLElement> {
async function autoCompleteDone (object, name) {
if (acOptions.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 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))
// console.log('@@ acceptButton', acceptButton)
decoratedAutocomplete.appendChild(acceptButton)
// console.log('@@ cancelButton', cancelButton)
decoratedAutocomplete.appendChild(cancelButton)
// console.log('@@ editButton', editButton)
decoratedAutocomplete.appendChild(editButton)
// console.log('@@ deleteButtonContainer', deleteButtonContainer)
decoratedAutocomplete.appendChild(deleteButtonContainer)
creationArea.appendChild(decoratedAutocomplete)
}
async function searchButtonHandler (_event) {
if (decoratedAutocomplete) {
creationArea.removeChild(decoratedAutocomplete)
decoratedAutocomplete = undefined
} else {
await displayAutocomplete()
}
}
async function droppedURIHandler (uris) {
for (const webid of uris) { // normally one but can be more than one
await addOneIdAndRefresh(person, webid)
}
}
const acceptButton = widgets.continueButton(dom)
acceptButton.setAttribute('data-testid', 'accept-button')
const cancelButton = widgets.cancelButton(dom)
cancelButton.setAttribute('data-testid', 'cancel-button')
const deleteButtonContainer = dom.createElement('div')
const noun = acOptions.targetClass ? utils.label(acOptions.targetClass) : 'item'
const deleteButton = widgets.deleteButtonWithCheck(dom, deleteButtonContainer, noun, deleteOne) // need to knock out this UI or caller does that
deleteButton.setAttribute('data-testid', 'delete-button')
const editButton = widgets.button(dom, EDIT_ICON, 'Edit', _event => {
editing = !editing
syncEditingStatus()
})
editButton.setAttribute('data-testid', 'edit-button')
let editing = true
function syncEditingStatus () {
if (editing) {
setVisible(editButton, false)
setVisible(acceptButton, false) // not till got it
setVisible(cancelButton, false)
} else {
setVisible(editButton, true)
setVisible(acceptButton, false)
setVisible(cancelButton, false)
}
}
const decoration:AutocompleteDecoration = {
acceptButton, cancelButton, editButton, deleteButton
}
let decoratedAutocomplete = undefined as HTMLElement | undefined
const creationArea = dom.createElement('div')
creationArea.style.display = 'flex'
creationArea.style.flexDirection = 'row'
if (acOptions.permanent || acOptions.currentObject) {
await displayAutocomplete()
}
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 && !acOptions.permanent) {
creationArea.appendChild(widgets.button(dom, SEARCH_ICON, barOptions.idNoun, searchButtonHandler))
}
}
syncEditingStatus()
return creationArea
} // renderAutocompleteControl
// ends