aboutsummaryrefslogtreecommitdiffstats
path: root/apps/theming/src/components/admin/AppMenuSection.vue
blob: fb8fdc67f984522897374b296c1d83dba1004fa7 (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
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
<!--
  - SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  - SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
	<NcSettingsSection :name="t('theming', 'Navigation bar settings')">
		<h3>{{ t('theming', 'Default app') }}</h3>
		<p class="info-note">
			{{ t('theming', 'The default app is the app that is e.g. opened after login or when the logo in the menu is clicked.') }}
		</p>

		<NcCheckboxRadioSwitch :checked.sync="hasCustomDefaultApp" type="switch" data-cy-switch-default-app="">
			{{ t('theming', 'Use custom default app') }}
		</NcCheckboxRadioSwitch>

		<template v-if="hasCustomDefaultApp">
			<h4>{{ t('theming', 'Global default app') }}</h4>
			<NcSelect v-model="selectedApps"
				:close-on-select="false"
				:placeholder="t('theming', 'Global default apps')"
				:options="allApps"
				:multiple="true" />
			<h5>{{ t('theming', 'Default app priority') }}</h5>
			<p class="info-note">
				{{ t('theming', 'If an app is not enabled for a user, the next app with lower priority is used.') }}
			</p>
			<AppOrderSelector :value.sync="selectedApps" />
		</template>
	</NcSettingsSection>
</template>

<script lang="ts">
import type { INavigationEntry } from '../../../../../core/src/types/navigation'

import { showError } from '@nextcloud/dialogs'
import { loadState } from '@nextcloud/initial-state'
import { translate as t } from '@nextcloud/l10n'
import { generateUrl } from '@nextcloud/router'
import { computed, defineComponent } from 'vue'

import axios from '@nextcloud/axios'

import NcCheckboxRadioSwitch from '@nextcloud/vue/dist/Components/NcCheckboxRadioSwitch.js'
import NcSelect from '@nextcloud/vue/dist/Components/NcSelect.js'
import NcSettingsSection from '@nextcloud/vue/dist/Components/NcSettingsSection.js'
import AppOrderSelector from '../AppOrderSelector.vue'

export default defineComponent({
	name: 'AppMenuSection',
	components: {
		AppOrderSelector,
		NcCheckboxRadioSwitch,
		NcSelect,
		NcSettingsSection,
	},
	props: {
		defaultApps: {
			type: Array,
			required: true,
		},
	},
	emits: {
		'update:defaultApps': (value: string[]) => Array.isArray(value) && value.every((id) => typeof id === 'string'),
	},
	setup(props, { emit }) {
		const hasCustomDefaultApp = computed({
			get: () => props.defaultApps.length > 0,
			set: (checked: boolean) => {
				if (checked) {
					emit('update:defaultApps', ['dashboard', 'files'])
				} else {
					selectedApps.value = []
				}
			},
		})

		/**
		 * All enabled apps which can be navigated
		 */
		const allApps = loadState<INavigationEntry[]>('core', 'apps')
			.map(({ id, name, icon }) => ({ label: name, id, icon }))

		/**
		 * Currently selected app, wrapps the setter
		 */
		const selectedApps = computed({
			get: () => props.defaultApps.map((id) => allApps.filter(app => app.id === id)[0]),
			set(value) {
				saveSetting('defaultApps', value.map(app => app.id))
					.then(() => emit('update:defaultApps', value.map(app => app.id)))
					.catch(() => showError(t('theming', 'Could not set global default apps')))
			},
		})

		const saveSetting = async (key: string, value: unknown) => {
			const url = generateUrl('/apps/theming/ajax/updateAppMenu')
			return await axios.put(url, {
				setting: key,
				value,
			})
		}

		return {
			allApps,
			selectedApps,
			hasCustomDefaultApp,

			t,
		}
	},
})
</script>

<style scoped lang="scss">
h3, h4 {
	font-weight: bold;
}
h4, h5 {
	margin-block-start: 12px;
}

.info-note {
	color: var(--color-text-maxcontrast);
}
</style>