Add diff web links to switch from normal diff to preview diff
This makes the preview diff screens accessible for user. If a preview
is available for a file then the side-by-side/unified diff screen
shows an icon to switch to the side-by-side/unified preview diff
screen. Before this change the URL for the preview diff screens had
to be typed manually.
Icons to navigate back from the preview diff screens to the normal
diff screens will be added in a follow-up change.
Change-Id: Ic88beef1f8c2d1e5c7f260f38cfe56702a92e759
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/Module.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/Module.java
index d822faf..7ca2d9a 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/Module.java
@@ -20,6 +20,7 @@
import com.google.gerrit.extensions.registration.DynamicMap;
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.extensions.webui.BranchWebLink;
+import com.google.gerrit.extensions.webui.DiffWebLink;
import com.google.gerrit.extensions.webui.FileWebLink;
import com.google.gerrit.extensions.webui.GerritTopMenu;
import com.google.gerrit.extensions.webui.ProjectWebLink;
@@ -27,6 +28,8 @@
import com.google.gerrit.server.config.FactoryModule;
import com.google.inject.Inject;
+import com.googlesource.gerrit.plugins.xdocs.client.SideBySideDiffPreviewWebLink;
+import com.googlesource.gerrit.plugins.xdocs.client.UnifiedDiffPreviewWebLink;
import com.googlesource.gerrit.plugins.xdocs.formatter.AsciidoctorFormatter;
import com.googlesource.gerrit.plugins.xdocs.formatter.Formatter;
import com.googlesource.gerrit.plugins.xdocs.formatter.MarkdownFormatter;
@@ -68,6 +71,10 @@
.to(XDocWebLink.class);
DynamicSet.bind(binder(), FileWebLink.class)
.to(XDocWebLink.class);
+ DynamicSet.bind(binder(), DiffWebLink.class)
+ .to(SideBySideDiffPreviewWebLink.class);
+ DynamicSet.bind(binder(), DiffWebLink.class)
+ .to(UnifiedDiffPreviewWebLink.class);
DynamicSet.bind(binder(), TopMenu.class).toInstance(new TopMenu() {
@Override
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/SideBySideDiffPreviewWebLink.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/SideBySideDiffPreviewWebLink.java
new file mode 100644
index 0000000..bed011e
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/SideBySideDiffPreviewWebLink.java
@@ -0,0 +1,72 @@
+// Copyright (C) 2014 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.xdocs.client;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.extensions.common.DiffWebLinkInfo;
+import com.google.gerrit.extensions.restapi.Url;
+import com.google.gerrit.extensions.webui.DiffWebLink;
+import com.google.inject.Inject;
+
+import com.googlesource.gerrit.plugins.xdocs.formatter.Formatters;
+import com.googlesource.gerrit.plugins.xdocs.formatter.Formatters.FormatterProvider;
+
+public class SideBySideDiffPreviewWebLink implements DiffWebLink {
+ private static final String SIDE_BY_SIDE_PREVIEW_DIFF = "side-by-side preview diff";
+
+ private final String pluginName;
+ private final Formatters formatters;
+
+ @Inject
+ SideBySideDiffPreviewWebLink(
+ @PluginName String pluginName,
+ Formatters formatters) {
+ this.pluginName = pluginName;
+ this.formatters = formatters;
+ }
+
+ @Override
+ public DiffWebLinkInfo getDiffLink(String projectName, int changeId,
+ Integer patchSetIdA, String revisionA, String fileNameA, int patchSetIdB,
+ String revisionB, String fileNameB) {
+ FormatterProvider formatter = formatters.get(projectName, fileNameB);
+ if (formatter == null) {
+ return null;
+ }
+
+ return DiffWebLinkInfo.forSideBySideDiffView(SIDE_BY_SIDE_PREVIEW_DIFF,
+ "plugins/" + pluginName + "/static/sideBySideDiffPreview.png",
+ getUrl(pluginName, changeId, patchSetIdA, patchSetIdB, fileNameB),
+ Target.SELF);
+ }
+
+ public static String getUrl(String pluginName, int changeId,
+ Integer patchSetIdA, int patchSetIdB, String fileName) {
+ StringBuilder url = new StringBuilder();
+ url.append("#/x/");
+ url.append(pluginName);
+ url.append("/c/");
+ url.append(changeId);
+ url.append("/");
+ if (patchSetIdA != null) {
+ url.append(patchSetIdA);
+ url.append("..");
+ }
+ url.append(patchSetIdB);
+ url.append("/");
+ url.append(Url.encode(fileName));
+ return url.toString();
+ }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/UnifiedDiffPreviewWebLink.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/UnifiedDiffPreviewWebLink.java
new file mode 100644
index 0000000..d52ca4c
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/client/UnifiedDiffPreviewWebLink.java
@@ -0,0 +1,59 @@
+// Copyright (C) 2014 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.xdocs.client;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.extensions.common.DiffWebLinkInfo;
+import com.google.gerrit.extensions.webui.DiffWebLink;
+import com.google.inject.Inject;
+
+import com.googlesource.gerrit.plugins.xdocs.formatter.Formatters;
+import com.googlesource.gerrit.plugins.xdocs.formatter.Formatters.FormatterProvider;
+
+public class UnifiedDiffPreviewWebLink implements DiffWebLink {
+ private static final String UNIFIED_PREVIEW_DIFF = "unified preview diff";
+
+ private final String pluginName;
+ private final Formatters formatters;
+
+ @Inject
+ UnifiedDiffPreviewWebLink(
+ @PluginName String pluginName,
+ Formatters formatters) {
+ this.pluginName = pluginName;
+ this.formatters = formatters;
+ }
+
+ @Override
+ public DiffWebLinkInfo getDiffLink(String projectName, int changeId,
+ Integer patchSetIdA, String revisionA, String fileNameA, int patchSetIdB,
+ String revisionB, String fileNameB) {
+ FormatterProvider formatter = formatters.get(projectName, fileNameB);
+ if (formatter == null) {
+ return null;
+ }
+
+ return DiffWebLinkInfo.forUnifiedDiffView(UNIFIED_PREVIEW_DIFF,
+ "plugins/" + pluginName + "/static/unifiedDiffPreview.png",
+ getUrl(pluginName, changeId, patchSetIdA, patchSetIdB, fileNameB),
+ Target.SELF);
+ }
+
+ private static String getUrl(String pluginName, int changeId,
+ Integer patchSetIdA, int patchSetIdB, String fileName) {
+ return SideBySideDiffPreviewWebLink.getUrl(pluginName, changeId,
+ patchSetIdA, patchSetIdB, fileName) + ",unified";
+ }
+}
diff --git a/src/main/resources/static/sideBySideDiffPreview.png b/src/main/resources/static/sideBySideDiffPreview.png
new file mode 100644
index 0000000..4525590
--- /dev/null
+++ b/src/main/resources/static/sideBySideDiffPreview.png
Binary files differ
diff --git a/src/main/resources/static/unifiedDiffPreview.png b/src/main/resources/static/unifiedDiffPreview.png
new file mode 100644
index 0000000..7625730
--- /dev/null
+++ b/src/main/resources/static/unifiedDiffPreview.png
Binary files differ