aboutsummaryrefslogtreecommitdiffstats
path: root/apps/settings/src/main-declarative-settings-forms.ts
blob: 6e2d71b69ca56b2398199e364588a939cf60ef35 (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
/**
 * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
import type { ComponentInstance } from 'vue'

import { loadState } from '@nextcloud/initial-state'
import { t, n } from '@nextcloud/l10n'
import Vue from 'vue'
import DeclarativeSection from './components/DeclarativeSettings/DeclarativeSection.vue'
import logger from './logger'

interface DeclarativeFormField {
	id: string,
	title: string,
	description: string,
	type: string,
	placeholder: string,
	label: string,
	options: Array<unknown>|null,
	value: unknown,
	default: unknown,
}

interface DeclarativeForm {
	id: number,
	priority: number,
	section_type: string,
	section_id: string,
	storage_type: string,
	title: string,
	description: string,
	doc_url: string,
	app: string,
	fields: Array<DeclarativeFormField>,
}

const forms = loadState<DeclarativeForm[]>('settings', 'declarative-settings-forms', [])

/**
 * @param forms The forms to render
 */
function renderDeclarativeSettingsSections(forms: Array<DeclarativeForm>): ComponentInstance[] {
	Vue.mixin({ methods: { t, n } })
	const DeclarativeSettingsSection = Vue.extend(DeclarativeSection as never)

	return forms.map((form) => {
		const el = `#${form.app}_${form.id}`
		return new DeclarativeSettingsSection({
			el,
			propsData: {
				form,
			},
		})
	})
}

document.addEventListener('DOMContentLoaded', () => {
	logger.debug('Loaded declarative forms', { forms })
	renderDeclarativeSettingsSections(forms)
})