Skip to content

The Unified and Autonomous Development Platform

Add document links to wiki.pmease.com

robin shine0256fa042973
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/GitPlex.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/GitPlex.java
index a1ea2f00bf..a695aad989 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/GitPlex.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/GitPlex.java
@@ -1,157 +1,161 @@
-package com.pmease.gitplex.core;
-
-import java.io.ObjectStreamException;
-import java.io.Serializable;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
-import java.util.List;
-import java.util.Set;
-
-import javax.annotation.Nullable;
-import javax.inject.Inject;
-
-import org.apache.commons.lang3.StringUtils;
-import org.apache.shiro.util.ThreadContext;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.pmease.commons.hibernate.Sessional;
-import com.pmease.commons.loader.AbstractPlugin;
-import com.pmease.commons.loader.AppLoader;
-import com.pmease.commons.loader.ListenerRegistry;
-import com.pmease.commons.loader.ManagedSerializedForm;
-import com.pmease.commons.util.init.InitStage;
-import com.pmease.commons.util.init.ManualConfig;
-import com.pmease.gitplex.core.event.lifecycle.SystemStarted;
-import com.pmease.gitplex.core.event.lifecycle.SystemStarting;
-import com.pmease.gitplex.core.event.lifecycle.SystemStopped;
-import com.pmease.gitplex.core.event.lifecycle.SystemStopping;
-import com.pmease.gitplex.core.manager.AccountManager;
-import com.pmease.gitplex.core.manager.ConfigManager;
-import com.pmease.gitplex.core.manager.DataManager;
-import com.pmease.gitplex.core.setting.ServerConfig;
-
-public class GitPlex extends AbstractPlugin implements Serializable {
-
- private static final Logger logger = LoggerFactory.getLogger(GitPlex.class);
-
- private final ConfigManager configManager;
-
- private final DataManager dataManager;
-
- private final AccountManager accountManager;
-
- private final ServerConfig serverConfig;
-
- private final ListenerRegistry listenerRegistry;
-
- private volatile InitStage initStage;
-
- @Inject
- public GitPlex(ServerConfig serverConfig, DataManager dataManager, ConfigManager configManager,
- AccountManager accountManager, ListenerRegistry listenerRegistry) {
- this.configManager = configManager;
- this.dataManager = dataManager;
- this.serverConfig = serverConfig;
- this.accountManager = accountManager;
- this.listenerRegistry = listenerRegistry;
-
- initStage = new InitStage("Server is Starting...");
- }
-
- @Override
- @Sessional
- public void start() {
- List<ManualConfig> manualConfigs = dataManager.init();
-
- if (!manualConfigs.isEmpty()) {
- logger.warn("Please set up the server at " + guessServerUrl());
- initStage = new InitStage("Server Setup", manualConfigs);
-
- initStage.waitFor();
- }
-
- ThreadContext.bind(accountManager.getRoot().asSubject());
-
- listenerRegistry.post(new SystemStarting());
- }
-
- @Sessional
- @Override
- public void postStart() {
- initStage = null;
-
- listenerRegistry.post(new SystemStarted());
-
- ThreadContext.unbindSubject();
-
- logger.info("Server is ready at " + configManager.getSystemSetting().getServerUrl() + ".");
- }
-
- public String guessServerUrl() {
- String hostName;
- try {
- hostName = InetAddress.getLocalHost().getHostName();
- } catch (UnknownHostException e) {
- throw new RuntimeException(e);
- }
-
- String serverUrl;
- if (serverConfig.getHttpPort() != 0)
- serverUrl = "http://" + hostName + ":" + serverConfig.getHttpPort();
- else
- serverUrl = "https://" + hostName + ":" + serverConfig.getSslConfig().getPort();
-
- return StringUtils.stripEnd(serverUrl, "/");
- }
-
- /**
- * This method can be called from different UI threads, so we clone initStage to
- * make it thread-safe.
- * <p>
- * @return
- * cloned initStage, or <tt>null</tt> if system initialization is completed
- */
- public @Nullable InitStage getInitStage() {
- if (initStage != null) {
- return initStage.clone();
- } else {
- return null;
- }
- }
-
- public boolean isReady() {
- return initStage == null;
- }
-
- public static GitPlex getInstance() {
- return AppLoader.getInstance(GitPlex.class);
- }
-
- public static <T> T getInstance(Class<T> type) {
- return AppLoader.getInstance(type);
- }
-
- public static <T> Set<T> getExtensions(Class<T> extensionPoint) {
- return AppLoader.getExtensions(extensionPoint);
- }
-
- @Sessional
- @Override
- public void preStop() {
- ThreadContext.bind(accountManager.getRoot().asSubject());
- listenerRegistry.post(new SystemStopping());
- }
-
- @Sessional
- @Override
- public void stop() {
- listenerRegistry.post(new SystemStopped());
- ThreadContext.unbindSubject();
- }
-
- public Object writeReplace() throws ObjectStreamException {
- return new ManagedSerializedForm(GitPlex.class);
- }
-
-}
+package com.pmease.gitplex.core;
+
+import java.io.ObjectStreamException;
+import java.io.Serializable;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.List;
+import java.util.Set;
+
+import javax.annotation.Nullable;
+import javax.inject.Inject;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.shiro.util.ThreadContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.pmease.commons.hibernate.Sessional;
+import com.pmease.commons.loader.AbstractPlugin;
+import com.pmease.commons.loader.AppLoader;
+import com.pmease.commons.loader.ListenerRegistry;
+import com.pmease.commons.loader.ManagedSerializedForm;
+import com.pmease.commons.util.init.InitStage;
+import com.pmease.commons.util.init.ManualConfig;
+import com.pmease.gitplex.core.event.lifecycle.SystemStarted;
+import com.pmease.gitplex.core.event.lifecycle.SystemStarting;
+import com.pmease.gitplex.core.event.lifecycle.SystemStopped;
+import com.pmease.gitplex.core.event.lifecycle.SystemStopping;
+import com.pmease.gitplex.core.manager.AccountManager;
+import com.pmease.gitplex.core.manager.ConfigManager;
+import com.pmease.gitplex.core.manager.DataManager;
+import com.pmease.gitplex.core.setting.ServerConfig;
+
+public class GitPlex extends AbstractPlugin implements Serializable {
+
+ private static final Logger logger = LoggerFactory.getLogger(GitPlex.class);
+
+ private final ConfigManager configManager;
+
+ private final DataManager dataManager;
+
+ private final AccountManager accountManager;
+
+ private final ServerConfig serverConfig;
+
+ private final ListenerRegistry listenerRegistry;
+
+ private volatile InitStage initStage;
+
+ @Inject
+ public GitPlex(ServerConfig serverConfig, DataManager dataManager, ConfigManager configManager,
+ AccountManager accountManager, ListenerRegistry listenerRegistry) {
+ this.configManager = configManager;
+ this.dataManager = dataManager;
+ this.serverConfig = serverConfig;
+ this.accountManager = accountManager;
+ this.listenerRegistry = listenerRegistry;
+
+ initStage = new InitStage("Server is Starting...");
+ }
+
+ @Override
+ @Sessional
+ public void start() {
+ List<ManualConfig> manualConfigs = dataManager.init();
+
+ if (!manualConfigs.isEmpty()) {
+ logger.warn("Please set up the server at " + guessServerUrl());
+ initStage = new InitStage("Server Setup", manualConfigs);
+
+ initStage.waitFor();
+ }
+
+ ThreadContext.bind(accountManager.getRoot().asSubject());
+
+ listenerRegistry.post(new SystemStarting());
+ }
+
+ @Sessional
+ @Override
+ public void postStart() {
+ initStage = null;
+
+ listenerRegistry.post(new SystemStarted());
+
+ ThreadContext.unbindSubject();
+
+ logger.info("Server is ready at " + configManager.getSystemSetting().getServerUrl() + ".");
+ }
+
+ public String guessServerUrl() {
+ String hostName;
+ try {
+ hostName = InetAddress.getLocalHost().getHostName();
+ } catch (UnknownHostException e) {
+ throw new RuntimeException(e);
+ }
+
+ String serverUrl;
+ if (serverConfig.getHttpPort() != 0)
+ serverUrl = "http://" + hostName + ":" + serverConfig.getHttpPort();
+ else
+ serverUrl = "https://" + hostName + ":" + serverConfig.getSslConfig().getPort();
+
+ return StringUtils.stripEnd(serverUrl, "/");
+ }
+
+ /**
+ * This method can be called from different UI threads, so we clone initStage to
+ * make it thread-safe.
+ * <p>
+ * @return
+ * cloned initStage, or <tt>null</tt> if system initialization is completed
+ */
+ public @Nullable InitStage getInitStage() {
+ if (initStage != null) {
+ return initStage.clone();
+ } else {
+ return null;
+ }
+ }
+
+ public boolean isReady() {
+ return initStage == null;
+ }
+
+ public static GitPlex getInstance() {
+ return AppLoader.getInstance(GitPlex.class);
+ }
+
+ public static <T> T getInstance(Class<T> type) {
+ return AppLoader.getInstance(type);
+ }
+
+ public static <T> Set<T> getExtensions(Class<T> extensionPoint) {
+ return AppLoader.getExtensions(extensionPoint);
+ }
+
+ @Sessional
+ @Override
+ public void preStop() {
+ ThreadContext.bind(accountManager.getRoot().asSubject());
+ listenerRegistry.post(new SystemStopping());
+ }
+
+ @Sessional
+ @Override
+ public void stop() {
+ listenerRegistry.post(new SystemStopped());
+ ThreadContext.unbindSubject();
+ }
+
+ public Object writeReplace() throws ObjectStreamException {
+ return new ManagedSerializedForm(GitPlex.class);
+ }
+
+ public String getDocLink() {
+ return "http://wiki.pmease.com//display/gp" + StringUtils.substringBefore(AppLoader.getProduct().getVersion(), ".");
+ }
+
+}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/blob/text/TextDiffPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/blob/text/TextDiffPanel.java
index 06aa6324c6..634a05c84d 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/blob/text/TextDiffPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/blob/text/TextDiffPanel.java
@@ -502,12 +502,13 @@ 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.textdiff.init('%s', '%s', '%s', '%s', %s, %s, %s, %s, %s, %s, %s, %s);",
+ String script = String.format("gitplex.textdiff.init('%s', '%s', '%s', '%s', %s, %s, %s, %s, %s, %s, %s, %s, '%s');",
getMarkupId(), symbolTooltip.getMarkupId(),
change.getOldBlobIdent().revision, change.getNewBlobIdent().revision,
RequestCycle.get().find(AjaxRequestTarget.class) == null, callback,
markSupport!=null, jsonOfMark, jsonOfCommentInfo,
- jsonOfOldCommentInfos, jsonOfNewCommentInfos, dirtyContainerId);
+ jsonOfOldCommentInfos, jsonOfNewCommentInfos, dirtyContainerId,
+ GitPlex.getInstance().getDocLink());
response.render(OnDomReadyHeaderItem.forScript(script));
}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/blob/text/text-diff.js b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/blob/text/text-diff.js
index 34df81f571..c1f5e66515 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/blob/text/text-diff.js
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/diff/blob/text/text-diff.js
@@ -1,917 +1,917 @@
-gitplex.textdiff = {
- symbolClasses: ".cm-property, .cm-variable, .cm-variable-2, .cm-variable-3, .cm-def, .cm-meta",
- init: function(containerId, symbolTooltipId, oldRev, newRev, scroll, callback,
- markSupport, mark, openComment, oldComments, newComments, dirtyContainerId) {
- var $container = $("#" + containerId);
- $container.data("dirtyContainerId", dirtyContainerId);
- $container.data("callback", callback);
- $container.data("symbolHover", function() {
- var revision;
- var $symbol = $(this);
- if ($symbol.hasClass("delete")) {
- revision = oldRev;
- } else {
- var $td = $symbol.closest("td");
-
- // if is deleted line or if it is on left side of split view
- if ($td.hasClass("old") && !$td.hasClass("new") || $td.next().is("td"))
- revision = oldRev;
- else
- revision = newRev;
- }
- var symbolTooltip = document.getElementById(symbolTooltipId);
- if (symbolTooltip.onMouseOverSymbol)
- symbolTooltip.onMouseOverSymbol(revision, this);
- });
- $container.data("onMouseOverContent", function() {
- if (!gitplex.mouseState.pressed) {
- if ($(this).hasClass("left")) {
- $container.find("td.content.right").addClass("noselect");
- $container.find("td.content.left").removeClass("noselect");
- } else if ($(this).hasClass("right")) {
- $container.find("td.content.left").addClass("noselect");
- $container.find("td.content.right").removeClass("noselect");
- }
- }
- });
- var $symbols = $container.find(gitplex.textdiff.symbolClasses);
- $symbols.mouseover($container.data("symbolHover"));
- $container.find("td.content").mouseover($container.data("onMouseOverContent"));
-
- gitplex.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");
-
- // 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");
- }
-
- /*
- * 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();
- if ($tr.length == 0) {
- return "close";
- }
- 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";
- }
- $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();
- if ($tr.length == 0) {
- return "close";
- }
- 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 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) {
- return "close";
- } else {
- $start = $content;
- startOffset = 0;
- }
- }
-
- $content = $end;
- while (!$content.is($start) && (endOffset == 0 || !hasOldOrNewData($content))) {
- $content = prevContent($content);
- if (!$content) {
- return "close";
- } else {
- $end = $content;
- endOffset = $content.text().length;
- }
- }
-
- // check if there is anything selected
- if ($start.is($end) && startOffset >= endOffset
- || !hasOldOrNewData($start) || !hasOldOrNewData($end)) {
- 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();
-
- 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", "http://wiki.pmease.com/display/gp/Diff+Selection");
- return {
- position: position,
- content: $content
- }
- }
-
- // 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();
- }
-
- /*
- * 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.textdiff.addCommentIndicator($container, true, line, oldComments[line][1]);
- }
- }
- for (var line in newComments) {
- if (newComments.hasOwnProperty(line)) {
- gitplex.textdiff.addCommentIndicator($container, false, line, newComments[line][1]);
- }
- }
-
- gitplex.textdiff.highlightCommentTrigger($container);
-
- if (mark) {
- $container.data("mark", mark);
- gitplex.textdiff.mark($container, mark);
- if (scroll && !pmease.commons.history.isVisited())
- gitplex.textdiff.scroll($container, mark);
- }
- }
-
- },
- initBlameTooltip: function(containerId, $hashLink) {
- var $container = $("#" + containerId);
- var alignment = {targetX: 100, targetY: 100, x: 0, y: 0};
- $hashLink.hover(function() {
- var $commitLink = $(this);
- var $content = $commitLink.closest("tr").children("td.content");
- var oldLine = $content.data("old");
- if (oldLine == undefined)
- oldLine = -1;
- var newLine = $content.data("new");
- if (newLine == undefined)
- newLine = -1;
-
- var tooltipId = "blame-message-" + containerId + "_" + oldLine + "_" + newLine;
- $container.data("callback")("showBlameMessage", tooltipId, $(this).data("hash"));
- var $tooltip = $("<div class='blame-message'><div class='loading'>Loading...</div></div>");
- $tooltip.attr("id", tooltipId);
- $tooltip.data("trigger", this);
- $tooltip.data("alignment", alignment);
- $container.append($tooltip);
- return $tooltip;
- }, alignment);
- },
- showBlameMessage: function(tooltipId, message) {
- var $blameTooltip = $("#" + tooltipId);
- $blameTooltip.empty().text(message);
- $blameTooltip.align({placement: $blameTooltip.data("alignment"), target: {element: $blameTooltip.data("trigger")}});
- },
- openSelectionPopover: function(containerId, position, mark, markUrl, loggedIn) {
- var $container = $("#" + containerId);
-
- 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);
- 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 ($("#"+$container.data("dirtyContainerId")).find("form.dirty").length != 0
- && !confirm("There are unsaved changes, discard and continue?")) {
- return;
- }
- $container.data("callback")("addComment", mark.leftSide,
- mark.beginLine, mark.beginChar, mark.endLine, mark.endChar);
- });
- } else {
- $content.append("<span class='comment'><i class='fa fa-warning'></i> Log in to comment on selection</span>");
- }
-
- $container.selectionPopover("open", {
- position: position,
- content: $content
- });
- },
- expand: function(containerId, blockIndex, expandedHtml) {
- var $container = $("#" + containerId);
- var $expanderTr = $container.find(".expander" + blockIndex);
- var $prevTr = $expanderTr.prev();
- var $nextTr = $expanderTr.next();
- $expanderTr.replaceWith(expandedHtml);
- var $expandedTrs;
- if ($prevTr.length != 0 && $nextTr.length != 0) {
- $expandedTrs = $prevTr.nextAll().filter($nextTr.prevAll());
- } else if ($prevTr.length != 0) {
- $expandedTrs = $prevTr.nextAll();
- } else {
- $expandedTrs = $nextTr.prevAll();
- }
- var $symbols = $expandedTrs.find(gitplex.textdiff.symbolClasses);
- $symbols.mouseover($container.data("symbolHover"));
- $expandedTrs.find("td.content").mouseover($container.data("onMouseOverContent"));
- var $firstExpandedTr = $expandedTrs.first();
-
- function processBlames(index) {
- var commitHash = $firstExpandedTr.children("td.blame").eq(index).children("a.hash").data("hash");
- var prevCommitHash;
- $firstExpandedTr.prevAll().each(function() {
- if (!prevCommitHash) {
- var $tr = $(this);
- var $prevCommitLink = $tr.children("td.blame").eq(index).children("a.hash");
- if ($prevCommitLink.length != 0) {
- prevCommitHash = $prevCommitLink.data("hash");
- }
- }
- });
- if (commitHash == prevCommitHash) {
- $firstExpandedTr.children("td.blame").eq(index).html("<div class='same-as-above'>...</div>");
- }
-
- if ($nextTr.length != 0) {
- commitHash = $nextTr.children("td.blame").eq(index).children("a.hash").data("hash");
- prevCommitHash = undefined;
- $nextTr.prevAll().each(function() {
- if (!prevCommitHash) {
- var $prevCommitLink = $(this).children("td.blame").eq(index).children("a.hash");
- if ($prevCommitLink.length != 0) {
- prevCommitHash = $prevCommitLink.data("hash");
- }
- }
- });
- if (commitHash == prevCommitHash) {
- $nextTr.children("td.blame").eq(index).html("<div class='same-as-above'>...</div>");
- }
- }
- }
- if ($firstExpandedTr.children("td.blame").length == 1) {
- processBlames(0);
- } else if ($firstExpandedTr.children("td.blame").length == 2) {
- processBlames(0);
- processBlames(1);
- }
-
- gitplex.textdiff.initBlameTooltip(containerId, $expandedTrs.find(">td.blame>a.hash"));
-
- $(window).resize();
- },
- getMarkInfo: function($container, mark) {
- var oldOrNew = mark.leftSide?"old":"new";
- var startCursor = [mark.beginLine+1, mark.beginChar];
- var endCursor = [mark.endLine+1, mark.endChar];
- var $startTd = $container.find("td.content[data-" + oldOrNew + "='" + (startCursor[0]-1) + "']");
- var $endTd = $container.find("td.content[data-" + oldOrNew + "='" + (endCursor[0]-1) + "']");
- if ($startTd.length == 0) {
- console.error("Unable to find start td!");
- $startTd = undefined;
- } else if ($startTd.length == 2) {
- if (oldOrNew == "old") {
- $startTd = $startTd.first();
- } else {
- $startTd = $startTd.last();
- }
- }
- if ($endTd.length == 0) {
- console.error("Unable to find end td!");
- $endTd = undefined;
- } else if ($endTd.length == 2) {
- if (oldOrNew == "old") {
- $endTd = $endTd.first();
- } else {
- $endTd = $endTd.last();
- }
- }
- return {
- startTd: $startTd,
- startCursor: startCursor,
- endTd: $endTd,
- endCursor: endCursor,
- oldOrNew: oldOrNew
- };
- },
- scroll: function($container, mark) {
- // use a timer to scroll after possible view port resize
- setTimeout(function() {
- var markInfo = gitplex.textdiff.getMarkInfo($container, mark);
- var $startTd = markInfo.startTd;
- var $endTd = markInfo.endTd;
- if ($startTd && $endTd) {
- var top = $startTd.offset().top;
- var bottom = $endTd.offset().top + $endTd.outerHeight();
- var markHeight = bottom - top;
- var windowHeight = $(window).height();
- if (windowHeight <= markHeight) {
- $(window).scrollTop(top-50);
- } else {
- $(window).scrollTop((top+bottom-windowHeight)/2);
- }
- }
- }, 0);
- },
- mark: function($container, mark) {
- gitplex.textdiff.clearMark($container);
-
- var markInfo = gitplex.textdiff.getMarkInfo($container, mark);
- var $startTd = markInfo.startTd;
- var $endTd = markInfo.endTd;
- if ($startTd && $endTd) {
- var startCursor = markInfo.startCursor;
- var endCursor = markInfo.endCursor;
- var oldOrNew = markInfo.oldOrNew;
- var $td = $startTd;
- while (true) {
- var ch = 0;
- $td.addClass("content-mark");
- $td.data("beforemark", $td.html());
- $td.contents().each(function() {
- var $this = $(this);
- var text = $this.text();
- function markText(from, to) {
- var text = $this.text();
- var left = text.substring(0, from);
- var middle = text.substring(from, to);
- var right = text.substring(to);
-
- if (left.length == 0 && right.length == 0 && $this.is("span")) {
- $this.addClass("content-mark");
- } else {
- var classes;
- if ($this.is("span"))
- classes = $this.attr("class");
- else
- classes = "";
-
- var $current = $this;
- if (left.length != 0) {
- $current.after("<span></span>");
- $current = $current.next();
- $current.attr("class", classes).text(left);
- }
-
- $current.after("<span></span>");
- $current = $current.next();
- $current.attr("class", classes + " content-mark").text(middle);
-
- if (right.length != 0) {
- $current.after("<span></span>");
- $current = $current.next();
- $current.attr("class", classes).text(right);
- }
- $this.remove();
- }
- }
- if ($this.hasClass("insert") && oldOrNew == "old"
- || $this.hasClass("delete") && oldOrNew == "new") {
- if (!$td.is($startTd) && !$td.is($endTd)) {
- markText(0, text.length);
- } else if ($td.is($startTd) && $td.is($endTd)) {
- if (endCursor[1]>ch && startCursor[1]<ch) {
- markText(0, text.length);
- }
- } else if ($td.is($startTd)) {
- if (startCursor[1]<ch) {
- markText(0, text.length);
- }
- } else {
- if (endCursor[1]>ch) {
- markText(0, text.length);
- }
- }
- } else {
- var nextCh = ch + text.length;
- if (!$td.is($startTd) && !$td.is($endTd)) {
- markText(0, text.length);
- } else if ($td.is($startTd) && $td.is($endTd)) {
- if (endCursor[1]>ch && startCursor[1]<nextCh) {
- if (ch>=startCursor[1] && nextCh<=endCursor[1]) {
- markText(0, text.length);
- } else if (ch<startCursor[1] && nextCh>endCursor[1]) {
- markText(startCursor[1]-ch, endCursor[1]-ch);
- } else if (ch<startCursor[1]) {
- markText(startCursor[1]-ch, text.length);
- } else {
- markText(0, endCursor[1]-ch);
- }
- }
- } else if ($td.is($startTd)) {
- if (ch>=startCursor[1]) {
- markText(0, text.length);
- } else if (nextCh>startCursor[1]) {
- markText(startCursor[1]-ch, text.length);
- }
- } else {
- if (nextCh<=endCursor[1]) {
- markText(0, text.length);
- } else if (ch<endCursor[1]) {
- markText(0, endCursor[1]-ch);
- }
- }
- ch = nextCh;
- }
- });
- if ($td.is($endTd) || $td.length == 0) {
- break;
- } else {
- if ($startTd.hasClass("left")) {
- $td = $td.parent().next().children("td.left");
- } else if ($startTd.hasClass("right")) {
- $td = $td.parent().next().children("td.right");
- } else {
- $td = $td.parent().next().children("td.content");
- }
- }
- }
- }
- },
- clearMark: function($container) {
- $container.find("td.content.content-mark").each(function() {
- var $this = $(this);
- $this.html($this.data("beforemark"));
- $this.removeClass("content-mark");
- $this.removeData("beforemark");
- $container = $this.closest(".text-diff").parent();
- $this.find(gitplex.textdiff.symbolClasses).mouseover($container.data("symbolHover"));
- });
- },
- restoreMark: function($container) {
- var mark = $container.data("mark");
- if (mark) {
- gitplex.textdiff.mark($container, mark);
- } else {
- gitplex.textdiff.clearMark($container);
- }
- },
- addCommentIndicator: function($container, leftSide, line, comments) {
- var oldOrNew = leftSide?"old":"new";
- $container.find("." + oldOrNew + ".comment-popover[data-line='" + line + "']").remove();
- var $lineNumTd = gitplex.textdiff.getLineNumTd($container, leftSide, line);
-
- var callback = $container.data("callback");
-
- var $indicator = $(document.createElement("a"));
- $indicator.addClass("comment-indicator");
- $indicator.data("comments", comments);
- if (comments.length != 1) {
- /*
- * when there are multiple comments starts with the same line, we should
- * display a comment indicator which will display a comment popover with
- * list of comment triggers upon click, user can then click one of the
- * trigger link to display the actual comment content
- */
- var oldOrNew = leftSide?"old":"new";
- $indicator.append("<i class='fa fa-comments'></i>");
- var content = "";
- for (var i in comments) {
- var comment = comments[i];
- var index = parseInt(i) + 1;
- content += "<a class='comment-trigger' title='Click to show comment of marked text'>#" + comment.id + "</a>";
- }
- $indicator.popover({
- html: true,
- container: $lineNumTd,
- placement: "right auto",
- template: "<div class='" + oldOrNew + " popover comment-popover' data-line='" + line + "'><div class='arrow'></div><div class='popover-content'></div></div>",
- content: content
- });
- $indicator.on('shown.bs.popover', function () {
- $("." + oldOrNew + ".comment-popover[data-line='" + line + "'] a").each(function() {
- $(this).mouseover(function() {
- var comment = comments[$(this).index()];
- gitplex.textdiff.mark($container, comment.mark);
- });
- $(this).mouseout(function() {
- gitplex.textdiff.restoreMark($container);
- });
- $(this).click(function() {
- if ($("#"+$container.data("dirtyContainerId")).find("form.dirty").length != 0
- && !confirm("There are unsaved changes, discard and continue?")) {
- return;
- }
- var comment = comments[$(this).index()];
- callback("openComment", comment.id);
- });
- });
- gitplex.textdiff.highlightCommentTrigger($container);
- });
- } else {
- var comment = comments[0];
- $indicator.addClass("comment-trigger").attr("title", "Click to show comment of marked text");
- $indicator.append("<i class='fa fa-commenting'></i>");
- $indicator.mouseover(function() {
- gitplex.textdiff.mark($container, comment.mark);
- });
- $indicator.mouseout(function() {
- gitplex.textdiff.restoreMark($container);
- });
- $indicator.click(function() {
- if ($("#"+$container.data("dirtyContainerId")).find("form.dirty").length != 0
- && !confirm("There are unsaved changes, discard and continue?")) {
- return;
- }
- callback("openComment", comment.id);
- });
- }
- $lineNumTd.children(".comment-indicator").remove();
- $lineNumTd.prepend($indicator);
- },
- getLineNumTd: function($container, leftSide, line) {
- if (leftSide) {
- var $contentTd = $container.find("td.left.content[data-old='" + line + "']");
- if ($contentTd.length == 0)
- $contentTd = $container.find("td.content[data-old='" + line + "']");
- return $contentTd.prevAll("td.number").last();
- } else {
- var $contentTd = $container.find("td.right.content[data-new='" + line + "']");
- if ($contentTd.length == 0)
- $contentTd = $container.find("td.content[data-new='" + line + "']");
- return $contentTd.prevAll("td.number").first();
- }
- },
- highlightCommentTrigger: function($container) {
- $container.find(".comment-trigger").removeClass("active");
- var openComment = $container.data("openComment");
- if (openComment) {
- var line = parseInt(openComment.mark.beginLine);
- var $indicator = gitplex.textdiff.getLineNumTd($container, openComment.mark.leftSide, line).children(".comment-indicator");
- if ($indicator.length != 0) {
- var comments = $indicator.data("comments");
- if (comments.length == 1) {
- $indicator.addClass("active");
- } else {
- var oldOrNew = openComment.mark.leftSide?"old":"new";
- $container.find("." + oldOrNew + ".comment-popover[data-line='" + line + "'] a").each(function() {
- var comment = comments[$(this).index()];
- if (comment.id == openComment.id) {
- $(this).addClass("active");
- }
- });
- }
- }
- }
- },
- onCommentAdded: function($container, comment) {
- $container.data("openComment", comment);
- $container.data("mark", comment.mark);
-
- var line = parseInt(comment.mark.beginLine);
- var leftSide = comment.mark.leftSide;
-
- var $indicator = gitplex.textdiff.getLineNumTd($container, leftSide, line).children(".comment-indicator");
- var comments;
- if ($indicator.length != 0) {
- comments = $indicator.data("comments");
- } else {
- comments = [];
- }
- comments.push(comment);
- gitplex.textdiff.addCommentIndicator($container, leftSide, line, comments);
- gitplex.textdiff.highlightCommentTrigger($container);
- },
- onCommentDeleted: function($container, comment) {
- $container.removeData("openComment");
-
- var line = parseInt(comment.mark.beginLine);
- var leftSide = comment.mark.leftSide;
- var $indicator = gitplex.textdiff.getLineNumTd($container, leftSide, line).children(".comment-indicator");
- var comments = $indicator.data("comments");
- if (comments.length == 1) {
- $indicator.remove();
- } else {
- for (var i in comments) {
- var each = comments[i];
- if (each.id == comment.id) {
- comments.splice(i, 1);
- break;
- }
- }
- gitplex.textdiff.addCommentIndicator($container, leftSide, line, comments);
- }
- gitplex.textdiff.highlightCommentTrigger($container);
- },
- onOpenComment: function($container, comment) {
- $container.data("openComment", comment);
- $container.data("mark", comment.mark);
- gitplex.textdiff.highlightCommentTrigger($container);
- gitplex.textdiff.mark($container, comment.mark);
- gitplex.textdiff.scroll($container, comment.mark);
- },
- onCloseComment: function($container) {
- $container.removeData("openComment");
- gitplex.textdiff.highlightCommentTrigger($container);
- },
- onAddComment: function($container, mark) {
- $container.removeData("openComment");
- $container.data("mark", mark);
-
- $container.selectionPopover("close");
- window.getSelection().removeAllRanges();
-
- // continue to operate DOM in a timer to give browser a chance to
- // clear selections
- setTimeout(function() {
- gitplex.textdiff.highlightCommentTrigger($container);
- gitplex.textdiff.mark($container, mark);
- gitplex.textdiff.scroll($container, mark);
- }, 100);
- }
-};
+gitplex.textdiff = {
+ symbolClasses: ".cm-property, .cm-variable, .cm-variable-2, .cm-variable-3, .cm-def, .cm-meta",
+ init: function(containerId, symbolTooltipId, oldRev, newRev, scroll, callback,
+ markSupport, mark, openComment, oldComments, newComments, dirtyContainerId, doclink) {
+ var $container = $("#" + containerId);
+ $container.data("dirtyContainerId", dirtyContainerId);
+ $container.data("callback", callback);
+ $container.data("symbolHover", function() {
+ var revision;
+ var $symbol = $(this);
+ if ($symbol.hasClass("delete")) {
+ revision = oldRev;
+ } else {
+ var $td = $symbol.closest("td");
+
+ // if is deleted line or if it is on left side of split view
+ if ($td.hasClass("old") && !$td.hasClass("new") || $td.next().is("td"))
+ revision = oldRev;
+ else
+ revision = newRev;
+ }
+ var symbolTooltip = document.getElementById(symbolTooltipId);
+ if (symbolTooltip.onMouseOverSymbol)
+ symbolTooltip.onMouseOverSymbol(revision, this);
+ });
+ $container.data("onMouseOverContent", function() {
+ if (!gitplex.mouseState.pressed) {
+ if ($(this).hasClass("left")) {
+ $container.find("td.content.right").addClass("noselect");
+ $container.find("td.content.left").removeClass("noselect");
+ } else if ($(this).hasClass("right")) {
+ $container.find("td.content.left").addClass("noselect");
+ $container.find("td.content.right").removeClass("noselect");
+ }
+ }
+ });
+ var $symbols = $container.find(gitplex.textdiff.symbolClasses);
+ $symbols.mouseover($container.data("symbolHover"));
+ $container.find("td.content").mouseover($container.data("onMouseOverContent"));
+
+ gitplex.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");
+
+ // 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");
+ }
+
+ /*
+ * 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();
+ if ($tr.length == 0) {
+ return "close";
+ }
+ 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";
+ }
+ $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();
+ if ($tr.length == 0) {
+ return "close";
+ }
+ 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 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) {
+ return "close";
+ } else {
+ $start = $content;
+ startOffset = 0;
+ }
+ }
+
+ $content = $end;
+ while (!$content.is($start) && (endOffset == 0 || !hasOldOrNewData($content))) {
+ $content = prevContent($content);
+ if (!$content) {
+ return "close";
+ } else {
+ $end = $content;
+ endOffset = $content.text().length;
+ }
+ }
+
+ // check if there is anything selected
+ if ($start.is($end) && startOffset >= endOffset
+ || !hasOldOrNewData($start) || !hasOldOrNewData($end)) {
+ 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();
+
+ 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
+ }
+ }
+
+ // 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();
+ }
+
+ /*
+ * 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.textdiff.addCommentIndicator($container, true, line, oldComments[line][1]);
+ }
+ }
+ for (var line in newComments) {
+ if (newComments.hasOwnProperty(line)) {
+ gitplex.textdiff.addCommentIndicator($container, false, line, newComments[line][1]);
+ }
+ }
+
+ gitplex.textdiff.highlightCommentTrigger($container);
+
+ if (mark) {
+ $container.data("mark", mark);
+ gitplex.textdiff.mark($container, mark);
+ if (scroll && !pmease.commons.history.isVisited())
+ gitplex.textdiff.scroll($container, mark);
+ }
+ }
+
+ },
+ initBlameTooltip: function(containerId, $hashLink) {
+ var $container = $("#" + containerId);
+ var alignment = {targetX: 100, targetY: 100, x: 0, y: 0};
+ $hashLink.hover(function() {
+ var $commitLink = $(this);
+ var $content = $commitLink.closest("tr").children("td.content");
+ var oldLine = $content.data("old");
+ if (oldLine == undefined)
+ oldLine = -1;
+ var newLine = $content.data("new");
+ if (newLine == undefined)
+ newLine = -1;
+
+ var tooltipId = "blame-message-" + containerId + "_" + oldLine + "_" + newLine;
+ $container.data("callback")("showBlameMessage", tooltipId, $(this).data("hash"));
+ var $tooltip = $("<div class='blame-message'><div class='loading'>Loading...</div></div>");
+ $tooltip.attr("id", tooltipId);
+ $tooltip.data("trigger", this);
+ $tooltip.data("alignment", alignment);
+ $container.append($tooltip);
+ return $tooltip;
+ }, alignment);
+ },
+ showBlameMessage: function(tooltipId, message) {
+ var $blameTooltip = $("#" + tooltipId);
+ $blameTooltip.empty().text(message);
+ $blameTooltip.align({placement: $blameTooltip.data("alignment"), target: {element: $blameTooltip.data("trigger")}});
+ },
+ openSelectionPopover: function(containerId, position, mark, markUrl, loggedIn) {
+ var $container = $("#" + containerId);
+
+ 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);
+ 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 ($("#"+$container.data("dirtyContainerId")).find("form.dirty").length != 0
+ && !confirm("There are unsaved changes, discard and continue?")) {
+ return;
+ }
+ $container.data("callback")("addComment", mark.leftSide,
+ mark.beginLine, mark.beginChar, mark.endLine, mark.endChar);
+ });
+ } else {
+ $content.append("<span class='comment'><i class='fa fa-warning'></i> Log in to comment on selection</span>");
+ }
+
+ $container.selectionPopover("open", {
+ position: position,
+ content: $content
+ });
+ },
+ expand: function(containerId, blockIndex, expandedHtml) {
+ var $container = $("#" + containerId);
+ var $expanderTr = $container.find(".expander" + blockIndex);
+ var $prevTr = $expanderTr.prev();
+ var $nextTr = $expanderTr.next();
+ $expanderTr.replaceWith(expandedHtml);
+ var $expandedTrs;
+ if ($prevTr.length != 0 && $nextTr.length != 0) {
+ $expandedTrs = $prevTr.nextAll().filter($nextTr.prevAll());
+ } else if ($prevTr.length != 0) {
+ $expandedTrs = $prevTr.nextAll();
+ } else {
+ $expandedTrs = $nextTr.prevAll();
+ }
+ var $symbols = $expandedTrs.find(gitplex.textdiff.symbolClasses);
+ $symbols.mouseover($container.data("symbolHover"));
+ $expandedTrs.find("td.content").mouseover($container.data("onMouseOverContent"));
+ var $firstExpandedTr = $expandedTrs.first();
+
+ function processBlames(index) {
+ var commitHash = $firstExpandedTr.children("td.blame").eq(index).children("a.hash").data("hash");
+ var prevCommitHash;
+ $firstExpandedTr.prevAll().each(function() {
+ if (!prevCommitHash) {
+ var $tr = $(this);
+ var $prevCommitLink = $tr.children("td.blame").eq(index).children("a.hash");
+ if ($prevCommitLink.length != 0) {
+ prevCommitHash = $prevCommitLink.data("hash");
+ }
+ }
+ });
+ if (commitHash == prevCommitHash) {
+ $firstExpandedTr.children("td.blame").eq(index).html("<div class='same-as-above'>...</div>");
+ }
+
+ if ($nextTr.length != 0) {
+ commitHash = $nextTr.children("td.blame").eq(index).children("a.hash").data("hash");
+ prevCommitHash = undefined;
+ $nextTr.prevAll().each(function() {
+ if (!prevCommitHash) {
+ var $prevCommitLink = $(this).children("td.blame").eq(index).children("a.hash");
+ if ($prevCommitLink.length != 0) {
+ prevCommitHash = $prevCommitLink.data("hash");
+ }
+ }
+ });
+ if (commitHash == prevCommitHash) {
+ $nextTr.children("td.blame").eq(index).html("<div class='same-as-above'>...</div>");
+ }
+ }
+ }
+ if ($firstExpandedTr.children("td.blame").length == 1) {
+ processBlames(0);
+ } else if ($firstExpandedTr.children("td.blame").length == 2) {
+ processBlames(0);
+ processBlames(1);
+ }
+
+ gitplex.textdiff.initBlameTooltip(containerId, $expandedTrs.find(">td.blame>a.hash"));
+
+ $(window).resize();
+ },
+ getMarkInfo: function($container, mark) {
+ var oldOrNew = mark.leftSide?"old":"new";
+ var startCursor = [mark.beginLine+1, mark.beginChar];
+ var endCursor = [mark.endLine+1, mark.endChar];
+ var $startTd = $container.find("td.content[data-" + oldOrNew + "='" + (startCursor[0]-1) + "']");
+ var $endTd = $container.find("td.content[data-" + oldOrNew + "='" + (endCursor[0]-1) + "']");
+ if ($startTd.length == 0) {
+ console.error("Unable to find start td!");
+ $startTd = undefined;
+ } else if ($startTd.length == 2) {
+ if (oldOrNew == "old") {
+ $startTd = $startTd.first();
+ } else {
+ $startTd = $startTd.last();
+ }
+ }
+ if ($endTd.length == 0) {
+ console.error("Unable to find end td!");
+ $endTd = undefined;
+ } else if ($endTd.length == 2) {
+ if (oldOrNew == "old") {
+ $endTd = $endTd.first();
+ } else {
+ $endTd = $endTd.last();
+ }
+ }
+ return {
+ startTd: $startTd,
+ startCursor: startCursor,
+ endTd: $endTd,
+ endCursor: endCursor,
+ oldOrNew: oldOrNew
+ };
+ },
+ scroll: function($container, mark) {
+ // use a timer to scroll after possible view port resize
+ setTimeout(function() {
+ var markInfo = gitplex.textdiff.getMarkInfo($container, mark);
+ var $startTd = markInfo.startTd;
+ var $endTd = markInfo.endTd;
+ if ($startTd && $endTd) {
+ var top = $startTd.offset().top;
+ var bottom = $endTd.offset().top + $endTd.outerHeight();
+ var markHeight = bottom - top;
+ var windowHeight = $(window).height();
+ if (windowHeight <= markHeight) {
+ $(window).scrollTop(top-50);
+ } else {
+ $(window).scrollTop((top+bottom-windowHeight)/2);
+ }
+ }
+ }, 0);
+ },
+ mark: function($container, mark) {
+ gitplex.textdiff.clearMark($container);
+
+ var markInfo = gitplex.textdiff.getMarkInfo($container, mark);
+ var $startTd = markInfo.startTd;
+ var $endTd = markInfo.endTd;
+ if ($startTd && $endTd) {
+ var startCursor = markInfo.startCursor;
+ var endCursor = markInfo.endCursor;
+ var oldOrNew = markInfo.oldOrNew;
+ var $td = $startTd;
+ while (true) {
+ var ch = 0;
+ $td.addClass("content-mark");
+ $td.data("beforemark", $td.html());
+ $td.contents().each(function() {
+ var $this = $(this);
+ var text = $this.text();
+ function markText(from, to) {
+ var text = $this.text();
+ var left = text.substring(0, from);
+ var middle = text.substring(from, to);
+ var right = text.substring(to);
+
+ if (left.length == 0 && right.length == 0 && $this.is("span")) {
+ $this.addClass("content-mark");
+ } else {
+ var classes;
+ if ($this.is("span"))
+ classes = $this.attr("class");
+ else
+ classes = "";
+
+ var $current = $this;
+ if (left.length != 0) {
+ $current.after("<span></span>");
+ $current = $current.next();
+ $current.attr("class", classes).text(left);
+ }
+
+ $current.after("<span></span>");
+ $current = $current.next();
+ $current.attr("class", classes + " content-mark").text(middle);
+
+ if (right.length != 0) {
+ $current.after("<span></span>");
+ $current = $current.next();
+ $current.attr("class", classes).text(right);
+ }
+ $this.remove();
+ }
+ }
+ if ($this.hasClass("insert") && oldOrNew == "old"
+ || $this.hasClass("delete") && oldOrNew == "new") {
+ if (!$td.is($startTd) && !$td.is($endTd)) {
+ markText(0, text.length);
+ } else if ($td.is($startTd) && $td.is($endTd)) {
+ if (endCursor[1]>ch && startCursor[1]<ch) {
+ markText(0, text.length);
+ }
+ } else if ($td.is($startTd)) {
+ if (startCursor[1]<ch) {
+ markText(0, text.length);
+ }
+ } else {
+ if (endCursor[1]>ch) {
+ markText(0, text.length);
+ }
+ }
+ } else {
+ var nextCh = ch + text.length;
+ if (!$td.is($startTd) && !$td.is($endTd)) {
+ markText(0, text.length);
+ } else if ($td.is($startTd) && $td.is($endTd)) {
+ if (endCursor[1]>ch && startCursor[1]<nextCh) {
+ if (ch>=startCursor[1] && nextCh<=endCursor[1]) {
+ markText(0, text.length);
+ } else if (ch<startCursor[1] && nextCh>endCursor[1]) {
+ markText(startCursor[1]-ch, endCursor[1]-ch);
+ } else if (ch<startCursor[1]) {
+ markText(startCursor[1]-ch, text.length);
+ } else {
+ markText(0, endCursor[1]-ch);
+ }
+ }
+ } else if ($td.is($startTd)) {
+ if (ch>=startCursor[1]) {
+ markText(0, text.length);
+ } else if (nextCh>startCursor[1]) {
+ markText(startCursor[1]-ch, text.length);
+ }
+ } else {
+ if (nextCh<=endCursor[1]) {
+ markText(0, text.length);
+ } else if (ch<endCursor[1]) {
+ markText(0, endCursor[1]-ch);
+ }
+ }
+ ch = nextCh;
+ }
+ });
+ if ($td.is($endTd) || $td.length == 0) {
+ break;
+ } else {
+ if ($startTd.hasClass("left")) {
+ $td = $td.parent().next().children("td.left");
+ } else if ($startTd.hasClass("right")) {
+ $td = $td.parent().next().children("td.right");
+ } else {
+ $td = $td.parent().next().children("td.content");
+ }
+ }
+ }
+ }
+ },
+ clearMark: function($container) {
+ $container.find("td.content.content-mark").each(function() {
+ var $this = $(this);
+ $this.html($this.data("beforemark"));
+ $this.removeClass("content-mark");
+ $this.removeData("beforemark");
+ $container = $this.closest(".text-diff").parent();
+ $this.find(gitplex.textdiff.symbolClasses).mouseover($container.data("symbolHover"));
+ });
+ },
+ restoreMark: function($container) {
+ var mark = $container.data("mark");
+ if (mark) {
+ gitplex.textdiff.mark($container, mark);
+ } else {
+ gitplex.textdiff.clearMark($container);
+ }
+ },
+ addCommentIndicator: function($container, leftSide, line, comments) {
+ var oldOrNew = leftSide?"old":"new";
+ $container.find("." + oldOrNew + ".comment-popover[data-line='" + line + "']").remove();
+ var $lineNumTd = gitplex.textdiff.getLineNumTd($container, leftSide, line);
+
+ var callback = $container.data("callback");
+
+ var $indicator = $(document.createElement("a"));
+ $indicator.addClass("comment-indicator");
+ $indicator.data("comments", comments);
+ if (comments.length != 1) {
+ /*
+ * when there are multiple comments starts with the same line, we should
+ * display a comment indicator which will display a comment popover with
+ * list of comment triggers upon click, user can then click one of the
+ * trigger link to display the actual comment content
+ */
+ var oldOrNew = leftSide?"old":"new";
+ $indicator.append("<i class='fa fa-comments'></i>");
+ var content = "";
+ for (var i in comments) {
+ var comment = comments[i];
+ var index = parseInt(i) + 1;
+ content += "<a class='comment-trigger' title='Click to show comment of marked text'>#" + comment.id + "</a>";
+ }
+ $indicator.popover({
+ html: true,
+ container: $lineNumTd,
+ placement: "right auto",
+ template: "<div class='" + oldOrNew + " popover comment-popover' data-line='" + line + "'><div class='arrow'></div><div class='popover-content'></div></div>",
+ content: content
+ });
+ $indicator.on('shown.bs.popover', function () {
+ $("." + oldOrNew + ".comment-popover[data-line='" + line + "'] a").each(function() {
+ $(this).mouseover(function() {
+ var comment = comments[$(this).index()];
+ gitplex.textdiff.mark($container, comment.mark);
+ });
+ $(this).mouseout(function() {
+ gitplex.textdiff.restoreMark($container);
+ });
+ $(this).click(function() {
+ if ($("#"+$container.data("dirtyContainerId")).find("form.dirty").length != 0
+ && !confirm("There are unsaved changes, discard and continue?")) {
+ return;
+ }
+ var comment = comments[$(this).index()];
+ callback("openComment", comment.id);
+ });
+ });
+ gitplex.textdiff.highlightCommentTrigger($container);
+ });
+ } else {
+ var comment = comments[0];
+ $indicator.addClass("comment-trigger").attr("title", "Click to show comment of marked text");
+ $indicator.append("<i class='fa fa-commenting'></i>");
+ $indicator.mouseover(function() {
+ gitplex.textdiff.mark($container, comment.mark);
+ });
+ $indicator.mouseout(function() {
+ gitplex.textdiff.restoreMark($container);
+ });
+ $indicator.click(function() {
+ if ($("#"+$container.data("dirtyContainerId")).find("form.dirty").length != 0
+ && !confirm("There are unsaved changes, discard and continue?")) {
+ return;
+ }
+ callback("openComment", comment.id);
+ });
+ }
+ $lineNumTd.children(".comment-indicator").remove();
+ $lineNumTd.prepend($indicator);
+ },
+ getLineNumTd: function($container, leftSide, line) {
+ if (leftSide) {
+ var $contentTd = $container.find("td.left.content[data-old='" + line + "']");
+ if ($contentTd.length == 0)
+ $contentTd = $container.find("td.content[data-old='" + line + "']");
+ return $contentTd.prevAll("td.number").last();
+ } else {
+ var $contentTd = $container.find("td.right.content[data-new='" + line + "']");
+ if ($contentTd.length == 0)
+ $contentTd = $container.find("td.content[data-new='" + line + "']");
+ return $contentTd.prevAll("td.number").first();
+ }
+ },
+ highlightCommentTrigger: function($container) {
+ $container.find(".comment-trigger").removeClass("active");
+ var openComment = $container.data("openComment");
+ if (openComment) {
+ var line = parseInt(openComment.mark.beginLine);
+ var $indicator = gitplex.textdiff.getLineNumTd($container, openComment.mark.leftSide, line).children(".comment-indicator");
+ if ($indicator.length != 0) {
+ var comments = $indicator.data("comments");
+ if (comments.length == 1) {
+ $indicator.addClass("active");
+ } else {
+ var oldOrNew = openComment.mark.leftSide?"old":"new";
+ $container.find("." + oldOrNew + ".comment-popover[data-line='" + line + "'] a").each(function() {
+ var comment = comments[$(this).index()];
+ if (comment.id == openComment.id) {
+ $(this).addClass("active");
+ }
+ });
+ }
+ }
+ }
+ },
+ onCommentAdded: function($container, comment) {
+ $container.data("openComment", comment);
+ $container.data("mark", comment.mark);
+
+ var line = parseInt(comment.mark.beginLine);
+ var leftSide = comment.mark.leftSide;
+
+ var $indicator = gitplex.textdiff.getLineNumTd($container, leftSide, line).children(".comment-indicator");
+ var comments;
+ if ($indicator.length != 0) {
+ comments = $indicator.data("comments");
+ } else {
+ comments = [];
+ }
+ comments.push(comment);
+ gitplex.textdiff.addCommentIndicator($container, leftSide, line, comments);
+ gitplex.textdiff.highlightCommentTrigger($container);
+ },
+ onCommentDeleted: function($container, comment) {
+ $container.removeData("openComment");
+
+ var line = parseInt(comment.mark.beginLine);
+ var leftSide = comment.mark.leftSide;
+ var $indicator = gitplex.textdiff.getLineNumTd($container, leftSide, line).children(".comment-indicator");
+ var comments = $indicator.data("comments");
+ if (comments.length == 1) {
+ $indicator.remove();
+ } else {
+ for (var i in comments) {
+ var each = comments[i];
+ if (each.id == comment.id) {
+ comments.splice(i, 1);
+ break;
+ }
+ }
+ gitplex.textdiff.addCommentIndicator($container, leftSide, line, comments);
+ }
+ gitplex.textdiff.highlightCommentTrigger($container);
+ },
+ onOpenComment: function($container, comment) {
+ $container.data("openComment", comment);
+ $container.data("mark", comment.mark);
+ gitplex.textdiff.highlightCommentTrigger($container);
+ gitplex.textdiff.mark($container, comment.mark);
+ gitplex.textdiff.scroll($container, comment.mark);
+ },
+ onCloseComment: function($container) {
+ $container.removeData("openComment");
+ gitplex.textdiff.highlightCommentTrigger($container);
+ },
+ onAddComment: function($container, mark) {
+ $container.removeData("openComment");
+ $container.data("mark", mark);
+
+ $container.selectionPopover("close");
+ window.getSelection().removeAllRanges();
+
+ // continue to operate DOM in a timer to give browser a chance to
+ // clear selections
+ setTimeout(function() {
+ gitplex.textdiff.highlightCommentTrigger($container);
+ gitplex.textdiff.mark($container, mark);
+ gitplex.textdiff.scroll($container, mark);
+ }, 100);
+ }
+};
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/depot/setting/gatekeeper/GateKeeperPage.html b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/depot/setting/gatekeeper/GateKeeperPage.html
index cd2c7eafcc..172f12a5b8 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/depot/setting/gatekeeper/GateKeeperPage.html
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/depot/setting/gatekeeper/GateKeeperPage.html
@@ -9,9 +9,10 @@
<div class="help-block">
<ul>
<li>Commits have to pass all Gatekeepers defined here in order to be accepted by the repository</li>
- <li>If a gatekeeper is dragged and dropped on another gatekeepe, their positions will be swapped</li>
+ <li>If a gatekeeper is dragged and dropped on another gatekeeper, their positions will be swapped</li>
<li>If a gatekeeper is dragged and dropped on <i>Add/Define</i> placeholder, it will be moved to the placeholder position</li>
- <li>A gatekeeper can not be dragged to a position containing itself or contained by itself</li>
+ <li>A gatekeeper can not be dragged to a position containing itself or contained by itself</li>
+ <li>Check out <a wicket:id="gateKeeperDocLink">examples here</a> to get better understanding of gatekeepers</li>
</ul>
</div>
</div>
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/depot/setting/gatekeeper/GateKeeperPage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/depot/setting/gatekeeper/GateKeeperPage.java
index d71f39ba60..1e55879aeb 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/depot/setting/gatekeeper/GateKeeperPage.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/depot/setting/gatekeeper/GateKeeperPage.java
@@ -1,121 +1,124 @@
-package com.pmease.gitplex.web.page.depot.setting.gatekeeper;
-
-import java.util.List;
-
-import org.apache.wicket.Component;
-import org.apache.wicket.ajax.AjaxRequestTarget;
-import org.apache.wicket.markup.html.WebMarkupContainer;
-import org.apache.wicket.markup.html.list.ListItem;
-import org.apache.wicket.markup.html.list.ListView;
-import org.apache.wicket.request.mapper.parameter.PageParameters;
-
-import com.pmease.commons.util.ReflectionUtils;
-import com.pmease.commons.wicket.component.modal.ModalPanel;
-import com.pmease.commons.wicket.editable.EditableUtils;
-import com.pmease.gitplex.core.GitPlex;
-import com.pmease.gitplex.core.entity.Depot;
-import com.pmease.gitplex.core.gatekeeper.GateKeeper;
-import com.pmease.gitplex.core.manager.DepotManager;
-import com.pmease.gitplex.web.page.depot.setting.DepotSettingPage;
-
-import jersey.repackaged.com.google.common.collect.Lists;
-
-@SuppressWarnings("serial")
-public class GateKeeperPage extends DepotSettingPage {
-
- private static final String CONTAINER_ID = "gateKeeperSetting";
-
- public GateKeeperPage(PageParameters params) {
- super(params);
- }
-
- private WebMarkupContainer newContent() {
- WebMarkupContainer content = new WebMarkupContainer(CONTAINER_ID);
- content.setOutputMarkupId(true);
-
- content.add(new ListView<GateKeeper>("gateKeepers", getDepot().getGateKeepers()) {
-
- @Override
- protected void populateItem(ListItem<GateKeeper> item) {
- item.add(new GateKeeperPanel("gateKeeper", item.getModelObject(), Lists.newArrayList(item.getIndex())) {
-
- @Override
- protected void onDelete(AjaxRequestTarget target) {
- getDepot().getGateKeepers().remove(item.getIndex());
- onGateKeeperChanged(target);
- }
-
- @Override
- protected void onChange(AjaxRequestTarget target, GateKeeper gateKeeper) {
- getDepot().getGateKeepers().set(item.getIndex(), gateKeeper);
- onGateKeeperChanged(target);
- }
-
- });
- }
-
- });
- content.add(new GateKeeperLink("gateKeeperDropdownTrigger") {
-
- @Override
- protected void onSelect(AjaxRequestTarget target, Class<? extends GateKeeper> gateKeeperClass) {
- final GateKeeper gateKeeper = ReflectionUtils.instantiateClass(gateKeeperClass);
- if (EditableUtils.isDefaultInstanceValid(gateKeeperClass)) {
- getDepot().getGateKeepers().add(gateKeeper);
- onGateKeeperChanged(target);
- } else {
- new ModalPanel(target) {
-
- @Override
- protected Component newContent(String id) {
- return new GateKeeperEditor(id, gateKeeper) {
-
- @Override
- protected void onCancel(AjaxRequestTarget target) {
- close(target);
- }
-
- @Override
- protected void onSave(AjaxRequestTarget target, GateKeeper gateKeeper) {
- close(target);
- getDepot().getGateKeepers().add(gateKeeper);
- onGateKeeperChanged(target);
- }
-
- };
- }
-
- };
- }
- }
-
- @Override
- protected List<Integer> getPosition() {
- return Lists.newArrayList(getDepot().getGateKeepers().size());
- }
-
- });
- content.add(new WebMarkupContainer("gateKeeperPanel").setVisible(false));
- content.add(new WebMarkupContainer("gateKeeperModal").setOutputMarkupPlaceholderTag(true).setVisible(false));
- return content;
- }
-
- @Override
- protected void onInitialize() {
- super.onInitialize();
-
- add(newContent());
- }
-
- void onGateKeeperChanged(AjaxRequestTarget target) {
- GitPlex.getInstance(DepotManager.class).save(getDepot(), null, null);
- replace(newContent());
- target.add(get(CONTAINER_ID));
- }
-
- @Override
- protected void onSelect(AjaxRequestTarget target, Depot depot) {
- setResponsePage(GateKeeperPage.class, GateKeeperPage.paramsOf(depot));
- }
-
-}
+package com.pmease.gitplex.web.page.depot.setting.gatekeeper;
+
+import java.util.List;
+
+import org.apache.wicket.Component;
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.markup.html.WebMarkupContainer;
+import org.apache.wicket.markup.html.link.ExternalLink;
+import org.apache.wicket.markup.html.list.ListItem;
+import org.apache.wicket.markup.html.list.ListView;
+import org.apache.wicket.request.mapper.parameter.PageParameters;
+
+import com.pmease.commons.util.ReflectionUtils;
+import com.pmease.commons.wicket.component.modal.ModalPanel;
+import com.pmease.commons.wicket.editable.EditableUtils;
+import com.pmease.gitplex.core.GitPlex;
+import com.pmease.gitplex.core.entity.Depot;
+import com.pmease.gitplex.core.gatekeeper.GateKeeper;
+import com.pmease.gitplex.core.manager.DepotManager;
+import com.pmease.gitplex.web.page.depot.setting.DepotSettingPage;
+
+import jersey.repackaged.com.google.common.collect.Lists;
+
+@SuppressWarnings("serial")
+public class GateKeeperPage extends DepotSettingPage {
+
+ private static final String CONTAINER_ID = "gateKeeperSetting";
+
+ public GateKeeperPage(PageParameters params) {
+ super(params);
+ }
+
+ private WebMarkupContainer newContent() {
+ WebMarkupContainer content = new WebMarkupContainer(CONTAINER_ID);
+ content.setOutputMarkupId(true);
+
+ content.add(new ListView<GateKeeper>("gateKeepers", getDepot().getGateKeepers()) {
+
+ @Override
+ protected void populateItem(ListItem<GateKeeper> item) {
+ item.add(new GateKeeperPanel("gateKeeper", item.getModelObject(), Lists.newArrayList(item.getIndex())) {
+
+ @Override
+ protected void onDelete(AjaxRequestTarget target) {
+ getDepot().getGateKeepers().remove(item.getIndex());
+ onGateKeeperChanged(target);
+ }
+
+ @Override
+ protected void onChange(AjaxRequestTarget target, GateKeeper gateKeeper) {
+ getDepot().getGateKeepers().set(item.getIndex(), gateKeeper);
+ onGateKeeperChanged(target);
+ }
+
+ });
+ }
+
+ });
+ content.add(new GateKeeperLink("gateKeeperDropdownTrigger") {
+
+ @Override
+ protected void onSelect(AjaxRequestTarget target, Class<? extends GateKeeper> gateKeeperClass) {
+ final GateKeeper gateKeeper = ReflectionUtils.instantiateClass(gateKeeperClass);
+ if (EditableUtils.isDefaultInstanceValid(gateKeeperClass)) {
+ getDepot().getGateKeepers().add(gateKeeper);
+ onGateKeeperChanged(target);
+ } else {
+ new ModalPanel(target) {
+
+ @Override
+ protected Component newContent(String id) {
+ return new GateKeeperEditor(id, gateKeeper) {
+
+ @Override
+ protected void onCancel(AjaxRequestTarget target) {
+ close(target);
+ }
+
+ @Override
+ protected void onSave(AjaxRequestTarget target, GateKeeper gateKeeper) {
+ close(target);
+ getDepot().getGateKeepers().add(gateKeeper);
+ onGateKeeperChanged(target);
+ }
+
+ };
+ }
+
+ };
+ }
+ }
+
+ @Override
+ protected List<Integer> getPosition() {
+ return Lists.newArrayList(getDepot().getGateKeepers().size());
+ }
+
+ });
+ content.add(new WebMarkupContainer("gateKeeperPanel").setVisible(false));
+ content.add(new WebMarkupContainer("gateKeeperModal").setOutputMarkupPlaceholderTag(true).setVisible(false));
+ return content;
+ }
+
+ @Override
+ protected void onInitialize() {
+ super.onInitialize();
+
+ add(newContent());
+
+ add(new ExternalLink("gateKeeperDocLink", GitPlex.getInstance().getDocLink() + "/Working+with+Gate+Keepers"));
+ }
+
+ void onGateKeeperChanged(AjaxRequestTarget target) {
+ GitPlex.getInstance(DepotManager.class).save(getDepot(), null, null);
+ replace(newContent());
+ target.add(get(CONTAINER_ID));
+ }
+
+ @Override
+ protected void onSelect(AjaxRequestTarget target, Depot depot) {
+ setResponsePage(GateKeeperPage.class, GateKeeperPage.paramsOf(depot));
+ }
+
+}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/layout/LayoutPage.html b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/layout/LayoutPage.html
index 7ca15420e4..2223540cc0 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/layout/LayoutPage.html
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/layout/LayoutPage.html
@@ -25,6 +25,7 @@
<a wicket:id="organizations" title="Joined organizations"><i class="fa fa-group"></i> <i class="fa fa-caret-down"></i></a>
<a wicket:id="repositories" title="Accessible repositories"><i class="fa fa-ext fa-repo"></i> <i class="fa fa-caret-down"></i></a>
<a wicket:id="administration" title="System administration"><i class="fa fa-gear"></i></a>
+ <a wicket:id="docLink" title="Help"><i class="fa fa-question"></i></a>
</div>
<div class="search pull-right">
<input class="hidden form-control input-sm" placeholder="Search..."></input>
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/layout/LayoutPage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/layout/LayoutPage.java
index c7f62bcca8..49dbe0c458 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/layout/LayoutPage.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/layout/LayoutPage.java
@@ -16,6 +16,7 @@ import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.AbstractLink;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
+import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.model.AbstractReadOnlyModel;
@@ -143,6 +144,7 @@ public abstract class LayoutPage extends BasePage {
head.add(new BookmarkablePageLink<Void>("administration", SystemSettingPage.class)
.setVisible(SecurityUtils.canManageSystem()));
+ head.add(new ExternalLink("docLink", GitPlex.getInstance().getDocLink()));
Account user = getLoginUser();
boolean signedIn = user != null;