1
0
Fork 0
mirror of https://gitlab.com/bramw/baserow.git synced 2025-03-16 05:23:33 +00:00
bramw_baserow/web-frontend/modules/builder/mixins/elementSidePanel.js

93 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2023-06-27 09:29:02 +00:00
import { mapActions, mapGetters } from 'vuex'
import _ from 'lodash'
import { clone } from '@baserow/modules/core/utils/object'
import { notifyIf } from '@baserow/modules/core/utils/error'
export default {
inject: ['workspace', 'builder', 'applicationContext'],
provide() {
return {
applicationContext: {
...this.applicationContext,
element: this.element,
page: this.elementPage,
},
// We add the current element page
elementPage: this.elementPage,
}
},
2023-06-27 09:29:02 +00:00
computed: {
...mapGetters({
element: 'element/getSelected',
}),
elementType() {
if (this.element) {
return this.$registry.get('element', this.element.type)
}
return null
},
parentElement() {
return this.$store.getters['element/getElementById'](
this.elementPage,
this.element?.parent_element_id
)
},
elementPage() {
// We use the page from the element itself
return this.$store.getters['page/getById'](
this.builder,
this.element.page_id
)
},
2023-06-27 09:29:02 +00:00
defaultValues() {
return this.element
},
},
methods: {
...mapActions({
actionDebouncedUpdateSelectedElement: 'element/debouncedUpdateSelected',
}),
async onChange(newValues) {
if (
!this.$hasPermission(
'builder.page.element.update',
this.element,
this.workspace.id
) ||
!this.$refs.panelForm?.isFormValid(true)
) {
return
}
2023-10-24 11:50:14 +00:00
const differences = Object.fromEntries(
Object.entries(newValues).filter(
([key, value]) => !_.isEqual(value, this.element[key])
)
)
2023-12-08 14:15:07 +00:00
// We never want to update the order this way
delete differences.order
2023-10-24 11:50:14 +00:00
if (Object.keys(differences).length > 0) {
2023-06-27 09:29:02 +00:00
try {
await this.actionDebouncedUpdateSelectedElement({
page: this.elementPage,
2023-06-27 09:29:02 +00:00
// Here we clone the values to prevent
// "modification outside of the store" error
2023-10-24 11:50:14 +00:00
values: clone(differences),
2023-06-27 09:29:02 +00:00
})
} catch (error) {
// Restore the previous saved values from the store
this.$refs.panelForm?.reset(true)
2023-06-27 09:29:02 +00:00
notifyIf(error)
}
}
},
},
}