Skip to content

The Unified and Autonomous Development Platform

Add OnTypingDoneBehavior and apply to search box of branches page to only update branches table when we've not typing for 1 second.

robin shine86ca340c7983
diff --git a/commons.git/pom.xml b/commons.git/pom.xml
index 00f872001f..8eab287c54 100644
--- a/commons.git/pom.xml
+++ b/commons.git/pom.xml
@@ -46,7 +46,7 @@
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit.http.server</artifactId>
- <version>3.4.1.201406201815-r</version>
+ <version>3.5.1.201410131835-r</version>
</dependency>
<dependency>
<groupId>com.pmease</groupId>
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/git/command/AheadBehind.java b/commons.git/src/main/java/com/pmease/commons/git/AheadBehind.java
similarity index 93%
rename from gitplex.web/src/main/java/com/pmease/gitplex/web/git/command/AheadBehind.java
rename to commons.git/src/main/java/com/pmease/commons/git/AheadBehind.java
index 684df5c562..348d799767 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/git/command/AheadBehind.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/AheadBehind.java
@@ -1,4 +1,4 @@
-package com.pmease.gitplex.web.git.command;
+package com.pmease.commons.git;
import java.io.Serializable;
diff --git a/commons.git/src/main/java/com/pmease/commons/git/Git.java b/commons.git/src/main/java/com/pmease/commons/git/Git.java
index 3f5c2c36f2..09c07b46f7 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/Git.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/Git.java
@@ -25,6 +25,7 @@ import com.google.common.base.Preconditions;
import com.pmease.commons.git.command.AddCommand;
import com.pmease.commons.git.command.AddNoteCommand;
import com.pmease.commons.git.command.AddSubModuleCommand;
+import com.pmease.commons.git.command.AheadBehindCommand;
import com.pmease.commons.git.command.BlameCommand;
import com.pmease.commons.git.command.CalcMergeBaseCommand;
import com.pmease.commons.git.command.CheckoutCommand;
@@ -459,6 +460,11 @@ public class Git implements Serializable {
return new ListBranchCommand(repoDir).call();
}
+ public AheadBehind getAheadBehind(String leftRev, String rightRev) {
+ AheadBehindCommand cmd = new AheadBehindCommand(repoDir);
+ return cmd.leftRev(leftRev).rightRev(rightRev).call();
+ }
+
/**
* List head commits of all local branches.
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/AheadBehindCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/AheadBehindCommand.java
new file mode 100644
index 0000000000..37f119c83c
--- /dev/null
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/AheadBehindCommand.java
@@ -0,0 +1,65 @@
+package com.pmease.commons.git.command;
+
+import java.io.File;
+import java.util.StringTokenizer;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Preconditions;
+import com.pmease.commons.git.AheadBehind;
+import com.pmease.commons.util.execution.Commandline;
+import com.pmease.commons.util.execution.LineConsumer;
+
+public class AheadBehindCommand extends GitCommand<AheadBehind> {
+
+ private static final Logger logger = LoggerFactory.getLogger(AheadBehindCommand.class);
+
+ private String leftRev;
+
+ private String rightRev;
+
+ public AheadBehindCommand(File repoDir) {
+ super(repoDir);
+ }
+
+ public AheadBehindCommand leftRev(String leftRev) {
+ this.leftRev = Preconditions.checkNotNull(leftRev);
+ return this;
+ }
+
+ public AheadBehindCommand rightRev(String rightRev) {
+ this.rightRev = Preconditions.checkNotNull(rightRev);
+ return this;
+ }
+
+ @Override
+ public AheadBehind call() {
+ Commandline cmd = cmd();
+ cmd.addArgs("rev-list", "--left-right", "--count");
+ cmd.addArgs(Preconditions.checkNotNull(leftRev, "leftRev")
+ + "..." + Preconditions.checkNotNull(rightRev, "rightRev"));
+
+ final AheadBehind counter = new AheadBehind();
+ cmd.execute(new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ StringTokenizer tokenizer = new StringTokenizer(line);
+ counter.setAhead(Integer.parseInt(tokenizer.nextToken()));
+ counter.setBehind(Integer.parseInt(tokenizer.nextToken()));
+ }
+
+ }, new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ logger.error(line);
+ }
+
+ });
+
+ return counter;
+ }
+
+}
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/asset/common.css b/commons.wicket/src/main/java/com/pmease/commons/wicket/asset/common.css
index 1ab90c3149..2e0f3b2d2a 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/asset/common.css
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/asset/common.css
@@ -209,6 +209,7 @@ span.delete.icon {padding: 12px; background: url("delete.png") no-repeat 50% 50%
span.move.icon {padding: 12px; background: url("move.png") no-repeat 50% 50%;}
/* CLEARABLE TEXT FIELD */
+input.clearable::-ms-clear {display: none;}
.input-clear {font-size: 18px; margin-left: -16px; z-index: 1000; position: relative;
cursor: pointer; top: 4px; color: #999; display: none;}
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 a1554f8e46..ce53f813a9 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
@@ -793,11 +793,12 @@ pmease.commons = {
setupClearableInput: function(inputId) {
var $input = $("#" + inputId);
+ $input.addClass("clearable");
$input.after("<span class='input-clear'>x</span>");
$input.next().click(function() {
$input.val("");
$(this).hide();
- $input.trigger("inputchange");
+ $input.trigger("donetyping");
});
$input.keyup(function() {
var value = $(this).val();
@@ -807,7 +808,17 @@ pmease.commons = {
$input.next().hide();
});
},
-
+
+ setupDoneTyping: function(inputId, timeout) {
+ var $input = $("#" + inputId);
+ $input.on("input", function() {
+ if ($input[0].typingTimer)
+ clearTimeout($input[0].typingTimer);
+ $input[0].typingTimer = setTimeout(function() {
+ $input.trigger("donetyping");
+ }, timeout);
+ });
+ },
};
$(function() {
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/OnTypingDoneBehavior.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/OnTypingDoneBehavior.java
new file mode 100644
index 0000000000..a41ff8d893
--- /dev/null
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/behavior/OnTypingDoneBehavior.java
@@ -0,0 +1,27 @@
+package com.pmease.commons.wicket.behavior;
+
+import org.apache.wicket.Component;
+import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
+import org.apache.wicket.markup.head.IHeaderResponse;
+import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
+
+@SuppressWarnings("serial")
+public abstract class OnTypingDoneBehavior extends AjaxFormComponentUpdatingBehavior{
+
+ private int timeout;
+
+ public OnTypingDoneBehavior(int timeout) {
+ super("donetyping");
+ this.timeout = timeout;
+ }
+
+ @Override
+ public void renderHead(Component component, IHeaderResponse response) {
+ super.renderHead(component, response);
+
+ String script = String.format("pmease.commons.setupDoneTyping('%s', %d);",
+ component.getMarkupId(true), timeout);
+ response.render(OnDomReadyHeaderItem.forScript(script));
+ }
+
+}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/git/command/AheadBehindCommand.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/git/command/AheadBehindCommand.java
deleted file mode 100644
index df52139352..0000000000
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/git/command/AheadBehindCommand.java
+++ /dev/null
@@ -1,75 +0,0 @@
-package com.pmease.gitplex.web.git.command;
-
-import java.io.File;
-import java.util.Map;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import javax.annotation.Nullable;
-
-import org.parboiled.common.Preconditions;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.pmease.commons.git.command.GitCommand;
-import com.pmease.commons.util.execution.Commandline;
-import com.pmease.commons.util.execution.LineConsumer;
-
-public class AheadBehindCommand extends GitCommand<AheadBehind> {
-
- private static final Logger logger = LoggerFactory.getLogger(AheadBehindCommand.class);
-
- public AheadBehindCommand(File repoDir) {
- super(repoDir);
- }
-
- public AheadBehindCommand(File repoDir, @Nullable Map<String, String> environments) {
- super(repoDir, environments);
- }
-
- private String leftBranch;
- private String rightBranch;
-
- public AheadBehindCommand leftBranch(String leftBranch) {
- this.leftBranch = Preconditions.checkNotNull(leftBranch);
- return this;
- }
-
- public AheadBehindCommand rightBranch(String rightBranch) {
- this.rightBranch = Preconditions.checkNotNull(rightBranch);
- return this;
- }
-
- static final Pattern pLeftRight = Pattern.compile("(\\d+)\\s+(\\d+)");
- @Override
- public AheadBehind call() {
- Commandline cmd = cmd();
- cmd.addArgs("rev-list", "--left-right", "--count");
- cmd.addArgs(Preconditions.checkArgNotNull(leftBranch, "leftBranch")
- + "..." + Preconditions.checkArgNotNull(rightBranch, "rightBranch"));
-
- final AheadBehind counter = new AheadBehind();
- cmd.execute(new LineConsumer() {
-
- @Override
- public void consume(String line) {
- Matcher m = pLeftRight.matcher(line);
- if (m.find()) {
- counter.setAhead(Integer.valueOf(m.group(1)));
- counter.setBehind(Integer.valueOf(m.group(2)));
- }
- }
-
- }, new LineConsumer() {
-
- @Override
- public void consume(String line) {
- logger.error(line);
- }
-
- });
-
- return counter;
- }
-
-}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/TestPage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/TestPage.java
index 1a26a891e9..f5843d11fd 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/TestPage.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/TestPage.java
@@ -1,12 +1,11 @@
package com.pmease.gitplex.web.page;
+import java.io.File;
+
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.request.mapper.parameter.PageParameters;
-import com.pmease.commons.hibernate.dao.Dao;
-import com.pmease.commons.hibernate.dao.EntityCriteria;
-import com.pmease.gitplex.core.GitPlex;
-import com.pmease.gitplex.core.model.PullRequestCommentReply;
+import com.pmease.commons.git.Git;
@SuppressWarnings("serial")
public class TestPage extends BasePage {
@@ -23,8 +22,9 @@ public class TestPage extends BasePage {
@Override
public void onClick() {
- Dao dao = GitPlex.getInstance(Dao.class);
- System.out.println(dao.query(EntityCriteria.of(PullRequestCommentReply.class)).size());
+ Git git = new Git(new File("W:\\temp\\gitplex_storage\\repositories\\1"));
+ for (int i=1; i<100; i++)
+ git.createBranch("dev" + i, "master~" + i*10);
}
});
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/branches/RepoBranchesPage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/branches/RepoBranchesPage.java
index 11621f6ba7..94b14104f7 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/branches/RepoBranchesPage.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/branches/RepoBranchesPage.java
@@ -1,6 +1,5 @@
package com.pmease.gitplex.web.page.repository.code.branches;
-import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@@ -10,13 +9,13 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
+import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.validator.routines.PercentValidator;
import org.apache.shiro.SecurityUtils;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.IAjaxIndicatorAware;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
-import org.apache.wicket.ajax.form.OnChangeAjaxBehavior;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.ComponentTag;
@@ -36,10 +35,12 @@ import org.apache.wicket.request.mapper.parameter.PageParameters;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
+import com.pmease.commons.git.AheadBehind;
import com.pmease.commons.git.BriefCommit;
import com.pmease.commons.hibernate.dao.Dao;
import com.pmease.commons.util.StringUtils;
import com.pmease.commons.wicket.behavior.ConfirmBehavior;
+import com.pmease.commons.wicket.behavior.OnTypingDoneBehavior;
import com.pmease.commons.wicket.behavior.TooltipBehavior;
import com.pmease.commons.wicket.component.clearable.ClearableTextField;
import com.pmease.commons.wicket.component.navigator.PagingNavigator;
@@ -62,8 +63,6 @@ import com.pmease.gitplex.web.component.branch.BranchSingleChoice;
import com.pmease.gitplex.web.component.label.AgeLabel;
import com.pmease.gitplex.web.component.user.AvatarMode;
import com.pmease.gitplex.web.component.user.PersonLink;
-import com.pmease.gitplex.web.git.command.AheadBehind;
-import com.pmease.gitplex.web.git.command.AheadBehindCommand;
import com.pmease.gitplex.web.page.repository.NoCommitsPage;
import com.pmease.gitplex.web.page.repository.RepositoryPage;
import com.pmease.gitplex.web.page.repository.code.tree.RepoTreePage;
@@ -77,12 +76,16 @@ public class RepoBranchesPage extends RepositoryPage {
private Long baseBranchId;
+ private PageableListView<Branch> branchesView;
+
private PagingNavigator pagingNavigator;
private WebMarkupContainer branchesContainer;
private TextField<String> searchInput;
+ private String searchFor;
+
private final IModel<Map<String, BriefCommit>> lastCommitsModel =
new LoadableDetachableModel<Map<String, BriefCommit>>() {
@@ -99,14 +102,16 @@ public class RepoBranchesPage extends RepositoryPage {
@Override
protected Map<String, AheadBehind> load() {
Map<String, AheadBehind> map = Maps.newHashMap();
- File repoDir = getRepository().git().repoDir();
- for (Branch branch: getRepository().getBranches()) {
+ List<Branch> branches = branchesView.getModelObject();
+ for (long i=branchesView.getFirstItemOffset(); i<branches.size(); i++) {
+ if (i-branchesView.getFirstItemOffset() >= branchesView.getItemsPerPage())
+ break;
+ Branch branch = branches.get((int)i);
if (branch.equals(getBaseBranch())) {
map.put(branch.getName(), new AheadBehind());
} else {
- AheadBehindCommand command = new AheadBehindCommand(repoDir);
- AheadBehind ab = command.leftBranch(branch.getName()).rightBranch(getBaseBranch().getName()).call();
- map.put(branch.getName(), ab);
+ map.put(branch.getName(),
+ getRepository().git().getAheadBehind(branch.getName(), getBaseBranch().getName()));
}
}
@@ -128,7 +133,10 @@ public class RepoBranchesPage extends RepositoryPage {
map.put(ab.getBehind(), "0");
}
List<Integer> abValues = new ArrayList<>(map.keySet());
- abValues.remove(0);
+ for (Iterator<Integer> it = abValues.iterator(); it.hasNext();) {
+ if (it.next().equals(0))
+ it.remove();
+ }
Collections.sort(abValues);
for (int i=0; i<abValues.size(); i++) {
double percent = (i+1.0d)/abValues.size();
@@ -190,7 +198,7 @@ public class RepoBranchesPage extends RepositoryPage {
protected String getPageTitle() {
return getRepository() + " - Branches";
}
-
+
@Override
protected void onInitialize() {
super.onInitialize();
@@ -221,19 +229,35 @@ public class RepoBranchesPage extends RepositoryPage {
}));
add(searchInput = new ClearableTextField<String>("searchBranches", Model.of("")));
- searchInput.add(new OnSearchingBehavior());
+ searchInput.add(new OnSearchingBehavior() {
+
+ @Override
+ protected void onUpdate(AjaxRequestTarget target) {
+ // IE triggers "input" event when the focused on the search input even if nothing is
+ // input into search box yet. To work around this issue, we compare search string
+ // against previous value to only update the branches table if there is an actual
+ // change.
+ String newSearchFor = searchInput.getInput();
+ if (StringUtils.isNotBlank(newSearchFor))
+ newSearchFor = newSearchFor.trim().toLowerCase();
+ else
+ newSearchFor = null;
+ if (!ObjectUtils.equals(newSearchFor, searchFor))
+ super.onUpdate(target);
+ }
+
+ });
branchesContainer = new WebMarkupContainer("branchesContainer");
branchesContainer.setOutputMarkupId(true);
add(branchesContainer);
- final PageableListView<Branch> branchesView;
branchesContainer.add(branchesView = new PageableListView<Branch>("branches", new AbstractReadOnlyModel<List<Branch>>() {
@Override
public List<Branch> getObject() {
List<Branch> branches = new ArrayList<>(getRepository().getBranches());
- String searchFor = searchInput.getInput();
+ searchFor = searchInput.getInput();
if (StringUtils.isNotBlank(searchFor)) {
searchFor = searchFor.trim().toLowerCase();
for (Iterator<Branch> it = branches.iterator(); it.hasNext();) {
@@ -241,6 +265,8 @@ public class RepoBranchesPage extends RepositoryPage {
if (!branch.getName().toLowerCase().contains(searchFor))
it.remove();
}
+ } else {
+ searchFor = null;
}
Collections.sort(branches, new Comparator<Branch>() {
@@ -531,7 +557,11 @@ public class RepoBranchesPage extends RepositoryPage {
super.onDetach();
}
- private class OnSearchingBehavior extends OnChangeAjaxBehavior implements IAjaxIndicatorAware {
+ private class OnSearchingBehavior extends OnTypingDoneBehavior implements IAjaxIndicatorAware {
+
+ public OnSearchingBehavior() {
+ super(1000);
+ }
@Override
protected void onUpdate(AjaxRequestTarget target) {