Repository navigation
Expand file tree
/
Copy pathNodeTemplateMenu.vue
More file actions
321 lines (303 loc) · 11 KB
/
Copy pathNodeTemplateMenu.vue
File metadata and controls
321 lines (303 loc) · 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
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
<!--
- Copyright (c) 2026 OneLiteFeather
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-->
<script setup lang="ts">
import { computed, ref, useId, type PropType } from 'vue'
import MdAlert from '../../md/MdAlert.vue'
import MdButton from '../../md/MdButton.vue'
import MdListbox from '../../md/MdListbox.vue'
import MdTextField from '../../md/MdTextField.vue'
import { filterOptions, useCombobox } from '../../../helpers/useCombobox'
import { artifactIdProblem, groupIdProblem } from './artifactNodes'
import { isRepositorySegment, parseNode } from './nodeGrammar'
import { repositoryNames } from './nodeList'
import { applyTemplate, fillTemplate, needsArtifact, needsRepository, type ArtifactCoordinates } from './nodeTemplates'
import type { CatalogItem, NodeEntry, NodeTemplate } from './permissionNodesApi'
/*
* Adds the nodes of a template. The button opens a panel with the templates. A template that has a
* {repository} placeholder asks for the repository next (a text field with the repositories of the
* catalog as suggestions) and fills the placeholder. A template with {groupId} or {artifactId} asks
* for the groupId (required) and the artifactId (optional) in the same form, and an empty artifactId
* leaves out its colon. The instance admin template has no placeholder and is added at once. Every
* filled node is checked against the grammar before the form can add it, so the form never adds a
* node the server would refuse. Only the nodes that are not listed yet are added, all as Allow, and
* a status line says how many were added and how many were already there.
*/
const props = defineProps({
/** [{ id, label, nodes }] */
templates: {
type: Array as PropType<NodeTemplate[]>,
default: () => []
},
catalog: {
type: Array as PropType<CatalogItem[]>,
default: () => []
},
/** The nodes already listed (the draft). */
nodes: {
type: Array as PropType<NodeEntry[]>,
default: () => []
}
})
const emit = defineEmits(['apply'])
const uid = useId()
const panelId = `template-panel-${uid}`
const listboxId = `template-repositories-${uid}`
const open = ref(false)
const chosen = ref<NodeTemplate | null>(null)
const repository = ref('')
const groupId = ref('')
const artifactId = ref('')
const notice = ref('')
const trimmed = computed(() => repository.value.trim())
const group = computed(() => groupId.value.trim())
const artifact = computed(() => artifactId.value.trim())
const names = computed(() => repositoryNames(props.catalog))
// The suggestions are the repositories whose name contains the typed text, as a datalist filtered them.
const repositoryOptions = computed(() => filterOptions(names.value.map(value => ({ value })), trimmed.value))
const asksRepository = computed(() => chosen.value !== null && needsRepository(chosen.value))
const asksArtifact = computed(() => chosen.value !== null && needsArtifact(chosen.value))
const repositoryError = computed(() => {
if (trimmed.value === '') return ''
return isRepositorySegment(trimmed.value) ? '' : "Use letters, digits, '_' and '-', or '*'"
})
const groupError = computed(() => (asksArtifact.value && group.value !== '' ? groupIdProblem(group.value) : ''))
const artifactError = computed(() => (asksArtifact.value ? artifactIdProblem(artifact.value, group.value) : ''))
const coordinates = computed(() => ({ groupId: group.value, artifactId: artifact.value }))
const filled = computed(() => (chosen.value ? fillTemplate(chosen.value, trimmed.value, coordinates.value) : []))
// The first filled node the grammar refuses, once every field is filled in and valid.
const nodeError = computed(() => {
if (!chosen.value || repositoryError.value !== '' || groupError.value !== '' || artifactError.value !== '') return ''
for (const node of filled.value) {
const parsed = parseNode(node)
if (!parsed.valid) return parsed.problems[0].message
}
return ''
})
const fieldsReady = computed(() =>
(!asksRepository.value || (trimmed.value !== '' && repositoryError.value === '')) &&
(!asksArtifact.value || (group.value !== '' && groupError.value === '' && artifactError.value === '')))
const canApply = computed(() => chosen.value !== null && fieldsReady.value && nodeError.value === '')
const preview = computed(() => (canApply.value ? filled.value.join(', ') : ''))
const {
open: repositoryOpen,
active: repositoryActive,
visible: repositoryVisible,
inputAttrs: repositoryAttrs,
onInput: repositoryTyped,
onBlur: repositoryBlur,
onKeydown: repositoryKey,
choose: pickRepository
} = useCombobox({
id: listboxId,
options: () => repositoryOptions.value,
onChoose: value => {
repository.value = value
}
})
const typeRepository = (value: string) => {
repository.value = value
repositoryTyped()
}
const openRepositories = () => {
repositoryOpen.value = true
}
const onRepositoryKeydown = (event: KeyboardEvent) => {
if (repositoryKey(event.key)) event.preventDefault()
}
const close = () => {
open.value = false
chosen.value = null
repository.value = ''
groupId.value = ''
artifactId.value = ''
}
const toggle = () => {
if (open.value) close()
else open.value = true
}
const apply = (template: NodeTemplate | null, repositoryName: string, coordinatesOf: ArtifactCoordinates) => {
const { added, skipped } = applyTemplate(template, repositoryName, props.nodes, coordinatesOf)
if (added.length > 0) emit('apply', added)
const parts = [`Added ${added.length} ${added.length === 1 ? 'node' : 'nodes'}`]
if (skipped.length > 0) parts.push(`${skipped.length} already listed`)
notice.value = parts.join(', ')
close()
}
const choose = (template: NodeTemplate) => {
if (needsRepository(template) || needsArtifact(template)) {
chosen.value = template
repository.value = ''
groupId.value = ''
artifactId.value = ''
notice.value = ''
} else {
apply(template, '', {})
}
}
const submit = () => {
if (chosen.value && canApply.value) apply(chosen.value, trimmed.value, coordinates.value)
}
</script>
<template>
<div
class="flex flex-col gap-2"
data-ingot-component="node-template-menu"
>
<div class="flex flex-wrap items-center gap-3">
<MdButton
variant="tonal"
:aria-expanded="open ? 'true' : 'false'"
:aria-controls="panelId"
data-test="template-toggle"
@click="toggle"
>
Add from template
</MdButton>
<p
role="status"
class="md-body-medium text-on-surface-variant"
data-test="template-notice"
>
{{ notice }}
</p>
</div>
<div
v-if="open"
:id="panelId"
class="flex flex-col gap-3 rounded-corner-medium bg-surface-container-low p-4"
data-test="template-panel"
>
<ul
v-if="!chosen"
class="flex flex-col"
aria-label="Templates"
>
<li
v-for="template in templates"
:key="template.id"
>
<button
type="button"
class="md-state-layer md-focus-ring md-touch-target relative flex min-h-10 w-full flex-col items-start justify-center rounded-corner-small px-3 py-2 text-left"
data-test="template-option"
@click="choose(template)"
>
<span class="md-label-large text-on-surface">{{ template.label }}</span>
<span class="break-all md-code-small text-on-surface-variant">{{ template.nodes.join(', ') }}</span>
</button>
</li>
<li
v-if="templates.length === 0"
class="px-3 py-2 md-body-medium text-on-surface-variant"
>
No templates are available.
</li>
</ul>
<form
v-else
class="flex flex-col gap-3"
@submit.prevent="submit"
>
<p class="md-title-small text-on-surface">
{{ chosen.label }}
</p>
<template v-if="asksRepository">
<div class="relative min-w-0">
<MdTextField
v-bind="repositoryAttrs"
:model-value="repository"
label="Repository"
variant="outlined"
autocomplete="off"
spellcheck="false"
:error="repositoryError !== ''"
:error-text="repositoryError"
:supporting-text="asksArtifact ? 'Choose a repository or type its name' : (preview ? `Adds ${preview}` : 'Choose a repository or type its name')"
data-test="template-repository"
@update:model-value="typeRepository"
@keydown="onRepositoryKeydown"
@focus="openRepositories"
@blur="repositoryBlur"
/>
<MdListbox
:id="listboxId"
class="absolute inset-x-0 top-14"
label="Repository suggestions"
:options="repositoryOptions"
:active="repositoryActive"
:open="repositoryVisible"
@choose="pickRepository"
/>
</div>
</template>
<template v-if="asksArtifact">
<MdTextField
v-model="groupId"
label="groupId"
variant="outlined"
autocomplete="off"
spellcheck="false"
:error="groupError !== ''"
:error-text="groupError"
supporting-text="Required, for example net.onelitefeather. The groupId covers its subgroups"
data-test="template-group"
/>
<MdTextField
v-model="artifactId"
label="artifactId (optional)"
variant="outlined"
autocomplete="off"
spellcheck="false"
:error="artifactError !== ''"
:error-text="artifactError"
supporting-text="Leave empty for every artifact of the groupId"
data-test="template-artifact"
/>
</template>
<p
v-if="preview"
class="break-all md-code-small text-on-surface-variant"
data-test="template-preview"
>
Adds {{ preview }}
</p>
<MdAlert
v-if="nodeError"
tone="error"
data-test="template-problem"
>
{{ nodeError }}
</MdAlert>
<div class="flex justify-end gap-2">
<MdButton
variant="text"
@click="close"
>
Cancel
</MdButton>
<MdButton
variant="tonal"
type="submit"
:disabled="!canApply"
data-test="template-apply"
>
Add nodes
</MdButton>
</div>
</form>
</div>
</div>
</template>