Vue.component('tt-resolver', { props: { value: {type: Number, required: true}, reference: {type: String, required: true}, }, data() { return { window: window, loading: true, text: '', } }, template: `
Loading...
{{ text }} `, async created() { const entry = await axios.get(window.TT_CONFIG['BASE_PATH'] + '/' + this.reference + '/getById?id=' + this.value); this.text = entry.data.name ?? entry.data.title ?? entry.data.text ?? '[E] Key not found'; this.loading = false; } }) Vue.component('tt-positions-manager', { props: { value: {type: Array, required: false}, config: {type: Object, required: true}, groupMode: {type: Boolean, default: false}, }, data() { return { window: window, positions: this.value, formData: {}, groupName: '', selectedIndex: null, resolvingFields: {}, } }, template: `
{{ field.label }} Actions
`, methods: { updateField(key, value) { this.$set(this.formData, key, value); }, async saveEntry() { if (this.config.validateForm && !await this.config.validateForm(this.formData)) return; if (this.selectedIndex === null) this.positions.push(this.formData); else this.$set(this.positions, this.selectedIndex, this.formData); if (this.config.customOrdering) { this.positions.sort((a, b) => a[this.config.customOrdering] - b[this.config.customOrdering]); } this.$emit('input', this.positions); this.resetForm(); }, addGroup() { this.positions.push({_group: this.groupName}); this.groupName = ''; }, editEntry(index) { this.selectedIndex = index; this.formData = {...this.positions[index]}; }, deleteEntry(index) { this.positions.splice(index, 1); this.$emit('input', this.positions); }, resetForm() { this.formData = {}; this.selectedIndex = null; }, formatFieldValue(value, field) { if (field.formatter) return field.formatter(value); return value; }, async resolveFields() { for (let i = 0; i < this.positions.length; i++) { for (let key in this.config.fields) { if (this.config.fields[key].customFieldResolver) { this.$set(this.resolvingFields, i + key, true); const textValue = await this.config.fields[key].customFieldResolver(this.positions[i][key]); this.$set(this.resolvingFields, i + key, textValue); } else if (this.config.fields[key].customFieldReference && this.positions[i][key]) { this.$set(this.resolvingFields, i + key, true); if (this.config.fields[key].customFieldReference) { const entry = await axios.get(window.TT_CONFIG['BASE_PATH'] + '/' + this.config.fields[key].customFieldReference + '/getById?id=' + this.positions[i][key]); const textValue = entry.data.name ?? entry.data.title ?? entry.data.text ?? '[E] Key not found'; console.log(textValue); this.$set(this.resolvingFields, i + key, textValue); } else this.$set(this.resolvingFields, i + key, ''); } } } } }, created() { if (this.config.customMethods) Object.assign(this, this.config.customMethods); }, computed: { groupedPositions() { const groups = {}; for (const position of this.positions) { const group = position._group ?? 'Keine Gruppe'; if (!groups[group]) groups[group] = []; if (Object.keys(position).length !== 1) groups[group].push(position); } return groups; }, allGroups() { return Object.keys(this.groupedPositions); } }, watch: { positions: { handler() { this.resolveFields().then(); }, deep: true }, value: { handler() { this.positions = this.value; }, deep: true } } });