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 {
|
2024-11-28 08:47:54 +00:00
|
|
|
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
|
|
|
|
},
|
|
|
|
|
2023-08-08 13:28:03 +00:00
|
|
|
parentElement() {
|
|
|
|
return this.$store.getters['element/getElementById'](
|
2024-11-28 08:47:54 +00:00
|
|
|
this.elementPage,
|
2023-08-08 13:28:03 +00:00
|
|
|
this.element?.parent_element_id
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2024-11-28 08:47:54 +00:00
|
|
|
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) {
|
2024-05-02 14:25:42 +00:00
|
|
|
if (
|
|
|
|
!this.$hasPermission(
|
|
|
|
'builder.page.element.update',
|
|
|
|
this.element,
|
|
|
|
this.workspace.id
|
|
|
|
) ||
|
2024-10-30 15:32:27 +00:00
|
|
|
!this.$refs.panelForm?.isFormValid(true)
|
2024-05-02 14:25:42 +00:00
|
|
|
) {
|
2023-08-04 14:29:34 +00:00
|
|
|
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({
|
2024-11-28 08:47:54 +00:00
|
|
|
page: this.elementPage,
|
2023-06-27 09:29:02 +00:00
|
|
|
// Here we clone the values to prevent
|
2023-08-04 14:29:34 +00:00
|
|
|
// "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
|
2024-10-30 15:32:27 +00:00
|
|
|
this.$refs.panelForm?.reset(true)
|
2023-06-27 09:29:02 +00:00
|
|
|
notifyIf(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|