@stsdti/auditing-vue 0.0.0-stage → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +197 -2
- package/dist/auditing-vue.mjs +2278 -0
- package/dist/auditing-vue.mjs.map +1 -0
- package/package.json +68 -4
- package/scripts/vendor-funky-styles.mjs +135 -0
- package/src/components/audit/AuditChanges.vue +181 -0
- package/src/components/audit/AuditChangesDialog.vue +106 -0
- package/src/components/audit/AuditEventBadge.vue +51 -0
- package/src/components/audit/AuditFilterSelect.vue +303 -0
- package/src/components/audit/AuditFilters.vue +164 -0
- package/src/components/audit/AuditHistory.vue +133 -0
- package/src/components/audit/AuditHistoryButton.vue +99 -0
- package/src/components/audit/AuditTable.vue +622 -0
- package/src/components/audit/AuditTimeline.vue +311 -0
- package/src/components/ui/UiDialog.vue +166 -0
- package/src/components/ui/UiLoadingBlur.vue +29 -0
- package/src/index.js +61 -0
- package/src/shared.js +95 -0
- package/src/styles/base.css +104 -0
- package/src/styles/generated/style-nova.css +256 -0
- package/src/styles/generated/style-rhea.css +256 -0
- package/src/styles/generated/tokens.css +288 -0
- package/src/styles/nova.css +8 -0
- package/src/styles/rhea.css +8 -0
- package/src/themes/nova.css +2 -0
- package/src/themes/rhea.css +2 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div :class="cn('laravel-auditing', ui?.root)" data-slot="audit-changes">
|
|
3
|
+
<div class="flex flex-col gap-1.5">
|
|
4
|
+
<ul
|
|
5
|
+
v-if="rows.length && layout === 'list'"
|
|
6
|
+
data-slot="timeline-changes"
|
|
7
|
+
:class="cn('flex flex-col gap-1 rounded-item bg-muted/50 px-2.5 py-2 text-xs', ui?.list)"
|
|
8
|
+
>
|
|
9
|
+
<li
|
|
10
|
+
v-for="row in rows"
|
|
11
|
+
:key="row.field"
|
|
12
|
+
class="flex flex-wrap items-baseline gap-x-1.5 gap-y-0.5"
|
|
13
|
+
>
|
|
14
|
+
<span class="font-medium text-foreground" :title="row.field">
|
|
15
|
+
{{ fieldLabel(row.field, value) }}:
|
|
16
|
+
</span>
|
|
17
|
+
<template v-if="showOld && showNew">
|
|
18
|
+
<span class="whitespace-pre-wrap text-muted-foreground line-through decoration-muted-foreground/50 wrap-anywhere" :title="row.oldValue">
|
|
19
|
+
<slot name="value" v-bind="scope(row, 'old')">
|
|
20
|
+
{{ clip(row.oldValue, maxLength) || '—' }}
|
|
21
|
+
</slot>
|
|
22
|
+
</span>
|
|
23
|
+
<arrow-right-icon class="cn-rtl-flip size-3 shrink-0 self-center text-muted-foreground" aria-hidden="true" />
|
|
24
|
+
<span class="sr-only">
|
|
25
|
+
{{ texts.becomes }}
|
|
26
|
+
</span>
|
|
27
|
+
<span class="whitespace-pre-wrap text-foreground wrap-anywhere" :title="row.newValue">
|
|
28
|
+
<slot name="value" v-bind="scope(row, 'new')">
|
|
29
|
+
{{ clip(row.newValue, maxLength) || '—' }}
|
|
30
|
+
</slot>
|
|
31
|
+
</span>
|
|
32
|
+
</template>
|
|
33
|
+
<span
|
|
34
|
+
v-else-if="showNew"
|
|
35
|
+
class="whitespace-pre-wrap text-foreground wrap-anywhere"
|
|
36
|
+
:title="row.newValue"
|
|
37
|
+
>
|
|
38
|
+
<slot name="value" v-bind="scope(row, 'new')">
|
|
39
|
+
{{ clip(row.newValue, maxLength) || '—' }}
|
|
40
|
+
</slot>
|
|
41
|
+
</span>
|
|
42
|
+
<span
|
|
43
|
+
v-else
|
|
44
|
+
class="whitespace-pre-wrap text-muted-foreground line-through decoration-muted-foreground/50 wrap-anywhere"
|
|
45
|
+
:title="row.oldValue"
|
|
46
|
+
>
|
|
47
|
+
<slot name="value" v-bind="scope(row, 'old')">
|
|
48
|
+
{{ clip(row.oldValue, maxLength) || '—' }}
|
|
49
|
+
</slot>
|
|
50
|
+
</span>
|
|
51
|
+
</li>
|
|
52
|
+
</ul>
|
|
53
|
+
|
|
54
|
+
<div
|
|
55
|
+
v-else-if="rows.length"
|
|
56
|
+
data-slot="table-container"
|
|
57
|
+
:class="cn('cn-table-container overflow-hidden rounded-item border', ui?.table)"
|
|
58
|
+
>
|
|
59
|
+
<table data-slot="table" :class="cn('cn-table table-fixed', compact ? 'text-xs' : 'text-sm')">
|
|
60
|
+
<thead data-slot="table-header" class="cn-table-header bg-muted/50">
|
|
61
|
+
<tr data-slot="table-row" class="cn-table-row hover:bg-transparent">
|
|
62
|
+
<th data-slot="table-head" :class="cn('cn-table-head font-medium text-muted-foreground', compact ? 'h-8 w-36 px-2.5 text-xs' : 'h-10 w-1/3 px-3')">
|
|
63
|
+
{{ texts.field }}
|
|
64
|
+
</th>
|
|
65
|
+
<th
|
|
66
|
+
v-if="showOld"
|
|
67
|
+
data-slot="table-head"
|
|
68
|
+
:class="cn('cn-table-head font-medium text-muted-foreground', compact ? 'h-8 px-2.5 text-xs' : 'h-10 px-3')"
|
|
69
|
+
>
|
|
70
|
+
{{ texts.oldValue }}
|
|
71
|
+
</th>
|
|
72
|
+
<th
|
|
73
|
+
v-if="showNew"
|
|
74
|
+
data-slot="table-head"
|
|
75
|
+
:class="cn('cn-table-head font-medium text-muted-foreground', compact ? 'h-8 px-2.5 text-xs' : 'h-10 px-3')"
|
|
76
|
+
>
|
|
77
|
+
{{ texts.newValue }}
|
|
78
|
+
</th>
|
|
79
|
+
</tr>
|
|
80
|
+
</thead>
|
|
81
|
+
<tbody data-slot="table-body" class="cn-table-body">
|
|
82
|
+
<tr
|
|
83
|
+
v-for="row in rows"
|
|
84
|
+
:key="row.field"
|
|
85
|
+
data-slot="table-row"
|
|
86
|
+
class="cn-table-row hover:bg-transparent"
|
|
87
|
+
>
|
|
88
|
+
<td
|
|
89
|
+
data-slot="table-cell"
|
|
90
|
+
:class="cn('cn-table-cell align-top font-medium whitespace-normal text-foreground wrap-anywhere', cell)"
|
|
91
|
+
:title="row.field"
|
|
92
|
+
>
|
|
93
|
+
{{ fieldLabel(row.field, value) }}
|
|
94
|
+
</td>
|
|
95
|
+
<td
|
|
96
|
+
v-if="showOld"
|
|
97
|
+
data-slot="table-cell"
|
|
98
|
+
:class="cn('cn-table-cell align-top text-muted-foreground wrap-anywhere', cell)"
|
|
99
|
+
:title="row.oldValue"
|
|
100
|
+
>
|
|
101
|
+
<slot name="value" v-bind="scope(row, 'old')">
|
|
102
|
+
<span :class="cn('whitespace-pre-wrap', row.oldValue && 'line-through decoration-muted-foreground/50')" v-text="clip(row.oldValue, maxLength) || '—'" />
|
|
103
|
+
</slot>
|
|
104
|
+
</td>
|
|
105
|
+
<td
|
|
106
|
+
v-if="showNew"
|
|
107
|
+
data-slot="table-cell"
|
|
108
|
+
:class="cn('cn-table-cell align-top text-foreground wrap-anywhere', cell)"
|
|
109
|
+
:title="row.newValue"
|
|
110
|
+
>
|
|
111
|
+
<slot name="value" v-bind="scope(row, 'new')">
|
|
112
|
+
<span class="whitespace-pre-wrap" v-text="clip(row.newValue, maxLength) || '—'" />
|
|
113
|
+
</slot>
|
|
114
|
+
</td>
|
|
115
|
+
</tr>
|
|
116
|
+
</tbody>
|
|
117
|
+
</table>
|
|
118
|
+
</div>
|
|
119
|
+
|
|
120
|
+
<p v-else class="text-xs text-muted-foreground">
|
|
121
|
+
{{ texts.noChanges }}
|
|
122
|
+
</p>
|
|
123
|
+
|
|
124
|
+
<ui-button
|
|
125
|
+
v-if="rows.length < changes.length"
|
|
126
|
+
variant="link"
|
|
127
|
+
size="xs"
|
|
128
|
+
class="self-start px-0"
|
|
129
|
+
@click="emits('expand', value)"
|
|
130
|
+
>
|
|
131
|
+
{{ formatMessage(texts.showAllChanges, { count: changes.length }) }}
|
|
132
|
+
<arrow-right-icon
|
|
133
|
+
class="cn-rtl-flip"
|
|
134
|
+
data-icon="inline-end"
|
|
135
|
+
aria-hidden="true"
|
|
136
|
+
/>
|
|
137
|
+
</ui-button>
|
|
138
|
+
</div>
|
|
139
|
+
</div>
|
|
140
|
+
</template>
|
|
141
|
+
|
|
142
|
+
<script setup>
|
|
143
|
+
import { ArrowRightIcon } from '@lucide/vue'
|
|
144
|
+
import { auditChanges, clip, formatValue as defaultFormatValue, formatMessage, showsNewValues, showsOldValues } from '@stsdti/auditing-core'
|
|
145
|
+
import { computed } from 'vue'
|
|
146
|
+
import { cn, UiButton, useMessages } from '../../shared.js'
|
|
147
|
+
|
|
148
|
+
const props = defineProps({
|
|
149
|
+
value: { type: Object, required: true },
|
|
150
|
+
layout: { type: String, default: 'table' },
|
|
151
|
+
size: { type: String, default: 'default' },
|
|
152
|
+
limit: { type: Number, default: null },
|
|
153
|
+
maxLength: { type: Number, default: null },
|
|
154
|
+
formatValue: { type: Function, default: defaultFormatValue },
|
|
155
|
+
fieldLabel: { type: Function, default: (field) => field },
|
|
156
|
+
messages: { type: Object, default: null },
|
|
157
|
+
ui: { type: Object, default: null }
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
const emits = defineEmits(['expand'])
|
|
161
|
+
|
|
162
|
+
const texts = useMessages(props.messages)
|
|
163
|
+
|
|
164
|
+
const changes = computed(() => auditChanges(props.value, { format: props.formatValue }))
|
|
165
|
+
const rows = computed(() => (props.limit === null ? changes.value : changes.value.slice(0, props.limit)))
|
|
166
|
+
const showOld = computed(() => showsOldValues(props.value))
|
|
167
|
+
const showNew = computed(() => showsNewValues(props.value))
|
|
168
|
+
const compact = computed(() => props.size === 'sm')
|
|
169
|
+
const cell = computed(() => (compact.value ? 'px-2.5 py-1.5' : 'px-3 py-2.5'))
|
|
170
|
+
|
|
171
|
+
function scope(row, side) {
|
|
172
|
+
const raw = side === 'old' ? props.value.old_values : props.value.new_values
|
|
173
|
+
return {
|
|
174
|
+
audit: props.value,
|
|
175
|
+
field: row.field,
|
|
176
|
+
side,
|
|
177
|
+
value: raw?.[row.field] ?? null,
|
|
178
|
+
text: clip(side === 'old' ? row.oldValue : row.newValue, props.maxLength) || '—'
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
</script>
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<ui-dialog
|
|
3
|
+
v-model:open="open"
|
|
4
|
+
width="sm:max-w-2xl"
|
|
5
|
+
:close-label="texts.close"
|
|
6
|
+
:ui="ui"
|
|
7
|
+
:title="audit ? formatMessage(texts.changesTitle, { count: changeCount }) : ''"
|
|
8
|
+
>
|
|
9
|
+
<template #description>
|
|
10
|
+
<span v-if="audit" class="flex flex-wrap items-center gap-x-2 gap-y-1">
|
|
11
|
+
<audit-event-badge :event="audit.event" :messages="messages" />
|
|
12
|
+
<span class="font-medium text-foreground">
|
|
13
|
+
{{ typeLabel(audit.auditable_type) }}
|
|
14
|
+
</span>
|
|
15
|
+
<span class="tabular-nums">
|
|
16
|
+
#{{ audit.auditable_id }}
|
|
17
|
+
</span>
|
|
18
|
+
<span aria-hidden="true">
|
|
19
|
+
·
|
|
20
|
+
</span>
|
|
21
|
+
<span>
|
|
22
|
+
{{ audit.user ? userName(audit.user, userLabel) : texts.system }}
|
|
23
|
+
</span>
|
|
24
|
+
<span aria-hidden="true">
|
|
25
|
+
·
|
|
26
|
+
</span>
|
|
27
|
+
<time :datetime="audit.created_at" class="tabular-nums">
|
|
28
|
+
{{ formatDateTime(audit.created_at) }}
|
|
29
|
+
</time>
|
|
30
|
+
</span>
|
|
31
|
+
</template>
|
|
32
|
+
|
|
33
|
+
<div v-if="audit" class="flex flex-col gap-4">
|
|
34
|
+
<audit-changes
|
|
35
|
+
layout="table"
|
|
36
|
+
:value="audit"
|
|
37
|
+
:ui="{ table: ui?.changes }"
|
|
38
|
+
:format-value="formatValue"
|
|
39
|
+
:field-label="fieldLabel"
|
|
40
|
+
:messages="messages"
|
|
41
|
+
/>
|
|
42
|
+
|
|
43
|
+
<dl
|
|
44
|
+
v-if="details.length"
|
|
45
|
+
data-slot="audit-context"
|
|
46
|
+
:class="cn('grid grid-cols-[auto_1fr] gap-x-4 gap-y-1 rounded-item bg-muted/50 px-2.5 py-2 text-xs', ui?.details)"
|
|
47
|
+
>
|
|
48
|
+
<template v-for="detail in details" :key="detail.label">
|
|
49
|
+
<dt class="text-muted-foreground">
|
|
50
|
+
{{ detail.label }}
|
|
51
|
+
</dt>
|
|
52
|
+
<dd class="font-mono wrap-anywhere">
|
|
53
|
+
{{ detail.value }}
|
|
54
|
+
</dd>
|
|
55
|
+
</template>
|
|
56
|
+
</dl>
|
|
57
|
+
|
|
58
|
+
<slot :audit="audit" />
|
|
59
|
+
</div>
|
|
60
|
+
|
|
61
|
+
<template #footer>
|
|
62
|
+
<slot name="footer" :audit="audit" />
|
|
63
|
+
<ui-button variant="outline" @click="open = false">
|
|
64
|
+
{{ texts.close }}
|
|
65
|
+
</ui-button>
|
|
66
|
+
</template>
|
|
67
|
+
</ui-dialog>
|
|
68
|
+
</template>
|
|
69
|
+
|
|
70
|
+
<script setup>
|
|
71
|
+
import { auditChanges, formatValue as defaultFormatValue, formatDateTime, formatMessage, shortClassName, userName } from '@stsdti/auditing-core'
|
|
72
|
+
import { computed } from 'vue'
|
|
73
|
+
import { cn, UiButton, useMessages } from '../../shared.js'
|
|
74
|
+
import UiDialog from '../ui/UiDialog.vue'
|
|
75
|
+
import AuditChanges from './AuditChanges.vue'
|
|
76
|
+
import AuditEventBadge from './AuditEventBadge.vue'
|
|
77
|
+
|
|
78
|
+
const props = defineProps({
|
|
79
|
+
audit: { type: Object, default: null },
|
|
80
|
+
typeLabel: { type: Function, default: shortClassName },
|
|
81
|
+
userLabel: { type: [String, Function], default: 'name' },
|
|
82
|
+
formatValue: { type: Function, default: defaultFormatValue },
|
|
83
|
+
fieldLabel: { type: Function, default: (field) => field },
|
|
84
|
+
showContext: { type: Boolean, default: true },
|
|
85
|
+
messages: { type: Object, default: null },
|
|
86
|
+
ui: { type: Object, default: null }
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
const open = defineModel('open', { type: Boolean, default: false })
|
|
90
|
+
|
|
91
|
+
const texts = useMessages(props.messages)
|
|
92
|
+
|
|
93
|
+
const changeCount = computed(() => auditChanges(props.audit).length)
|
|
94
|
+
|
|
95
|
+
const details = computed(() => {
|
|
96
|
+
if (!props.showContext || !props.audit) {
|
|
97
|
+
return []
|
|
98
|
+
}
|
|
99
|
+
return [
|
|
100
|
+
{ label: texts.contextUrl, value: props.audit.url },
|
|
101
|
+
{ label: texts.contextIp, value: props.audit.ip_address },
|
|
102
|
+
{ label: texts.contextUserAgent, value: props.audit.user_agent },
|
|
103
|
+
{ label: texts.contextBatch, value: props.audit.batch }
|
|
104
|
+
].filter((detail) => detail.value)
|
|
105
|
+
})
|
|
106
|
+
</script>
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<span
|
|
3
|
+
:class="cn('laravel-auditing', ui?.root)"
|
|
4
|
+
data-slot="audit-event-badge"
|
|
5
|
+
:data-event="event"
|
|
6
|
+
>
|
|
7
|
+
<ui-badge
|
|
8
|
+
v-if="iconOnly"
|
|
9
|
+
:variant="variant ?? toneVariant[meta.tone]"
|
|
10
|
+
:class="cn('h-6 min-w-6 px-0 [&>svg]:size-3.5!', ui?.badge)"
|
|
11
|
+
:title="text"
|
|
12
|
+
role="img"
|
|
13
|
+
:aria-label="text"
|
|
14
|
+
>
|
|
15
|
+
<component :is="eventIcon(meta.glyph)" aria-hidden="true" />
|
|
16
|
+
</ui-badge>
|
|
17
|
+
<ui-badge
|
|
18
|
+
v-else
|
|
19
|
+
:variant="variant ?? toneVariant[meta.tone]"
|
|
20
|
+
:class="ui?.badge"
|
|
21
|
+
>
|
|
22
|
+
<component
|
|
23
|
+
:is="eventIcon(meta.glyph)"
|
|
24
|
+
v-if="icon"
|
|
25
|
+
data-icon="inline-start"
|
|
26
|
+
aria-hidden="true"
|
|
27
|
+
/>
|
|
28
|
+
{{ text }}
|
|
29
|
+
</ui-badge>
|
|
30
|
+
</span>
|
|
31
|
+
</template>
|
|
32
|
+
|
|
33
|
+
<script setup>
|
|
34
|
+
import { auditEvent, eventLabel } from '@stsdti/auditing-core'
|
|
35
|
+
import { computed } from 'vue'
|
|
36
|
+
import { cn, eventIcon, toneVariant, UiBadge, useMessages } from '../../shared.js'
|
|
37
|
+
|
|
38
|
+
const props = defineProps({
|
|
39
|
+
event: { type: String, required: true },
|
|
40
|
+
label: { type: String, default: null },
|
|
41
|
+
variant: { type: String, default: null },
|
|
42
|
+
icon: { type: Boolean, default: true },
|
|
43
|
+
iconOnly: { type: Boolean, default: false },
|
|
44
|
+
messages: { type: Object, default: null },
|
|
45
|
+
ui: { type: Object, default: null }
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
const texts = useMessages(props.messages)
|
|
49
|
+
const meta = computed(() => auditEvent(props.event))
|
|
50
|
+
const text = computed(() => props.label ?? eventLabel(texts, props.event))
|
|
51
|
+
</script>
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div :class="cn('laravel-auditing', ui?.root)" data-slot="audit-filter-select">
|
|
3
|
+
<div
|
|
4
|
+
ref="root"
|
|
5
|
+
class="relative w-full"
|
|
6
|
+
@keydown.esc="onEscape"
|
|
7
|
+
>
|
|
8
|
+
<div
|
|
9
|
+
ref="trigger"
|
|
10
|
+
role="combobox"
|
|
11
|
+
tabindex="0"
|
|
12
|
+
:aria-expanded="open"
|
|
13
|
+
:aria-controls="panelId"
|
|
14
|
+
:aria-label="label"
|
|
15
|
+
data-slot="combobox-chips"
|
|
16
|
+
:class="cn('cn-combobox-chips cursor-pointer pr-8', ui?.trigger)"
|
|
17
|
+
@click="toggle"
|
|
18
|
+
@keydown.enter.prevent="toggle"
|
|
19
|
+
@keydown.space.prevent="toggle"
|
|
20
|
+
@keydown.down.prevent="openAndFocus"
|
|
21
|
+
>
|
|
22
|
+
<span v-if="!model.length" class="truncate text-muted-foreground">
|
|
23
|
+
{{ placeholder || texts.allPlaceholder }}
|
|
24
|
+
</span>
|
|
25
|
+
<span
|
|
26
|
+
v-for="item in model"
|
|
27
|
+
:key="keyOf(item)"
|
|
28
|
+
data-slot="combobox-chip"
|
|
29
|
+
class="cn-combobox-chip max-w-48"
|
|
30
|
+
>
|
|
31
|
+
<span class="truncate">
|
|
32
|
+
{{ labelOf(item) }}
|
|
33
|
+
</span>
|
|
34
|
+
<button
|
|
35
|
+
type="button"
|
|
36
|
+
data-slot="combobox-chip-remove"
|
|
37
|
+
class="cn-combobox-chip-remove inline-flex size-4 items-center justify-center rounded-item focus-ring"
|
|
38
|
+
:aria-label="formatMessage(texts.removeFilter, { filter: labelOf(item) })"
|
|
39
|
+
@click.stop="select(item)"
|
|
40
|
+
>
|
|
41
|
+
<x-icon class="size-3" aria-hidden="true" />
|
|
42
|
+
</button>
|
|
43
|
+
</span>
|
|
44
|
+
<chevron-down-icon class="cn-combobox-trigger-icon pointer-events-none absolute top-1/2 right-2.5 -translate-y-1/2" aria-hidden="true" />
|
|
45
|
+
</div>
|
|
46
|
+
|
|
47
|
+
<div
|
|
48
|
+
v-if="open"
|
|
49
|
+
:id="panelId"
|
|
50
|
+
data-slot="combobox-content"
|
|
51
|
+
data-state="open"
|
|
52
|
+
data-side="bottom"
|
|
53
|
+
:data-empty="!loading && !items.length ? '' : undefined"
|
|
54
|
+
:style="{ '--available-height': `${availableHeight}px` }"
|
|
55
|
+
:class="cn('cn-combobox-content cn-menu-target group/combobox-content absolute top-full left-0 z-50 mt-1 w-full', ui?.content)"
|
|
56
|
+
>
|
|
57
|
+
<div
|
|
58
|
+
v-if="searchable"
|
|
59
|
+
data-slot="input-group"
|
|
60
|
+
role="group"
|
|
61
|
+
class="group/input-group cn-input-group relative flex min-w-0 items-center outline-none"
|
|
62
|
+
>
|
|
63
|
+
<input
|
|
64
|
+
ref="searchInput"
|
|
65
|
+
v-model="query"
|
|
66
|
+
type="text"
|
|
67
|
+
data-slot="input-group-control"
|
|
68
|
+
:placeholder="texts.selectSearch"
|
|
69
|
+
class="cn-input-group-input flex-1"
|
|
70
|
+
:class="[inputClasses]"
|
|
71
|
+
@keydown.down.prevent="move(1)"
|
|
72
|
+
@keydown.up.prevent="move(-1)"
|
|
73
|
+
@keydown.enter.prevent="toggleActive"
|
|
74
|
+
>
|
|
75
|
+
<div
|
|
76
|
+
data-slot="input-group-addon"
|
|
77
|
+
data-align="inline-start"
|
|
78
|
+
class="cn-input-group-addon flex cursor-text items-center justify-center select-none cn-input-group-addon-align-inline-start order-first"
|
|
79
|
+
>
|
|
80
|
+
<search-icon aria-hidden="true" />
|
|
81
|
+
</div>
|
|
82
|
+
</div>
|
|
83
|
+
|
|
84
|
+
<div
|
|
85
|
+
ref="list"
|
|
86
|
+
role="listbox"
|
|
87
|
+
aria-multiselectable="true"
|
|
88
|
+
tabindex="-1"
|
|
89
|
+
data-slot="combobox-list"
|
|
90
|
+
class="cn-combobox-list outline-none"
|
|
91
|
+
@keydown.down.prevent="move(1)"
|
|
92
|
+
@keydown.up.prevent="move(-1)"
|
|
93
|
+
@keydown.enter.prevent="toggleActive"
|
|
94
|
+
@keydown.space.prevent="toggleActive"
|
|
95
|
+
>
|
|
96
|
+
<div v-if="loading" class="flex items-center justify-center gap-2 py-2 text-sm text-muted-foreground">
|
|
97
|
+
<loader-circle-icon class="size-4 animate-spin motion-reduce:animate-none" aria-hidden="true" /> {{ texts.selectLoading }}
|
|
98
|
+
</div>
|
|
99
|
+
<div
|
|
100
|
+
v-for="(item, index) in items"
|
|
101
|
+
v-else
|
|
102
|
+
:key="keyOf(item)"
|
|
103
|
+
role="option"
|
|
104
|
+
data-slot="combobox-item"
|
|
105
|
+
:aria-selected="isSelected(item)"
|
|
106
|
+
:data-highlighted="index === active ? '' : undefined"
|
|
107
|
+
:class="cn('cn-combobox-item relative flex w-full cursor-default items-center outline-hidden select-none [&_svg]:pointer-events-none [&_svg]:shrink-0', ui?.item)"
|
|
108
|
+
@mousemove="active = index"
|
|
109
|
+
@click="select(item)"
|
|
110
|
+
>
|
|
111
|
+
<span class="cn-combobox-item-text">
|
|
112
|
+
<slot
|
|
113
|
+
name="option"
|
|
114
|
+
:option="item"
|
|
115
|
+
:label="labelOf(item)"
|
|
116
|
+
>
|
|
117
|
+
{{ labelOf(item) }}
|
|
118
|
+
</slot>
|
|
119
|
+
</span>
|
|
120
|
+
<span
|
|
121
|
+
v-if="isSelected(item)"
|
|
122
|
+
data-slot="combobox-item-indicator"
|
|
123
|
+
class="cn-combobox-item-indicator"
|
|
124
|
+
>
|
|
125
|
+
<check-icon aria-hidden="true" />
|
|
126
|
+
</span>
|
|
127
|
+
</div>
|
|
128
|
+
</div>
|
|
129
|
+
|
|
130
|
+
<div data-slot="combobox-empty" class="cn-combobox-empty">
|
|
131
|
+
{{ texts.selectEmpty }}
|
|
132
|
+
</div>
|
|
133
|
+
</div>
|
|
134
|
+
</div>
|
|
135
|
+
</div>
|
|
136
|
+
</template>
|
|
137
|
+
|
|
138
|
+
<script setup>
|
|
139
|
+
import { CheckIcon, ChevronDownIcon, LoaderCircleIcon, SearchIcon, XIcon } from '@lucide/vue'
|
|
140
|
+
import { formatMessage } from '@stsdti/auditing-core'
|
|
141
|
+
import { computed, nextTick, onBeforeUnmount, ref, shallowRef, useId, watch } from 'vue'
|
|
142
|
+
import { cn, inputClasses, useMessages } from '../../shared.js'
|
|
143
|
+
|
|
144
|
+
const props = defineProps({
|
|
145
|
+
label: { type: String, required: true },
|
|
146
|
+
placeholder: { type: String, default: '' },
|
|
147
|
+
options: { type: Array, default: () => [] },
|
|
148
|
+
search: { type: Function, default: null },
|
|
149
|
+
optionKey: { type: [String, Function], default: 'id' },
|
|
150
|
+
optionLabel: { type: [String, Function], default: 'name' },
|
|
151
|
+
messages: { type: Object, default: null },
|
|
152
|
+
ui: { type: Object, default: null }
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
const model = defineModel({ type: Array, default: () => [] })
|
|
156
|
+
|
|
157
|
+
const texts = useMessages(props.messages)
|
|
158
|
+
const panelId = useId()
|
|
159
|
+
|
|
160
|
+
const open = ref(false)
|
|
161
|
+
const root = ref(null)
|
|
162
|
+
const trigger = ref(null)
|
|
163
|
+
const searchInput = ref(null)
|
|
164
|
+
const list = ref(null)
|
|
165
|
+
const query = ref('')
|
|
166
|
+
const found = shallowRef([])
|
|
167
|
+
const loading = ref(false)
|
|
168
|
+
const active = ref(-1)
|
|
169
|
+
const availableHeight = ref(288)
|
|
170
|
+
|
|
171
|
+
const read = (item, accessor) => {
|
|
172
|
+
if (typeof accessor === 'function') {
|
|
173
|
+
return accessor(item)
|
|
174
|
+
}
|
|
175
|
+
return item !== null && typeof item === 'object' ? item[accessor] : item
|
|
176
|
+
}
|
|
177
|
+
const keyOf = (item) => String(read(item, props.optionKey))
|
|
178
|
+
const labelOf = (item) => read(item, props.optionLabel) ?? keyOf(item)
|
|
179
|
+
|
|
180
|
+
const searchable = computed(() => Boolean(props.search) || props.options.length > 7)
|
|
181
|
+
const selectedKeys = computed(() => new Set(model.value.map(keyOf)))
|
|
182
|
+
const isSelected = (item) => selectedKeys.value.has(keyOf(item))
|
|
183
|
+
|
|
184
|
+
const items = computed(() => {
|
|
185
|
+
if (props.search) {
|
|
186
|
+
const foundKeys = new Set(found.value.map(keyOf))
|
|
187
|
+
const missing = query.value ? [] : model.value.filter((item) => !foundKeys.has(keyOf(item)))
|
|
188
|
+
return [...missing, ...found.value]
|
|
189
|
+
}
|
|
190
|
+
const term = query.value.trim().toLowerCase()
|
|
191
|
+
return term ? props.options.filter((item) => String(labelOf(item)).toLowerCase().includes(term)) : props.options
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
let controller = null
|
|
195
|
+
let timer = null
|
|
196
|
+
|
|
197
|
+
async function load() {
|
|
198
|
+
controller?.abort()
|
|
199
|
+
controller = new AbortController()
|
|
200
|
+
const { signal } = controller
|
|
201
|
+
loading.value = true
|
|
202
|
+
try {
|
|
203
|
+
const result = await props.search(query.value, { signal })
|
|
204
|
+
if (!signal.aborted) {
|
|
205
|
+
found.value = Array.isArray(result) ? result : []
|
|
206
|
+
}
|
|
207
|
+
} catch {
|
|
208
|
+
if (!signal.aborted) {
|
|
209
|
+
found.value = []
|
|
210
|
+
}
|
|
211
|
+
} finally {
|
|
212
|
+
if (!signal.aborted) {
|
|
213
|
+
loading.value = false
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
watch(query, () => {
|
|
219
|
+
active.value = 0
|
|
220
|
+
if (!props.search) {
|
|
221
|
+
return
|
|
222
|
+
}
|
|
223
|
+
clearTimeout(timer)
|
|
224
|
+
timer = setTimeout(load, 250)
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
function select(item) {
|
|
228
|
+
model.value = isSelected(item)
|
|
229
|
+
? model.value.filter((other) => keyOf(other) !== keyOf(item))
|
|
230
|
+
: [...model.value, item]
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function move(step) {
|
|
234
|
+
if (!items.value.length) {
|
|
235
|
+
return
|
|
236
|
+
}
|
|
237
|
+
active.value = (active.value + step + items.value.length) % items.value.length
|
|
238
|
+
nextTick(() => list.value?.querySelector('[data-highlighted]')?.scrollIntoView({ block: 'nearest' }))
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function toggleActive() {
|
|
242
|
+
const item = items.value[active.value]
|
|
243
|
+
if (item !== undefined) {
|
|
244
|
+
select(item)
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
async function openAndFocus() {
|
|
249
|
+
if (!open.value) {
|
|
250
|
+
await toggle()
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async function toggle() {
|
|
255
|
+
open.value = !open.value
|
|
256
|
+
if (!open.value) {
|
|
257
|
+
return
|
|
258
|
+
}
|
|
259
|
+
query.value = ''
|
|
260
|
+
active.value = 0
|
|
261
|
+
const bottom = trigger.value?.getBoundingClientRect().bottom ?? 0
|
|
262
|
+
availableHeight.value = Math.max(220, Math.min(288, window.innerHeight - bottom - 16))
|
|
263
|
+
if (props.search) {
|
|
264
|
+
load()
|
|
265
|
+
}
|
|
266
|
+
await nextTick()
|
|
267
|
+
;(searchInput.value ?? list.value)?.focus()
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function onEscape(event) {
|
|
271
|
+
if (!open.value) {
|
|
272
|
+
return
|
|
273
|
+
}
|
|
274
|
+
event.stopPropagation()
|
|
275
|
+
open.value = false
|
|
276
|
+
trigger.value?.focus()
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function onPointerDown(event) {
|
|
280
|
+
if (open.value && root.value && !root.value.contains(event.target)) {
|
|
281
|
+
open.value = false
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
watch(open, (isOpen) => {
|
|
286
|
+
if (typeof document === 'undefined') {
|
|
287
|
+
return
|
|
288
|
+
}
|
|
289
|
+
if (isOpen) {
|
|
290
|
+
document.addEventListener('pointerdown', onPointerDown, true)
|
|
291
|
+
} else {
|
|
292
|
+
document.removeEventListener('pointerdown', onPointerDown, true)
|
|
293
|
+
}
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
onBeforeUnmount(() => {
|
|
297
|
+
controller?.abort()
|
|
298
|
+
clearTimeout(timer)
|
|
299
|
+
if (typeof document !== 'undefined') {
|
|
300
|
+
document.removeEventListener('pointerdown', onPointerDown, true)
|
|
301
|
+
}
|
|
302
|
+
})
|
|
303
|
+
</script>
|