import { markRaw, defineAsyncComponent } from 'vue'; import {CoreRESTClient} from '../RESTClient.js'; import accessibility from "../directives/accessibility.js"; export default { directives: { accessibility }, emits: [ 'update:modelValue', 'change', 'changed' ], props: { config: { type: [String, Object], required: true }, default: String, modelValue: [String, Number, Boolean, Array, Object, Date, Function, Symbol], vertical: Boolean, border: Boolean }, data() { return { current: null, tabs: {} } }, computed: { currentTab() { if (this.tabs[this.current]) return this.tabs[this.current]; return { component: 'div' }; }, value: { get() { return this.modelValue; }, set(v) { this.$emit('update:modelValue', v); } } }, watch: { config(n) { this.initConfig(n); } }, methods: { change(key) { this.$emit("change", key) this.current = key; this.$nextTick(() => this.$emit("changed", key)); }, initConfig(config) { if (!config) return; if (typeof config === 'string' || config instanceof String) return CoreRESTClient.get(config) .then(result => CoreRESTClient.getData(result.data)) .then(this.initConfig) .catch(this.$fhcAlert.handleSystemError); const tabs = {}; if (Array.isArray(config)) { config.forEach((item, key) => { if (!item.component) return console.error('Component missing for ' + key); tabs[key] = { component: markRaw(defineAsyncComponent(() => import(item.component))), title: item.title || key, config: item.config, key } }); } else { Object.entries(config).forEach(([key, item]) => { if (!item.component) return console.error('Component missing for ' + key); tabs[key] = { component: markRaw(defineAsyncComponent(() => import(item.component))), title: item.title || key, config: item.config, key } }); } if (this.current === null || !tabs[this.current]) { if (tabs[this.default]) this.current = this.default; else this.current = Object.keys(tabs)[0]; } this.tabs = tabs; } }, created() { this.initConfig(this.config); }, template: `