Merge branch 'master' into 12033-multisite-search
[arvados.git] / apps / workbench / app / assets / javascripts / components / save_ui_state.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 window.SaveUIState = {
6     saveState: function() {
7         var state = history.state || {}
8         state.bodyHeight = window.getComputedStyle(document.body)['height']
9         state.currentState = this.currentState()
10         history.replaceState(state, '')
11     },
12     oninit: function(vnode) {
13         vnode.state.currentState = vnode.attrs.currentState
14         var hstate = history.state || {}
15
16         if (vnode.attrs.saveBodyHeight && hstate.bodyHeight) {
17             document.body.style['min-height'] = hstate.bodyHeight
18             delete hstate.bodyHeight
19         }
20
21         if (hstate.currentState) {
22             vnode.attrs.currentState(hstate.currentState)
23             delete hstate.currentState
24         } else {
25             vnode.attrs.currentState(vnode.attrs.defaultState)
26         }
27
28         history.replaceState(hstate, '')
29     },
30     oncreate: function(vnode) {
31         vnode.state.saveState = vnode.state.saveState.bind(vnode.state)
32         window.addEventListener('beforeunload', vnode.state.saveState)
33         vnode.state.onupdate(vnode)
34     },
35     onupdate: function(vnode) {
36         if (vnode.attrs.saveBodyHeight && vnode.attrs.forgetSavedState) {
37             document.body.style['min-height'] = null
38         }
39     },
40     onremove: function(vnode) {
41         window.removeEventListener('beforeunload', vnode.state.saveState)
42     },
43     view: function(vnode) {
44         return null
45     },
46 }