Skip to content

The Unified and Autonomous Development Platform

Do not re-create SourceViewPanel upon blame on/off.

robin shineab7299a897f9
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.html b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.html
index a03fdffd7e..13d737eae4 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.html
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.html
@@ -13,7 +13,7 @@
<div class="pull-right actions">
<div class="btn-group">
<a wicket:id="raw" class="btn btn-default btn-xs"><i class="fa fa-file-o"></i> Raw</a>
- <a wicket:id="blame" class="btn btn-default btn-xs blame-toggle"><i class="fa fa-comments"></i> Blame</a>
+ <a wicket:id="blame" class="btn btn-default btn-xs blame-toggle"><i class="fa fa-user"></i> Blame</a>
<a wicket:id="history" class="btn btn-default btn-xs"><i class="fa fa-history"></i> History</a>
<a wicket:id="edit" class="btn btn-default btn-xs"><i class="fa fa-pencil"></i> Edit</a>
<a wicket:id="delete" class="btn btn-default btn-xs"><i class="fa fa-trash"></i> Delete</a>
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.java
index 831eb0aa6a..066d83b0cd 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.java
@@ -87,13 +87,30 @@ public abstract class BlobViewPanel extends Panel {
protected void onInitialize() {
super.onInitialize();
- if (context.isBlame())
- add(AttributeAppender.append("class", " active"));
+ add(AttributeAppender.append("class", new AbstractReadOnlyModel<String>() {
+
+ @Override
+ public String getObject() {
+ if (context.isBlame())
+ return " active";
+ else
+ return " ";
+ }
+
+ }));
+
+ setOutputMarkupId(true);
}
@Override
public void onClick(AjaxRequestTarget target) {
context.onBlameChange(target);
+
+ // this blob view panel might be replaced with another panel
+ if (findPage() != null) {
+ target.add(this);
+ target.focusComponent(null);
+ }
}
@Override
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/markdown/MarkdownRenderer.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/markdown/MarkdownRenderer.java
index 1dccc8efe0..5dc1539ffc 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/markdown/MarkdownRenderer.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/markdown/MarkdownRenderer.java
@@ -12,7 +12,7 @@ public class MarkdownRenderer implements BlobRenderer {
if (context.getBlobIdent().isFile()
&& context.getBlob().getText() != null
&& context.getBlobIdent().path.endsWith(".md")) {
- if (context.getTokenPosition() != null)
+ if (context.getTokenPosition() != null || context.isBlame())
return new SourceViewPanel(panelId, context);
else
return new MarkdownViewPanel(panelId, context);
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/SourceViewPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/SourceViewPanel.java
index 466edadd6f..bb971f0994 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/SourceViewPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/SourceViewPanel.java
@@ -3,6 +3,8 @@ package com.pmease.gitplex.web.component.blobview.source;
import java.util.ArrayList;
import java.util.List;
+import javax.annotation.Nullable;
+
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
@@ -98,15 +100,20 @@ public class SourceViewPanel extends BlobViewPanel {
return fragment;
}
- public void highlightToken(AjaxRequestTarget target, TokenPosition tokenPos) {
- try {
- String json = GitPlex.getInstance(ObjectMapper.class).writeValueAsString(tokenPos);
- String script = String.format("gitplex.sourceview.highlightToken('%s', %s);",
- codeContainer.getMarkupId(), json);
- target.appendJavaScript(script);
- } catch (JsonProcessingException e) {
- throw new RuntimeException(e);
+ public void highlightToken(AjaxRequestTarget target, @Nullable TokenPosition tokenPos) {
+ String json;
+ if (tokenPos != null) {
+ try {
+ json = GitPlex.getInstance(ObjectMapper.class).writeValueAsString(tokenPos);
+ } catch (JsonProcessingException e) {
+ throw new RuntimeException(e);
+ }
+ } else {
+ json = "undefined";
}
+ String script = String.format("gitplex.sourceview.highlightToken('%s', %s);",
+ codeContainer.getMarkupId(), json);
+ target.appendJavaScript(script);
}
@Override
@@ -269,7 +276,7 @@ public class SourceViewPanel extends BlobViewPanel {
highlightToken,
RequestCycle.get().urlFor(ajaxIndicator, new PageParameters()),
getCallbackFunction(CallbackParameter.explicit("symbol")),
- getBlameBlocks());
+ getBlameCommits());
response.render(OnDomReadyHeaderItem.forScript(script));
}
@@ -277,8 +284,14 @@ public class SourceViewPanel extends BlobViewPanel {
setOutputMarkupId(true);
}
+
+ public void onBlameChange(AjaxRequestTarget target) {
+ String script = String.format("gitplex.sourceview.blame('%s', %s);",
+ codeContainer.getMarkupId(), getBlameCommits());
+ target.appendJavaScript(script);
+ }
- private String getBlameBlocks() {
+ private String getBlameCommits() {
if (context.isBlame()) {
List<BlameCommit> commits = new ArrayList<>();
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.css b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.css
index 3053c5927e..e6430e6022 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.css
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.css
@@ -44,3 +44,11 @@
.CodeMirror-annotation .hash, .CodeMirror-annotation .date {
margin-right: 8px;
}
+.CodeMirror-annotation .same-as-above {
+ margin-left: 100px;
+ color: #999;
+ font-size: 10px;
+}
+.CodeMirror-activeline .CodeMirror-annotation {
+ background: #E8F2FE;
+}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.js b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.js
index 6b1c1e17c3..ba0582319e 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.js
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.js
@@ -1,5 +1,5 @@
gitplex.sourceview = {
- init: function(codeId, fileContent, filePath, tokenPos, ajaxIndicatorUrl, symbolQuery, blames) {
+ init: function(codeId, fileContent, filePath, tokenPos, ajaxIndicatorUrl, symbolQuery, blameCommits) {
var cm;
var $code = $("#" + codeId);
@@ -100,11 +100,11 @@ gitplex.sourceview = {
if (scrollPos)
cm.scrollTo(scrollPos.left, scrollPos.top);
- if (blames) {
+ if (blameCommits) {
// render blame blocks with a timer to avoid the issue that occasionally
// blame gutter becomes much wider than expected
setTimeout(function() {
- gitplex.sourceview.blame(cm, blames);
+ gitplex.sourceview.blame(cm, blameCommits);
}, 10);
}
}
@@ -124,35 +124,46 @@ gitplex.sourceview = {
if (typeof cm === "string")
cm = $("#"+ cm + ">.CodeMirror")[0].CodeMirror;
- var h = cm.getScrollInfo().clientHeight;
- var coords = cm.charCoords({line: tokenPos.line, ch: 0}, "local");
- cm.scrollTo(null, (coords.top + coords.bottom - h) / 2);
-
- if (tokenPos.range) {
- var anchor = {line: tokenPos.line, ch: tokenPos.range.start};
- var head = {line: tokenPos.line, ch: tokenPos.range.end};
- cm.setSelection(anchor, head);
+ if (tokenPos) {
+ var h = cm.getScrollInfo().clientHeight;
+ var coords = cm.charCoords({line: tokenPos.line, ch: 0}, "local");
+ cm.scrollTo(null, (coords.top + coords.bottom - h) / 2);
+
+ if (tokenPos.range) {
+ var anchor = {line: tokenPos.line, ch: tokenPos.range.start};
+ var head = {line: tokenPos.line, ch: tokenPos.range.end};
+ cm.setSelection(anchor, head);
+ } else {
+ cm.setCursor(tokenPos.line);
+ }
} else {
- cm.setCursor(tokenPos.line);
+ cm.setCursor(0, 0);
}
-
},
- blame: function(cm, blames) {
+ blame: function(cm, blameCommits) {
if (typeof cm === "string")
cm = $("#"+ cm + ">.CodeMirror")[0].CodeMirror;
- if (blames) {
+ if (blameCommits) {
cm.setOption("gutters", ["CodeMirror-annotations", "CodeMirror-linenumbers", "CodeMirror-foldgutter"]);
- for (var i in blames) {
- var blame = blames[i];
- for (var j in blame.ranges) {
+ for (var i in blameCommits) {
+ var commit = blameCommits[i];
+ for (var j in commit.ranges) {
+ var range = commit.ranges[j];
var $ele = $(document.createElement("div"));
$ele.addClass("CodeMirror-annotation");
- $("<a class='hash'>" + blame.hash + "</a>").appendTo($ele).attr("href", blame.url).attr("title", blame.message);
- $ele.append("<span class='date'>" + blame.authorDate + "</span>");
- $ele.append("<span class='author'>" + blame.authorName + "</span>");
- cm.setGutterMarker(blame.ranges[j].beginLine, "CodeMirror-annotations", $ele[0]);
+ $("<a class='hash'>" + commit.hash + "</a>").appendTo($ele).attr("href", commit.url).attr("title", commit.message);
+ $ele.append("<span class='date'>" + commit.authorDate + "</span>");
+ $ele.append("<span class='author'>" + commit.authorName + "</span>");
+ cm.setGutterMarker(range.beginLine, "CodeMirror-annotations", $ele[0]);
+
+ for (var line = range.beginLine+1; line<range.endLine; line++) {
+ var $ele = $(document.createElement("div"));
+ $ele.addClass("CodeMirror-annotation");
+ $ele.append("<span class='same-as-above fa fa-arrow-up' title='same as above'></span>");
+ cm.setGutterMarker(line, "CodeMirror-annotations", $ele[0]);
+ }
}
}
} else {
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/RepoFilePage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/RepoFilePage.java
index 81ee697476..aab686602a 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/RepoFilePage.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/file/RepoFilePage.java
@@ -320,26 +320,6 @@ public class RepoFilePage extends RepositoryPage {
}
private void newFileViewer(AjaxRequestTarget target) {
- if (target != null && fileViewer instanceof SourceViewPanel) {
- SourceViewPanel sourceViewer = (SourceViewPanel) fileViewer;
- BlobViewContext context = sourceViewer.getContext();
- if (context.getBlobIdent().equals(file) && context.isBlame() == blame) {
- sourceViewer.getContext().setTokenPosition(tokenPos);
- if (tokenPos != null) {
- sourceViewer.highlightToken(target, tokenPos);
- } else {
- BlobViewPanel blobViewer = sourceViewer.getContext().render(FILE_VIEWER_ID);
- if (!(blobViewer instanceof SourceViewPanel)) {
- fileViewer.replaceWith(blobViewer);
- fileViewer = blobViewer;
- target.add(fileViewer);
- target.appendJavaScript("$(window).resize();");
- }
- }
- return;
- }
- }
-
if (file.path == null || file.isTree()) {
fileViewer = new FileListPanel(FILE_VIEWER_ID, repoModel, file) {
@@ -386,8 +366,27 @@ public class RepoFilePage extends RepositoryPage {
public void onBlameChange(AjaxRequestTarget target) {
blame = !blame;
- newFileViewer(target);
-
+ if (fileViewer instanceof SourceViewPanel) {
+ SourceViewPanel sourceViewer = (SourceViewPanel) fileViewer;
+ BlobViewContext context = sourceViewer.getContext();
+ context.setBlame(blame);
+ if (blame || tokenPos != null) {
+ sourceViewer.onBlameChange(target);
+ } else {
+ BlobViewPanel blobViewer = sourceViewer.getContext().render(FILE_VIEWER_ID);
+ if (blobViewer instanceof SourceViewPanel) {
+ sourceViewer.onBlameChange(target);
+ } else {
+ fileViewer.replaceWith(blobViewer);
+ fileViewer = blobViewer;
+ target.add(fileViewer);
+ target.appendJavaScript("$(window).resize();");
+ }
+ }
+ } else {
+ newFileViewer(target);
+ }
+
pushState(target);
}
@@ -486,13 +485,31 @@ public class RepoFilePage extends RepositoryPage {
@Override
protected void onSelect(AjaxRequestTarget target, QueryHit hit) {
- file = new BlobIdent(file.revision, hit.getBlobPath(), FileMode.REGULAR_FILE.getBits());
tokenPos = hit.getTokenPos();
- blame = false;
-
- newFileNavigator(target);
- newFileViewer(target);
-
+ if (hit.getBlobPath().equals(file.path) && fileViewer instanceof SourceViewPanel) {
+ SourceViewPanel sourceViewer = (SourceViewPanel) fileViewer;
+ BlobViewContext context = sourceViewer.getContext();
+ context.setTokenPosition(tokenPos);
+ if (tokenPos != null || blame) {
+ sourceViewer.highlightToken(target, tokenPos);
+ } else {
+ BlobViewPanel blobViewer = sourceViewer.getContext().render(FILE_VIEWER_ID);
+ if (blobViewer instanceof SourceViewPanel) {
+ sourceViewer.highlightToken(target, tokenPos);
+ } else {
+ fileViewer.replaceWith(blobViewer);
+ fileViewer = blobViewer;
+ target.add(fileViewer);
+ target.appendJavaScript("$(window).resize();");
+ }
+ }
+ } else {
+ blame = false;
+ file.path = hit.getBlobPath();
+ file.mode = FileMode.REGULAR_FILE.getBits();
+ newFileNavigator(target);
+ newFileViewer(target);
+ }
pushState(target);
}