2020-08-17 15:10:19 +02:00
|
|
|
<template>
|
|
|
|
<select
|
|
|
|
ref="editField"
|
2020-12-15 17:08:36 +01:00
|
|
|
class="form-select pl-1 pr-4"
|
|
|
|
:class="{'small-select': size === 'small'}"
|
2020-08-17 15:10:19 +02:00
|
|
|
@change="onChange"
|
|
|
|
@blur="$emit('blur')"
|
|
|
|
>
|
2020-12-15 17:08:36 +01:00
|
|
|
<option v-if="!isValidDefault" :value="value">
|
|
|
|
{{ value }} - {{ $t('message.invalidDefault') }}
|
|
|
|
</option>
|
2020-08-17 15:10:19 +02:00
|
|
|
<option
|
|
|
|
v-for="row in foreignList"
|
2021-03-21 11:51:22 +01:00
|
|
|
:key="row.foreign_column"
|
|
|
|
:value="row.foreign_column"
|
|
|
|
:selected="row.foreign_column === value"
|
2020-08-17 15:10:19 +02:00
|
|
|
>
|
2021-03-21 11:51:22 +01:00
|
|
|
{{ row.foreign_column }} {{ 'foreign_description' in row ? ` - ${row.foreign_description}` : '' | cutText }}
|
2020-08-17 15:10:19 +02:00
|
|
|
</option>
|
|
|
|
</select>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import Tables from '@/ipc-api/Tables';
|
|
|
|
import { mapGetters, mapActions } from 'vuex';
|
|
|
|
import { TEXT, LONG_TEXT } from 'common/fieldTypes';
|
|
|
|
export default {
|
|
|
|
name: 'ForeignKeySelect',
|
|
|
|
filters: {
|
|
|
|
cutText (val) {
|
|
|
|
if (typeof val !== 'string') return val;
|
|
|
|
return val.length > 15 ? `${val.substring(0, 15)}...` : val;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
value: [String, Number],
|
2020-12-15 17:08:36 +01:00
|
|
|
keyUsage: Object,
|
|
|
|
size: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
}
|
2020-08-17 15:10:19 +02:00
|
|
|
},
|
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
foreignList: []
|
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
...mapGetters({
|
|
|
|
selectedWorkspace: 'workspaces/getSelected'
|
2020-12-15 17:08:36 +01:00
|
|
|
}),
|
|
|
|
isValidDefault () {
|
2021-02-17 18:49:02 +01:00
|
|
|
if (!this.foreignList.length) return true;
|
2021-05-20 15:07:51 +02:00
|
|
|
if (this.value === null) return false;
|
|
|
|
return this.foreignList.some(foreign => foreign.foreign_column.toString() === this.value.toString());
|
2020-12-15 17:08:36 +01:00
|
|
|
}
|
2020-08-17 15:10:19 +02:00
|
|
|
},
|
|
|
|
async created () {
|
2021-03-21 11:51:22 +01:00
|
|
|
let foreignDesc;
|
2020-08-17 15:10:19 +02:00
|
|
|
const params = {
|
|
|
|
uid: this.selectedWorkspace,
|
|
|
|
schema: this.keyUsage.refSchema,
|
|
|
|
table: this.keyUsage.refTable
|
|
|
|
};
|
|
|
|
|
|
|
|
try { // Field data
|
|
|
|
const { status, response } = await Tables.getTableColumns(params);
|
2021-03-21 11:51:22 +01:00
|
|
|
if (status === 'success') {
|
2021-05-20 15:07:51 +02:00
|
|
|
const textField = response.find(field => [...TEXT, ...LONG_TEXT].includes(field.type) && field.name !== this.keyUsage.refField);
|
2021-03-21 11:51:22 +01:00
|
|
|
foreignDesc = textField ? textField.name : false;
|
|
|
|
}
|
2020-08-17 15:10:19 +02:00
|
|
|
else
|
|
|
|
this.addNotification({ status: 'error', message: response });
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
this.addNotification({ status: 'error', message: err.stack });
|
|
|
|
}
|
|
|
|
|
|
|
|
try { // Foregn list
|
|
|
|
const { status, response } = await Tables.getForeignList({
|
|
|
|
...params,
|
2020-12-15 17:08:36 +01:00
|
|
|
column: this.keyUsage.refField,
|
2021-03-21 11:51:22 +01:00
|
|
|
description: foreignDesc
|
2020-08-17 15:10:19 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (status === 'success')
|
|
|
|
this.foreignList = response.rows;
|
|
|
|
else
|
|
|
|
this.addNotification({ status: 'error', message: response });
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
this.addNotification({ status: 'error', message: err.stack });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
...mapActions({
|
|
|
|
addNotification: 'notifications/addNotification'
|
|
|
|
}),
|
|
|
|
onChange () {
|
|
|
|
this.$emit('update:value', this.$refs.editField.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|