Skip to content

The Unified and Autonomous Development Platform

Browser history support for file selections in request compare page.

robin shine508b8ef6a20f
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/CommonPage.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/CommonPage.java
index 08f3bdbbf5..05dd56f848 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/CommonPage.java
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/CommonPage.java
@@ -1,18 +1,31 @@
package com.pmease.commons.wicket;
+import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.lang3.SerializationUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.wicket.Component;
+import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
+import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.AjaxSelfUpdatingTimerBehavior;
+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.JavaScriptHeaderItem;
+import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
import org.apache.wicket.markup.head.PriorityHeaderItem;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.IModel;
+import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.time.Duration;
import org.eclipse.jetty.server.SessionManager;
import com.pmease.commons.loader.AppLoader;
+import com.pmease.commons.wicket.component.history.HistoryAwarePanel;
+import com.pmease.commons.wicket.component.history.HistoryState;
@SuppressWarnings("serial")
public abstract class CommonPage extends WebPage {
@@ -41,6 +54,46 @@ public abstract class CommonPage extends WebPage {
int sessionTimeout = AppLoader.getInstance(SessionManager.class).getMaxInactiveInterval();
add(new WebMarkupContainer("keepSessionAlive").add(
new AjaxSelfUpdatingTimerBehavior(Duration.milliseconds(sessionTimeout*500L))));
+
+ add(new AbstractDefaultAjaxBehavior() {
+
+ @Override
+ protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
+ super.updateAjaxAttributes(attributes);
+ attributes.setMethod(Method.POST);
+ }
+
+ @Override
+ protected void respond(AjaxRequestTarget target) {
+ String encodedHistoryState = RequestCycle.get().getRequest().getPostParameters()
+ .getParameterValue("state").toOptionalString();
+ if (StringUtils.isNotBlank(encodedHistoryState)) {
+ byte[] bytes = Base64.decodeBase64(encodedHistoryState.getBytes());
+ HistoryState historyState = (HistoryState) SerializationUtils.deserialize(bytes);
+ HistoryAwarePanel historyAwarePanel = (HistoryAwarePanel) getPage().get(historyState.getComponentPath());
+ if (historyAwarePanel != null)
+ historyAwarePanel.onPopState(target, historyState.getCustomObj());
+ }
+ }
+
+ @Override
+ public void renderHead(Component component, IHeaderResponse response) {
+ super.renderHead(component, response);
+
+ String script = String.format(""
+ + "window.onpopstate=function(e) {"
+ + " if (e.state != null) {"
+ + " %s"
+ + " } else {"
+ + " location.reload();"
+ + " }"
+ + "};",
+ getCallbackFunctionBody(CallbackParameter.resolved("state", "e.state")));
+ response.render(OnDomReadyHeaderItem.forScript(script));
+ }
+
+ });
+
}
public FeedbackPanel getSessionFeedback() {
@@ -52,5 +105,5 @@ public abstract class CommonPage extends WebPage {
super.renderHead(response);
response.render(new PriorityHeaderItem(JavaScriptHeaderItem.forReference(CommonResourceReference.get())));
}
-
+
}
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/asset/common.js b/commons.wicket/src/main/java/com/pmease/commons/wicket/asset/common.js
index be7b0fc884..3bf9a3568b 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/asset/common.js
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/asset/common.js
@@ -804,7 +804,7 @@ pmease.commons = {
else
$input.next().hide();
});
- }
+ },
};
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/component/history/HistoryAwarePanel.html b/commons.wicket/src/main/java/com/pmease/commons/wicket/component/history/HistoryAwarePanel.html
new file mode 100644
index 0000000000..d64b7f8c5c
--- /dev/null
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/component/history/HistoryAwarePanel.html
@@ -0,0 +1,3 @@
+<wicket:panel>
+ <div wicket:id="content"></div>
+</wicket:panel>
\ No newline at end of file
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/component/history/HistoryAwarePanel.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/component/history/HistoryAwarePanel.java
new file mode 100644
index 0000000000..e61230eb86
--- /dev/null
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/component/history/HistoryAwarePanel.java
@@ -0,0 +1,42 @@
+package com.pmease.commons.wicket.component.history;
+
+import java.io.Serializable;
+
+import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.lang3.SerializationUtils;
+import org.apache.wicket.Component;
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.markup.html.panel.Panel;
+import org.apache.wicket.model.IModel;
+
+@SuppressWarnings("serial")
+public abstract class HistoryAwarePanel extends Panel {
+
+ public HistoryAwarePanel(String id) {
+ super(id);
+ }
+
+ public HistoryAwarePanel(String id, IModel<?> model) {
+ super(id, model);
+ }
+
+ @Override
+ protected void onInitialize() {
+ super.onInitialize();
+
+ add(newContent("content"));
+
+ setRenderBodyOnly(true);
+ }
+
+ protected abstract Component newContent(String id);
+
+ public abstract void onPopState(AjaxRequestTarget target, Serializable state);
+
+ public void pushState(AjaxRequestTarget target, String url, Serializable state) {
+ HistoryState historyState = new HistoryState(getPageRelativePath(), state);
+ String encodedHistoryState = new String(Base64.encodeBase64(SerializationUtils.serialize(historyState)));
+ target.appendJavaScript(String.format("history.pushState('%s', '', '%s');", encodedHistoryState, url));
+ }
+
+}
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/component/history/HistoryState.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/component/history/HistoryState.java
new file mode 100644
index 0000000000..30f5a60264
--- /dev/null
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/component/history/HistoryState.java
@@ -0,0 +1,25 @@
+package com.pmease.commons.wicket.component.history;
+
+import java.io.Serializable;
+
+@SuppressWarnings("serial")
+public class HistoryState implements Serializable {
+
+ private final String componentPath;
+
+ private final Serializable customObj;
+
+ public HistoryState(String componentPath, Serializable customObj) {
+ this.componentPath = componentPath;
+ this.customObj = customObj;
+ }
+
+ public String getComponentPath() {
+ return componentPath;
+ }
+
+ public Serializable getCustomObj() {
+ return customObj;
+ }
+
+}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/CompareResultPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/CompareResultPanel.java
index a1f4cb4592..40d125e4e3 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/CompareResultPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/CompareResultPanel.java
@@ -46,24 +46,31 @@ public abstract class CompareResultPanel extends Panel {
this.oldCommitHash = oldCommit;
this.newCommitHash = newCommit;
- List<Change> changes = repoModel.getObject().getChanges(oldCommit, newCommit);
if (file != null) {
- for (Change each: repoModel.getObject().getChanges(oldCommit, newCommit)) {
- if (file.equals(each.getOldPath()) || file.equals(each.getNewPath())) {
- activeChange = each;
- break;
- }
+ activeChange = findChange(file);
+ } else {
+ List<Change> changes = repoModel.getObject().getChanges(oldCommit, newCommit);
+ if (!changes.isEmpty())
+ activeChange = changes.get(0);
+ }
+ }
+
+ private Change findChange(String file) {
+ Change change = null;
+ for (Change each: repoModel.getObject().getChanges(oldCommitHash, newCommitHash)) {
+ if (file.equals(each.getOldPath()) || file.equals(each.getNewPath())) {
+ change = each;
+ break;
}
- if (activeChange == null) {
- List<TreeNode> result = repoModel.getObject().git().listTree(newCommit, file);
- if (!result.isEmpty() && result.get(0).getMode() != FileMode.TYPE_TREE) {
- activeChange = new Change(UNCHANGED, file, file,
- result.get(0).getMode(), result.get(0).getMode());
- }
+ }
+ if (change == null) {
+ List<TreeNode> result = repoModel.getObject().git().listTree(newCommitHash, file);
+ if (!result.isEmpty() && result.get(0).getMode() != FileMode.TYPE_TREE) {
+ change = new Change(UNCHANGED, file, file,
+ result.get(0).getMode(), result.get(0).getMode());
}
- } else if (!changes.isEmpty()) {
- activeChange = changes.get(0);
}
+ return change;
}
@Override
@@ -160,12 +167,32 @@ public abstract class CompareResultPanel extends Panel {
target.add(this);
- onChangeSelection(target, change);
+ onSelection(target, change);
}
}
- protected abstract void onChangeSelection(AjaxRequestTarget target, Change change);
+ protected abstract void onSelection(AjaxRequestTarget target, Change change);
+
+ public void select(AjaxRequestTarget target, String file) {
+ activeChange = findChange(file);
+ if (activeChange != null) {
+ if (changeNav instanceof DiffTreePanel) {
+ ((DiffTreePanel) changeNav).reveal(activeChange);
+ } else if (activeChange.getStatus() == UNCHANGED) {
+ replace(changeNav = newChangeNav(false));
+ }
+ changeContent = new BlobDiffPanel(changeContent.getId(), repoModel,
+ new RevAwareChange(activeChange, oldCommitHash, newCommitHash),
+ getInlineCommentSupport(activeChange));
+ } else {
+ changeContent = new WebMarkupContainer(changeContent.getId());
+ changeContent.setOutputMarkupId(true);
+ }
+ target.add(changeNav);
+ replace(changeContent);
+ target.add(changeContent);
+ }
public void toggleChangedOnly(AjaxRequestTarget target) {
changeNav = newChangeNav(!(changeNav instanceof ChangedFilesPanel));
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/compare/BranchComparePage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/compare/BranchComparePage.java
index 4b06cfcfab..16b329e37a 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/compare/BranchComparePage.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/compare/BranchComparePage.java
@@ -350,7 +350,7 @@ public class BranchComparePage extends RepositoryPage {
sourceModel.getObject().getHeadCommitHash(), null) {
@Override
- protected void onChangeSelection(AjaxRequestTarget target, Change change) {
+ protected void onSelection(AjaxRequestTarget target, Change change) {
}
@Override
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/NewRequestPage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/NewRequestPage.java
index 11f4616743..23f876c24d 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/NewRequestPage.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/NewRequestPage.java
@@ -294,7 +294,7 @@ public class NewRequestPage extends RepositoryPage {
pullRequest.getLatestUpdate().getHeadCommitHash(), null) {
@Override
- protected void onChangeSelection(AjaxRequestTarget target, Change change) {
+ protected void onSelection(AjaxRequestTarget target, Change change) {
}
@Override
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestComparePage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestComparePage.java
index e75572a5ca..da47366aa9 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestComparePage.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestComparePage.java
@@ -44,6 +44,7 @@ import com.pmease.commons.wicket.behavior.dropdown.DropdownPanel;
import com.pmease.commons.wicket.behavior.menu.MenuBehavior;
import com.pmease.commons.wicket.behavior.menu.MenuItem;
import com.pmease.commons.wicket.behavior.menu.MenuPanel;
+import com.pmease.commons.wicket.component.history.HistoryAwarePanel;
import com.pmease.gitplex.core.GitPlex;
import com.pmease.gitplex.core.comment.InlineComment;
import com.pmease.gitplex.core.comment.InlineCommentSupport;
@@ -176,7 +177,7 @@ public class RequestComparePage extends RequestDetailPage {
protected void onInitialize() {
super.onInitialize();
- WebMarkupContainer optionsContainer = new WebMarkupContainer("compareOptions");
+ final WebMarkupContainer optionsContainer = new WebMarkupContainer("compareOptions");
optionsContainer.add(new StickyBehavior());
add(optionsContainer);
@@ -378,64 +379,92 @@ public class RequestComparePage extends RequestDetailPage {
});
- add(compareResult = new CompareResultPanel("compareResult", repoModel, oldCommitHash, newCommitHash, file) {
+ add(new HistoryAwarePanel("compareResult") {
@Override
- protected InlineCommentSupport getInlineCommentSupport(final Change change) {
- List<String> commentables = getPullRequest().getCommentables();
- int oldCommitIndex = commentables.indexOf(oldCommitHash);
- int newCommitIndex = commentables.indexOf(newCommitHash);
- if (oldCommitIndex == -1 || newCommitIndex == -1 || oldCommitIndex > newCommitIndex) {
- return null;
- } else {
- return new InlineCommentSupport() {
-
- @Override
- public Map<Integer, List<InlineComment>> getOldComments() {
- RevAwareChange revAwareChange = new RevAwareChange(change, oldCommitHash, newCommitHash);
- return getPullRequest().getChangeComments(revAwareChange).getOldComments();
- }
-
- @Override
- public Map<Integer, List<InlineComment>> getNewComments() {
- RevAwareChange revAwareChange = new RevAwareChange(change, oldCommitHash, newCommitHash);
- return getPullRequest().getChangeComments(revAwareChange).getNewComments();
+ public void onPopState(AjaxRequestTarget target, Serializable state) {
+ compareResult.select(target, (String) state);
+ target.add(optionsContainer);
+ }
+
+ @Override
+ protected Component newContent(String id) {
+ compareResult = new CompareResultPanel(id, repoModel, oldCommitHash, newCommitHash, file) {
+
+ @Override
+ protected InlineCommentSupport getInlineCommentSupport(final Change change) {
+ List<String> commentables = getPullRequest().getCommentables();
+ int oldCommitIndex = commentables.indexOf(oldCommitHash);
+ int newCommitIndex = commentables.indexOf(newCommitHash);
+ if (oldCommitIndex == -1 || newCommitIndex == -1 || oldCommitIndex > newCommitIndex) {
+ return null;
+ } else {
+ return new InlineCommentSupport() {
+
+ @Override
+ public Map<Integer, List<InlineComment>> getOldComments() {
+ RevAwareChange revAwareChange = new RevAwareChange(change, oldCommitHash, newCommitHash);
+ return getPullRequest().getChangeComments(revAwareChange).getOldComments();
+ }
+
+ @Override
+ public Map<Integer, List<InlineComment>> getNewComments() {
+ RevAwareChange revAwareChange = new RevAwareChange(change, oldCommitHash, newCommitHash);
+ return getPullRequest().getChangeComments(revAwareChange).getNewComments();
+ }
+
+ @Override
+ public InlineComment getConcernedComment() {
+ return RequestComparePage.this.getComment();
+ }
+
+ @Override
+ public InlineComment addComment(BlobInfo blobInfo, BlobInfo compareWith,
+ AroundContext commentContext, int line, String content) {
+ User user = GitPlex.getInstance(UserManager.class).getCurrent();
+ Preconditions.checkNotNull(user);
+ PullRequestInlineComment comment = new PullRequestInlineComment();
+ getPullRequest().getInlineComments().add(comment);
+ comment.setUser(user);
+ comment.setDate(new Date());
+ comment.setContent(content);
+ comment.setRequest(getPullRequest());
+ comment.setBlobInfo(blobInfo);
+ comment.setCompareWith(compareWith);
+ comment.setLine(line);
+ comment.setContext(commentContext);
+ GitPlex.getInstance(Dao.class).persist(comment);
+ return comment;
+ }
+ };
}
+ }
+
+ @Override
+ protected void onSelection(AjaxRequestTarget target, Change change) {
+ file = change.getPath();
+
+ String original = getPageParameters().get(ORIGINAL_PARAM).toString();
+ String revised = getPageParameters().get(REVISED_PARAM).toString();
+ Long commentId = getPageParameters().get(COMMENT_PARAM).toOptionalLong();
- @Override
- public InlineComment getConcernedComment() {
- return RequestComparePage.this.getComment();
- }
+ StringBuilder url = new StringBuilder("compare?file=");
+ url.append(file);
+ if (original != null)
+ url.append("&original=").append(original);
+ if (revised != null)
+ url.append("&revised=").append(revised);
+ if (commentId != null)
+ url.append("&comment=").append(commentId);
- @Override
- public InlineComment addComment(BlobInfo blobInfo, BlobInfo compareWith,
- AroundContext commentContext, int line, String content) {
- User user = GitPlex.getInstance(UserManager.class).getCurrent();
- Preconditions.checkNotNull(user);
- PullRequestInlineComment comment = new PullRequestInlineComment();
- getPullRequest().getInlineComments().add(comment);
- comment.setUser(user);
- comment.setDate(new Date());
- comment.setContent(content);
- comment.setRequest(getPullRequest());
- comment.setBlobInfo(blobInfo);
- comment.setCompareWith(compareWith);
- comment.setLine(line);
- comment.setContext(commentContext);
- GitPlex.getInstance(Dao.class).persist(comment);
- return comment;
- }
- };
- }
+ pushState(target, url.toString(), file);
+ }
+
+ };
+ return compareResult;
}
- @Override
- protected void onChangeSelection(AjaxRequestTarget target, Change change) {
- file = change.getPath();
- }
-
});
-
}
@Override