Vue.component('tt-resolver', { props: { value: { type: [Number, String], required: true }, reference: { type: String, required: true }, autocomplete: { type: Boolean, default: false } }, data: () => ({ loading: true, text: '' }), template: `
Loading...
{{ text }} `, async created() { const endpoint = this.autocomplete ? `${window.TT_CONFIG["BASE_PATH"]}${this.reference}?searchedID=${this.value}` : `${window.TT_CONFIG["BASE_PATH"]}/${this.reference}/getById?id=${this.value}`; const { data } = await axios.get(endpoint); if (this.reference === 'WarehouseArticle') this.text = `${data.articleNumber} | ${data.title}`; else this.text = this.autocomplete ? data[0]?.text : data.name ?? data.title ?? data.text ?? '[E] Key not found'; this.loading = false; } }); Vue.component('tt-positions-manager', { props: { value: {type: [Array, String], required: false}, config: {type: Object, required: true}, groupMode: {type: Boolean, default: false}, submitLoading: {type: Boolean, default: false} }, data() { return { window: window, positions: this.value, formData: {}, groupName: '', selectedIndex: null, } }, template: `
{{ field.label }} Aktionen
`, methods: { updateField(key, value) { this.$set(this.formData, key, value); }, defaultValidateForm(formData) { for (const field of this.config["validateFormOptions"]) { if (!(formData[field.key] || formData[field.key + '_text'])) { window.notify('error', field.message); return false; } } return true; }, checkEmitDisplayValueAutocomplete() { for (const [key, field] of Object.entries(this.config.fields)) { if ((typeof field.showCondition === 'function' && field.showCondition(this.formData) === true || !field.showCondition) && field.type === 'autocomplete' && field.emitDisplayValue && (isNaN(this.formData[key]) || !this.formData[key]) && this.$refs['autocomplete-' + key][0]) { this.$set(this.formData, key + '_text', this.$refs['autocomplete-' + key][0].displayValue); this.$delete(this.formData, key); } if (field.emitDisplayValue && this.formData[key] !== null && this.formData[key] !== undefined) { this.$delete(this.formData, key + '_text'); } } }, async saveEntry() { this.checkEmitDisplayValueAutocomplete(); if (this.config.hasOwnProperty('validateFormOptions') && !this.defaultValidateForm(this.formData)) return; else 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 = ''; }, async editEntry(index) { this.selectedIndex = index; for (const [key, field] of Object.entries(this.config.fields)) { if (field.type === 'autocomplete' && field.emitDisplayValue) { await this.$nextTick(); if (this.positions[index][key + '_text'] && this.$refs['autocomplete-' + key][0]) { console.log('inhere'); this.$refs['autocomplete-' + key][0].displayValue = this.positions[index][key + '_text']; this.$set(this.formData, key, this.positions[index][key]); } } } this.formData = {...this.positions[index]}; }, deleteEntry(index) { this.positions.splice(index, 1); this.$emit('input', this.positions); }, resetForm() { this.formData = {}; for (const [key, field] of Object.entries(this.config.fields)) { if ( typeof field.showCondition === 'function' && field.showCondition(this.formData) === true && field.type === 'autocomplete' && field.emitDisplayValue && this.$refs['autocomplete-' + key][0]) { this.$refs['autocomplete-' + key][0].displayValue = ''; } if (field.inputType === 'date') { this.$set(this.formData, key, moment().format('YYYY-MM-DD')); } } this.selectedIndex = null; }, formatFieldValue(value, field) { if (field.formatter) return field.formatter(value); if (field.inputType === 'date') return moment(value).format('DD.MM.YYYY'); return value; }, }, //TODO: cleanup created() { if (this.config["customMethods"]) Object.assign(this, this.config.customMethods); if (!this.positions) this.positions = []; if (typeof this.positions === 'string') { try { this.positions = JSON.parse(this.positions); } catch (e) { console.error(e); this.positions = []; } } }, mounted() { this.resetForm(); }, 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; }, positionsToRender() { return this.groupMode ? this.groupedPositions : {'': this.positions}; }, allGroups() { return Object.keys(this.groupedPositions); } }, watch: { value: { handler() { this.positions = this.value; }, deep: true } } });