Files
baserow/web-frontend/modules/dashboard/components/data_source/AggregateRowsDataSourceForm.vue

390 lines
11 KiB
Vue

<template>
<form @submit.prevent>
<div class="separator"></div>
<FormSection
:title="$t('aggregateRowsDataSourceForm.data')"
class="margin-bottom-2"
>
<DashboardTableSelector
v-model="computedTableId"
:databases="databases"
:data-source="{ ...dataSource, filters: values.filters }"
:store-prefix="storePrefix"
:error="fieldHasErrors('table_id')"
/>
<FormGroup
v-if="values.table_id && !fieldHasErrors('table_id')"
:label="$t('aggregateRowsDataSourceForm.viewFieldLabel')"
class="margin-bottom-2"
small-label
required
horizontal
horizontal-narrow
>
<Dropdown
v-model="v$.values.view_id.$model"
:show-search="false"
fixed-items
:disabled="fieldsLoading"
:error="fieldHasErrors('view_id')"
>
<DropdownItem
:name="$t('aggregateRowsDataSourceForm.notSelected')"
:value="null"
>{{ $t('aggregateRowsDataSourceForm.notSelected') }}</DropdownItem
>
<DropdownItem
v-for="view in tableViews"
:key="view.id"
:name="view.name"
:value="view.id"
>
{{ view.name }}
</DropdownItem>
</Dropdown>
</FormGroup>
<FormGroup
v-if="values.table_id && !fieldHasErrors('table_id')"
class="margin-bottom-2"
small-label
:label="$t('aggregateRowsDataSourceForm.aggregationFieldLabel')"
required
horizontal
horizontal-narrow
>
<Dropdown
v-model="v$.values.field_id.$model"
:disabled="tableFields.length === 0 || fieldsLoading"
:error="fieldHasErrors('field_id')"
>
<DropdownItem
v-for="field in tableFields"
:key="field.id"
:name="field.name"
:value="field.id"
:icon="fieldIconClass(field)"
>
</DropdownItem>
</Dropdown>
</FormGroup>
<FormGroup
v-if="!fieldHasErrors('field_id')"
small-label
:label="$t('aggregateRowsDataSourceForm.aggregationTypeLabel')"
required
horizontal
horizontal-narrow
>
<Dropdown
v-model="v$.values.aggregation_type.$model"
:error="fieldHasErrors('aggregation_type')"
:disabled="fieldsLoading"
>
<DropdownItem
v-for="viewAggregation in viewAggregationTypes"
:key="viewAggregation.getType()"
:name="viewAggregation.getName()"
:value="viewAggregation.getType()"
:disabled="
unsupportedAggregationTypes.includes(viewAggregation.getType())
"
>
</DropdownItem>
</Dropdown>
</FormGroup>
</FormSection>
<DashboardServiceRefinementForms
v-if="
showOwnFilters &&
values.table_id &&
!fieldHasErrors('table_id') &&
!fieldsLoading
"
:small="small"
:values="values"
:table-fields="tableFields"
:widget="widget"
:dashboard="dashboard"
:store-prefix="storePrefix"
show-filter
/>
</form>
</template>
<script>
import DashboardTableSelector from '@baserow/modules/dashboard/components/data_source/DashboardTableSelector'
import DashboardServiceRefinementForms from '@baserow/modules/dashboard/components/data_source/DashboardServiceRefinementForms'
import { useVuelidate } from '@vuelidate/core'
import form from '@baserow/modules/core/mixins/form'
import _ from 'lodash'
import { computed } from 'vue'
import { clone } from '@baserow/modules/core/utils/object'
import serviceFiltersSync from '@baserow/modules/integrations/localBaserow/mixins/serviceFiltersSync'
import { required } from '@vuelidate/validators'
import tableFields from '@baserow/modules/database/mixins/tableFields'
const includes = (array) => (value) => {
return array.includes(value)
}
// The saved values copied into the open form when they change elsewhere, e.g. by
// another user or an undo. The filters are kept in sync by `serviceFiltersSync`.
const SYNCED_VALUES = ['table_id', 'view_id', 'field_id', 'aggregation_type']
const includesIfSet = (array) => (value) => {
if (value === null || value === undefined) {
return true
}
return array.includes(value)
}
export default {
name: 'AggregateRowsDataSourceForm',
components: { DashboardTableSelector, DashboardServiceRefinementForms },
mixins: [form, tableFields, serviceFiltersSync],
provide() {
return {
serviceFilterTarget: computed(() => ({
kind: 'dashboard_data_source',
id: this.dataSource.id,
})),
}
},
props: {
/** Whether the widget's own filter conditions can be edited here. */
showOwnFilters: {
type: Boolean,
default: true,
},
small: {
type: Boolean,
default: true,
},
dashboard: {
type: Object,
required: true,
},
widget: {
type: Object,
required: true,
},
dataSource: {
type: Object,
required: true,
},
storePrefix: {
type: String,
required: false,
default: '',
},
},
setup() {
return { v$: useVuelidate({ $lazy: true }) }
},
data() {
return {
allowedValues: [
'table_id',
'view_id',
'field_id',
'aggregation_type',
'filters',
'filter_groups',
'filter_type',
],
values: {
table_id: null,
view_id: null,
field_id: null,
aggregation_type: 'sum',
filters: [],
filter_groups: [],
filter_type: 'AND',
},
tableLoading: false,
skipFirstValuesEmit: true,
tableIdHasChanged: false,
}
},
computed: {
computedTableId: {
get() {
return this.v$.values.table_id.$model
},
set(tableId) {
if (tableId !== this.v$.values.table_id.$model) {
this.v$.values.table_id.$model = tableId
this.tableIdHasChanged = true
this.v$.values.view_id.$model = null
this.v$.values.field_id.$model = null
this.values.filters = []
this.values.filter_groups = []
}
},
},
integration() {
return this.$store.getters[
`${this.storePrefix}dashboardApplication/getIntegrationById`
](this.dataSource.integration_id)
},
databases() {
return this.integration?.context_data?.databases || []
},
databaseSelected() {
return this.databases.find((database) =>
database.tables.some((table) => table.id === this.values.table_id)
)
},
tables() {
return this.databases.map((database) => database.tables).flat()
},
tableIds() {
return this.tables.map((table) => table.id)
},
tableSelected() {
return this.tables.find(({ id }) => id === this.values.table_id)
},
tableFieldIds() {
return this.tableFields.map((field) => field.id)
},
tableViews() {
return (
this.databaseSelected?.views.filter(
(view) => view.table_id === this.values.table_id
) || []
)
},
tableViewIds() {
return this.tableViews.map((view) => view.id)
},
viewAggregationTypes() {
const selectedField = this.tableFields.find(
(field) => field.id === this.values.field_id
)
if (!selectedField) return []
return this.$registry
.getOrderedList('viewAggregation')
.filter((agg) => agg.fieldIsCompatible(selectedField))
},
aggregationTypeNames() {
return this.viewAggregationTypes.map((aggType) => aggType.getType())
},
unsupportedAggregationTypes() {
return this.$registry.get('service', 'local_baserow_aggregate_rows')
.unsupportedAggregationTypes
},
},
watch: {
'dataSource.id': {
async handler() {
this.setEmitValues(false)
// Reset the form to set default values
// again after a different widget is selected
await this.reset(true)
// Run form validation so that
// problems are highlighted immediately
this.v$.$touch()
await this.$nextTick()
this.setEmitValues(true)
},
},
dataSource: {
handler(dataSource, previous) {
// Another data source is handled by the `dataSource.id` watcher.
if (dataSource !== previous) {
return
}
const keys = SYNCED_VALUES.filter(
(key) =>
key in dataSource && !_.isEqual(this.values[key], dataSource[key])
)
if (!keys.length) {
return
}
// The values now match what's saved, so there is nothing to emit or save.
this.skipFirstValuesEmit = true
keys.forEach((key) => {
this.values[key] = clone(dataSource[key])
})
},
deep: true,
},
fieldsLoading(loading) {
if (this.tableIdHasChanged && !loading) {
if (this.tableFields.length > 0) {
this.v$.values.field_id.$model = this.tableFields[0].id
}
this.tableIdHasChanged = false
}
},
'values.field_id': {
handler(fieldId) {
if (fieldId !== null) {
if (
!this.viewAggregationTypes.some(
(agg) => agg.getType() === this.v$.values.aggregation_type.$model
)
) {
if (this.viewAggregationTypes.length > 0) {
this.v$.values.aggregation_type.$model =
this.viewAggregationTypes[0].getType()
}
}
}
},
immediate: false,
},
},
mounted() {
this.v$.$validate()
},
validations() {
return {
values: {
table_id: {
required,
isValidTableId: (value) => {
const ids = this.tableIds
return includes(ids)(value)
},
},
view_id: {
isValidViewId: (value) => {
const ids = this.tableViewIds
return includesIfSet(ids)(value)
},
},
field_id: {
required,
isValidFieldId: (value) => {
const ids = this.tableFieldIds
return includes(ids)(value)
},
},
aggregation_type: {
required,
isValidAggregationType: (value) => {
const types = this.aggregationTypeNames
return includes(types)(value)
},
},
},
}
},
methods: {
getServiceFiltersSource() {
return this.dataSource
},
/* Overrides the method in the tableFields mixin */
getTableId() {
return this.values.table_id
},
fieldIconClass(field) {
const fieldType = this.$registry.get('field', field.type)
return fieldType.iconClass
},
},
}
</script>