aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/components/TemplateFiller.vue
blob: 3f1db8dfd5861b31ab1e54c0e9a5ea972cf2bc2c (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
<!--
  - SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  - SPDX-License-Identifier: AGPL-3.0-or-later
-->

<template>
	<NcModal label-id="template-field-modal__label">
		<div class="template-field-modal__content">
			<form>
				<h3 id="template-field-modal__label">
					{{ t('files', 'Fill template fields') }}
				</h3>

				<div v-for="field in fields" :key="field.index">
					<component :is="getFieldComponent(field.type)"
						v-if="fieldHasLabel(field)"
						:field="field"
						@input="trackInput" />
				</div>
			</form>
		</div>

		<div class="template-field-modal__buttons">
			<NcLoadingIcon v-if="loading" :name="t('files', 'Submitting fields …')" />
			<NcButton aria-label="Submit button"
				type="primary"
				@click="submit">
				{{ t('files', 'Submit') }}
			</NcButton>
		</div>
	</NcModal>
</template>

<script>
import { defineComponent } from 'vue'
import { t } from '@nextcloud/l10n'
import NcButton from '@nextcloud/vue/components/NcButton'
import NcLoadingIcon from '@nextcloud/vue/components/NcLoadingIcon'
import NcModal from '@nextcloud/vue/components/NcModal'
import TemplateRichTextField from './TemplateFiller/TemplateRichTextField.vue'
import TemplateCheckboxField from './TemplateFiller/TemplateCheckboxField.vue'

export default defineComponent({
	name: 'TemplateFiller',

	components: {
		NcModal,
		NcButton,
		NcLoadingIcon,
		TemplateRichTextField,
		TemplateCheckboxField,
	},

	props: {
		fields: {
			type: Array,
			default: () => [],
		},
		onSubmit: {
			type: Function,
			default: async () => {},
		},
	},

	data() {
		return {
			localFields: {},
			loading: false,
		}
	},

	methods: {
		t,
		trackInput({ index, property, value }) {
			if (!this.localFields[index]) {
				this.localFields[index] = {}
			}

			this.localFields[index][property] = value
		},
		getFieldComponent(fieldType) {
			const fieldComponentType = fieldType.split('-')
				.map((str) => {
					return str.charAt(0).toUpperCase() + str.slice(1)
				})
				.join('')

			return `Template${fieldComponentType}Field`
		},
		fieldHasLabel(field) {
			return field.name || field.alias
		},
		async submit() {
			this.loading = true

			await this.onSubmit(this.localFields)

			this.$emit('close')
		},
	},
})
</script>

<style lang="scss" scoped>
$modal-margin: calc(var(--default-grid-baseline) * 4);

.template-field-modal__content {
	padding: $modal-margin;

	h3 {
		text-align: center;
	}
}

.template-field-modal__buttons {
	display: flex;
	justify-content: flex-end;
	gap: var(--default-grid-baseline);
	margin: $modal-margin;
	margin-top: 0;
}
</style>