1
0
mirror of https://gitlab.com/bramw/baserow.git synced 2024-11-21 23:37:55 +00:00
bramw_baserow/web-frontend/modules/database/mixins/linkRowField.js

79 lines
2.2 KiB
JavaScript

import { getPrimaryOrFirstField } from '@baserow/modules/database/utils/field'
export default {
computed: {
/**
* Returns the value of the field that can be used when creating a new row
* in the linked table starting from the current row.
*/
presetsForNewRowInLinkedTable() {
const presets = {}
const value = this.primaryFieldLinkRowValue
if (value) {
presets[`field_${this.field.link_row_related_field_id}`] = [value]
}
return presets
},
/**
* Returns the value of the field.
*/
primaryFieldLinkRowValue() {
// Set the starting row as preset so that can be used later if the user wants to create a new row
// starting from the selected row.
if (!this.allFieldsInTable) {
return
}
const primaryField = getPrimaryOrFirstField(this.allFieldsInTable)
const primaryFieldType = this.$registry.get(
'field',
primaryField._.type.type
)
return {
id: this.row.id,
value: primaryFieldType.toHumanReadableString(
primaryField,
this.row[`field_${primaryField.id}`]
),
}
},
},
methods: {
/**
* Removes an existing relation from the value.
*/
removeValue(event, value, id) {
const newValue = JSON.parse(JSON.stringify(value))
const index = newValue.findIndex((item) => item.id === id)
if (index === -1) {
return
}
newValue.splice(index, 1)
this.$emit('update', newValue, value)
},
/**
* Adds a new relation to the value. This typically happens via the modal.
*/
addValue(value, { row, primary }) {
// Check if the relation already exists.
for (let i = 0; i < value.length; i++) {
if (value[i].id === row.id) {
return
}
}
// Prepare the new value with all the relations and emit that value to the
// parent.
const newValue = JSON.parse(JSON.stringify(value))
const rowValue = this.$registry
.get('field', primary.type)
.toHumanReadableString(primary, row[`field_${primary.id}`])
newValue.push({
id: row.id,
value: rowValue,
})
this.$emit('update', newValue, value)
},
},
}