1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 // SaveUIState avoids losing scroll position due to navigation
6 // events, and saves/restores other caller-specified UI state.
8 // It does not display any content itself: do not pass any children.
10 // Use of multiple SaveUIState components on the same page is not
13 // The problem being solved:
15 // Page 1 loads some content dynamically (e.g., via infinite scroll)
16 // after the initial render. User scrolls down, clicks a link, and
17 // lands on page 2. User clicks the Back button, and lands on page
18 // 1. Page 1 renders its initial content while waiting for AJAX.
20 // But (without SaveUIState) the document body is small now, so the
21 // browser resets scroll position to the top of the page. Even if we
22 // end up displaying the same dynamic content, the user's place on the
23 // page has been lost.
25 // SaveUIState fixes this by stashing the current body height when
26 // navigating away from page 1. When navigating back, it restores the
27 // body height even before the page has loaded, so the browser does
28 // not reset the scroll position.
30 // SaveUIState also saves/restores arbitrary UI state (like text typed
31 // in a search box) in response to navigation events.
33 // See CollectionsSearch for an example.
37 // {getter-setter} currentState: the current UI state
39 // {any} defaultState: value to initialize currentState with, if
40 // nothing is stashed in browser history.
42 // {boolean} forgetSavedHeight: the body height loaded from the
43 // browser history (if any) is outdated; we should let the browser
44 // determine the correct body height from the current page
45 // content. Set this when dynamic content has been reset.
47 // {boolean} saveBodyHeight: save/restore body height as described
49 window.SaveUIState = {
50 saveState: function() {
51 var state = history.state || {}
52 state.bodyHeight = window.getComputedStyle(document.body)['height']
53 state.currentState = this.currentState()
54 history.replaceState(state, '')
56 oninit: function(vnode) {
57 vnode.state.currentState = vnode.attrs.currentState
58 var hstate = history.state || {}
60 if (vnode.attrs.saveBodyHeight && hstate.bodyHeight) {
61 document.body.style['min-height'] = hstate.bodyHeight
62 delete hstate.bodyHeight
65 if (hstate.currentState) {
66 vnode.attrs.currentState(hstate.currentState)
67 delete hstate.currentState
69 vnode.attrs.currentState(vnode.attrs.defaultState)
72 history.replaceState(hstate, '')
74 oncreate: function(vnode) {
75 vnode.state.saveState = vnode.state.saveState.bind(vnode.state)
76 window.addEventListener('beforeunload', vnode.state.saveState)
77 vnode.state.onupdate(vnode)
79 onupdate: function(vnode) {
80 if (vnode.attrs.saveBodyHeight && vnode.attrs.forgetSavedHeight) {
81 document.body.style['min-height'] = null
84 onremove: function(vnode) {
85 window.removeEventListener('beforeunload', vnode.state.saveState)
87 view: function(vnode) {