Skip to content

Commit

Permalink
persist resource table scroll position on navigate back
Browse files Browse the repository at this point in the history
  • Loading branch information
omohokcoj committed Apr 8, 2023
1 parent 88331b6 commit a57126d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ui/src/data_resources/components/audit_item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export default {
this.$emit('revert', result.data.data)
}).catch((error) => {
console.log(error)
console.error(error)
this.$Message.error(`${this.i18n.action_has_failed_with_code} ${error.response.status}`)
})
Expand Down
1 change: 1 addition & 0 deletions ui/src/data_resources/components/resource.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
/>
<ResourceTable
v-else-if="showTable"
ref="table"
:key="resourceName + resourceId + associationName"
:height="isFullscreenTable ? 'calc(var(--vh, 100vh) - 199px)' : 'calc((var(--vh) / 2) - 108px)'"
:with-resize="true"
Expand Down
8 changes: 7 additions & 1 deletion ui/src/data_resources/components/table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,13 @@ export default {
mounted () {
this.assignCachedItemsCount()
this.assignFromQueryParams(this.$route.query)
this.loadDataAndCount()
this.loadDataAndCount().then(() => {
if (typeof history.state.resourceTableScrollTop === 'number') {
this.$nextTick(() => {
this.$refs.table.scrollTo(history.state.resourceTableScrollTop, history.state.resourceTableScrollLeft)
})
}
})
},
methods: {
widthLessThan,
Expand Down
20 changes: 20 additions & 0 deletions ui/src/data_resources/pages/base.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
/>
<Resource
v-if="fragments.length > 1"
ref="resource"
:resource-name="resourceName"
:resource-id="resourceId"
:association-name="associationName"
Expand All @@ -52,6 +53,7 @@
/>
<ResourceTable
v-else
ref="table"
:key="resourceName"
:height="`calc(var(--vh, 100vh) - ${widthLessThan('sm') ? '200px' : '148px'})`"
:with-title="!widthLessThan('sm')"
Expand Down Expand Up @@ -90,6 +92,22 @@ import { breadcrumbStore } from 'navigation/scripts/breadcrumb_store'
import { isShowSettings, closeSettings } from 'settings/scripts/toggle'
import SettingsMask from 'settings/components/mask'
function pushResourceTableScrollState () {
if (this.$refs.table) {
history.replaceState({
...history.state,
resourceTableScrollTop: this.$refs.table.$refs.table.$refs.wrapper.scrollTop,
resourceTableScrollLeft: this.$refs.table.$refs.table.$refs.wrapper.scrollLeft
}, document.title, location.href)
} else if (this.$refs.resource?.$refs?.table) {
history.replaceState({
...history.state,
resourceTableScrollTop: this.$refs.resource.$refs.table.$refs.table.$refs.wrapper.scrollTop,
resourceTableScrollLeft: this.$refs.resource.$refs.table.$refs.table.$refs.wrapper.scrollLeft
}, document.title, location.href)
}
}
export default {
name: 'ResourcesBase',
components: {
Expand All @@ -101,6 +119,8 @@ export default {
Home,
SettingsMask
},
beforeRouteUpdate: pushResourceTableScrollState,
beforeRouteLeave: pushResourceTableScrollState,
data () {
return {
isMenuSider: false,
Expand Down
9 changes: 8 additions & 1 deletion ui/src/data_tables/components/table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,14 @@ export default {
},
methods: {
scrollToTop () {
this.$refs.wrapper.scrollTop = 0
this.scrollTo(0)
},
scrollTo (top, left) {
this.$refs.wrapper.scrollTop = top
if (typeof left === 'number') {
this.$refs.wrapper.scrollLeft = left
}
},
onRowClick (row) {
setTimeout(() => {
Expand Down
4 changes: 4 additions & 0 deletions ui/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,7 @@ document.addEventListener('DOMContentLoaded', () => {

subscribeToNotifications()
})

window.onbeforeunload = () => {
history.replaceState({ ...history.state, resourceTableScrollTop: 0, resourceTableScrollLeft: 0 }, document.title, location.href)
}

0 comments on commit a57126d

Please sign in to comment.