Commit f227d985 authored by Rafael Ostertag's avatar Rafael Ostertag
Browse files

PageState.js: allows to use window.history.state, which is dubbed `data`....

PageState.js: allows to use window.history.state, which is dubbed `data`. PopState Handlers are now called with the PageState object as first and only argument, in order to allow access to `data`.
QfqPage.js: use data to store page title in page state. Constructor sets page state explicitly, if none exists so far by setting it to the active tab; this allows to navigate to the first state.
parent d8ea13a9
...@@ -12,6 +12,7 @@ if (!QfqNS) { ...@@ -12,6 +12,7 @@ if (!QfqNS) {
n.PageState = function () { n.PageState = function () {
this.pageState = location.hash.slice(1); this.pageState = location.hash.slice(1);
this.data = null;
this.inPoppingHandler = false; this.inPoppingHandler = false;
this.userPopStateHandlers = []; this.userPopStateHandlers = [];
...@@ -29,10 +30,11 @@ if (!QfqNS) { ...@@ -29,10 +30,11 @@ if (!QfqNS) {
this.inPoppingHandler = true; this.inPoppingHandler = true;
this.pageState = location.hash.slice(1); this.pageState = location.hash.slice(1);
this.data = window.history.state;
QfqNS.Log.debug("PageState.popStateHandler(): invoke user pop state handler(s)"); QfqNS.Log.debug("PageState.popStateHandler(): invoke user pop state handler(s)");
this.userPopStateHandlers.forEach(function (handler) { this.userPopStateHandlers.forEach(function (handler) {
handler(this.pageState); handler(this);
}, this); }, this);
this.inPoppingHandler = false; this.inPoppingHandler = false;
...@@ -44,24 +46,31 @@ if (!QfqNS) { ...@@ -44,24 +46,31 @@ if (!QfqNS) {
return this.pageState; return this.pageState;
}; };
n.PageState.prototype.setPageState = function (state) { n.PageState.prototype.getPageData = function () {
return this.data;
};
n.PageState.prototype.setPageState = function (state, data) {
if (state.startsWith('#')) { if (state.startsWith('#')) {
this.pageState = state.slice(1); this.pageState = state.slice(1);
window.history.replaceState(null, null, state); window.history.replaceState(data, null, state);
} else { } else {
this.pageState = state; this.pageState = state;
window.history.replaceState(null, null, '#' + state); window.history.replaceState(data, null, '#' + state);
} }
this.data = data;
}; };
n.PageState.prototype.newPageState = function (state) { n.PageState.prototype.newPageState = function (state, data) {
if (state.startsWith('#')) { if (state.startsWith('#')) {
this.pageState = state.slice(1); this.pageState = state.slice(1);
window.history.pushState(null, null, state); window.history.pushState(data, null, state);
} else { } else {
this.pageState = state; this.pageState = state;
window.history.pushState(null, null, '#' + state); window.history.pushState(data, null, '#' + state);
} }
this.data = data;
}; };
n.PageState.prototype.addStateActivationHandler = function (handler) { n.PageState.prototype.addStateActivationHandler = function (handler) {
......
...@@ -22,8 +22,12 @@ if (!QfqNS) { ...@@ -22,8 +22,12 @@ if (!QfqNS) {
this.bsTabs = new QfqNS.BSTabs(this.settings.tabsId); this.bsTabs = new QfqNS.BSTabs(this.settings.tabsId);
if (this.settings.pageState.getPageState() !== "") { var currentState = this.settings.pageState.getPageState();
this.bsTabs.activateTab(this.settings.pageState.getPageState()); if (currentState !== "") {
this.bsTabs.activateTab(currentState);
QfqNS.PageTitle.setSubTitle(this.bsTabs.getTabName(currentState));
} else {
this.settings.pageState.setPageState(this.bsTabs.getCurrentTab(), QfqNS.PageTitle.get());
} }
this.bsTabs.addTabShowHandler(this.tabShowHandler.bind(this)); this.bsTabs.addTabShowHandler(this.tabShowHandler.bind(this));
...@@ -37,18 +41,20 @@ if (!QfqNS) { ...@@ -37,18 +41,20 @@ if (!QfqNS) {
// Therefore, we have to make sure, that tabShowHandler() does not save the page state while we're restoring // Therefore, we have to make sure, that tabShowHandler() does not save the page state while we're restoring
// a previous state, i.e. we're called because of the popStateHandler() below. // a previous state, i.e. we're called because of the popStateHandler() below.
if (this.settings.pageState.inPoppingHandler) { if (this.settings.pageState.inPoppingHandler) {
QfqNS.Log.debug("Prematurely terminating QfqPage.tabShowHandler(): called while due to page state" + QfqNS.Log.debug("Prematurely terminating QfqPage.tabShowHandler(): called due to page state" +
" restoration."); " restoration.");
return; return;
} }
var currentTabId = bsTabs.getCurrentTab(); var currentTabId = bsTabs.getCurrentTab();
QfqNS.Log.debug('Saving state: ' + currentTabId); QfqNS.Log.debug('Saving state: ' + currentTabId);
this.settings.pageState.newPageState(currentTabId);
QfqNS.PageTitle.setSubTitle(bsTabs.getTabName(currentTabId)); QfqNS.PageTitle.setSubTitle(bsTabs.getTabName(currentTabId));
this.settings.pageState.newPageState(currentTabId, QfqNS.PageTitle.get());
}; };
n.QfqPage.prototype.popStateHandler = function (state) { n.QfqPage.prototype.popStateHandler = function (pageState) {
this.bsTabs.activateTab(state); this.bsTabs.activateTab(pageState.getPageState());
QfqNS.PageTitle.set(pageState.getPageData());
}; };
})(QfqNS); })(QfqNS);
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment