Skip to content

The Unified and Autonomous Development Platform

Show appropriate hint on pages not able to comment

robin shen965ebfca2b0f
diff --git a/core/src/main/java/com/gitplex/server/model/PullRequest.java b/core/src/main/java/com/gitplex/server/model/PullRequest.java
index 0607290b96..64f782b7d2 100644
--- a/core/src/main/java/com/gitplex/server/model/PullRequest.java
+++ b/core/src/main/java/com/gitplex/server/model/PullRequest.java
@@ -783,7 +783,7 @@ public class PullRequest extends AbstractEntity {
CompareContext compareContext = commentComparingInfo.getCompareContext();
if (commit.equals(compareContext.getCompareCommit())) {
int index = commits.indexOf(commit);
- if (index == 0) {
+ if (index == -1) {
return null;
} else {
return new ComparingInfo(commits.get(index-1), commit,
diff --git a/web/src/main/java/com/gitplex/server/web/component/diff/blob/text/TextDiffPanel.java b/web/src/main/java/com/gitplex/server/web/component/diff/blob/text/TextDiffPanel.java
index 3df7875e9e..3877e496bf 100644
--- a/web/src/main/java/com/gitplex/server/web/component/diff/blob/text/TextDiffPanel.java
+++ b/web/src/main/java/com/gitplex/server/web/component/diff/blob/text/TextDiffPanel.java
@@ -194,7 +194,7 @@ public class TextDiffPanel extends Panel implements SourceAware {
Project project = projectModel.getObject();
PullRequest request = requestModel.getObject();
ProjectBlobPage.State viewState = new ProjectBlobPage.State(change.getBlobIdent());
-
+
viewState.requestId = PullRequest.idOf(request);
PageParameters params = ProjectBlobPage.paramsOf(project, viewState);
actions.add(new ViewStateAwarePageLink<Void>("viewFile", ProjectBlobPage.class, params));
@@ -548,11 +548,18 @@ public class TextDiffPanel extends Panel implements SourceAware {
explicit("action"), explicit("param1"), explicit("param2"),
explicit("param3"), explicit("param4"), explicit("param5"),
explicit("param6"), explicit("param7"), explicit("param8"));
- String script = String.format("gitplex.server.textDiff.onDomReady('%s', '%s', '%s', '%s', %s, %s, %s, %s, "
+
+ String unableCommentMessage;
+ if (getPage() instanceof MergePreviewPage) {
+ unableCommentMessage = "Unable to comment on merge preview";
+ } else {
+ unableCommentMessage = "Unable to comment at this place";
+ }
+ String script = String.format("gitplex.server.textDiff.onDomReady('%s', '%s', '%s', '%s', %s, %s, %s, '%s', %s,"
+ "%s, %s, %s, %s, '%s');", getMarkupId(), symbolTooltip.getMarkupId(),
change.getOldBlobIdent().revision, change.getNewBlobIdent().revision,
callback, blameMessageBehavior.getCallback(),
- commentSupport!=null, jsonOfMark, jsonOfCommentInfo,
+ commentSupport!=null, unableCommentMessage, jsonOfMark, jsonOfCommentInfo,
jsonOfOldCommentInfos, jsonOfNewCommentInfos, dirtyContainerId,
GitPlex.getInstance().getDocLink());
diff --git a/web/src/main/java/com/gitplex/server/web/component/diff/blob/text/text-diff.js b/web/src/main/java/com/gitplex/server/web/component/diff/blob/text/text-diff.js
index 0e106112ee..94cf5f776e 100644
--- a/web/src/main/java/com/gitplex/server/web/component/diff/blob/text/text-diff.js
+++ b/web/src/main/java/com/gitplex/server/web/component/diff/blob/text/text-diff.js
@@ -1,7 +1,7 @@
gitplex.server.textDiff = {
symbolClasses: ".cm-property, .cm-variable, .cm-variable-2, .cm-variable-3, .cm-def, .cm-meta, .cm-string, .cm-tag, .cm-attribute, cm-builtin, cm-qualifier",
onDomReady: function(containerId, symbolTooltipId, oldRev, newRev, callback, blameMessageCallback,
- markSupport, mark, openComment, oldComments, newComments, dirtyContainerId, doclink) {
+ commentSupport, unableCommentMessage, mark, openComment, oldComments, newComments, dirtyContainerId, doclink) {
var $container = $("#" + containerId);
$container.data("dirtyContainerId", dirtyContainerId);
$container.data("callback", callback);
@@ -41,404 +41,410 @@ gitplex.server.textDiff = {
gitplex.server.textDiff.initBlameTooltip(containerId, $container.find("td.blame>a.hash"));
- if (markSupport) {
- $container.selectionPopover("init", function() {
- var selection = window.getSelection();
- if (!selection.rangeCount) {
- return "close";
- }
- var firstRange = selection.getRangeAt(0).cloneRange();
- var lastRange = selection.getRangeAt(selection.rangeCount-1).cloneRange();
- var $start = $(firstRange.startContainer);
- var $end = $(lastRange.endContainer);
-
- /*
- * offset represents offset within $start or $end node, for instance for below selection
- * <span>hello</span><span>world</span>
- * ^ ^
- * $start will be <span>hello</span> and startOffset will be index of character 'l' which is 2,
- * $end will be <span>world</span> and endOffset will be index of character 'd' which will be 4
- */
- var startOffset = firstRange.startOffset;
- var endOffset = lastRange.endOffset;
-
- var $startDiff;
- if (!$start.hasClass("text-diff"))
- $startDiff = $start.closest(".text-diff");
- else
- $startDiff = $start;
-
- var $endDiff;
- if (!$end.hasClass("text-diff"))
- $endDiff = $end.closest(".text-diff");
- else
- $endDiff = $end;
-
- // selection must be within same file
- if ($startDiff.length == 0 || $endDiff.length == 0 || !$startDiff.is($endDiff)) {
- return "close";
- }
-
- var $startTd = $start.is("td.content")? $start: $start.closest(".text-diff td.content");
- var $endTd = $end.is("td.content")? $end: $end.closest(".text-diff td.content");
+ $container.selectionPopover("init", function() {
+ var selection = window.getSelection();
+ if (!selection.rangeCount) {
+ return "close";
+ }
+ var firstRange = selection.getRangeAt(0).cloneRange();
+ var lastRange = selection.getRangeAt(selection.rangeCount-1).cloneRange();
+ var $start = $(firstRange.startContainer);
+ var $end = $(lastRange.endContainer);
+
+ /*
+ * offset represents offset within $start or $end node, for instance for below selection
+ * <span>hello</span><span>world</span>
+ * ^ ^
+ * $start will be <span>hello</span> and startOffset will be index of character 'l' which is 2,
+ * $end will be <span>world</span> and endOffset will be index of character 'd' which will be 4
+ */
+ var startOffset = firstRange.startOffset;
+ var endOffset = lastRange.endOffset;
+
+ var $startDiff;
+ if (!$start.hasClass("text-diff"))
+ $startDiff = $start.closest(".text-diff");
+ else
+ $startDiff = $start;
+
+ var $endDiff;
+ if (!$end.hasClass("text-diff"))
+ $endDiff = $end.closest(".text-diff");
+ else
+ $endDiff = $end;
+
+ // selection must be within same file
+ if ($startDiff.length == 0 || $endDiff.length == 0 || !$startDiff.is($endDiff)) {
+ return "close";
+ }
+
+ var $startTd = $start.is("td.content")? $start: $start.closest(".text-diff td.content");
+ var $endTd = $end.is("td.content")? $end: $end.closest(".text-diff td.content");
- // at least one side of selection must be within a table cell
- if ($startTd.length == 0 && $endTd.length == 0) {
- return "close";
- }
+ // at least one side of selection must be within a table cell
+ if ($startTd.length == 0 && $endTd.length == 0) {
+ return "close";
+ }
- // make sure we are processing td.content on the same side on split view
- function getTd($tr) {
- var $td = $startTd.length != 0? $startTd: $endTd;
- if ($td.hasClass("left"))
- return $tr.find("td.content.left");
- else if ($td.hasClass("right"))
- return $tr.find("td.content.right");
- else
- return $tr.find("td.content");
- }
+ // make sure we are processing td.content on the same side on split view
+ function getTd($tr) {
+ var $td = $startTd.length != 0? $startTd: $endTd;
+ if ($td.hasClass("left"))
+ return $tr.find("td.content.left");
+ else if ($td.hasClass("right"))
+ return $tr.find("td.content.right");
+ else
+ return $tr.find("td.content");
+ }
- /*
- * sometimes the selection returns $start and $end as td instead of
- * children under td, for instance:
- * <td><span>hello</span><span>world</span></td>
- * ^
- * in this case, both $start and $end is the whole td, and startOffset
- * is index of first span element which is 0, and endOffset is index of
- * last span element plus 1 which is 2.
- *
- * this function returns the equivalent child node and offset under td.
- * with above example, the equivalent child node will be <span>hello</span>
- * and offset will be 0 for selection start, while equivalent end child node
- * is <span>world</span> with offset being 5
- *
- */
- function getNodeAndOffsetUnderTd($td, contentIndex) {
- var $contents = $td.contents();
- for (var i=0; i<$contents.length; i++) {
- if (i == contentIndex) {
- return {
- node: $($contents[i]),
- offset: 0
- }
- }
- }
- var $lastContent = $contents.last();
- return {
- node: $lastContent,
- offset: $lastContent.text().length
- }
+ /*
+ * sometimes the selection returns $start and $end as td instead of
+ * children under td, for instance:
+ * <td><span>hello</span><span>world</span></td>
+ * ^
+ * in this case, both $start and $end is the whole td, and startOffset
+ * is index of first span element which is 0, and endOffset is index of
+ * last span element plus 1 which is 2.
+ *
+ * this function returns the equivalent child node and offset under td.
+ * with above example, the equivalent child node will be <span>hello</span>
+ * and offset will be 0 for selection start, while equivalent end child node
+ * is <span>world</span> with offset being 5
+ *
+ */
+ function getNodeAndOffsetUnderTd($td, contentIndex) {
+ var $contents = $td.contents();
+ for (var i=0; i<$contents.length; i++) {
+ if (i == contentIndex) {
+ return {
+ node: $($contents[i]),
+ offset: 0
+ }
+ }
+ }
+ var $lastContent = $contents.last();
+ return {
+ node: $lastContent,
+ offset: $lastContent.text().length
}
+ }
- // normalize $start to be within td.content
- if ($start.parent().parent().is("td.content")) // $start might be the text node under span
- $start = $start.parent();
- if (!$start.parent().is("td.content")) {
- var $td;
- if ($start.is("td.content")) { // this happens if we triple click to select a line in firefox
- $td = $start;
- } else if ($start.is("tr.code")) { // this may happen if we drag mouse to select multiple lines in firefox
- $td = getTd($start);
- startOffset = startOffset<=$td.index()? 0: $td.contents().length;
- } else if ($start.is("tr.expander") || $start.closest("tr.expander").length != 0) { // this may happen if we drag mouse over to expander line
- var $tr = $start.is("tr.expander")? $start.next(): $start.closest("tr.expander").next();
- if ($tr.length == 0) {
- return "close";
- }
- $td = getTd($tr);
- startOffset = 0;
- } else {
- var $tr = $startDiff.find("tr.code").first();
- if ($tr.length == 0) {
- return "close";
- }
- $td = getTd($tr);
- startOffset = 0;
- }
- var nodeAndOffset = getNodeAndOffsetUnderTd($td, startOffset);
- $start = nodeAndOffset.node;
- startOffset = nodeAndOffset.offset;
- }
-
- // drag mouse over a line below expander, chrome may set $start as the line
- // above expander, below code normalizes this case
- $startTd = $start.parent();
- var $lastContent = $startTd.contents().last();
- var noneSelected = $start.is($lastContent) && startOffset >= $lastContent.text().length;
- if (noneSelected && $startTd.parent().next().is("tr.expander")) {
- var $tr = $startTd.parent().next().next();
+ // normalize $start to be within td.content
+ if ($start.parent().parent().is("td.content")) // $start might be the text node under span
+ $start = $start.parent();
+ if (!$start.parent().is("td.content")) {
+ var $td;
+ if ($start.is("td.content")) { // this happens if we triple click to select a line in firefox
+ $td = $start;
+ } else if ($start.is("tr.code")) { // this may happen if we drag mouse to select multiple lines in firefox
+ $td = getTd($start);
+ startOffset = startOffset<=$td.index()? 0: $td.contents().length;
+ } else if ($start.is("tr.expander") || $start.closest("tr.expander").length != 0) { // this may happen if we drag mouse over to expander line
+ var $tr = $start.is("tr.expander")? $start.next(): $start.closest("tr.expander").next();
if ($tr.length == 0) {
return "close";
}
- var $td = getTd($tr);
- $start = $td.contents().first();
+ $td = getTd($tr);
startOffset = 0;
- }
-
- // normalize $end to be within td.content
- if ($end.parent().parent().is("td.content"))
- $end = $end.parent();
- if (!$end.parent().is("td.content")) {
- var $td;
- if ($end.is("td.content")) {
- $td = $end;
- } else if ($end.is("tr.code")) {
- $td = getTd($end);
- endOffset = endOffset>$td.index()? $td.contents().length: 0;
- } else if ($end.is("tr.expander") || $end.closest("tr.expander").length != 0) {
- var $tr = $end.is("tr.expander")? $end.prev(): $end.closest("tr.expander").prev();
- if ($tr.length == 0) {
- return "close";
- }
- $td = getTd($tr);
- endOffset = $td.contents().length;
- } else {
- var $tr = $endDiff.find("tr.code").last();
- if ($tr.length == 0) {
- return "close";
- }
- $td = getTd($tr);
- endOffset = $td.contents().length;
- }
- var nodeAndOffset = getNodeAndOffsetUnderTd($td, endOffset);
- $end = nodeAndOffset.node;
- endOffset = nodeAndOffset.offset;
- }
-
- // drag mouse over a line above expander, chrome may set $end as the line
- // below expander, below code normalizes this case
- $endTd = $end.parent();
- var $firstContent = $endTd.contents().first();
- var noneSelected = $end.is($firstContent) && endOffset == 0;
- if (noneSelected && $endTd.parent().prev().is("tr.expander")) {
- var $tr = $endTd.parent().prev().prev();
+ } else {
+ var $tr = $startDiff.find("tr.code").first();
if ($tr.length == 0) {
return "close";
}
- var $td = getTd($tr);
- $end = $td.contents().last();
- endOffset = $end.text().length;
+ $td = getTd($tr);
+ startOffset = 0;
}
-
- $startTd = $start.parent();
- $endTd = $end.parent();
-
- // selection must not span the split view
- if ($startTd.hasClass("left") && $endTd.hasClass("right")
- || $startTd.hasClass("right") && $endTd.hasClass("left")) {
- return "close";
- }
-
- var $startTr = $startTd.parent();
- var $endTr = $endTd.parent();
- if ($startTr.index() > $endTr.index()) {
+ var nodeAndOffset = getNodeAndOffsetUnderTd($td, startOffset);
+ $start = nodeAndOffset.node;
+ startOffset = nodeAndOffset.offset;
+ }
+
+ // drag mouse over a line below expander, chrome may set $start as the line
+ // above expander, below code normalizes this case
+ $startTd = $start.parent();
+ var $lastContent = $startTd.contents().last();
+ var noneSelected = $start.is($lastContent) && startOffset >= $lastContent.text().length;
+ if (noneSelected && $startTd.parent().next().is("tr.expander")) {
+ var $tr = $startTd.parent().next().next();
+ if ($tr.length == 0) {
return "close";
}
-
- function nextContent($content) {
- var $td = $content.parent();
- var array = $td.contents().toArray();
- var index = array.indexOf($content[0]);
- if (index == array.length-1) {
- var $nextTr = $td.parent().next();
- if ($nextTr.hasClass("expander")) {
- $nextTr = $nextTr.next();
- }
- if ($nextTr.length == 0)
- return undefined;
- return getTd($nextTr).contents().first();
- } else {
- return $(array[index+1]);
- }
- }
-
- function prevContent($content) {
- var $td = $content.parent();
- var array = $td.contents().toArray();
- var index = array.indexOf($content[0]);
- if (index == 0) {
- var $prevTr = $td.parent().prev();
- if ($prevTr.hasClass("expander")) {
- $prevTr = $prevTr.next();
- }
- if ($prevTr.length == 0)
- return undefined;
- return getTd($prevTr).contents().last();
- } else {
- return $(array[index-1]);
- }
- }
-
- function hasOldOrNewData($content) {
- var $td = $content.parent();
- return $td.attr("data-old") || $td.attr("data-new");
- }
-
- /*
- * continue to normalize $start and $end. This time we convert below selection:
- * <span>begin</span><span>middle</span><span>end</span>
- * ^ ^
- * into this selection:
- * <span>begin</span><span>middle</span><span>end</span>
- * ^ ^
- * this normalization makes it accurate to detect invalid selections where start
- * and end of selection points to different revisions
- */
- var $content = $start;
- while (!$content.is($end) && (startOffset >= $content.text().length || !hasOldOrNewData($content))) {
- $content = nextContent($content);
- if (!$content) {
+ var $td = getTd($tr);
+ $start = $td.contents().first();
+ startOffset = 0;
+ }
+
+ // normalize $end to be within td.content
+ if ($end.parent().parent().is("td.content"))
+ $end = $end.parent();
+ if (!$end.parent().is("td.content")) {
+ var $td;
+ if ($end.is("td.content")) {
+ $td = $end;
+ } else if ($end.is("tr.code")) {
+ $td = getTd($end);
+ endOffset = endOffset>$td.index()? $td.contents().length: 0;
+ } else if ($end.is("tr.expander") || $end.closest("tr.expander").length != 0) {
+ var $tr = $end.is("tr.expander")? $end.prev(): $end.closest("tr.expander").prev();
+ if ($tr.length == 0) {
return "close";
- } else {
- $start = $content;
- startOffset = 0;
}
- }
-
- $content = $end;
- while (!$content.is($start) && (endOffset == 0 || !hasOldOrNewData($content))) {
- $content = prevContent($content);
- if (!$content) {
+ $td = getTd($tr);
+ endOffset = $td.contents().length;
+ } else {
+ var $tr = $endDiff.find("tr.code").last();
+ if ($tr.length == 0) {
return "close";
- } else {
- $end = $content;
- endOffset = $content.text().length;
}
+ $td = getTd($tr);
+ endOffset = $td.contents().length;
}
-
- // check if there is anything selected
- if ($start.is($end) && startOffset >= endOffset
- || !hasOldOrNewData($start) || !hasOldOrNewData($end)) {
+ var nodeAndOffset = getNodeAndOffsetUnderTd($td, endOffset);
+ $end = nodeAndOffset.node;
+ endOffset = nodeAndOffset.offset;
+ }
+
+ // drag mouse over a line above expander, chrome may set $end as the line
+ // below expander, below code normalizes this case
+ $endTd = $end.parent();
+ var $firstContent = $endTd.contents().first();
+ var noneSelected = $end.is($firstContent) && endOffset == 0;
+ if (noneSelected && $endTd.parent().prev().is("tr.expander")) {
+ var $tr = $endTd.parent().prev().prev();
+ if ($tr.length == 0) {
return "close";
}
-
- firstRange.collapse(true);
- var startRect = firstRange.getClientRects()[0];
- lastRange.collapse(false);
- var endRect = lastRange.getClientRects()[0];
- if (!startRect || !endRect) {
- startRect = selection.getRangeAt(0).getClientRects()[0];
- endRect = startRect;
- }
- var position = {
- left: (startRect.left + endRect.right)/2,
- top: startRect.top
- };
- position.left += $(window).scrollLeft();
- position.top += $(window).scrollTop();
+ var $td = getTd($tr);
+ $end = $td.contents().last();
+ endOffset = $end.text().length;
+ }
+
+ $startTd = $start.parent();
+ $endTd = $end.parent();
+
+ // selection must not span the split view
+ if ($startTd.hasClass("left") && $endTd.hasClass("right")
+ || $startTd.hasClass("right") && $endTd.hasClass("left")) {
+ return "close";
+ }
+
+ var $startTr = $startTd.parent();
+ var $endTr = $endTd.parent();
+ if ($startTr.index() > $endTr.index()) {
+ return "close";
+ }
- function showInvalidSelection() {
- var $content = $("<div></div>");
- $content.append("<a class='invalid'><i class='fa fa-warning'></i> Invalid selection, click for details</a>");
- $content.children("a").attr("href", doclink + "/Diff+Selection");
- return {
- position: position,
- content: $content
+ function nextContent($content) {
+ var $td = $content.parent();
+ var array = $td.contents().toArray();
+ var index = array.indexOf($content[0]);
+ if (index == array.length-1) {
+ var $nextTr = $td.parent().next();
+ if ($nextTr.hasClass("expander")) {
+ $nextTr = $nextTr.next();
}
+ if ($nextTr.length == 0)
+ return undefined;
+ return getTd($nextTr).contents().first();
+ } else {
+ return $(array[index+1]);
}
+ }
+
+ function prevContent($content) {
+ var $td = $content.parent();
+ var array = $td.contents().toArray();
+ var index = array.indexOf($content[0]);
+ if (index == 0) {
+ var $prevTr = $td.parent().prev();
+ if ($prevTr.hasClass("expander")) {
+ $prevTr = $prevTr.next();
+ }
+ if ($prevTr.length == 0)
+ return undefined;
+ return getTd($prevTr).contents().last();
+ } else {
+ return $(array[index-1]);
+ }
+ }
- // all lines between selection has been expanded
- if ($startTr.nextAll("tr.expander").filter($endTr.prevAll("tr.expander")).length != 0
- || $startTr.prevAll("tr.expander").filter($endTr.nextAll("tr.expander")).length != 0) {
- return showInvalidSelection();
+ function hasOldOrNewData($content) {
+ var $td = $content.parent();
+ return $td.attr("data-old") || $td.attr("data-new");
+ }
+
+ /*
+ * continue to normalize $start and $end. This time we convert below selection:
+ * <span>begin</span><span>middle</span><span>end</span>
+ * ^ ^
+ * into this selection:
+ * <span>begin</span><span>middle</span><span>end</span>
+ * ^ ^
+ * this normalization makes it accurate to detect invalid selections where start
+ * and end of selection points to different revisions
+ */
+ var $content = $start;
+ while (!$content.is($end) && (startOffset >= $content.text().length || !hasOldOrNewData($content))) {
+ $content = nextContent($content);
+ if (!$content) {
+ return "close";
+ } else {
+ $start = $content;
+ startOffset = 0;
}
-
- /*
- * cursor.line represents line number of selection and cursor.ch represents
- * character index inside text of the whole line, without considering element
- * tags.
- */
- function getCursor($node, offset) {
- $nodeTd = $node.parent();
- var oldLine, newLine;
- if (!$nodeTd.hasClass("old") && !$nodeTd.hasClass("new")
- || $nodeTd.hasClass("old") && $nodeTd.hasClass("new")) {
- oldLine = $nodeTd.data("old") + 1;
- newLine = $nodeTd.data("new") + 1;
- } else if ($nodeTd.hasClass("old")) {
- oldLine = $nodeTd.data("old") + 1;
- } else {
- newLine = $nodeTd.data("new") + 1;
- }
- var $contents = $nodeTd.contents();
- var oldOffset = 0, newOffset = 0;
- for (var i=0; i<$contents.length; i++) {
- var $content = $($contents[i]);
- if ($content.is($node)) {
- if ($content.hasClass("delete")) {
- oldCh = oldOffset + offset;
- newCh = undefined;
- newLine = undefined;
- } else if ($content.hasClass("insert")) {
- oldCh = undefined;
- oldLine = undefined;
- newCh = newOffset + offset;
- } else {
- oldCh = oldOffset + offset;
- newCh = newOffset + offset;
- }
- break;
- } else {
- var len = $content.text().length;
- if ($content.hasClass("delete")) {
- oldOffset += len;
- } else if ($content.hasClass("insert")) {
- newOffset += len;
- } else {
- oldOffset += len;
- newOffset += len;
- }
- }
- }
- if ($nodeTd.hasClass("left")) {
- newLine = newCh = undefined;
- } else if ($nodeTd.hasClass("right")) {
- oldLine = oldCh = undefined;
- }
- return {
- oldLine: oldLine,
- oldCh: oldCh,
- newLine: newLine,
- newCh: newCh
- };
- }
+ }
- var startCursor = getCursor($start, startOffset);
- var endCursor = getCursor($end, endOffset);
+ $content = $end;
+ while (!$content.is($start) && (endOffset == 0 || !hasOldOrNewData($content))) {
+ $content = prevContent($content);
+ if (!$content) {
+ return "close";
+ } else {
+ $end = $content;
+ endOffset = $content.text().length;
+ }
+ }
- if (startCursor.newLine && endCursor.newLine) {
- callback("openSelectionPopover", Math.round(position.left), Math.round(position.top), false,
- startCursor.newLine-1, startCursor.newCh, endCursor.newLine-1, endCursor.newCh);
- } else if (startCursor.oldLine && endCursor.oldLine) {
- callback("openSelectionPopover", Math.round(position.left), Math.round(position.top), true,
- startCursor.oldLine-1, startCursor.oldCh, endCursor.oldLine-1, endCursor.oldCh);
- } else {
- return showInvalidSelection();
- }
- });
+ // check if there is anything selected
+ if ($start.is($end) && startOffset >= endOffset
+ || !hasOldOrNewData($start) || !hasOldOrNewData($end)) {
+ return "close";
+ }
- if (openComment) {
- $container.data("openComment", openComment);
+ firstRange.collapse(true);
+ var startRect = firstRange.getClientRects()[0];
+ lastRange.collapse(false);
+ var endRect = lastRange.getClientRects()[0];
+ if (!startRect || !endRect) {
+ startRect = selection.getRangeAt(0).getClientRects()[0];
+ endRect = startRect;
}
- for (var line in oldComments) {
- if (oldComments.hasOwnProperty(line)) {
- gitplex.server.textDiff.addCommentIndicator($container, true, line, oldComments[line][1]);
- }
+ var position = {
+ left: (startRect.left + endRect.right)/2,
+ top: startRect.top
+ };
+ position.left += $(window).scrollLeft();
+ position.top += $(window).scrollTop();
+
+ if (!commentSupport) {
+ var $content = $("<div></div>");
+ $content.append("<span class='invalid'><i class='fa fa-warning'></i> " + unableCommentMessage + "</span>");
+ return {
+ position: position,
+ content: $content
+ }
}
- for (var line in newComments) {
- if (newComments.hasOwnProperty(line)) {
- gitplex.server.textDiff.addCommentIndicator($container, false, line, newComments[line][1]);
- }
+
+ function showInvalidSelection() {
+ var $content = $("<div></div>");
+ $content.append("<a class='invalid'><i class='fa fa-warning'></i> Invalid selection, click for details</a>");
+ $content.children("a").attr("href", doclink + "/Diff+Selection");
+ return {
+ position: position,
+ content: $content
+ }
}
- gitplex.server.textDiff.highlightCommentTrigger($container);
+ // all lines between selection has been expanded
+ if ($startTr.nextAll("tr.expander").filter($endTr.prevAll("tr.expander")).length != 0
+ || $startTr.prevAll("tr.expander").filter($endTr.nextAll("tr.expander")).length != 0) {
+ return showInvalidSelection();
+ }
- if (mark) {
- $container.data("mark", mark);
- gitplex.server.textDiff.mark($container, mark);
- }
+ /*
+ * cursor.line represents line number of selection and cursor.ch represents
+ * character index inside text of the whole line, without considering element
+ * tags.
+ */
+ function getCursor($node, offset) {
+ $nodeTd = $node.parent();
+ var oldLine, newLine;
+ if (!$nodeTd.hasClass("old") && !$nodeTd.hasClass("new")
+ || $nodeTd.hasClass("old") && $nodeTd.hasClass("new")) {
+ oldLine = $nodeTd.data("old") + 1;
+ newLine = $nodeTd.data("new") + 1;
+ } else if ($nodeTd.hasClass("old")) {
+ oldLine = $nodeTd.data("old") + 1;
+ } else {
+ newLine = $nodeTd.data("new") + 1;
+ }
+ var $contents = $nodeTd.contents();
+ var oldOffset = 0, newOffset = 0;
+ for (var i=0; i<$contents.length; i++) {
+ var $content = $($contents[i]);
+ if ($content.is($node)) {
+ if ($content.hasClass("delete")) {
+ oldCh = oldOffset + offset;
+ newCh = undefined;
+ newLine = undefined;
+ } else if ($content.hasClass("insert")) {
+ oldCh = undefined;
+ oldLine = undefined;
+ newCh = newOffset + offset;
+ } else {
+ oldCh = oldOffset + offset;
+ newCh = newOffset + offset;
+ }
+ break;
+ } else {
+ var len = $content.text().length;
+ if ($content.hasClass("delete")) {
+ oldOffset += len;
+ } else if ($content.hasClass("insert")) {
+ newOffset += len;
+ } else {
+ oldOffset += len;
+ newOffset += len;
+ }
+ }
+ }
+ if ($nodeTd.hasClass("left")) {
+ newLine = newCh = undefined;
+ } else if ($nodeTd.hasClass("right")) {
+ oldLine = oldCh = undefined;
+ }
+ return {
+ oldLine: oldLine,
+ oldCh: oldCh,
+ newLine: newLine,
+ newCh: newCh
+ };
+ }
+
+ var startCursor = getCursor($start, startOffset);
+ var endCursor = getCursor($end, endOffset);
+
+ if (startCursor.newLine && endCursor.newLine) {
+ callback("openSelectionPopover", Math.round(position.left), Math.round(position.top), false,
+ startCursor.newLine-1, startCursor.newCh, endCursor.newLine-1, endCursor.newCh);
+ } else if (startCursor.oldLine && endCursor.oldLine) {
+ callback("openSelectionPopover", Math.round(position.left), Math.round(position.top), true,
+ startCursor.oldLine-1, startCursor.oldCh, endCursor.oldLine-1, endCursor.oldCh);
+ } else {
+ return showInvalidSelection();
+ }
+ });
+
+ if (openComment) {
+ $container.data("openComment", openComment);
+ }
+ for (var line in oldComments) {
+ if (oldComments.hasOwnProperty(line)) {
+ gitplex.server.textDiff.addCommentIndicator($container, true, line, oldComments[line][1]);
+ }
+ }
+ for (var line in newComments) {
+ if (newComments.hasOwnProperty(line)) {
+ gitplex.server.textDiff.addCommentIndicator($container, false, line, newComments[line][1]);
+ }
}
+ gitplex.server.textDiff.highlightCommentTrigger($container);
+
+ if (mark) {
+ $container.data("mark", mark);
+ gitplex.server.textDiff.mark($container, mark);
+ }
},
onWindowLoad: function(containerId, ajax, mark) {
var $container = $("#" + containerId);
diff --git a/web/src/main/java/com/gitplex/server/web/page/project/blob/render/renderers/source/SourceViewPanel.java b/web/src/main/java/com/gitplex/server/web/page/project/blob/render/renderers/source/SourceViewPanel.java
index 22d78eb300..4430319782 100644
--- a/web/src/main/java/com/gitplex/server/web/page/project/blob/render/renderers/source/SourceViewPanel.java
+++ b/web/src/main/java/com/gitplex/server/web/page/project/blob/render/renderers/source/SourceViewPanel.java
@@ -45,6 +45,7 @@ import org.apache.wicket.request.http.WebRequest;
import org.apache.wicket.request.http.WebResponse;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.eclipse.jgit.lib.FileMode;
+import org.eclipse.jgit.revwalk.RevCommit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.unbescape.html.HtmlEscape;
@@ -392,8 +393,32 @@ public class SourceViewPanel extends BlobViewPanel implements Markable, SearchMe
switch(params.getParameterValue("action").toString()) {
case "openSelectionPopover":
TextRange mark = getMark(params, "param1", "param2", "param3", "param4");
- String script = String.format("gitplex.server.sourceView.openSelectionPopover(%s, '%s', %s);",
- getJson(mark), context.getMarkUrl(mark), SecurityUtils.getUser()!=null);
+ String unableCommentMessage = null;
+ PullRequest request = context.getPullRequest();
+ String commitHash = context.getCommit().name();
+ if (request != null && !commitHash.equals(request.getBaseCommitHash())) {
+ boolean found = false;
+ for (RevCommit commit: context.getPullRequest().getCommits()) {
+ if (commit.name().equals(commitHash)) {
+ found = true;
+ break;
+ }
+ }
+
+ if (!found) {
+ if (request.getMergePreview() != null
+ && request.getMergePreview().getMerged() != null
+ && request.getMergePreview().getMerged().equals(commitHash)) {
+ unableCommentMessage = "Unable to comment on pull request merge preview";
+ } else {
+ unableCommentMessage = "Unable to comment on commits not belonging to pull request";
+ }
+ }
+ }
+
+ String script = String.format("gitplex.server.sourceView.openSelectionPopover(%s, '%s', %s, %s);",
+ getJson(mark), context.getMarkUrl(mark), SecurityUtils.getUser()!=null,
+ unableCommentMessage!=null?"'" + unableCommentMessage + "'":"undefined");
target.appendJavaScript(script);
break;
case "addComment":
@@ -850,6 +875,7 @@ public class SourceViewPanel extends BlobViewPanel implements Markable, SearchMe
CharSequence callback = ajaxBehavior.getCallbackFunction(
explicit("action"), explicit("param1"), explicit("param2"),
explicit("param3"), explicit("param4"));
+
String script = String.format("gitplex.server.sourceView.onDomReady('%s', '%s', %s, %s, '%s', '%s', "
+ "%s, %s, %s, %s, %s, '%s');",
JavaScriptEscape.escapeJavaScript(context.getBlobIdent().path),
diff --git a/web/src/main/java/com/gitplex/server/web/page/project/blob/render/renderers/source/source-view.js b/web/src/main/java/com/gitplex/server/web/page/project/blob/render/renderers/source/source-view.js
index 5df5ac3d09..9377e8e691 100644
--- a/web/src/main/java/com/gitplex/server/web/page/project/blob/render/renderers/source/source-view.js
+++ b/web/src/main/java/com/gitplex/server/web/page/project/blob/render/renderers/source/source-view.js
@@ -327,35 +327,40 @@ gitplex.server.sourceView = {
}
cm.setGutterMarker(parseInt(line), "CodeMirror-comments", $gutter[0]);
},
- openSelectionPopover: function(mark, markUrl, loggedIn) {
+ openSelectionPopover: function(mark, markUrl, loggedIn, unableCommentMessage) {
var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;
var ch = (mark.beginChar + mark.endChar)/2;
var position = cm.charCoords({line:mark.beginLine, ch:ch});
- var $content = $("<div><a class='permanent'><i class='fa fa-link'></i> Permanent link of this selection</a>");
- $content.children("a.permanent").attr("href", markUrl);
- $content.append("<a class='copy-marked'><i class='fa fa-clipboard'></i> Copy selected text to clipboard</a>");
- var clipboard = new Clipboard(".copy-marked", {
- text: function(trigger) {
- return cm.getSelection("\n");
- }
- });
- clipboard.on("success", function(e) {
- clipboard.destroy();
- });
- if (loggedIn) {
- $content.append("<a class='comment'><i class='fa fa-comment'></i> Add comment on this selection</a>");
- $content.children("a.comment").click(function() {
- if ($(".source-view").find("form.dirty").length != 0
- && !confirm("There are unsaved changes, discard and continue?")) {
- return;
- }
- var callback = $(".source-view").data("callback");
- callback("addComment", mark.beginLine, mark.beginChar, mark.endLine, mark.endChar);
- });
+ var $content;
+ if (unableCommentMessage) {
+ $content = $("<div><span class='invalid'><i class='fa fa-warning'></i> " + unableCommentMessage + "</a>");
} else {
- $content.append("<span class='comment'><i class='fa fa-warning'></i> Login to comment on selection</span>");
- }
+ $content = $("<div><a class='permanent'><i class='fa fa-link'></i> Permanent link of this selection</a>");
+ $content.children("a.permanent").attr("href", markUrl);
+ $content.append("<a class='copy-marked'><i class='fa fa-clipboard'></i> Copy selected text to clipboard</a>");
+ var clipboard = new Clipboard(".copy-marked", {
+ text: function(trigger) {
+ return cm.getSelection("\n");
+ }
+ });
+ clipboard.on("success", function(e) {
+ clipboard.destroy();
+ });
+ if (loggedIn) {
+ $content.append("<a class='comment'><i class='fa fa-comment'></i> Add comment on this selection</a>");
+ $content.children("a.comment").click(function() {
+ if ($(".source-view").find("form.dirty").length != 0
+ && !confirm("There are unsaved changes, discard and continue?")) {
+ return;
+ }
+ var callback = $(".source-view").data("callback");
+ callback("addComment", mark.beginLine, mark.beginChar, mark.endLine, mark.endChar);
+ });
+ } else {
+ $content.append("<span class='comment'><i class='fa fa-warning'></i> Login to comment on selection</span>");
+ }
+ }
$(".source-view>.code").selectionPopover("open", {
position: position,