Skip to content

The Unified and Autonomous Development Platform

Able to remember scroll position when back/forward

robin shine36d0e1fe5238
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/common.js b/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/common.js
index 3abe1591f9..62153db067 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/common.js
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/common.js
@@ -818,19 +818,34 @@ pmease.commons = {
});
},
- setupHistory: function() {
- // Use a timeout here solve the problem that Safari (and previous versions of Chrome)
- // fires event "onpopstate" on initial page load and this causes the page to reload
- // infinitely
- setTimeout(function() {
- window.onpopstate = function(event) {
- if (!event.state)
- location.reload();
- else
- $(document).trigger("onpopstate", [event.state.component, event.state.state]);
- };
- }, 1000);
- }
+ history: {
+ init: function() {
+ // Use a timeout here solve the problem that Safari (and previous versions of Chrome)
+ // fires event "onpopstate" on initial page load and this causes the page to reload
+ // infinitely
+ setTimeout(function() {
+ window.onpopstate = function(event) {
+ if (!event.state || !event.state.state)
+ location.reload();
+ else
+ $(document).trigger("onpopstate", [event.state.component, event.state.state]);
+ };
+ }, 1000);
+ },
+ setScrollPos: function(scrollPos) {
+ var state = history.state;
+ if (!state)
+ state = {};
+ history.replaceState({scrollPos: scrollPos, state: state.state, component: state.component}, '', window.location.href );
+ },
+ getScrollPos: function() {
+ if (history.state && history.state.scrollPos)
+ return history.state.scrollPos;
+ else
+ return undefined;
+ }
+ },
+
};
$(function() {
@@ -840,5 +855,5 @@ $(function() {
pmease.commons.focus.setupAutoFocus();
pmease.commons.scroll.setupScrollStop();
pmease.commons.websocket.setupCallback();
- pmease.commons.setupHistory();
+ pmease.commons.history.init();
});
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/HistoryBehavior.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/HistoryBehavior.java
index cf54bbcf6c..95fc537945 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/HistoryBehavior.java
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/HistoryBehavior.java
@@ -1,5 +1,7 @@
package com.pmease.commons.wicket.behavior;
+import static org.apache.wicket.ajax.attributes.CallbackParameter.resolved;
+
import java.io.Serializable;
import org.apache.commons.codec.binary.Base64;
@@ -9,9 +11,9 @@ import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes.Method;
-import org.apache.wicket.ajax.attributes.CallbackParameter;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
+import org.apache.wicket.request.IRequestParameters;
import org.apache.wicket.request.cycle.RequestCycle;
@SuppressWarnings("serial")
@@ -25,8 +27,9 @@ public abstract class HistoryBehavior extends AbstractDefaultAjaxBehavior {
@Override
protected void respond(AjaxRequestTarget target) {
- String encodedState = RequestCycle.get().getRequest().getPostParameters()
- .getParameterValue("state").toOptionalString();
+ IRequestParameters params = RequestCycle.get().getRequest().getPostParameters();
+ String encodedState = params.getParameterValue("state").toString();
+
byte[] bytes = Base64.decodeBase64(encodedState.getBytes());
Serializable state = (Serializable) SerializationUtils.deserialize(bytes);
onPopState(target, state);
@@ -45,13 +48,13 @@ public abstract class HistoryBehavior extends AbstractDefaultAjaxBehavior {
+ " }"
+ "});",
getComponent().getPageRelativePath(),
- getCallbackFunctionBody(CallbackParameter.resolved("state", "state")));
+ getCallbackFunctionBody(resolved("state", "state")));
response.render(OnDomReadyHeaderItem.forScript(script));
}
public void pushState(AjaxRequestTarget target, String url, Serializable state) {
String encodedState = new String(Base64.encodeBase64(SerializationUtils.serialize(state)));
- target.appendJavaScript(String.format("history.pushState({state:'%s', component:'%s'}, '', '%s');",
+ target.prependJavaScript(String.format("history.pushState({state:'%s', component:'%s'}, '', '%s');",
encodedState, getComponent().getPageRelativePath(), url));
}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/blob-view.js b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/blob-view.js
index 50279ee7f8..d3a28ac4fd 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/blob-view.js
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/blob-view.js
@@ -2,11 +2,14 @@ gitplex.blobView = function(containerId) {
var $blobView = $("#" + containerId + ">.blob-view");
var $head = $blobView.find(">.head");
var $body = $blobView.find(">.body");
+ $body.scroll(function() {
+ pmease.commons.history.setScrollPos({left: $body.scrollLeft(), top: $body.scrollTop()});
+ });
gitplex.spaceGreedy.getScrollTop = function() {
return $body.scrollTop();
};
-
+
$blobView.on("autofit", function(event, width, height) {
event.stopPropagation();
$blobView.outerWidth(width);
@@ -15,5 +18,11 @@ gitplex.blobView = function(containerId) {
height = $blobView.height()-$head.outerHeight();
$body.outerWidth(width).outerHeight(height);
$body.closestDescendant(".autofit").trigger("autofit", [$body.width(), $body.height()]);
+
+ var scrollPos = pmease.commons.history.getScrollPos();
+ if (scrollPos) {
+ $body.scrollLeft(scrollPos.left);
+ $body.scrollTop(scrollPos.top);
+ }
});
}
\ No newline at end of file
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.js b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.js
index da2b5a17a1..a712fb9a2c 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.js
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.js
@@ -87,9 +87,19 @@ gitplex.sourceview = {
cm.setOption("mode", modeInfo.mime);
CodeMirror.autoLoadMode(cm, modeInfo.mode);
}
+
+ cm.on("scroll", function() {
+ var scrollInfo = cm.getScrollInfo();
+ pmease.commons.history.setScrollPos({left: scrollInfo.left, top: scrollInfo.top});
+ });
if (tokenPos)
gitplex.sourceview.highlightToken(cm, tokenPos);
+
+ var scrollPos = pmease.commons.history.getScrollPos();
+ if (scrollPos)
+ cm.scrollTo(scrollPos.left, scrollPos.top);
+
if (blameBlocks) {
// render blame blocks with a timer to avoid the issue that occasionally
// blame gutter becomes much wider than expected
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/filelist/file-list.js b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/filelist/file-list.js
index 6659453ef6..f43b4061ca 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/filelist/file-list.js
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/filelist/file-list.js
@@ -11,6 +11,16 @@ gitplex.filelist = {
event.stopPropagation();
$fileList.outerWidth(width);
$fileList.outerHeight(height);
+
+ var scrollPos = pmease.commons.history.getScrollPos();
+ if (scrollPos) {
+ $fileList.scrollLeft(scrollPos.left);
+ $fileList.scrollTop(scrollPos.top);
+ }
+ });
+
+ $fileList.scroll(function() {
+ pmease.commons.history.setScrollPos({left: $fileList.scrollLeft(), top: $fileList.scrollTop()});
});
$.ajax({