Skip to content

The Unified and Autonomous Development Platform

Improve performance of TextDiffPanel by rendering diff lines without using wicket components.

robin shineb2d163b30f27
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 34f3544afb..2e87ccfadc 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
@@ -412,32 +412,30 @@ pmease.commons = {
var $component = $("#" + componentId);
$component.closest("form").addClass("dirty").find(".dirty-aware").removeAttr("disabled");
},
-
- setupDirtyCheck: function() {
- var initAYS = function(form) {
- var $form = $(form);
- var $dirtyAware = $form.find(".dirty-aware");
-
- if ($dirtyAware.length != 0) {
- $form.addClass("ays-inited");
- $dirtyAware.attr("disabled", "disabled");
-
- $form.areYouSure({
- "silent": true,
- "addRemoveFieldsMarksDirty": true,
- change: function() {
- if ($(this).hasClass("dirty")) {
- $(this).find(".dirty-aware").removeAttr("disabled");
- } else {
- $(this).find(".dirty-aware").attr("disabled", "disabled")
- }
+ trackDirty: function(form) {
+ var $form = $(form);
+ var $dirtyAware = $form.find(".dirty-aware");
+
+ if ($dirtyAware.length != 0) {
+ $form.addClass("ays-inited");
+ $dirtyAware.attr("disabled", "disabled");
+
+ $form.areYouSure({
+ "silent": true,
+ "addRemoveFieldsMarksDirty": true,
+ change: function() {
+ if ($(this).hasClass("dirty")) {
+ $(this).find(".dirty-aware").removeAttr("disabled");
+ } else {
+ $(this).find(".dirty-aware").attr("disabled", "disabled")
}
- });
- }
- };
-
+ }
+ });
+ }
+ },
+ setupDirtyCheck: function() {
$("form").each(function() {
- initAYS(this);
+ pmease.commons.form.trackDirty(this);
});
$(document).on("replace", function(event, componentId) {
@@ -446,7 +444,7 @@ pmease.commons = {
if ($component.is("form"))
$forms = $forms.add($component);
$forms.each(function() {
- initAYS(this);
+ pmease.commons.form.trackDirty(this);
});
$component.closest("form.ays-inited").not($component).trigger("checkform.areYouSure");
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/comment/InlineContext.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/comment/InlineContext.java
index 47a5fe2879..6135ff7064 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/comment/InlineContext.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/comment/InlineContext.java
@@ -7,14 +7,18 @@ import com.pmease.commons.util.diff.DiffLine;
@SuppressWarnings("serial")
public class InlineContext implements Serializable {
+
private final List<DiffLine> diffs;
+ private final int line;
+
private final boolean aboveOmitted;
private final boolean belowOmitted;
- public InlineContext(List<DiffLine> diffs, boolean aboveOmitted, boolean belowOmitted) {
+ public InlineContext(List<DiffLine> diffs, int line, boolean aboveOmitted, boolean belowOmitted) {
this.diffs = diffs;
+ this.line = line;
this.aboveOmitted = aboveOmitted;
this.belowOmitted = belowOmitted;
}
@@ -23,6 +27,10 @@ public class InlineContext implements Serializable {
return diffs;
}
+ public int getCommentLine() {
+ return line;
+ }
+
public boolean isAboveOmitted() {
return aboveOmitted;
}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/assets/css/page.css b/gitplex.web/src/main/java/com/pmease/gitplex/web/assets/css/page.css
index a04dd6c7bd..d39c0f529c 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/assets/css/page.css
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/assets/css/page.css
@@ -790,12 +790,11 @@ h2.section-title { margin: 18px 0; font-size: 16px; }
.table-diff .line.comments>td {padding: 0 12px; border: 3px solid #DDD;}
.table-diff .line.comments td.avatar .concerned-comment {margin-top: 12px; font-size: 36px; color: #E7C325;}
-.table-diff form.add-comment {margin: 8px;}
+form#add-comment {margin: 16px 4px;}
.table-diff a.add-comment {position: absolute; left:0; margin-left: -20px;
width: 20px; height: 20px; text-decoration: none;}
.table-diff a.add-comment .fa {display:none;}
.table-diff tr:hover a.add-comment .fa {display:inline;}
-.table-diff div.add-comment {border-top: 1px dotted #bbb; padding-top: 12px;}
ul.diff-alerts li {text-align: left;}
ul.diff-alerts {padding-left: 12px;}
@@ -938,7 +937,8 @@ ul.status-help li:last-child .description {border-bottom: none;}
.request-detail>.activities>tbody>tr.activity>td.avatar {border-right: 1px dashed #CCC;}
.request-detail>.activities>tbody>tr.discussion+.non-discussion>td {border-top: 1px solid #DDD; padding-top: 12px;}
.request-detail>.activities>tbody>tr.discussion>.avatar img {width: 40px; height: 40px;}
-.request-detail>.activities>tbody>tr.non-discussion>td {padding: 6px;}
+.request-detail>.activities>tbody>tr.non-discussion>td {padding: 0 6px;}
+.request-detail>.activities>tbody>tr.non-discussion.non-update>td {padding: 6px;}
.request-detail>.activities>tbody>tr.non-discussion>td.avatar img {display: none;}
.request-detail>.activities>tbody>tr.non-discussion>.activity>div.summary .user a {font-weight: normal;}
.request-detail>.activities>tbody>tr.update+.update>td>table {margin-top: -12px;}
@@ -951,12 +951,13 @@ ul.status-help li:last-child .description {border-bottom: none;}
.request-detail>.activities .non-update+.update>td tr:first-child td {border-top: none;}
.request-detail>.activities .activity>.commits td.icon {width: 32px; white-space: nowrap; text-align: right;}
.request-detail>.activities .activity>.commits tr.integrated td.icon .fa {color: #35A612;}
-.request-detail>.activities .activity>.commits td.detail {padding-left: 6px; padding-right: 8px;}
+.request-detail>.activities .activity>.commits td.detail {padding-left: 15px; padding-right: 8px;}
.request-detail>.activities .activity>.commits td.status {text-align: right;}
.request-detail>.activities .activity>.commits tr:hover td.detail,
.request-detail>.activities .activity>.commits tr:hover td.status {background: #F7ECB9;}
.request-detail>.activities .activity>.commits tr.rebased td a {color: #AAA;}
.request-detail>.activities .activity>.commits tr.rebased td.icon .fa {color: #CCC;}
+.request-detail>.activities .activity>.commits tr.rebased img.avatar {opacity: 0.5;}
.request-detail>.activities .activity>.commits td.status .successful {color: #35A612;}
.request-detail>.activities .activity>.commits td.status .failed {color: #F93333;}
.request-detail>.activities .activity>.commits td.status .running {color: #AFAD1F;}
@@ -968,10 +969,10 @@ ul.status-help li:last-child .description {border-bottom: none;}
.request-detail>.activities .inline-comment.panel>.panel-heading .actions a {color: #888; margin-right: 4px;}
.request-detail>.activities .activity>.summary .fa {background: white;}
-.request-detail>.activities .activity>.discard .fa {margin-left: -19px; color: #F93333; font-size: 24px;}
-.request-detail>.activities .activity>.integrate .fa {margin-left: -19px; color: #35A612; font-size: 24px;}
-.request-detail>.activities .activity>.approve .fa {margin-left: -18px; color: #35A612; font-size: 24px;}
-.request-detail>.activities .activity>.disapprove .fa {margin-left: -18px; color: #F93333; font-size: 24px;}
+.request-detail>.activities .activity>.discard .fa {margin: 0 6px 0 -19px; color: #F93333; font-size: 24px;}
+.request-detail>.activities .activity>.integrate .fa {margin: 0 6px 0 -19px; color: #35A612; font-size: 24px;}
+.request-detail>.activities .activity>.approve .fa {margin: 0 6px 0 -18px; color: #35A612; font-size: 24px;}
+.request-detail>.activities .activity>.disapprove .fa {margin: 0 6px 0 -18px; color: #F93333; font-size: 24px;}
.request-detail>.updates>.update {padding: 12px; margin: 20px 0; border: 1px solid #DDD; border-radius: 4px;}
.request-detail>.updates>.update>.summary .fa {font-size: 16px;}
@@ -979,6 +980,7 @@ ul.status-help li:last-child .description {border-bottom: none;}
.request-detail>.updates>.update>.commits td {padding-top: 8px; padding-bottom: 8px;
color: #999; border-top: 1px dashed #CCC;}
.request-detail>.updates>.update>.commits tr.rebased td a {color: #AAA;}
+.request-detail>.updates>.update>.commits tr.rebased img.avatar {opacity: 0.5;}
.request-detail>.updates>.update>.commits td.avatar {width: 1px;}
.request-detail>.updates>.update>.commits td.avatar img {width: 36px; height: 36px;}
.request-detail>.updates>.update>.commits a {color: #666;}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/assets/js/page.js b/gitplex.web/src/main/java/com/pmease/gitplex/web/assets/js/page.js
index c9d29ad643..e6bbf3f316 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/assets/js/page.js
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/assets/js/page.js
@@ -117,6 +117,45 @@ var gitplex = {
}
});
}
+ },
+
+ comments: {
+ position: function() {
+ $("#comments-placeholder").children("table").each(function() {
+ var $lineComments = $(this);
+ var id = $lineComments.attr("id");
+ var lineId = id.substring("comment-".length);
+ var $line = $("#" + lineId);
+ $tr = $("<tr class='line comments'><td colspan='3'></td></tr>").insertAfter($line);
+ $tr.children().append($lineComments);
+ $line.find("a.add-comment").hide();
+ });
+ },
+ beforeAdd: function(index) {
+ var $line = $("#diffline-" + index);
+
+ $tr = $("<tr class='line comments'><td colspan='3'></td></tr>").insertAfter($line);
+ var $addComment = $("#add-comment");
+ var $commentsRow = $addComment.closest("tr.line.comments");
+ $addComment.find("textarea").val("");
+ $tr.children().append($addComment);
+ $commentsRow.prev().find("a.add-comment").show();
+ $commentsRow.remove();
+ $addComment.find("textarea").focus();
+ pmease.commons.form.trackDirty($addComment);
+ $line.find("a.add-comment").hide();
+ },
+ afterAdd: function(index) {
+ $("#comments-placeholder").append($("#add-comment"));
+ var $line = $("#diffline-" + index);
+ $line.next().find("td").append($("#comment-diffline-" + index));
+ },
+ cancelAdd: function(index) {
+ $("#comments-placeholder").append($("#add-comment"));
+ var $line = $("#diffline-" + index);
+ $line.next().remove();
+ $line.find("a.add-comment").show();
+ }
}
};
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/TextDiffPanel.html b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/TextDiffPanel.html
index a685dbe426..fb9dee4660 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/TextDiffPanel.html
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/TextDiffPanel.html
@@ -23,7 +23,31 @@
<span wicket:id="identical" class="label label-warning">Identical Content</span>
</div>
</div>
- <table class="table-diff">
+
+ <wicket:container wicket:id="diffs"></wicket:container>
+ <div id="comments-placeholder" class="hidden">
+ <table wicket:id="lines" class="comments">
+ <tr wicket:id="comments">
+ <td class="avatar">
+ <div wicket:id="avatar"></div>
+ <div wicket:id="concerned" class="fa fa-flag concerned-comment" title="Concerned comment"></div>
+ </td>
+ <td class="comment">
+ <div wicket:id="comment"></div>
+ </td>
+ </tr>
+ </table>
+ <form wicket:id="addComment" id="add-comment">
+ <div wicket:id="input"></div>
+ <div class="actions">
+ <button wicket:id="save" class="btn btn-sm btn-primary dirty-aware">Comment on this line</button>
+ &nbsp;&nbsp;&nbsp;&nbsp;
+ <button wicket:id="cancel" class="btn btn-sm btn-default">Close form</button>
+ </div>
+ </form>
+ </div>
+
+ <!-- table class="table-diff">
<wicket:container wicket:id="lines">
<tr wicket:id="contentRow" class="line content">
<td class="old line-no">
@@ -52,16 +76,6 @@
</td>
</tr>
</wicket:container>
- </table>
+ </table-->
</div>
- <wicket:fragment wicket:id="addCommentFrag">
- <form wicket:id="form" class="add-comment">
- <div wicket:id="input"></div>
- <div class="actions">
- <button wicket:id="save" class="btn btn-sm btn-primary dirty-aware">Comment on this line</button>
- &nbsp;&nbsp;&nbsp;&nbsp;
- <button wicket:id="cancel" class="btn btn-sm btn-default">Close form</button>
- </div>
- </form>
- </wicket:fragment>
</wicket:panel>
\ No newline at end of file
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/TextDiffPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/TextDiffPanel.java
index 8d10a00b82..6ad63050a1 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/TextDiffPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/TextDiffPanel.java
@@ -7,15 +7,19 @@ import static com.pmease.commons.util.diff.DiffLine.Action.EQUAL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import javax.annotation.Nullable;
import org.apache.shiro.SecurityUtils;
+import org.apache.wicket.Component;
+import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
-import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.event.IEvent;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
@@ -24,12 +28,14 @@ import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
-import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.markup.html.panel.Panel;
+import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
+import org.apache.wicket.request.cycle.RequestCycle;
+import org.apache.wicket.util.string.Strings;
import com.google.common.base.Preconditions;
import com.pmease.commons.git.BlobText;
@@ -67,14 +73,8 @@ public class TextDiffPanel extends Panel implements InlineContextAware {
private static final int SCROLL_MARGIN = 50;
- private static final String COMMENTS_ROW_ID = "commentsRow";
-
- private static final String CONTENT_ROW_ID = "contentRow";
-
private static final String COMMENT_ACTIONS_ID = "commentActions";
- private static final String ADD_COMMENT_ID = "addComment";
-
private static final String HEAD_ID = "head";
private final IModel<Repository> repoModel;
@@ -85,7 +85,7 @@ public class TextDiffPanel extends Panel implements InlineContextAware {
private final RevAwareChange change;
- private final InlineCommentSupport commentContext;
+ private final InlineCommentSupport commentSupport;
private final IModel<Boolean> allowToAddCommentModel;
@@ -97,8 +97,63 @@ public class TextDiffPanel extends Panel implements InlineContextAware {
private boolean showComments = true;
+ private int index;
+
private List<DiffLine> diffs;
+ private final IModel<Map<Integer, List<InlineComment>>> commentsModel =
+ new LoadableDetachableModel<Map<Integer, List<InlineComment>>>() {
+
+ @Override
+ protected Map<Integer, List<InlineComment>> load() {
+ Map<Integer, Integer> oldLinesMap = new HashMap<>();
+ Map<Integer, Integer> newLinesMap = new HashMap<>();
+
+ int index = 0;
+ for (DiffLine diff: diffs) {
+ oldLinesMap.put(diff.getOldLineNo(), index);
+ newLinesMap.put(diff.getNewLineNo(), index);
+ index++;
+ }
+
+ Map<Integer, List<InlineComment>> comments = new HashMap<>();
+ for (Map.Entry<Integer, List<InlineComment>> entry: commentSupport.getOldComments().entrySet()) {
+ int diffLineNo = oldLinesMap.get(entry.getKey());
+ List<InlineComment> lineComments = comments.get(diffLineNo);
+ if (lineComments == null) {
+ lineComments = new ArrayList<>();
+ comments.put(diffLineNo, lineComments);
+ }
+ lineComments.addAll(entry.getValue());
+ }
+ for (Map.Entry<Integer, List<InlineComment>> entry: commentSupport.getNewComments().entrySet()) {
+ int diffLineNo = newLinesMap.get(entry.getKey());
+ List<InlineComment> lineComments = comments.get(diffLineNo);
+ if (lineComments == null) {
+ lineComments = new ArrayList<>();
+ comments.put(diffLineNo, lineComments);
+ }
+ lineComments.addAll(entry.getValue());
+ }
+
+ for (List<InlineComment> lineComments: comments.values()) {
+ Collections.sort(lineComments, new Comparator<InlineComment>() {
+
+ @Override
+ public int compare(InlineComment o1, InlineComment o2) {
+ return o1.getDate().compareTo(o2.getDate());
+ }
+
+ });
+ }
+
+ return comments;
+ }
+
+ };
+
+ private RepeatingView commentsView;
+
private String autoScrollScript;
private void onDiffOptionChanged() {
@@ -137,7 +192,7 @@ public class TextDiffPanel extends Panel implements InlineContextAware {
Preconditions.checkArgument(!change.getOldRevision().equals(change.getNewRevision()));
this.change = change;
- this.commentContext = commentContext;
+ this.commentSupport = commentContext;
this.oldText = oldText;
this.newText = newText;
@@ -237,7 +292,7 @@ public class TextDiffPanel extends Panel implements InlineContextAware {
@Override
protected void onConfigure() {
super.onConfigure();
- setVisible(commentContext != null && showComments);
+ setVisible(commentSupport != null && showComments);
}
}.add(new ScrollBehavior(".comments.line", 50, false)));
@@ -247,7 +302,7 @@ public class TextDiffPanel extends Panel implements InlineContextAware {
@Override
protected void onConfigure() {
super.onConfigure();
- setVisible(commentContext != null && showComments);
+ setVisible(commentSupport != null && showComments);
}
}.add(new ScrollBehavior(".comments.line", 50, true)));
@@ -262,7 +317,7 @@ public class TextDiffPanel extends Panel implements InlineContextAware {
@Override
protected void onConfigure() {
super.onConfigure();
- setVisible(commentContext != null && !showComments);
+ setVisible(commentSupport != null && !showComments);
}
});
@@ -276,7 +331,7 @@ public class TextDiffPanel extends Panel implements InlineContextAware {
@Override
protected void onConfigure() {
super.onConfigure();
- setVisible(commentContext != null && showComments);
+ setVisible(commentSupport != null && showComments);
}
});
@@ -308,7 +363,7 @@ public class TextDiffPanel extends Panel implements InlineContextAware {
@Override
public void detach() {
}
-
+
@Override
public Boolean getObject() {
return diffOption == DiffOption.IGNORE_EOL;
@@ -412,118 +467,151 @@ public class TextDiffPanel extends Panel implements InlineContextAware {
head.add(diffOptionMenuPanel);
head.add(new WebMarkupContainer("diffOptionsTrigger").add(new MenuBehavior(diffOptionMenuPanel)));
+
+ Form<?> form = new Form<Void>("addComment");
+ form.setOutputMarkupId(true);
- add(new ListView<DiffLine>("lines", new AbstractReadOnlyModel<List<DiffLine>>() {
+ final CommentInput input;
+ form.add(input = new CommentInput("input", Model.of("")));
+ input.setRequired(true);
+
+ form.add(new AjaxLink<Void>("cancel") {
@Override
- public List<DiffLine> getObject() {
- return diffs;
+ public void onClick(AjaxRequestTarget target) {
+ input.setModelObject("");
+ target.appendJavaScript(String.format("gitplex.comments.cancelAdd(%d);", index));
}
- }) {
+ });
+ form.add(new AjaxSubmitLink("save") {
@Override
- protected void populateItem(final ListItem<DiffLine> lineItem) {
- final DiffLine diffLine = lineItem.getModelObject();
- final WebMarkupContainer contentRow = new WebMarkupContainer(CONTENT_ROW_ID);
+ protected void onError(AjaxRequestTarget target, Form<?> form) {
+ super.onError(target, form);
+ target.add(form);
+ }
- final IModel<List<InlineComment>> lineCommentsModel = new LoadableDetachableModel<List<InlineComment>>() {
+ @Override
+ protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
+ super.onSubmit(target, form);
- @Override
- protected List<InlineComment> load() {
- return getLineComments(diffLine);
- }
-
- };
- final WebMarkupContainer commentsRow = new WebMarkupContainer(COMMENTS_ROW_ID) {
+ DiffLine diff = diffs.get(index);
+ String commit;
+ int lineNo;
+
+ if (diff.getAction() == DELETE) {
+ commit = change.getOldRevision();
+ lineNo = diff.getOldLineNo();
+ } else {
+ commit = change.getNewRevision();
+ lineNo = diff.getNewLineNo();
+ }
+
+ commentSupport.addComment(commit, change.getPath(), lineNo, input.getModelObject());
+
+ Component commentsRow = newCommentsRow(commentsView.newChildId(), index);
+ commentsView.add(commentsRow);
+ target.add(commentsRow);
+
+ input.setModelObject("");
+ String prependScript = String.format("$('#comments-placeholder').append('<table id=\"%s\"></table>')",
+ commentsRow.getMarkupId());
+ target.prependJavaScript(prependScript);
+ target.appendJavaScript(String.format("gitplex.comments.afterAdd(%d);", index));
+ }
- @Override
- protected void onConfigure() {
- super.onConfigure();
-
- if (showComments) {
- if (isAddingComment(this))
- setVisible(true);
- else
- setVisible(!lineCommentsModel.getObject().isEmpty());
- } else {
- setVisible(false);
- }
- }
+ });
+ add(form);
+
+ final AbstractDefaultAjaxBehavior addCommentBehavior;
+ add(addCommentBehavior = new AbstractDefaultAjaxBehavior() {
+
+ @Override
+ protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
+ super.updateAjaxAttributes(attributes);
- @Override
- public void onEvent(IEvent<?> event) {
- super.onEvent(event);
-
- if (event.getPayload() instanceof CommentRemoved) {
- CommentRemoved commentRemoved = (CommentRemoved) event.getPayload();
- lineCommentsModel.getObject().remove(commentRemoved.getComment());
- commentRemoved.getTarget().add(this);
- }
- }
-
- };
- commentsRow.setOutputMarkupId(true);
-
- commentsRow.add(new ListView<InlineComment>("comments", lineCommentsModel) {
+ attributes.getDynamicExtraParameters().add("return {index: index}");
+ }
- @Override
- protected void populateItem(final ListItem<InlineComment> item) {
- item.add(new UserLink("avatar", new UserModel(item.getModelObject().getUser()), AvatarMode.AVATAR));
- item.add(new WebMarkupContainer("concerned")
- .setVisible(item.getModelObject().equals(commentContext.getConcernedComment())));
- item.add(new CommentPanel("comment", item.getModel()).setOutputMarkupId(true));
- }
-
- });
+ @Override
+ public boolean getStatelessHint(Component component) {
+ return false;
+ }
- commentsRow.add(new WebMarkupContainer(ADD_COMMENT_ID));
-
- lineItem.add(commentsRow);
-
- contentRow.add(new AddCommentLink("addComment", lineItem));
+ @Override
+ protected void respond(AjaxRequestTarget target) {
+ index = RequestCycle.get().getRequest().getQueryParameters()
+ .getParameterValue("index").toInt();
+
+ if (!showComments)
+ showComments(target);
- if (diffLine.getAction() == ADD) {
- if (lineItem.getIndex() == 0 || diffs.get(lineItem.getIndex()-1).getAction() == EQUAL)
- contentRow.add(AttributeAppender.append("class", " new diff-block"));
- else
- contentRow.add(AttributeAppender.append("class", " new"));
- contentRow.add(new Label("oldLineNo"));
- contentRow.add(new Label("newLineNo", "+ " + (diffLine.getNewLineNo()+1)));
- } else if (diffLine.getAction() == DELETE) {
- if (lineItem.getIndex() == 0 || diffs.get(lineItem.getIndex()-1).getAction() == EQUAL)
- contentRow.add(AttributeAppender.append("class", " old diff-block"));
- else
- contentRow.add(AttributeAppender.append("class", " old"));
- contentRow.add(new Label("oldLineNo", "- " + (diffLine.getOldLineNo()+1)));
- contentRow.add(new Label("newLineNo"));
- } else {
- contentRow.add(AttributeAppender.append("class", " equal"));
- contentRow.add(new Label("oldLineNo", " " + (diffLine.getOldLineNo()+1)));
- contentRow.add(new Label("newLineNo", " " + (diffLine.getNewLineNo()+1)));
- }
- contentRow.add(new ListView<Partial>("partials", diffLine.getPartials()) {
+ target.appendJavaScript(String.format("gitplex.comments.beforeAdd(%d);", index));
+ }
+
+ });
+
+ add(new Label("diffs", new LoadableDetachableModel<String>() {
- @Override
- protected void populateItem(ListItem<Partial> item) {
- Partial partial = item.getModelObject();
- Label label;
- if (partial.getContent().equals("\r"))
- label = new Label("partial", " ");
+ @Override
+ protected String load() {
+ StringBuilder builder = new StringBuilder("<table id='diffs-table' class='table-diff'>");
+ int index = 0;
+ for (DiffLine diff: diffs) {
+ String addCommentLink;
+ if (allowToAddCommentModel.getObject() && commentSupport != null) {
+ addCommentLink = "<a href='javascript: var index=" + index + "; "
+ + addCommentBehavior.getCallbackScript()
+ + ";' class='add-comment'><i class='fa fa-comment-add'></i></a>";
+ } else {
+ addCommentLink = "";
+ }
+ builder.append("<tr id='diffline-").append(index).append("' class='line content ");
+ if (diff.getAction() == ADD) {
+ if (index == 0 || diffs.get(index-1).getAction() == EQUAL)
+ builder.append("new diff-block'>");
else
- label = new Label("partial", partial.getContent());
- if (partial.isEmphasized())
- label.add(AttributeAppender.append("class", "emphasize"));
- item.add(label);
+ builder.append("new'>");
+ builder.append("<td class='old line-no'>").append(addCommentLink).append("</td>");
+ builder.append("<td class='new line-no'>");
+ builder.append("+ ").append(diff.getNewLineNo()+1).append("</td>");
+ } else if (diff.getAction() == DELETE) {
+ if (index == 0 || diffs.get(index-1).getAction() == EQUAL)
+ builder.append("old diff-block'>");
+ else
+ builder.append("old'>");
+ builder.append("<td class='old line-no'>").append(addCommentLink);
+ builder.append("- ").append(diff.getOldLineNo()+1).append("</td>");
+ builder.append("<td class='new line-no'></td>");
+ } else {
+ builder.append("equal'>");
+ builder.append("<td class='old line-no'>").append(addCommentLink)
+ .append(" ").append(diff.getOldLineNo()+1).append("</td>");
+ builder.append("<td class='new line-no'> ").append(diff.getNewLineNo()+1).append("</td>");
}
+ builder.append("<td class='text'>");
- });
- contentRow.setOutputMarkupId(true);
-
- lineItem.add(contentRow);
+ for (Partial partial: diff.getPartials()) {
+ if (partial.isEmphasized())
+ builder.append("<span class='emphasize'>");
+ else
+ builder.append("<span>");
+ if (partial.getContent().equals("\r"))
+ builder.append(" ");
+ else
+ builder.append(Strings.escapeMarkup(partial.getContent(), false, false));
+ builder.append("</span>");
+ }
+ builder.append("</td></tr>");
+ index++;
+ }
+ return builder.append("</table>").toString();
}
- });
+ }).setEscapeModelStrings(false));
+
+ add(newCommentsView());
autoScrollScript = String.format(""
+ "if ($('.concerned-comment').length != 0) "
@@ -532,144 +620,85 @@ public class TextDiffPanel extends Panel implements InlineContextAware {
+ " pmease.commons.scroll.next('.diff-block:first', %d);", SCROLL_MARGIN * 2, SCROLL_MARGIN);
}
- private void showComments(AjaxRequestTarget target) {
- showComments = true;
- target.add(get(HEAD_ID).get(COMMENT_ACTIONS_ID));
+ private Component newCommentsRow(String id, final int index) {
+ WebMarkupContainer row = new WebMarkupContainer(commentsView.newChildId()) {
- target.appendJavaScript("$('.comments.line').show();");
- }
-
- private void hideComments(AjaxRequestTarget target) {
- showComments = false;
- target.add(get(HEAD_ID).get(COMMENT_ACTIONS_ID));
+ @Override
+ public void onEvent(IEvent<?> event) {
+ super.onEvent(event);
+
+ if (event.getPayload() instanceof CommentRemoved) {
+ CommentRemoved commentRemoved = (CommentRemoved) event.getPayload();
+ commentsModel.getObject().get(index).remove(commentRemoved.getComment());
+ commentRemoved.getTarget().appendJavaScript(
+ String.format("$('#%s').closest('tr').remove();", getMarkupId()));
+ }
+ }
+
+ };
+ row.setOutputMarkupId(true);
+ row.setMarkupId("comment-diffline-" + index);
- target.appendJavaScript("$('.comments.line').hide();");
- }
-
- private boolean isAddingComment(WebMarkupContainer commentsRow) {
- return commentsRow.get(ADD_COMMENT_ID) instanceof Fragment;
- }
+ row.add(new ListView<InlineComment>("comments", new LoadableDetachableModel<List<InlineComment>>() {
- private List<InlineComment> getLineComments(DiffLine line) {
- List<InlineComment> lineComments = new ArrayList<>();
- if (commentContext.getOldComments().containsKey(line.getOldLineNo())
- && line.getAction() != ADD) {
- lineComments.addAll(commentContext.getOldComments().get(line.getOldLineNo()));
- }
- if (commentContext.getNewComments().containsKey(line.getNewLineNo())
- && line.getAction() != DELETE) {
- lineComments.addAll(commentContext.getNewComments().get(line.getNewLineNo()));
- }
- Collections.sort(lineComments, new Comparator<InlineComment>() {
+ @Override
+ protected List<InlineComment> load() {
+ return commentsModel.getObject().get(index);
+ }
+
+ }) {
@Override
- public int compare(InlineComment o1, InlineComment o2) {
- return o1.getDate().compareTo(o2.getDate());
+ protected void populateItem(ListItem<InlineComment> item) {
+ item.add(new UserLink("avatar", new UserModel(item.getModelObject().getUser()), AvatarMode.AVATAR));
+ item.add(new WebMarkupContainer("concerned")
+ .setVisible(item.getModelObject().equals(commentSupport.getConcernedComment())));
+ item.add(new CommentPanel("comment", item.getModel()).setOutputMarkupId(true));
}
});
- return lineComments;
+
+ return row;
}
- private class AddCommentLink extends AjaxLink<Void> {
-
- private final ListItem<DiffLine> item;
+ private Component newCommentsView() {
+ commentsView = new RepeatingView("lines");
- public AddCommentLink(String id, ListItem<DiffLine> item) {
- super(id);
- this.item = item;
+ if (commentSupport != null) {
+ for (int index: commentsModel.getObject().keySet())
+ commentsView.add(newCommentsRow(commentsView.newChildId(), index));
}
- @Override
- public void onClick(AjaxRequestTarget target) {
- if (!showComments)
- showComments(target);
-
- Fragment frag = new Fragment(ADD_COMMENT_ID, "addCommentFrag", TextDiffPanel.this);
- Form<?> form = new Form<Void>("form");
- form.setOutputMarkupId(true);
-
- final CommentInput input;
- form.add(input = new CommentInput("input", Model.of("")));
-
- final WebMarkupContainer contentRow = (WebMarkupContainer) item.get(CONTENT_ROW_ID);
- final WebMarkupContainer commentsRow = (WebMarkupContainer) item.get(COMMENTS_ROW_ID);
- final DiffLine diffLine = item.getModelObject();
-
- form.add(new AjaxLink<Void>("cancel") {
-
- @Override
- public void onClick(AjaxRequestTarget target) {
- commentsRow.replace(new WebMarkupContainer(ADD_COMMENT_ID));
- target.add(commentsRow);
- }
-
- });
- form.add(new AjaxSubmitLink("save") {
-
- @Override
- protected void onError(AjaxRequestTarget target, Form<?> form) {
- super.onError(target, form);
- target.add(form);
- }
+ return commentsView;
+ }
+
+ @Override
+ protected void onBeforeRender() {
+ replace(newCommentsView());
+
+ super.onBeforeRender();
+ }
- @Override
- protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
- super.onSubmit(target, form);
+ private void showComments(AjaxRequestTarget target) {
+ showComments = true;
+ target.add(get(HEAD_ID).get(COMMENT_ACTIONS_ID));
- String commit;
- int lineNo;
-
- if (diffLine.getAction() == DELETE) {
- commit = change.getOldRevision();
- lineNo = diffLine.getOldLineNo();
- } else {
- commit = change.getNewRevision();
- lineNo = diffLine.getNewLineNo();
- }
-
- commentContext.addComment(commit, change.getPath(), lineNo, input.getModelObject());
-
- commentsRow.replace(new WebMarkupContainer(ADD_COMMENT_ID));
- target.add(commentsRow);
- target.add(contentRow);
- }
+ target.appendJavaScript("$('.comments.line').show();");
+ }
- });
- frag.add(form);
- commentsRow.replace(frag);
- if (!commentsRow.isVisible()) {
- String script = String.format("$(\"<tr id='%s'></tr>\").insertAfter('#%s');",
- commentsRow.getMarkupId(), contentRow.getMarkupId());
- target.prependJavaScript(script);
- commentsRow.setVisible(true);
- }
- target.add(commentsRow);
- }
+ private void hideComments(AjaxRequestTarget target) {
+ showComments = false;
+ target.add(get(HEAD_ID).get(COMMENT_ACTIONS_ID));
- @Override
- protected void onConfigure() {
- super.onConfigure();
-
- if (!allowToAddCommentModel.getObject() || commentContext == null) {
- setVisible(false);
- } else {
- WebMarkupContainer commentsRow = (WebMarkupContainer) item.get(COMMENTS_ROW_ID);
- if (isAddingComment(commentsRow)) {
- setVisible(false);
- } else {
- DiffLine diffLine = item.getModelObject();
- setVisible(getLineComments(diffLine).isEmpty());
- }
- }
- }
-
+ target.appendJavaScript("$('.comments.line').hide();");
}
@Override
public void renderHead(IHeaderResponse response) {
super.renderHead(response);
+ response.render(OnDomReadyHeaderItem.forScript("gitplex.comments.position();"));
+
// only auto-scroll this panel once after it is created; otherwise page refreshing
// caused by actions such as integrate/discard on request compare page will also
// get scrolled
@@ -683,28 +712,44 @@ public class TextDiffPanel extends Panel implements InlineContextAware {
protected void onDetach() {
repoModel.detach();
allowToAddCommentModel.detach();
+ commentsModel.detach();
super.onDetach();
}
@Override
public InlineContext getInlineContext(InlineComment comment) {
- List<DiffLine> contextDiffs = new ArrayList<>();
- int index = -1;
- for (int i=0; i<diffs.size(); i++) {
- DiffLine diff = diffs.get(i);
- if (comment.getCommit().equals(change.getOldRevision())) {
- if (diff.getOldLineNo() == comment.getLine()) {
- index = i;
+ int oldLine = -1;
+ if (comment.getCommit().equals(change.getOldRevision())) {
+ oldLine = comment.getLine();
+ } else {
+ for (Map.Entry<Integer, List<InlineComment>> entry: commentSupport.getOldComments().entrySet()) {
+ if (entry.getValue().contains(comment)) {
+ oldLine = entry.getKey();
break;
}
- } else if (comment.getCommit().equals(change.getNewRevision())){
- if (diff.getNewLineNo() == comment.getLine()) {
- index = i;
+ }
+ }
+
+ int newLine = -1;
+ if (comment.getCommit().equals(change.getNewRevision())) {
+ newLine = comment.getLine();
+ } else {
+ for (Map.Entry<Integer, List<InlineComment>> entry: commentSupport.getNewComments().entrySet()) {
+ if (entry.getValue().contains(comment)
+ || comment.getCommit().equals(change.getNewRevision()) && comment.getLine() == entry.getKey()) {
+ newLine = entry.getKey();
break;
}
- } else {
- throw new IllegalStateException();
+ }
+ }
+ List<DiffLine> contextDiffs = new ArrayList<>();
+ int index = -1;
+ for (int i=0; i<diffs.size(); i++) {
+ DiffLine diff = diffs.get(i);
+ if (diff.getOldLineNo() == oldLine || diff.getNewLineNo() == newLine) {
+ index = i;
+ break;
}
}
@@ -720,7 +765,7 @@ public class TextDiffPanel extends Panel implements InlineContextAware {
for (int i=start; i<=end; i++)
contextDiffs.add(diffs.get(i));
- return new InlineContext(contextDiffs, start>0, end<diffs.size()-1);
+ return new InlineContext(contextDiffs, index-start, start>0, end<diffs.size()-1);
}
}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/info/pullrequest/RequestComparePage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/info/pullrequest/RequestComparePage.java
index 3618190136..0d742bd80c 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/info/pullrequest/RequestComparePage.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/info/pullrequest/RequestComparePage.java
@@ -826,6 +826,7 @@ public class RequestComparePage extends RequestDetailPage implements InlineComme
User user = GitPlex.getInstance(UserManager.class).getCurrent();
Preconditions.checkNotNull(user);
PullRequestComment comment = new PullRequestComment();
+ getPullRequest().getComments().add(comment);
comment.setUser(user);
comment.setDate(new Date());
comment.setContent(content);
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/info/pullrequest/activity/InlineCommentPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/info/pullrequest/activity/InlineCommentPanel.java
index 4f95141b5d..39f132e194 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/info/pullrequest/activity/InlineCommentPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/info/pullrequest/activity/InlineCommentPanel.java
@@ -98,12 +98,10 @@ public class InlineCommentPanel extends Panel {
});
InlineComment comment = commentModel.getObject();
- if (comment.getCommit().equals(comment.getOldCommit()) && comment.getLine() == diffLine.getOldLineNo()
- || comment.getCommit().equals(comment.getNewCommit()) && comment.getLine() == diffLine.getNewLineNo()) {
+ if (item.getIndex() == comment.getContext().getCommentLine())
item.add(new CommentPanel("comment", commentModel));
- } else {
+ else
item.add(new WebMarkupContainer("comment").setVisible(false));
- }
}
});