mirror of
https://github.com/baserow/baserow.git
synced 2026-12-04 08:18:42 +00:00
390 lines
11 KiB
Vue
390 lines
11 KiB
Vue
<template>
|
|
<Context ref="context" class="group-bys" max-height-if-outside-viewport>
|
|
<div class="group-bys__content">
|
|
<div v-if="view.group_bys.length > 0" class="group-bys__layout">
|
|
<div class="group-bys__layout-label">
|
|
{{ $t('viewGroupByContext.layout') }}
|
|
</div>
|
|
<SegmentControl
|
|
:segments="layoutSegments"
|
|
:active-index="activeLayoutIndex"
|
|
size="small"
|
|
@update:active-index="setLayout"
|
|
></SegmentControl>
|
|
</div>
|
|
<div
|
|
v-if="view.group_bys.length === 0"
|
|
v-auto-overflow-scroll
|
|
class="group-bys__none group-bys__none--scrollable"
|
|
>
|
|
<div class="group-bys__none-title">
|
|
{{ $t('viewGroupByContext.noGroupByTitle') }}
|
|
</div>
|
|
<div class="group-bys__none-description">
|
|
{{ $t('viewGroupByContext.noGroupByText') }}
|
|
</div>
|
|
</div>
|
|
|
|
<div
|
|
v-if="view.group_bys.length > 0"
|
|
v-auto-overflow-scroll
|
|
class="group-bys__items group-bys__items--scrollable"
|
|
>
|
|
<div
|
|
v-for="(groupBy, index) in view.group_bys"
|
|
:key="groupBy.id"
|
|
v-sortable="{
|
|
id: groupBy.id,
|
|
update: prioritizeGroupBys,
|
|
handle: '[data-group-by-handle]',
|
|
enabled: !disableGroupBy,
|
|
marginTop: -8,
|
|
marginTopLast: 8,
|
|
marginLeft: 16,
|
|
marginRight: 16,
|
|
}"
|
|
class="group-bys__item"
|
|
:class="{
|
|
'group-bys__item--loading': groupBy._.loading,
|
|
}"
|
|
:set="field = getField(groupBy.field)"
|
|
>
|
|
<a
|
|
v-if="!disableGroupBy"
|
|
class="group-bys__handle"
|
|
data-group-by-handle
|
|
></a>
|
|
<a
|
|
v-if="!disableGroupBy"
|
|
class="group-bys__remove"
|
|
@click="deleteGroupBy(groupBy)"
|
|
>
|
|
<i class="iconoir-cancel"></i>
|
|
</a>
|
|
<div class="group-bys__description">
|
|
<template v-if="index === 0">{{
|
|
$t('viewGroupByContext.groupBy')
|
|
}}</template>
|
|
<template v-if="index > 0">{{
|
|
$t('viewGroupByContext.thenBy')
|
|
}}</template>
|
|
</div>
|
|
<div class="group-bys__field">
|
|
<Dropdown
|
|
:value="groupBy.field"
|
|
:disabled="disableGroupBy"
|
|
:fixed-items="true"
|
|
class="dropdown--floating"
|
|
@input="updateGroupBy(groupBy, { field: $event })"
|
|
>
|
|
<DropdownItem
|
|
v-for="field in fields"
|
|
:key="'groupBy-field-' + groupBy.id + '-' + field.id"
|
|
:name="field.name"
|
|
:value="field.id"
|
|
:disabled="
|
|
groupBy.field !== field.id && !isFieldAvailable(field)
|
|
"
|
|
>
|
|
</DropdownItem>
|
|
</Dropdown>
|
|
</div>
|
|
<ViewSortOrder
|
|
v-if="field"
|
|
:disabled="disableGroupBy"
|
|
:sort-types="getSortTypes(field)"
|
|
:type="groupBy.type"
|
|
:order="groupBy.order"
|
|
@update-order="
|
|
updateGroupBy(groupBy, { order: $event.order, type: $event.type })
|
|
"
|
|
></ViewSortOrder>
|
|
<i
|
|
v-else
|
|
v-tooltip="$t('viewFilterContext.relatedFieldNotFound')"
|
|
class="iconoir-warning-triangle color-error"
|
|
></i>
|
|
</div>
|
|
<div v-if="contextWarning" class="group-bys__warning">
|
|
{{ contextWarning }}
|
|
</div>
|
|
</div>
|
|
<div
|
|
v-if="canAddGroupBy || view.group_bys.length > 0"
|
|
class="context__footer group-bys__footer"
|
|
>
|
|
<span
|
|
v-if="canAddGroupBy"
|
|
v-tooltip="
|
|
atGroupByLimit
|
|
? $t('viewGroupByContext.maxGroupBysReached', {
|
|
count: maxGroupBys,
|
|
})
|
|
: null
|
|
"
|
|
>
|
|
<ButtonText
|
|
ref="addDropdownToggle"
|
|
icon="iconoir-plus"
|
|
:disabled="atGroupByLimit"
|
|
@click="$refs.addDropdown.toggle($refs.addDropdownToggle.$el)"
|
|
>
|
|
{{ $t('viewGroupByContext.addGroupBy') }}</ButtonText
|
|
>
|
|
</span>
|
|
<div class="group-bys__add">
|
|
<Dropdown
|
|
ref="addDropdown"
|
|
:show-input="false"
|
|
:fixed-items="true"
|
|
@input="addGroupBy"
|
|
>
|
|
<DropdownItem
|
|
v-for="field in availableFields"
|
|
:key="field.id"
|
|
:name="field.name"
|
|
:value="field.id"
|
|
:icon="getFieldType(field).iconClass"
|
|
></DropdownItem>
|
|
</Dropdown>
|
|
</div>
|
|
<div v-if="view.group_bys.length > 0" class="group-bys__footer-actions">
|
|
<template v-if="!isColumnLayout">
|
|
<ButtonText @click.prevent="setGroupByCollapseAll(true)">
|
|
{{ $t('viewGroupByContext.collapseAllGroups') }}
|
|
</ButtonText>
|
|
<ButtonText @click.prevent="setGroupByCollapseAll(false)">
|
|
{{ $t('viewGroupByContext.expandAllGroups') }}
|
|
</ButtonText>
|
|
</template>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</Context>
|
|
</template>
|
|
|
|
<script>
|
|
import { notifyIf } from '@baserow/modules/core/utils/error'
|
|
import context from '@baserow/modules/core/mixins/context'
|
|
import {
|
|
DEFAULT_SORT_TYPE_KEY,
|
|
MAX_GROUP_BYS,
|
|
} from '@baserow/modules/database/constants'
|
|
import ViewSortOrder from '@baserow/modules/database/components/view/ViewSortOrder.vue'
|
|
|
|
const GROUP_BY_LAYOUTS = ['section', 'column']
|
|
|
|
export default {
|
|
name: 'ViewGroupByContext',
|
|
components: { ViewSortOrder },
|
|
mixins: [context],
|
|
props: {
|
|
database: {
|
|
type: Object,
|
|
required: true,
|
|
},
|
|
fields: {
|
|
type: Array,
|
|
required: true,
|
|
},
|
|
view: {
|
|
type: Object,
|
|
required: true,
|
|
},
|
|
readOnly: {
|
|
type: Boolean,
|
|
required: true,
|
|
},
|
|
disableGroupBy: {
|
|
type: Boolean,
|
|
required: true,
|
|
},
|
|
storePrefix: {
|
|
type: String,
|
|
required: false,
|
|
default: '',
|
|
},
|
|
},
|
|
emits: ['changed'],
|
|
data() {
|
|
return {
|
|
maxGroupBys: MAX_GROUP_BYS,
|
|
}
|
|
},
|
|
computed: {
|
|
/**
|
|
* Soft cap: once a view reaches the maximum number of group-bys, the add button
|
|
* is disabled (existing group-bys can still be removed). Views that already have
|
|
* more keep working.
|
|
*/
|
|
atGroupByLimit() {
|
|
return this.view.group_bys.length >= this.maxGroupBys
|
|
},
|
|
isColumnLayout() {
|
|
return this.view.group_by_layout === 'column'
|
|
},
|
|
activeLayoutIndex() {
|
|
return Math.max(0, GROUP_BY_LAYOUTS.indexOf(this.view.group_by_layout))
|
|
},
|
|
layoutSegments() {
|
|
return [
|
|
{ label: this.$t('viewGroupByContext.layoutSection') },
|
|
{ label: this.$t('viewGroupByContext.layoutColumn') },
|
|
]
|
|
},
|
|
availableFields() {
|
|
return this.fields.filter((f) => this.isFieldAvailable(f))
|
|
},
|
|
canAddGroupBy() {
|
|
return this.availableFields.length > 0 && !this.disableGroupBy
|
|
},
|
|
contextWarning() {
|
|
const ownershipType = this.$registry.get(
|
|
'viewOwnershipType',
|
|
this.view.ownership_type
|
|
)
|
|
return ownershipType.getGroupByContextWarning(
|
|
this.view,
|
|
this.fields,
|
|
this.database,
|
|
this.storePrefix
|
|
)
|
|
},
|
|
},
|
|
methods: {
|
|
getFieldType(field) {
|
|
return this.$registry.get('field', field.type)
|
|
},
|
|
getCanGroupByInView(field) {
|
|
return this.getFieldType(field).getCanGroupByInView(field)
|
|
},
|
|
getField(fieldId) {
|
|
for (const i in this.fields) {
|
|
if (this.fields[i].id === fieldId) {
|
|
return this.fields[i]
|
|
}
|
|
}
|
|
return undefined
|
|
},
|
|
isFieldAvailable(field) {
|
|
const allFieldIds = this.view.group_bys.map((groupBy) => groupBy.field)
|
|
return this.getCanGroupByInView(field) && !allFieldIds.includes(field.id)
|
|
},
|
|
async addGroupBy(fieldId) {
|
|
this.$refs.addDropdown.hide()
|
|
|
|
try {
|
|
await this.$store.dispatch('view/createGroupBy', {
|
|
view: this.view,
|
|
values: {
|
|
field: fieldId,
|
|
value: 'ASC',
|
|
type: DEFAULT_SORT_TYPE_KEY,
|
|
},
|
|
readOnly: this.readOnly,
|
|
})
|
|
this.$emit('changed')
|
|
} catch (error) {
|
|
notifyIf(error, 'view')
|
|
}
|
|
},
|
|
async prioritizeGroupBys(viewGroupByIds, oldViewGroupByIds) {
|
|
if (this.disableGroupBy) {
|
|
return
|
|
}
|
|
|
|
try {
|
|
await this.$store.dispatch('view/prioritizeGroupBys', {
|
|
view: this.view,
|
|
viewGroupByIds,
|
|
oldViewGroupByIds,
|
|
readOnly: this.readOnly,
|
|
})
|
|
this.$emit('changed')
|
|
} catch (error) {
|
|
notifyIf(error, 'view')
|
|
}
|
|
},
|
|
async deleteGroupBy(groupBy) {
|
|
try {
|
|
await this.$store.dispatch('view/deleteGroupBy', {
|
|
view: this.view,
|
|
groupBy,
|
|
readOnly: this.readOnly,
|
|
})
|
|
this.$emit('changed')
|
|
} catch (error) {
|
|
notifyIf(error, 'view')
|
|
}
|
|
},
|
|
async updateGroupBy(groupBy, values) {
|
|
if (this.disableGroupBy) {
|
|
return
|
|
}
|
|
|
|
// If the field has changed, the type might not be compatible anymore. If so,
|
|
// then we're falling back on the default sort type.
|
|
if (values.field) {
|
|
const sortType = values.type || groupBy.type
|
|
const field = this.getField(values.field)
|
|
const fieldType = this.getFieldType(field)
|
|
const sortTypes = fieldType.getSortTypes(field)
|
|
if (!Object.prototype.hasOwnProperty.call(sortTypes, sortType)) {
|
|
values.type = DEFAULT_SORT_TYPE_KEY
|
|
}
|
|
}
|
|
|
|
try {
|
|
await this.$store.dispatch('view/updateGroupBy', {
|
|
groupBy,
|
|
values,
|
|
readOnly: this.readOnly,
|
|
})
|
|
this.$emit('changed')
|
|
} catch (error) {
|
|
notifyIf(error, 'view')
|
|
}
|
|
},
|
|
getSortTypes(field) {
|
|
return this.getFieldType(field).getSortTypes(field)
|
|
},
|
|
async setLayout(index) {
|
|
const value = GROUP_BY_LAYOUTS[index]
|
|
if (value === this.view.group_by_layout) {
|
|
return
|
|
}
|
|
try {
|
|
await this.$store.dispatch('view/update', {
|
|
view: this.view,
|
|
values: { group_by_layout: value },
|
|
readOnly:
|
|
this.readOnly ||
|
|
!this.$hasPermission(
|
|
'database.table.view.update',
|
|
this.view,
|
|
this.database.workspace.id
|
|
),
|
|
})
|
|
} catch (error) {
|
|
notifyIf(error, 'view')
|
|
}
|
|
},
|
|
async setGroupByCollapseAll(collapse) {
|
|
try {
|
|
await this.$store.dispatch(
|
|
this.storePrefix + 'view/grid/setGroupByCollapseAll',
|
|
{
|
|
view: this.view,
|
|
fields: this.fields,
|
|
collapse,
|
|
}
|
|
)
|
|
this.$refs.context.hide()
|
|
} catch (error) {
|
|
notifyIf(error, 'view')
|
|
}
|
|
},
|
|
},
|
|
}
|
|
</script>
|