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
|
<!--
- SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<Fragment>
<div :id="statusInfoId"
aria-live="polite"
class="hidden-visually"
role="status">
{{ statusInfo }}
</div>
<ol ref="listElement" data-cy-app-order class="order-selector">
<AppOrderSelectorElement v-for="app,index in appList"
:key="`${app.id}${renderCount}`"
ref="selectorElements"
:app="app"
:aria-details="ariaDetails"
:aria-describedby="statusInfoId"
:is-first="index === 0 || !!appList[index - 1].default"
:is-last="index === value.length - 1"
v-on="app.default ? {} : {
'move:up': () => moveUp(index),
'move:down': () => moveDown(index),
'update:focus': () => updateStatusInfo(index),
}" />
</ol>
</Fragment>
</template>
<script lang="ts">
import type { PropType } from 'vue'
import { translate as t } from '@nextcloud/l10n'
import { useSortable } from '@vueuse/integrations/useSortable'
import { computed, defineComponent, onUpdated, ref } from 'vue'
import { Fragment } from 'vue-frag'
import AppOrderSelectorElement from './AppOrderSelectorElement.vue'
export interface IApp {
id: string // app id
icon: string // path to the icon svg
label: string // display name
default?: boolean // force app as default app
app?: string
}
export default defineComponent({
name: 'AppOrderSelector',
components: {
AppOrderSelectorElement,
Fragment,
},
props: {
/**
* Details like status information that need to be forwarded to the interactive elements
*/
ariaDetails: {
type: String,
default: null,
},
/**
* List of apps to reorder
*/
value: {
type: Array as PropType<IApp[]>,
required: true,
},
},
emits: {
/**
* Update the apps list on reorder
* @param value The new value of the app list
*/
'update:value': (value: IApp[]) => Array.isArray(value),
},
setup(props, { emit }) {
/**
* The Element that contains the app list
*/
const listElement = ref<HTMLElement | null>(null)
/**
* The app list with setter that will ement the `update:value` event
*/
const appList = computed({
get: () => props.value,
// Ensure the sortable.js does not mess with the default attribute
set: (list) => {
const newValue = [...list].sort((a, b) => ((b.default ? 1 : 0) - (a.default ? 1 : 0)) || list.indexOf(a) - list.indexOf(b))
if (newValue.some(({ id }, index) => id !== props.value[index].id)) {
emit('update:value', newValue)
} else {
// forceUpdate as the DOM has changed because of a drag event, but the reactive state has not -> wrong state
renderCount.value += 1
}
},
})
/**
* Helper to force rerender the list in case of a invalid drag event
*/
const renderCount = ref(0)
/**
* Handle drag & drop sorting
*/
useSortable(listElement, appList, { filter: '.order-selector-element--disabled' })
/**
* Array of all AppOrderSelectorElement components used to for keeping the focus after button click
*/
const selectorElements = ref<InstanceType<typeof AppOrderSelectorElement>[]>([])
/**
* We use the updated hook here to verify all selector elements keep the focus on the last pressed button
* This is needed to be done in this component to make sure Sortable.JS has finished sorting the elements before focussing an element
*/
onUpdated(() => {
selectorElements.value.forEach(element => element.keepFocus())
})
/**
* Handle element is moved up
* @param index The index of the element that is moved
*/
const moveUp = (index: number) => {
const before = index > 1 ? props.value.slice(0, index - 1) : []
// skip if not possible, because of default default app
if (props.value[index - 1]?.default) {
return
}
const after = [props.value[index - 1]]
if (index < props.value.length - 1) {
after.push(...props.value.slice(index + 1))
}
emit('update:value', [...before, props.value[index], ...after])
}
/**
* Handle element is moved down
* @param index The index of the element that is moved
*/
const moveDown = (index: number) => {
const before = index > 0 ? props.value.slice(0, index) : []
before.push(props.value[index + 1])
const after = index < (props.value.length - 2) ? props.value.slice(index + 2) : []
emit('update:value', [...before, props.value[index], ...after])
}
/**
* Additional status information to show to screen reader users for accessibility
*/
const statusInfo = ref('')
/**
* ID to be used on the status info element
*/
const statusInfoId = `sorting-status-info-${(Math.random() + 1).toString(36).substring(7)}`
/**
* Update the status information for the currently selected app
* @param index Index of the app that is currently selected
*/
const updateStatusInfo = (index: number) => {
statusInfo.value = t('theming', 'Current selected app: {app}, position {position} of {total}', {
app: props.value[index].label,
position: index + 1,
total: props.value.length,
})
}
return {
appList,
listElement,
moveDown,
moveUp,
statusInfoId,
statusInfo,
updateStatusInfo,
renderCount,
selectorElements,
}
},
})
</script>
<style scoped lang="scss">
.order-selector {
width: max-content;
min-width: 260px; // align with NcSelect
}
</style>
|