aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/mixins/actionsMixin.ts
blob: f81b0754431db4e0501d0b75134b10ade4e787b0 (plain)
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
/**
 * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

import type { FileAction } from '@nextcloud/files'
import { defineComponent } from 'vue'

export default defineComponent({

	data() {
		return {
			openedSubmenu: null as FileAction|null,
		}
	},

	computed: {
		enabledSubmenuActions(): Record<string, FileAction[]> {
			return (this.enabledFileActions as FileAction[])
				.reduce((record, action) => {
					if (action.parent !== undefined) {
						if (!record[action.parent]) {
							record[action.parent] = []
						}

						record[action.parent].push(action)
					}
					return record
				}, {} as Record<string, FileAction[]>)
		},
	},

	methods: {
		/**
		 * Check if a menu is valid, meaning it is
		 * defined and has at least one action
		 *
		 * @param action The action to check
		 */
		isValidMenu(action: FileAction): boolean {
			return this.enabledSubmenuActions[action.id]?.length > 0
		},

		async onBackToMenuClick(action: FileAction|null) {
			if (!action) {
				return
			}

			this.openedSubmenu = null
			// Wait for first render
			await this.$nextTick()

			// Focus the previous menu action button
			this.$nextTick(() => {
				// Focus the action button, test both batch and single action references
				// as this mixin is used in both single and batch actions.
				const menuAction = this.$refs[`action-batch-${action.id}`]?.[0]
					|| this.$refs[`action-${action.id}`]?.[0]
				if (menuAction) {
					menuAction.$el.querySelector('button')?.focus()
				}
			})
		},
	},
})