mirror of
https://github.com/baserow/baserow.git
synced 2026-12-04 00:08:25 +00:00
143 lines
4.2 KiB
Vue
143 lines
4.2 KiB
Vue
<template>
|
|
<div>
|
|
<p v-show="modelValue === null" class="margin-bottom-1">
|
|
<slot name="chooseValueState"></slot>
|
|
</p>
|
|
<Dropdown
|
|
:value="modelValue"
|
|
show-footer
|
|
class="data-source-dropdown"
|
|
@input="$emit('update:modelValue', $event)"
|
|
>
|
|
<DropdownItem
|
|
v-for="dataSource in sharedDataSources"
|
|
:key="dataSource.id"
|
|
:name="getDataSourceLabel(dataSource)"
|
|
:value="dataSource.id"
|
|
icon="iconoir-multiple-pages-empty"
|
|
:icon-tooltip="$t('dataSourceDropdown.shared')"
|
|
>
|
|
</DropdownItem>
|
|
<template v-if="localDataSources">
|
|
<DropdownItem
|
|
v-for="dataSource in localDataSources"
|
|
:key="dataSource.id"
|
|
:name="getDataSourceLabel(dataSource)"
|
|
:value="dataSource.id"
|
|
icon="iconoir-empty-page"
|
|
:icon-tooltip="$t('dataSourceDropdown.pageOnly')"
|
|
>
|
|
</DropdownItem
|
|
></template>
|
|
<template #emptyState>
|
|
<slot name="emptyState">
|
|
{{
|
|
isOnSharedPage
|
|
? $t('dataSourceDropdown.noSharedDataSources')
|
|
: $t('dataSourceDropdown.noDataSources')
|
|
}}
|
|
</slot>
|
|
</template>
|
|
<template #footer>
|
|
<a class="select__footer-button" @click="openDataSourceModal">
|
|
<i class="iconoir-plus"></i>
|
|
{{ $t('dataSourceDropdown.addNew') }}
|
|
</a>
|
|
</template>
|
|
</Dropdown>
|
|
<DataSourceCreateEditModal
|
|
:key="modalKey"
|
|
ref="dataSourceCreateEditModal"
|
|
:data-source-id="currentDataSourceId"
|
|
@data-source-created="currentDataSourceId = $event.id"
|
|
@updated="onDataSourceUpdated"
|
|
@hidden="currentDataSourceId = null"
|
|
/>
|
|
</div>
|
|
</template>
|
|
|
|
<script>
|
|
import DataSourceCreateEditModal from '@baserow/modules/builder/components/dataSource/DataSourceCreateEditModal'
|
|
|
|
export default {
|
|
name: 'DataSourceDropdown',
|
|
components: { DataSourceCreateEditModal },
|
|
props: {
|
|
modelValue: {
|
|
type: Number,
|
|
required: false,
|
|
default: null,
|
|
},
|
|
sharedDataSources: {
|
|
type: Array,
|
|
required: true,
|
|
},
|
|
localDataSources: {
|
|
type: Array,
|
|
required: false,
|
|
default: null,
|
|
},
|
|
small: {
|
|
type: Boolean,
|
|
required: false,
|
|
default: false,
|
|
},
|
|
},
|
|
emits: ['update:modelValue'],
|
|
data() {
|
|
return {
|
|
modalKey: 0,
|
|
currentDataSourceId: null,
|
|
}
|
|
},
|
|
computed: {
|
|
isOnSharedPage() {
|
|
return this.localDataSources === null
|
|
},
|
|
},
|
|
methods: {
|
|
/**
|
|
* Responsible for taking a data source object, and returning a
|
|
* label for our data source dropdowns to use in their items. The
|
|
* data source name is used, along with a suffix that indicates
|
|
* whether the data source returns a single row or multiple rows.
|
|
* @param dataSource - The data source object to generate a label for.
|
|
* @returns {string} - The label to use in the dropdown.
|
|
*/
|
|
getDataSourceLabel(dataSource) {
|
|
if (dataSource.type === null) {
|
|
// If the data source doesn't yet have a service type,
|
|
// we just return the data source name, for now.
|
|
return dataSource.name
|
|
}
|
|
const service = this.$registry.get('service', dataSource.type)
|
|
const suffix = service.returnsList(dataSource)
|
|
? this.$t('integrationsCommon.multipleRows')
|
|
: this.$t('integrationsCommon.singleRow')
|
|
return `${dataSource.name} (${suffix})`
|
|
},
|
|
async openDataSourceModal() {
|
|
this.currentDataSourceId = null
|
|
this.modalKey++
|
|
await this.$nextTick()
|
|
this.$refs.dataSourceCreateEditModal?.show()
|
|
},
|
|
/**
|
|
* When a data source is updated (i.e. the user has created the record,
|
|
* *and* updated it), we want to check if it has a valid schema. If it does,
|
|
* we emit the input event so that the dropdown updates to select the newly
|
|
* created data source.
|
|
* @param dataSource - The updated data source.
|
|
*/
|
|
onDataSourceUpdated(dataSource) {
|
|
const serviceType =
|
|
dataSource.type && this.$registry.get('service', dataSource.type)
|
|
if (serviceType?.getDataSchema(dataSource)) {
|
|
this.modalKey++
|
|
this.$emit('update:modelValue', dataSource.id)
|
|
}
|
|
},
|
|
},
|
|
}
|
|
</script>
|