The Unified and Autonomous Development Platform
Fix issues of using pmease namespace in various js
robin shine4cba90a814a9
diff --git a/gitplex.product/system/license.txt b/gitplex.product/system/license.txtindex 42f1998126..dcde6be98d 100644--- a/gitplex.product/system/license.txt+++ b/gitplex.product/system/license.txt@@ -1,41 +1,41 @@-Software License Agreement for PMEase @productName@.--This is a legal agreement between you and PMEase Inc.-covering your use of @productName@ (the "Software"). Be sure to read the-following agreement before using the Software. BY USING THE SOFTWARE-(REGARDLESS IF YOU HAVE GOT PERMANENT LICENSE OF THE SOFTWARE OR NOT),-YOU ARE AGREEING TO BE BOUND BY THE TERMS OF THIS AGREEMENT.-IF YOU DO NOT AGREE TO THE TERMS OF THIS AGREEMENT, DO NOT-USE THE SOFTWARE AND DESTROY ALL COPIES IN YOUR POSSESSION.--The Software is owned by PMEase Inc. and is protected by-international treaty provisions. Therefore, you must treat the Software-like any other copyrighted material (e. g., a book or musical recording).--Paying a permanent license fee allows you the right to install the license-to @productName@ servers inside the physical site for which the license is-purchased for. A physical site is defined as a facility or a place of-business where multiple computers are located. For example, 456 Main-Street Chicago, IL and 789 Elm Street Chicago are two different physical-sites. You may not install the permanent license to @productName@ servers-located other than the purchased site. The @productName@ users-are not restricted by physical site, and can be from anywhere.--You may not rent or lease the Software, nor may you modify, adapt, translate,-reverse engineer, decompile, or disassemble the Software. If you violate any-part of this agreement, your right to use this Software terminates-automatically and you must then destroy all copies of the Software in your-possession.--The Software and its related documentation are provided "AS IS" and-without warranty of any kind and PMEase Inc. expressly disclaims all other-warranties, expressed or implied, including, but not limited to, the-implied warranties of merchantability and fitness for a particular-purpose. Under no circumstances shall PMEase Inc. be liable for any-incidental, special, or consequential damages that result from the use or-inability to use the Software or related documentation, even if PMEase Inc.-has been advised of the possibility of such damages. In no event shall-PMEase Inc. liability exceed the license fee paid, if any.--Copyright (C) 2005-2011 by PMEase Inc. All rights-reserved. PMEase and PMEase @productName@ are trademarks of PMEase Inc.+Software License Agreement for PMEase @productName@.++This is a legal agreement between you and PMEase Inc.+covering your use of @productName@ (the "Software"). Be sure to read the+following agreement before using the Software. BY USING THE SOFTWARE+(REGARDLESS IF YOU HAVE GOT PERMANENT LICENSE OF THE SOFTWARE OR NOT),+YOU ARE AGREEING TO BE BOUND BY THE TERMS OF THIS AGREEMENT.+IF YOU DO NOT AGREE TO THE TERMS OF THIS AGREEMENT, DO NOT+USE THE SOFTWARE AND DESTROY ALL COPIES IN YOUR POSSESSION.++The Software is owned by PMEase Inc. and is protected by+international treaty provisions. Therefore, you must treat the Software+like any other copyrighted material (e. g., a book or musical recording).++Paying a permanent license fee allows you the right to install the license+to @productName@ servers inside the physical site for which the license is+purchased for. A physical site is defined as a facility or a place of+business where multiple computers are located. For example, 456 Main+Street Chicago, IL and 789 Elm Street Chicago are two different physical+sites. You may not install the permanent license to @productName@ servers+located other than the purchased site. The @productName@ users+are not restricted by physical site, and can be from anywhere.++You may not rent or lease the Software, nor may you modify, adapt, translate,+reverse engineer, decompile, or disassemble the Software. If you violate any+part of this agreement, your right to use this Software terminates+automatically and you must then destroy all copies of the Software in your+possession.++The Software and its related documentation are provided "AS IS" and+without warranty of any kind and PMEase Inc. expressly disclaims all other+warranties, expressed or implied, including, but not limited to, the+implied warranties of merchantability and fitness for a particular+purpose. Under no circumstances shall PMEase Inc. be liable for any+incidental, special, or consequential damages that result from the use or+inability to use the Software or related documentation, even if PMEase Inc.+has been advised of the possibility of such damages. In no event shall+PMEase Inc. liability exceed the license fee paid, if any.++Copyright (C) 2005-2011 by PMEase Inc. All rights+reserved. PMEase and PMEase @productName@ are trademarks of PMEase Inc.diff --git a/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/blobsearch/instant/instant-search.js b/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/blobsearch/instant/instant-search.jsindex 14d0492b83..79dbf4a3fe 100644--- a/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/blobsearch/instant/instant-search.js+++ b/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/blobsearch/instant/instant-search.js@@ -1,79 +1,79 @@-gitplex.blobInstantSearch = {- init: function(inputId, callback) {- var $input = $("#" + inputId);-- $input.doneEvents("inputchange focus click", function() {- callback("input", $(this).val());- }, 100);- $input.on("blur", function() {- if ($input.data("hint") == null && $input.is(":visible")) {- // in case user clicks advanced button while the- // instant input is shown, we need to hide the- // input after a timeout in order not to make- // the advanced button moving to lost the click- setTimeout(function(){$input.hide();}, 250);- }- });- $input.bind("keydown", "esc", function() {- $input.hide();- });-- $input.data("return", function() {- if (pmease.commons.form.confirmLeave())- callback("return");- });- $input.data("keyup", function(e) {- var $hint = $input.data("hint");- if ($hint != null) {- e.preventDefault();- var $active = $hint.find("li.hit.active");- var $prev = $active.prev("li.hit");- if ($prev.length != 0) {- $active.removeClass("active");- $prev.addClass("active");- callback("up");- }- $hint.scrollIntoView("li.hit.active", 36, 36);- }- });- $input.data("keydown", function(e) {- var $hint = $input.data("hint");- if ($hint != null) {- e.preventDefault();- var $active = $hint.find("li.hit.active");- var $next = $active.next("li.hit");- if ($next.length != 0) {- $active.removeClass("active");- $next.addClass("active");- callback("down");- }- $hint.scrollIntoView("li.hit.active", 36, 36);- }- });- $input.bind("keydown", "return", $input.data("return"));- $input.bind("keydown", "up", $input.data("keyup"));- $input.bind("keydown", "down", $input.data("keydown"));- },-- hintOpened: function(inputId, hintId) {- var $input = $("#" + inputId);- var $hint = $("#" + hintId);- $hint.bind("keydown", "up", $input.data("keyup"));- $hint.bind("keydown", "down", $input.data("keydown"));- $hint.bind("keydown", "return", $input.data("return"));-- $hint.on("close", function() {- if (!$input.is(":focus") && $input.is(":visible")) {- // in case user clicks advanced button while the- // instant input is shown, we need to hide the- // input after a timeout in order not to make- // the advanced button moving to lost the click- setTimeout(function(){$input.hide();}, 250);- }- $input.data("hint", null);- });-- $input.data("hint", $hint);- }-+gitplex.blobInstantSearch = {+ init: function(inputId, callback) {+ var $input = $("#" + inputId);++ $input.doneEvents("inputchange focus click", function() {+ callback("input", $(this).val());+ }, 100);+ $input.on("blur", function() {+ if ($input.data("hint") == null && $input.is(":visible")) {+ // in case user clicks advanced button while the+ // instant input is shown, we need to hide the+ // input after a timeout in order not to make+ // the advanced button moving to lost the click+ setTimeout(function(){$input.hide();}, 250);+ }+ });+ $input.bind("keydown", "esc", function() {+ $input.hide();+ });++ $input.data("return", function() {+ if (gitplex.commons.form.confirmLeave())+ callback("return");+ });+ $input.data("keyup", function(e) {+ var $hint = $input.data("hint");+ if ($hint != null) {+ e.preventDefault();+ var $active = $hint.find("li.hit.active");+ var $prev = $active.prev("li.hit");+ if ($prev.length != 0) {+ $active.removeClass("active");+ $prev.addClass("active");+ callback("up");+ }+ $hint.scrollIntoView("li.hit.active", 36, 36);+ }+ });+ $input.data("keydown", function(e) {+ var $hint = $input.data("hint");+ if ($hint != null) {+ e.preventDefault();+ var $active = $hint.find("li.hit.active");+ var $next = $active.next("li.hit");+ if ($next.length != 0) {+ $active.removeClass("active");+ $next.addClass("active");+ callback("down");+ }+ $hint.scrollIntoView("li.hit.active", 36, 36);+ }+ });+ $input.bind("keydown", "return", $input.data("return"));+ $input.bind("keydown", "up", $input.data("keyup"));+ $input.bind("keydown", "down", $input.data("keydown"));+ },++ hintOpened: function(inputId, hintId) {+ var $input = $("#" + inputId);+ var $hint = $("#" + hintId);+ $hint.bind("keydown", "up", $input.data("keyup"));+ $hint.bind("keydown", "down", $input.data("keydown"));+ $hint.bind("keydown", "return", $input.data("return"));++ $hint.on("close", function() {+ if (!$input.is(":focus") && $input.is(":visible")) {+ // in case user clicks advanced button while the+ // instant input is shown, we need to hide the+ // input after a timeout in order not to make+ // the advanced button moving to lost the click+ setTimeout(function(){$input.hide();}, 250);+ }+ $input.data("hint", null);+ });++ $input.data("hint", $hint);+ }+};\ No newline at end of filediff --git a/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/blobsearch/result/search-result.js b/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/blobsearch/result/search-result.jsindex 7185162f21..657db48455 100644--- a/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/blobsearch/result/search-result.js+++ b/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/blobsearch/result/search-result.js@@ -1,17 +1,17 @@-gitplex.searchresult = {- confirmSwitchFileByLink: function(linkId) {- var linkURI = new URI(document.getElementById(linkId));- var currentURI = new URI(window.location.href);- if (linkURI.search(true).path != currentURI.search(true).path)- return pmease.commons.form.confirmLeave();- else- return true;- },- confirmSwitchFileByPath: function(path) {- var currentURI = new URI(window.location.href);- if (path != currentURI.search(true).path)- return pmease.commons.form.confirmLeave();- else- return true;- }-};+gitplex.searchresult = {+ confirmSwitchFileByLink: function(linkId) {+ var linkURI = new URI(document.getElementById(linkId));+ var currentURI = new URI(window.location.href);+ if (linkURI.search(true).path != currentURI.search(true).path)+ return gitplex.commons.form.confirmLeave();+ else+ return true;+ },+ confirmSwitchFileByPath: function(path) {+ var currentURI = new URI(window.location.href);+ if (path != currentURI.search(true).path)+ return gitplex.commons.form.confirmLeave();+ else+ return true;+ }+};diff --git a/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/blobview/blob-view.js b/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/blobview/blob-view.jsindex b308a64586..29f33a938d 100644--- a/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/blobview/blob-view.js+++ b/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/blobview/blob-view.js@@ -1,27 +1,27 @@-gitplex.blobView = function(containerId) {- var $blobView = $("#" + containerId + ">.blob-view");- var $head = $blobView.find(">.head");- var $body = $blobView.find(">.body");-- $blobView.on("storeViewState", function(e) {- e.stopPropagation();- pmease.commons.history.setViewState({scroll:{left: $body.scrollLeft(), top: $body.scrollTop()}});- $body.find(".autofit:visible").first().trigger("storeViewState");- });-- $blobView.on("autofit", function(e, width, height) {- e.stopPropagation();- $blobView.outerWidth(width);- $blobView.outerHeight(height);-- height = $blobView.height()-$head.outerHeight();- $body.outerWidth(width).outerHeight(height);- $body.find(".autofit:visible").first().trigger("autofit", [$body.width(), $body.height()]);-- var viewState = pmease.commons.history.getViewState();- if (viewState && viewState.scroll) {- $body.scrollLeft(viewState.scroll.left);- $body.scrollTop(viewState.scroll.top);- }- });+gitplex.blobView = function(containerId) {+ var $blobView = $("#" + containerId + ">.blob-view");+ var $head = $blobView.find(">.head");+ var $body = $blobView.find(">.body");++ $blobView.on("storeViewState", function(e) {+ e.stopPropagation();+ gitplex.commons.history.setViewState({scroll:{left: $body.scrollLeft(), top: $body.scrollTop()}});+ $body.find(".autofit:visible").first().trigger("storeViewState");+ });++ $blobView.on("autofit", function(e, width, height) {+ e.stopPropagation();+ $blobView.outerWidth(width);+ $blobView.outerHeight(height);++ height = $blobView.height()-$head.outerHeight();+ $body.outerWidth(width).outerHeight(height);+ $body.find(".autofit:visible").first().trigger("autofit", [$body.width(), $body.height()]);++ var viewState = gitplex.commons.history.getViewState();+ if (viewState && viewState.scroll) {+ $body.scrollLeft(viewState.scroll.left);+ $body.scrollTop(viewState.scroll.top);+ }+ });};\ No newline at end of filediff --git a/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/blobview/source/source-view.js b/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/blobview/source/source-view.jsindex 3afc0da03a..5b8325ffb8 100644--- a/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/blobview/source/source-view.js+++ b/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/blobview/source/source-view.js@@ -1,544 +1,544 @@-gitplex.sourceview = {- init: function(fileContent, filePath, openComment, mark, symbolTooltipId,- revision, blameInfos, comments, markCallback, blameMessageCallback,- viewState, loggedIn, anchor, tabSize, lineWrapMode) {- var cm;-- var $sourceView = $(".source-view");- if (openComment)- $sourceView.data("openComment", openComment);- if (mark)- $sourceView.data("mark", mark);-- $sourceView.data("markCallback", markCallback);- $sourceView.data("blameMessageCallback", blameMessageCallback);-- var $code = $sourceView.children(".code");- $sourceView.closest(".scrollable").css("overflow", "visible");-- function repositionCommentPopovers() {- $(".comment-popover:visible").each(function() {- var $popover = $(this);- var lineInfo = cm.lineInfo($popover.data("line"));- if (lineInfo.gutterMarkers) {- var gutter = lineInfo.gutterMarkers["CodeMirror-comments"];- if (gutter) {- var $indicator = $(gutter).children("a");- if ($indicator.offset().top+$indicator.height() < $sourceView.offset().top- || $indicator.offset().top > $sourceView.offset().top+$sourceView.height()) {- $popover.css("left", "-10000px");- } else {- $popover.css({- "left": $indicator.offset().left + $indicator.outerWidth() - $sourceView.offset().left,- "top": $indicator.offset().top + ($indicator.outerHeight() - $popover.outerHeight())/2 - $sourceView.offset().top- });- }- }- }- });- }-- $sourceView.on("storeViewState", function(event) {- event.stopPropagation();- if (cm) {- pmease.commons.history.setViewState(pmease.commons.codemirror.getViewState(cm));- }- });-- $sourceView.on("autofit", function(event, width, height) {- event.stopPropagation();- $sourceView.outerWidth(width);- $sourceView.outerHeight(height);- $code.outerHeight($sourceView.height());-- var $comment = $sourceView.children(".comment");- var paddingLeft;- if ($comment.is(":visible")) {- paddingLeft = $comment.outerWidth();- $comment.children(".ui-resizable-handle").outerHeight($sourceView.height());-- var $commentHead = $comment.find(">.content>.head");- var $commentBody = $comment.find(">.content>.body");- $commentBody.outerHeight($sourceView.height() - $commentHead.outerHeight());- } else {- paddingLeft = 0;- }- $sourceView.css("padding-left", paddingLeft);-- var $outline = $sourceView.children(".outline");- if ($outline.is(":visible")) {- $sourceView.css("padding-right", $outline.outerWidth());- $outline.css("left", $code.outerWidth() + paddingLeft);- $outline.children(".ui-resizable-handle").outerHeight($sourceView.height());-- var $outlineHead = $outline.find(">.content>.head");- var $outlineBody = $outline.find(">.content>.body");- $outlineBody.outerHeight($sourceView.height() - $outlineHead.outerHeight());- } else {- $sourceView.css("padding-right", "0");- }-- /*- * initialize codemirror here when we know the container width and height- * as otherwise the annotatescrollbar addon is inaccurate when window- * initially loads- */- var initState = !cm;- if (!cm) {- var options = {- value: fileContent,- readOnly: pmease.commons.isDevice()?"nocursor":true,- theme: "eclipse",- lineNumbers: true,- lineWrapping: lineWrapMode == "Soft wrap",- indentUnit: tabSize,- tabSize: tabSize,- styleActiveLine: true,- styleSelectedText: true,- foldGutter: true,- matchBrackets: true,- scrollbarStyle: "simple",- highlightIdentifiers: {delay: 500},- gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],- extraKeys: {- "F11": function(cm) {- cm.setOption("fullScreen", !cm.getOption("fullScreen"));- },- "Esc": function(cm) {- if (cm.getOption("fullScreen"))- cm.setOption("fullScreen", false);- }- }- };-- cm = CodeMirror($code[0], options);- pmease.commons.codemirror.setMode(cm, filePath);-- // add gutters with a timer to avoid the issue that occasionally- // gutter becomes much wider than expected- setTimeout(function() {- var gutters = cm.getOption("gutters").slice();- gutters.splice(0, 0, "CodeMirror-comments");- cm.setOption("gutters", gutters);- for (var line in comments) {- if (comments.hasOwnProperty(line)) {- gitplex.sourceview.addCommentGutter(line, comments[line][1]);- }- }-- if (blameInfos) {- gitplex.sourceview.blame(blameInfos);- }- gitplex.sourceview.highlightCommentTrigger();- }, 10);-- $code.selectionPopover("init", function() {- if (cm.hasFocus()) {- var from = cm.getCursor("from");- var to = cm.getCursor("to");- if (from.line != to.line || from.ch != to.ch) {- markCallback("openSelectionPopover", from.line, from.ch, to.line, to.ch);- return;- }- }- return "close";- });-- $code.mouseover(function(e) {- var node = e.target || e.srcElement, $node = $(node);- if ($node.hasClass("cm-property") || $node.hasClass("cm-variable") || $node.hasClass("cm-variable-2")- || $node.hasClass("cm-variable-3") || $node.hasClass("cm-def") || $node.hasClass("cm-meta")) {- document.getElementById(symbolTooltipId).onMouseOverSymbol(revision, node);- }- });- cm.setSize($code.width(), $code.height());- if (mark)- pmease.commons.codemirror.mark(cm, mark, true);- if (initState) // restore cursor and scroll- pmease.commons.codemirror.initState(cm, viewState);- cm.on("scroll", function() {- gitplex.symboltooltip.removeTooltip(document.getElementById(symbolTooltipId));- gitplex.mouseState.moved = false;- repositionCommentPopovers(cm);- });- } else {- cm.setSize($code.width(), $code.height());- if (cm.getOption("fullScreen"))- cm.setOption("fullScreen", false);- repositionCommentPopovers(cm);- }- });- if (anchor) {- var $anchor = $("#"+anchor);- if ($anchor.length != 0) {- setTimeout(function() {- $anchor.closest(".code-comment").parent().scrollIntoView($anchor);- }, 10);- }- }- },- initComment: function() {- var $sourceView = $(".source-view");- var $code = $sourceView.children(".code");- var commentWidthCookieKey = "sourceView.comment.width";- var $comment = $sourceView.children(".comment");- var commentWidth = Cookies.get(commentWidthCookieKey);- if (!commentWidth)- commentWidth = 400;- $comment.outerWidth(commentWidth);- var $commentResizeHandle = $comment.children(".ui-resizable-handle");- $comment.resizable({- autoHide: false,- handles: {"e": $commentResizeHandle},- minWidth: 200,- resize: function(e, ui) {- var codeWidth = $code.outerWidth();- if(codeWidth < 300)- $(this).resizable({maxWidth: ui.size.width});- },- stop: function(e, ui) {- $(this).resizable({maxWidth: undefined});- Cookies.set(commentWidthCookieKey, ui.size.width, {expires: Infinity});- }- });- },- initOutline: function() {- var $sourceView = $(".source-view");- var $code = $sourceView.children(".code");- var outlineWidthCookieKey = "sourceView.outline.width";- var $outline = $sourceView.children(".outline");- var outlineWidth = Cookies.get(outlineWidthCookieKey);- if (!outlineWidth)- outlineWidth = 350;- $outline.outerWidth(outlineWidth);- var $outlineResizeHandle = $outline.children(".ui-resizable-handle");- $outline.resizable({- autoHide: false,- handles: {"w": $outlineResizeHandle},- minWidth: 100,- resize: function(e, ui) {- var codeWidth = $code.outerWidth();- if(codeWidth < 300)- $(this).resizable({maxWidth: ui.size.width});- },- stop: function(e, ui) {- $(this).resizable({maxWidth: undefined});- Cookies.set(outlineWidthCookieKey, ui.size.width, {expires: Infinity});- }- });- },- restoreMark: function() {- var $sourceView = $(".source-view");- var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;- var mark = $sourceView.data("mark");- if (mark) {- pmease.commons.codemirror.mark(cm, mark, false);- } else {- pmease.commons.codemirror.clearMark(cm);- }- },- addCommentGutter: function(line, comments) {- $(".comment-popover[data-line='" + line + "']").remove();-- var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;- var markCallback = $(".source-view").data("markCallback");-- var $gutter = $(document.createElement("div"));- $gutter.addClass("CodeMirror-comment");- $gutter.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- */- $gutter.append("<a><i class='fa fa-comments'></i></a>");- var $indicator = $gutter.children("a");- 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 details of this comment'>#" + comment.id + "</a>";- }- $indicator.popover({- html: true,- container: ".source-view",- placement: "right auto",- template: "<div data-line='" + line + "' class='popover comment-popover'><div class='arrow'></div><div class='popover-content'></div></div>",- content: content- });- $indicator.on('shown.bs.popover', function () {- $(".comment-popover[data-line='" + line + "'] a").each(function() {- $(this).mouseover(function() {- var comment = comments[$(this).index()];- pmease.commons.codemirror.mark(cm, comment.mark, false);- });- $(this).mouseout(function() {- gitplex.sourceview.restoreMark();- });- $(this).click(function() {- if ($(".source-view form.dirty").length != 0- && !confirm("There are unsaved changes, discard and continue?")) {- return;- }- var comment = comments[$(this).index()];- markCallback("openComment", comment.id);- });- });- gitplex.sourceview.highlightCommentTrigger();- });- } else {- var comment = comments[0];- $gutter.append("<a class='comment-trigger' title='Click to show comment of marked text'><i class='fa fa-commenting'></i></a>");- var $indicator = $gutter.children("a");- $indicator.mouseover(function() {- pmease.commons.codemirror.mark(cm, comment.mark, false);- });- $indicator.mouseout(function() {- gitplex.sourceview.restoreMark();- });- $indicator.click(function() {- if ($(".source-view form.dirty").length != 0- && !confirm("There are unsaved changes, discard and continue?")) {- return;- }- markCallback("openComment", comment.id);- });- }- cm.setGutterMarker(parseInt(line), "CodeMirror-comments", $gutter[0]);- },- openSelectionPopover: function(mark, markUrl, loggedIn) {- var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;- var ch = (mark.beginChar + mark.endChar)/2;- var position = cm.charCoords({line:mark.beginLine, ch:ch});-- var $content = $("<div><a class='permanent'><i class='fa fa-link'></i> Permanent link of this selection</a>");- $content.children("a.permanent").attr("href", markUrl);- if (loggedIn) {- $content.append("<a class='comment'><i class='fa fa-comment'></i> Add comment on this selection</a>");- $content.children("a.comment").click(function() {- if ($(".source-view").find("form.dirty").length != 0- && !confirm("There are unsaved changes, discard and continue?")) {- return;- }- var markCallback = $(".source-view").data("markCallback");- markCallback("addComment", 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>");- }-- $(".source-view>.code").selectionPopover("open", {- position: position,- content: $content- });- },- onCommentAdded: function(comment) {- var $sourceView = $(".source-view");- $sourceView.data("openComment", comment);- $sourceView.data("mark", comment.mark);-- var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;- var line = parseInt(comment.mark.beginLine);- var lineInfo = cm.lineInfo(line);- var gutter;- if (lineInfo.gutterMarkers)- gutter = lineInfo.gutterMarkers["CodeMirror-comments"];- var comments;- if (gutter) {- comments = $(gutter).data("comments");- } else {- comments = [];- }- comments.push(comment);- gitplex.sourceview.addCommentGutter(line, comments);- gitplex.sourceview.highlightCommentTrigger();- gitplex.sourceview.onLayoutChange();- },- onCommentDeleted: function(comment) {- var $sourceView = $(".source-view");- $sourceView.removeData("openComment");-- var line = parseInt(comment.mark.beginLine);- var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;- var lineInfo = cm.lineInfo(line);- var $gutter = $(lineInfo.gutterMarkers["CodeMirror-comments"]);- var comments = $gutter.data("comments");- if (comments.length == 1) {- cm.setGutterMarker(line, "CodeMirror-comments", null);- } else {- for (var i in comments) {- if (comments[i].id == comment.id) {- comments.splice(i, 1);- break;- }- }- gitplex.sourceview.addCommentGutter(line, comments);- }- gitplex.sourceview.highlightCommentTrigger();- gitplex.sourceview.onLayoutChange();-- gitplex.sourceview.mark(undefined, false);- },- onLayoutChange: function() {- $sourceView = $('.source-view');- $sourceView.trigger('autofit', [$sourceView.outerWidth(), $sourceView.outerHeight()]);- },- onAddComment: function(mark) {- var $sourceView = $(".source-view");- $sourceView.removeData("openComment");- $sourceView.data("mark", mark);- $(".source-view>.code").selectionPopover("close");-- var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;- pmease.commons.codemirror.clearSelection(cm);- gitplex.sourceview.onLayoutChange();-- // mark and scroll after changing layout to make sure scroll- // works correctly- pmease.commons.codemirror.mark(cm, mark, true);-- gitplex.sourceview.highlightCommentTrigger();- },- onOpenComment: function(comment) {- var $sourceView = $(".source-view");- $sourceView.data("openComment", comment);- $sourceView.data("mark", comment.mark);- gitplex.sourceview.highlightCommentTrigger();- gitplex.sourceview.onLayoutChange();-- // mark and scroll after changing layout to make sure scroll- // works correctly- gitplex.sourceview.mark(comment.mark, true);- },- onCloseComment: function() {- var $sourceView = $(".source-view");- $sourceView.removeData("openComment");- gitplex.sourceview.highlightCommentTrigger();- gitplex.sourceview.onLayoutChange();- gitplex.sourceview.mark(undefined, false);- },- onToggleOutline: function() {- gitplex.sourceview.onLayoutChange();- $(".outline-toggle").toggleClass("active");- },- highlightCommentTrigger: function() {- var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;- $(".comment-popover a").removeClass("active");-- /*- * we can not simply select all comment triggers via jQuery- * as comment gutter may be create/destroy dynamically by- * CodeMirror- */- for (var i=0; i<cm.lineCount(); i++) {- var gutterMarkers = cm.lineInfo(i).gutterMarkers;- if (gutterMarkers) {- var gutter = gutterMarkers["CodeMirror-comments"];- if (gutter) {- $(gutter).children("a").removeClass("active");- }- }- }-- var openComment = $(".source-view").data("openComment");- if (openComment) {- var line = parseInt(openComment.mark.beginLine);- var lineInfo = cm.lineInfo(line);- if (lineInfo && lineInfo.gutterMarkers) {- var gutter = lineInfo.gutterMarkers["CodeMirror-comments"];- if (gutter) {- var comments = $(gutter).data("comments");- if (comments.length == 1) {- $(gutter).children("a").addClass("active");- } else {- $(".comment-popover[data-line='" + line + "'] a").each(function() {- var comment = comments[$(this).index()];- if (comment.id == openComment.id) {- $(this).addClass("active");- }- });- }- }- }- }- },- mark: function(mark, scroll) {- var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;- if (mark) {- $(".source-view").data("mark", mark);- pmease.commons.codemirror.mark(cm, mark, scroll);- } else {- $(".source-view").removeData("mark");- pmease.commons.codemirror.clearMark(cm);- }- },- blame: function(blameInfos) {- var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;- var alignment = {targetX: 100, targetY: 100, x: 0, y: 0};-- if (blameInfos) {- var blameMessageCallback = $(".source-view").data("blameMessageCallback");- var gutters = cm.getOption("gutters").slice();- gutters.splice(1, 0, "CodeMirror-annotations");- cm.setOption("gutters", gutters);- for (var i in blameInfos) {- var blameInfo = blameInfos[i];- for (var j in blameInfo.ranges) {- var range = blameInfo.ranges[j];- var $gutter = $(document.createElement("div"));- $gutter.addClass("CodeMirror-annotation");- $("<a class='hash'>" + blameInfo.abbreviatedHash + "</a>").appendTo($gutter).attr("href", blameInfo.url);- var $hashLink = $gutter.children("a.hash");- $hashLink.data("hash", blameInfo.hash);- $hashLink.data("line", range.from);-- $hashLink.hover(function() {- var tooltipId = "blame-message-line-" + $(this).data("line");- blameMessageCallback(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);- $(".source-view").append($tooltip);- return $tooltip;- }, alignment);-- $gutter.append("<span class='date'>" + blameInfo.commitDate + "</span>");- $gutter.append("<span class='author'>" + blameInfo.authorName + "</span>");- cm.setGutterMarker(range.from, "CodeMirror-annotations", $gutter[0]);-- for (var line = range.from+1; line<range.to; line++) {- var $gutter = $(document.createElement("div"));- $gutter.addClass("CodeMirror-annotation");- $gutter.append("<span class='same-as-above'>...</span>");- cm.setGutterMarker(line, "CodeMirror-annotations", $gutter[0]);- }- }- }- } else {- cm.clearGutter("CodeMirror-annotations");- var gutters = cm.getOption("gutters").slice();- gutters.splice(1, 1);- cm.setOption("gutters", gutters);- }- },- onBlame: function(blameInfos) {- gitplex.sourceview.blame(blameInfos);- gitplex.sourceview.onLayoutChange();- $(".blame-toggle").toggleClass("active");- },- scrollToCommentBottom: function() {- var $body = $(".source-view>.comment>.content>.body");- $body.scrollTop($body[0].scrollHeight);- },- onLineWrapModeChange: function(lineWrapMode) {- var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;- cm.setOption("lineWrapping", lineWrapMode == "Soft wrap");- },- onTabSizeChange: function(tabSize) {- var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;- cm.setOption("tabSize", tabSize);- }-};+gitplex.sourceview = {+ init: function(fileContent, filePath, openComment, mark, symbolTooltipId,+ revision, blameInfos, comments, markCallback, blameMessageCallback,+ viewState, loggedIn, anchor, tabSize, lineWrapMode) {+ var cm;++ var $sourceView = $(".source-view");+ if (openComment)+ $sourceView.data("openComment", openComment);+ if (mark)+ $sourceView.data("mark", mark);++ $sourceView.data("markCallback", markCallback);+ $sourceView.data("blameMessageCallback", blameMessageCallback);++ var $code = $sourceView.children(".code");+ $sourceView.closest(".scrollable").css("overflow", "visible");++ function repositionCommentPopovers() {+ $(".comment-popover:visible").each(function() {+ var $popover = $(this);+ var lineInfo = cm.lineInfo($popover.data("line"));+ if (lineInfo.gutterMarkers) {+ var gutter = lineInfo.gutterMarkers["CodeMirror-comments"];+ if (gutter) {+ var $indicator = $(gutter).children("a");+ if ($indicator.offset().top+$indicator.height() < $sourceView.offset().top+ || $indicator.offset().top > $sourceView.offset().top+$sourceView.height()) {+ $popover.css("left", "-10000px");+ } else {+ $popover.css({+ "left": $indicator.offset().left + $indicator.outerWidth() - $sourceView.offset().left,+ "top": $indicator.offset().top + ($indicator.outerHeight() - $popover.outerHeight())/2 - $sourceView.offset().top+ });+ }+ }+ }+ });+ }++ $sourceView.on("storeViewState", function(event) {+ event.stopPropagation();+ if (cm) {+ gitplex.commons.history.setViewState(gitplex.commons.codemirror.getViewState(cm));+ }+ });++ $sourceView.on("autofit", function(event, width, height) {+ event.stopPropagation();+ $sourceView.outerWidth(width);+ $sourceView.outerHeight(height);+ $code.outerHeight($sourceView.height());++ var $comment = $sourceView.children(".comment");+ var paddingLeft;+ if ($comment.is(":visible")) {+ paddingLeft = $comment.outerWidth();+ $comment.children(".ui-resizable-handle").outerHeight($sourceView.height());++ var $commentHead = $comment.find(">.content>.head");+ var $commentBody = $comment.find(">.content>.body");+ $commentBody.outerHeight($sourceView.height() - $commentHead.outerHeight());+ } else {+ paddingLeft = 0;+ }+ $sourceView.css("padding-left", paddingLeft);++ var $outline = $sourceView.children(".outline");+ if ($outline.is(":visible")) {+ $sourceView.css("padding-right", $outline.outerWidth());+ $outline.css("left", $code.outerWidth() + paddingLeft);+ $outline.children(".ui-resizable-handle").outerHeight($sourceView.height());++ var $outlineHead = $outline.find(">.content>.head");+ var $outlineBody = $outline.find(">.content>.body");+ $outlineBody.outerHeight($sourceView.height() - $outlineHead.outerHeight());+ } else {+ $sourceView.css("padding-right", "0");+ }++ /*+ * initialize codemirror here when we know the container width and height+ * as otherwise the annotatescrollbar addon is inaccurate when window+ * initially loads+ */+ var initState = !cm;+ if (!cm) {+ var options = {+ value: fileContent,+ readOnly: gitplex.commons.isDevice()?"nocursor":true,+ theme: "eclipse",+ lineNumbers: true,+ lineWrapping: lineWrapMode == "Soft wrap",+ indentUnit: tabSize,+ tabSize: tabSize,+ styleActiveLine: true,+ styleSelectedText: true,+ foldGutter: true,+ matchBrackets: true,+ scrollbarStyle: "simple",+ highlightIdentifiers: {delay: 500},+ gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],+ extraKeys: {+ "F11": function(cm) {+ cm.setOption("fullScreen", !cm.getOption("fullScreen"));+ },+ "Esc": function(cm) {+ if (cm.getOption("fullScreen"))+ cm.setOption("fullScreen", false);+ }+ }+ };++ cm = CodeMirror($code[0], options);+ gitplex.commons.codemirror.setMode(cm, filePath);++ // add gutters with a timer to avoid the issue that occasionally+ // gutter becomes much wider than expected+ setTimeout(function() {+ var gutters = cm.getOption("gutters").slice();+ gutters.splice(0, 0, "CodeMirror-comments");+ cm.setOption("gutters", gutters);+ for (var line in comments) {+ if (comments.hasOwnProperty(line)) {+ gitplex.sourceview.addCommentGutter(line, comments[line][1]);+ }+ }++ if (blameInfos) {+ gitplex.sourceview.blame(blameInfos);+ }+ gitplex.sourceview.highlightCommentTrigger();+ }, 10);++ $code.selectionPopover("init", function() {+ if (cm.hasFocus()) {+ var from = cm.getCursor("from");+ var to = cm.getCursor("to");+ if (from.line != to.line || from.ch != to.ch) {+ markCallback("openSelectionPopover", from.line, from.ch, to.line, to.ch);+ return;+ }+ }+ return "close";+ });++ $code.mouseover(function(e) {+ var node = e.target || e.srcElement, $node = $(node);+ if ($node.hasClass("cm-property") || $node.hasClass("cm-variable") || $node.hasClass("cm-variable-2")+ || $node.hasClass("cm-variable-3") || $node.hasClass("cm-def") || $node.hasClass("cm-meta")) {+ document.getElementById(symbolTooltipId).onMouseOverSymbol(revision, node);+ }+ });+ cm.setSize($code.width(), $code.height());+ if (mark)+ gitplex.commons.codemirror.mark(cm, mark, true);+ if (initState) // restore cursor and scroll+ gitplex.commons.codemirror.initState(cm, viewState);+ cm.on("scroll", function() {+ gitplex.symboltooltip.removeTooltip(document.getElementById(symbolTooltipId));+ gitplex.mouseState.moved = false;+ repositionCommentPopovers(cm);+ });+ } else {+ cm.setSize($code.width(), $code.height());+ if (cm.getOption("fullScreen"))+ cm.setOption("fullScreen", false);+ repositionCommentPopovers(cm);+ }+ });+ if (anchor) {+ var $anchor = $("#"+anchor);+ if ($anchor.length != 0) {+ setTimeout(function() {+ $anchor.closest(".code-comment").parent().scrollIntoView($anchor);+ }, 10);+ }+ }+ },+ initComment: function() {+ var $sourceView = $(".source-view");+ var $code = $sourceView.children(".code");+ var commentWidthCookieKey = "sourceView.comment.width";+ var $comment = $sourceView.children(".comment");+ var commentWidth = Cookies.get(commentWidthCookieKey);+ if (!commentWidth)+ commentWidth = 400;+ $comment.outerWidth(commentWidth);+ var $commentResizeHandle = $comment.children(".ui-resizable-handle");+ $comment.resizable({+ autoHide: false,+ handles: {"e": $commentResizeHandle},+ minWidth: 200,+ resize: function(e, ui) {+ var codeWidth = $code.outerWidth();+ if(codeWidth < 300)+ $(this).resizable({maxWidth: ui.size.width});+ },+ stop: function(e, ui) {+ $(this).resizable({maxWidth: undefined});+ Cookies.set(commentWidthCookieKey, ui.size.width, {expires: Infinity});+ }+ });+ },+ initOutline: function() {+ var $sourceView = $(".source-view");+ var $code = $sourceView.children(".code");+ var outlineWidthCookieKey = "sourceView.outline.width";+ var $outline = $sourceView.children(".outline");+ var outlineWidth = Cookies.get(outlineWidthCookieKey);+ if (!outlineWidth)+ outlineWidth = 350;+ $outline.outerWidth(outlineWidth);+ var $outlineResizeHandle = $outline.children(".ui-resizable-handle");+ $outline.resizable({+ autoHide: false,+ handles: {"w": $outlineResizeHandle},+ minWidth: 100,+ resize: function(e, ui) {+ var codeWidth = $code.outerWidth();+ if(codeWidth < 300)+ $(this).resizable({maxWidth: ui.size.width});+ },+ stop: function(e, ui) {+ $(this).resizable({maxWidth: undefined});+ Cookies.set(outlineWidthCookieKey, ui.size.width, {expires: Infinity});+ }+ });+ },+ restoreMark: function() {+ var $sourceView = $(".source-view");+ var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;+ var mark = $sourceView.data("mark");+ if (mark) {+ gitplex.commons.codemirror.mark(cm, mark, false);+ } else {+ gitplex.commons.codemirror.clearMark(cm);+ }+ },+ addCommentGutter: function(line, comments) {+ $(".comment-popover[data-line='" + line + "']").remove();++ var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;+ var markCallback = $(".source-view").data("markCallback");++ var $gutter = $(document.createElement("div"));+ $gutter.addClass("CodeMirror-comment");+ $gutter.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+ */+ $gutter.append("<a><i class='fa fa-comments'></i></a>");+ var $indicator = $gutter.children("a");+ 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 details of this comment'>#" + comment.id + "</a>";+ }+ $indicator.popover({+ html: true,+ container: ".source-view",+ placement: "right auto",+ template: "<div data-line='" + line + "' class='popover comment-popover'><div class='arrow'></div><div class='popover-content'></div></div>",+ content: content+ });+ $indicator.on('shown.bs.popover', function () {+ $(".comment-popover[data-line='" + line + "'] a").each(function() {+ $(this).mouseover(function() {+ var comment = comments[$(this).index()];+ gitplex.commons.codemirror.mark(cm, comment.mark, false);+ });+ $(this).mouseout(function() {+ gitplex.sourceview.restoreMark();+ });+ $(this).click(function() {+ if ($(".source-view form.dirty").length != 0+ && !confirm("There are unsaved changes, discard and continue?")) {+ return;+ }+ var comment = comments[$(this).index()];+ markCallback("openComment", comment.id);+ });+ });+ gitplex.sourceview.highlightCommentTrigger();+ });+ } else {+ var comment = comments[0];+ $gutter.append("<a class='comment-trigger' title='Click to show comment of marked text'><i class='fa fa-commenting'></i></a>");+ var $indicator = $gutter.children("a");+ $indicator.mouseover(function() {+ gitplex.commons.codemirror.mark(cm, comment.mark, false);+ });+ $indicator.mouseout(function() {+ gitplex.sourceview.restoreMark();+ });+ $indicator.click(function() {+ if ($(".source-view form.dirty").length != 0+ && !confirm("There are unsaved changes, discard and continue?")) {+ return;+ }+ markCallback("openComment", comment.id);+ });+ }+ cm.setGutterMarker(parseInt(line), "CodeMirror-comments", $gutter[0]);+ },+ openSelectionPopover: function(mark, markUrl, loggedIn) {+ var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;+ var ch = (mark.beginChar + mark.endChar)/2;+ var position = cm.charCoords({line:mark.beginLine, ch:ch});++ var $content = $("<div><a class='permanent'><i class='fa fa-link'></i> Permanent link of this selection</a>");+ $content.children("a.permanent").attr("href", markUrl);+ if (loggedIn) {+ $content.append("<a class='comment'><i class='fa fa-comment'></i> Add comment on this selection</a>");+ $content.children("a.comment").click(function() {+ if ($(".source-view").find("form.dirty").length != 0+ && !confirm("There are unsaved changes, discard and continue?")) {+ return;+ }+ var markCallback = $(".source-view").data("markCallback");+ markCallback("addComment", 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>");+ }++ $(".source-view>.code").selectionPopover("open", {+ position: position,+ content: $content+ });+ },+ onCommentAdded: function(comment) {+ var $sourceView = $(".source-view");+ $sourceView.data("openComment", comment);+ $sourceView.data("mark", comment.mark);++ var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;+ var line = parseInt(comment.mark.beginLine);+ var lineInfo = cm.lineInfo(line);+ var gutter;+ if (lineInfo.gutterMarkers)+ gutter = lineInfo.gutterMarkers["CodeMirror-comments"];+ var comments;+ if (gutter) {+ comments = $(gutter).data("comments");+ } else {+ comments = [];+ }+ comments.push(comment);+ gitplex.sourceview.addCommentGutter(line, comments);+ gitplex.sourceview.highlightCommentTrigger();+ gitplex.sourceview.onLayoutChange();+ },+ onCommentDeleted: function(comment) {+ var $sourceView = $(".source-view");+ $sourceView.removeData("openComment");++ var line = parseInt(comment.mark.beginLine);+ var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;+ var lineInfo = cm.lineInfo(line);+ var $gutter = $(lineInfo.gutterMarkers["CodeMirror-comments"]);+ var comments = $gutter.data("comments");+ if (comments.length == 1) {+ cm.setGutterMarker(line, "CodeMirror-comments", null);+ } else {+ for (var i in comments) {+ if (comments[i].id == comment.id) {+ comments.splice(i, 1);+ break;+ }+ }+ gitplex.sourceview.addCommentGutter(line, comments);+ }+ gitplex.sourceview.highlightCommentTrigger();+ gitplex.sourceview.onLayoutChange();++ gitplex.sourceview.mark(undefined, false);+ },+ onLayoutChange: function() {+ $sourceView = $('.source-view');+ $sourceView.trigger('autofit', [$sourceView.outerWidth(), $sourceView.outerHeight()]);+ },+ onAddComment: function(mark) {+ var $sourceView = $(".source-view");+ $sourceView.removeData("openComment");+ $sourceView.data("mark", mark);+ $(".source-view>.code").selectionPopover("close");++ var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;+ gitplex.commons.codemirror.clearSelection(cm);+ gitplex.sourceview.onLayoutChange();++ // mark and scroll after changing layout to make sure scroll+ // works correctly+ gitplex.commons.codemirror.mark(cm, mark, true);++ gitplex.sourceview.highlightCommentTrigger();+ },+ onOpenComment: function(comment) {+ var $sourceView = $(".source-view");+ $sourceView.data("openComment", comment);+ $sourceView.data("mark", comment.mark);+ gitplex.sourceview.highlightCommentTrigger();+ gitplex.sourceview.onLayoutChange();++ // mark and scroll after changing layout to make sure scroll+ // works correctly+ gitplex.sourceview.mark(comment.mark, true);+ },+ onCloseComment: function() {+ var $sourceView = $(".source-view");+ $sourceView.removeData("openComment");+ gitplex.sourceview.highlightCommentTrigger();+ gitplex.sourceview.onLayoutChange();+ gitplex.sourceview.mark(undefined, false);+ },+ onToggleOutline: function() {+ gitplex.sourceview.onLayoutChange();+ $(".outline-toggle").toggleClass("active");+ },+ highlightCommentTrigger: function() {+ var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;+ $(".comment-popover a").removeClass("active");++ /*+ * we can not simply select all comment triggers via jQuery+ * as comment gutter may be create/destroy dynamically by+ * CodeMirror+ */+ for (var i=0; i<cm.lineCount(); i++) {+ var gutterMarkers = cm.lineInfo(i).gutterMarkers;+ if (gutterMarkers) {+ var gutter = gutterMarkers["CodeMirror-comments"];+ if (gutter) {+ $(gutter).children("a").removeClass("active");+ }+ }+ }++ var openComment = $(".source-view").data("openComment");+ if (openComment) {+ var line = parseInt(openComment.mark.beginLine);+ var lineInfo = cm.lineInfo(line);+ if (lineInfo && lineInfo.gutterMarkers) {+ var gutter = lineInfo.gutterMarkers["CodeMirror-comments"];+ if (gutter) {+ var comments = $(gutter).data("comments");+ if (comments.length == 1) {+ $(gutter).children("a").addClass("active");+ } else {+ $(".comment-popover[data-line='" + line + "'] a").each(function() {+ var comment = comments[$(this).index()];+ if (comment.id == openComment.id) {+ $(this).addClass("active");+ }+ });+ }+ }+ }+ }+ },+ mark: function(mark, scroll) {+ var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;+ if (mark) {+ $(".source-view").data("mark", mark);+ gitplex.commons.codemirror.mark(cm, mark, scroll);+ } else {+ $(".source-view").removeData("mark");+ gitplex.commons.codemirror.clearMark(cm);+ }+ },+ blame: function(blameInfos) {+ var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;+ var alignment = {targetX: 100, targetY: 100, x: 0, y: 0};++ if (blameInfos) {+ var blameMessageCallback = $(".source-view").data("blameMessageCallback");+ var gutters = cm.getOption("gutters").slice();+ gutters.splice(1, 0, "CodeMirror-annotations");+ cm.setOption("gutters", gutters);+ for (var i in blameInfos) {+ var blameInfo = blameInfos[i];+ for (var j in blameInfo.ranges) {+ var range = blameInfo.ranges[j];+ var $gutter = $(document.createElement("div"));+ $gutter.addClass("CodeMirror-annotation");+ $("<a class='hash'>" + blameInfo.abbreviatedHash + "</a>").appendTo($gutter).attr("href", blameInfo.url);+ var $hashLink = $gutter.children("a.hash");+ $hashLink.data("hash", blameInfo.hash);+ $hashLink.data("line", range.from);++ $hashLink.hover(function() {+ var tooltipId = "blame-message-line-" + $(this).data("line");+ blameMessageCallback(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);+ $(".source-view").append($tooltip);+ return $tooltip;+ }, alignment);++ $gutter.append("<span class='date'>" + blameInfo.commitDate + "</span>");+ $gutter.append("<span class='author'>" + blameInfo.authorName + "</span>");+ cm.setGutterMarker(range.from, "CodeMirror-annotations", $gutter[0]);++ for (var line = range.from+1; line<range.to; line++) {+ var $gutter = $(document.createElement("div"));+ $gutter.addClass("CodeMirror-annotation");+ $gutter.append("<span class='same-as-above'>...</span>");+ cm.setGutterMarker(line, "CodeMirror-annotations", $gutter[0]);+ }+ }+ }+ } else {+ cm.clearGutter("CodeMirror-annotations");+ var gutters = cm.getOption("gutters").slice();+ gutters.splice(1, 1);+ cm.setOption("gutters", gutters);+ }+ },+ onBlame: function(blameInfos) {+ gitplex.sourceview.blame(blameInfos);+ gitplex.sourceview.onLayoutChange();+ $(".blame-toggle").toggleClass("active");+ },+ scrollToCommentBottom: function() {+ var $body = $(".source-view>.comment>.content>.body");+ $body.scrollTop($body[0].scrollHeight);+ },+ onLineWrapModeChange: function(lineWrapMode) {+ var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;+ cm.setOption("lineWrapping", lineWrapMode == "Soft wrap");+ },+ onTabSizeChange: function(tabSize) {+ var cm = $(".source-view>.code>.CodeMirror")[0].CodeMirror;+ cm.setOption("tabSize", tabSize);+ }+};diff --git a/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/editsave/edit-save.js b/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/editsave/edit-save.jsindex b5a5299f96..5f6fd02e42 100644--- a/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/editsave/edit-save.js+++ b/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/editsave/edit-save.js@@ -1,43 +1,43 @@-gitplex.editsave = {- init: function(containerId) {- var $editSave = $("#" + containerId + ">.edit-save");- $editSave.on("storeViewState", function(e) {- e.stopPropagation();- pmease.commons.history.setViewState({scroll:{left: $editSave.scrollLeft(), top: $editSave.scrollTop()}});- });- $editSave.on("autofit", function(e, width, height) {- e.stopPropagation();-- $editSave.closest(".body").css("overflow", "hidden");- $editSave.outerWidth(width);- $editSave.outerHeight(height);-- var viewState = pmease.commons.history.getViewState();- if (viewState && viewState.scroll) {- $editSave.scrollLeft(viewState.scroll.left);- $editSave.scrollTop(viewState.scroll.top);- }- });-- $editSave.on("contentEdit", function(event, contentChanged) {- event.stopPropagation();- $editSave.data("contentChanged", contentChanged);- gitplex.editsave.updateSubmitBtnState($editSave);- });- },-- updateSubmitBtnState: function($editSave) {- var $submitBtn = $editSave.find("input[type=submit]");- if ($editSave.data("contentChanged") === true || $editSave.data("pathChanged") === true)- $submitBtn.removeAttr("disabled");- else- $submitBtn.attr("disabled", "disabled");- },-- onPathChange: function(containerId, defaultCommitMessage, equalsOldPath) {- var $editSave = $("#" + containerId + ">.edit-save");- $editSave.find(".summary-commit-message").attr("placeholder", defaultCommitMessage);- $editSave.data("pathChanged", !equalsOldPath);- gitplex.editsave.updateSubmitBtnState($editSave);- }-};+gitplex.editsave = {+ init: function(containerId) {+ var $editSave = $("#" + containerId + ">.edit-save");+ $editSave.on("storeViewState", function(e) {+ e.stopPropagation();+ gitplex.commons.history.setViewState({scroll:{left: $editSave.scrollLeft(), top: $editSave.scrollTop()}});+ });+ $editSave.on("autofit", function(e, width, height) {+ e.stopPropagation();++ $editSave.closest(".body").css("overflow", "hidden");+ $editSave.outerWidth(width);+ $editSave.outerHeight(height);++ var viewState = gitplex.commons.history.getViewState();+ if (viewState && viewState.scroll) {+ $editSave.scrollLeft(viewState.scroll.left);+ $editSave.scrollTop(viewState.scroll.top);+ }+ });++ $editSave.on("contentEdit", function(event, contentChanged) {+ event.stopPropagation();+ $editSave.data("contentChanged", contentChanged);+ gitplex.editsave.updateSubmitBtnState($editSave);+ });+ },++ updateSubmitBtnState: function($editSave) {+ var $submitBtn = $editSave.find("input[type=submit]");+ if ($editSave.data("contentChanged") === true || $editSave.data("pathChanged") === true)+ $submitBtn.removeAttr("disabled");+ else+ $submitBtn.attr("disabled", "disabled");+ },++ onPathChange: function(containerId, defaultCommitMessage, equalsOldPath) {+ var $editSave = $("#" + containerId + ">.edit-save");+ $editSave.find(".summary-commit-message").attr("placeholder", defaultCommitMessage);+ $editSave.data("pathChanged", !equalsOldPath);+ gitplex.editsave.updateSubmitBtnState($editSave);+ }+};diff --git a/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/fileedit/file-edit.js b/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/fileedit/file-edit.jsindex 0840e588bb..1512602a72 100644--- a/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/fileedit/file-edit.js+++ b/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/fileedit/file-edit.js@@ -1,220 +1,220 @@-gitplex.fileedit = {- init: function(containerId, filePath, fileContent, previewCallback, saveCallback, mark, viewState, indentType, tabSize, lineWrapMode) {- var $container = $("#" + containerId);- var $fileEdit = $container.find(">.file-edit");- var $head = $fileEdit.find(">.head");- var $body = $fileEdit.find(">.body");- var $edit = $body.find(">div.edit");- var cm;-- $head.find("a.edit").click(function() {- gitplex.fileedit.selectTab($(this));- });- $head.find("a.preview").click(function() {- previewCallback(cm.getValue());- });- $head.find("a.save").click(function() {- saveCallback(cm.getValue());- });-- $fileEdit.on("storeViewState", function(e) {- e.stopPropagation();- if (cm) {- pmease.commons.history.setViewState(pmease.commons.codemirror.getViewState(cm));- }- });-- $fileEdit.on("autofit", function(e, width, height) {- e.stopPropagation();- $fileEdit.outerWidth(width);- $fileEdit.outerHeight(height);-- height = $fileEdit.height()-$head.outerHeight();- $body.outerWidth(width).outerHeight(height-1);-- /*- * initialize codemirror here when we know the container width and height- * as otherwise the annotatescrollbar addon is inaccurate when window- * initially loads- */- var initState = !cm;- if (!cm) {- cm = CodeMirror($edit[0], {- value: fileContent,- theme: "eclipse",- indentWithTabs: indentType == "Tabs",- indentUnit: tabSize,- lineNumbers: true,- lineWrapping: lineWrapMode == "Soft wrap",- tabSize: tabSize,- styleActiveLine: true,- styleSelectedText: true,- foldGutter: true,- matchBrackets: true,- scrollbarStyle: "simple",- highlightIdentifiers: {delay: 500},- extraKeys: {- "F11": function(cm) {- cm.setOption("fullScreen", !cm.getOption("fullScreen"));- },- "Esc": function(cm) {- if (cm.getOption("fullScreen"))- cm.setOption("fullScreen", false);- }- }- });-- cm.oldDocValue = cm.doc.getValue();- cm.on("change", function() {- var $form = $body.find(">form.edit");- if (cm.doc.getValue() != cm.oldDocValue) {- $form.addClass("dirty");- $fileEdit.data("contentChanged", true);- } else {- $form.removeClass("dirty");- $fileEdit.data("contentChanged", false);- }- });- pmease.commons.codemirror.setMode(cm, filePath);-- if (mark)- pmease.commons.codemirror.mark(cm, mark);- }-- if ($edit.is(":visible")) {- $body.css("overflow", "hidden");- $edit.outerHeight($body.height());- $edit.outerWidth($body.width());- if (cm.getOption("fullScreen"))- cm.setOption("fullScreen", false);- cm.setSize($edit.width(), $edit.height());- if (initState)- pmease.commons.codemirror.initState(cm, viewState);- } else {- $body.css("overflow", "auto");- }- });- },- save: function(containerId) {- gitplex.fileedit.selectTab($("#" + containerId + ">.file-edit>.head>.save"));- },- preview: function(containerId) {- gitplex.fileedit.selectTab($("#" + containerId + ">.file-edit>.head>.preview"));- },- setMode: function(containerId, filePath) {- var cm = $("#"+ containerId + ">.file-edit>.body>div.edit>.CodeMirror")[0].CodeMirror;- pmease.commons.codemirror.setMode(cm, filePath);- },- mark: function(containerId, mark) {- var cm = $("#"+ containerId + ">.file-edit>.body>div.edit>.CodeMirror")[0].CodeMirror;- if (cm.oldDocValue) {- var dmp = new diff_match_patch();- var diffs = dmp.diff_main(cm.oldDocValue, cm.doc.getValue());-- var beginLine = mark.beginLine, beginChar = mark.beginChar;- var endLine = mark.endLine, endChar = mark.endChar;- var newBeginLine, newBeginChar, newEndLine, newEndChar;- var oldLine = oldChar = newLine = newChar = 0;- for (var i=0; i<diffs.length; i++) {- var diff = diffs[i];- var chars = diff[1];- var quit = false;- for (var j=0; j<chars.length; j++) {- var char = chars[j];- if (diff[0] == -1) {- if (char == '\n') {- oldLine++;- oldChar = 0;- } else {- oldChar++;- }- } else if (diff[0] == 1) {- if (char == '\n') {- newLine++;- newChar = 0;- } else {- newChar++;- }- } else {- if (oldLine == beginLine && oldChar == beginChar) {- newBeginLine = newLine;- newBeginChar = newChar;- }- if (char == '\n') {- oldLine++;- oldChar = 0;- newLine++;- newChar = 0;- } else {- oldChar++;- newChar++;- }- if (oldLine == endLine && oldChar == endChar) {- newEndLine = newLine;- newEndChar = newChar;- }- }- if (oldLine>beginLine && newBeginLine==undefined || oldLine>endLine) {- quit = true;- break;- }- }- if (quit)- break;- }- }-- if (newBeginLine!=undefined && newEndLine!=undefined) {- var newMark = {- beginLine: newBeginLine,- beginChar: newBeginChar,- endLine: newEndLine,- endChar: newEndChar- };- pmease.commons.codemirror.mark(cm, newMark);- }- },- selectTab: function($tab) {- var $active = $tab.parent().find("a.tab.active");- $active[0].hideableVisible = $(".hideable").is(":visible");- $active.removeClass("active");- $tab.addClass("active");- if ($tab[0].hideableVisible != undefined) {- if ($tab[0].hideableVisible)- $(".hideable").show();- else- $(".hideable").hide();- }- var $fileEdit = $tab.closest(".file-edit");- var $body = $fileEdit.find(">.body");-- var $cm = $fileEdit.find(">.body>div.edit>.CodeMirror");- if ($cm.is(":visible")) {- var cm = $cm[0].CodeMirror;- $fileEdit.find(">.body>div.save").data("viewState", pmease.commons.codemirror.getViewState(cm));- }-- $body.find(">div").hide();- if ($tab.hasClass("edit")) {- $body.find(">div.edit").show().find(">.CodeMirror")[0].CodeMirror.focus();- } else if ($tab.hasClass("preview")) {- $body.find(">div.preview").show();- } else {- $body.find(">div.save").show();- $body.find(">div.save>.edit-save").trigger("contentEdit", [$fileEdit.data("contentChanged")]);- }- $(window).resize();- },- onIndentTypeChange: function(containerId, indentType) {- var cm = $("#"+ containerId + ">.file-edit>.body>div.edit>.CodeMirror")[0].CodeMirror;- cm.setOption("indentWithTabs", indentType == "Tabs");- },- onLineWrapModeChange: function(containerId, lineWrapMode) {- var cm = $("#"+ containerId + ">.file-edit>.body>div.edit>.CodeMirror")[0].CodeMirror;- cm.setOption("lineWrapping", lineWrapMode == "Soft wrap");- },- onTabSizeChange: function(containerId, tabSize) {- var cm = $("#"+ containerId + ">.file-edit>.body>div.edit>.CodeMirror")[0].CodeMirror;- cm.setOption("tabSize", tabSize);- }-};+gitplex.fileedit = {+ init: function(containerId, filePath, fileContent, previewCallback, saveCallback, mark, viewState, indentType, tabSize, lineWrapMode) {+ var $container = $("#" + containerId);+ var $fileEdit = $container.find(">.file-edit");+ var $head = $fileEdit.find(">.head");+ var $body = $fileEdit.find(">.body");+ var $edit = $body.find(">div.edit");+ var cm;++ $head.find("a.edit").click(function() {+ gitplex.fileedit.selectTab($(this));+ });+ $head.find("a.preview").click(function() {+ previewCallback(cm.getValue());+ });+ $head.find("a.save").click(function() {+ saveCallback(cm.getValue());+ });++ $fileEdit.on("storeViewState", function(e) {+ e.stopPropagation();+ if (cm) {+ gitplex.commons.history.setViewState(gitplex.commons.codemirror.getViewState(cm));+ }+ });++ $fileEdit.on("autofit", function(e, width, height) {+ e.stopPropagation();+ $fileEdit.outerWidth(width);+ $fileEdit.outerHeight(height);++ height = $fileEdit.height()-$head.outerHeight();+ $body.outerWidth(width).outerHeight(height-1);++ /*+ * initialize codemirror here when we know the container width and height+ * as otherwise the annotatescrollbar addon is inaccurate when window+ * initially loads+ */+ var initState = !cm;+ if (!cm) {+ cm = CodeMirror($edit[0], {+ value: fileContent,+ theme: "eclipse",+ indentWithTabs: indentType == "Tabs",+ indentUnit: tabSize,+ lineNumbers: true,+ lineWrapping: lineWrapMode == "Soft wrap",+ tabSize: tabSize,+ styleActiveLine: true,+ styleSelectedText: true,+ foldGutter: true,+ matchBrackets: true,+ scrollbarStyle: "simple",+ highlightIdentifiers: {delay: 500},+ extraKeys: {+ "F11": function(cm) {+ cm.setOption("fullScreen", !cm.getOption("fullScreen"));+ },+ "Esc": function(cm) {+ if (cm.getOption("fullScreen"))+ cm.setOption("fullScreen", false);+ }+ }+ });++ cm.oldDocValue = cm.doc.getValue();+ cm.on("change", function() {+ var $form = $body.find(">form.edit");+ if (cm.doc.getValue() != cm.oldDocValue) {+ $form.addClass("dirty");+ $fileEdit.data("contentChanged", true);+ } else {+ $form.removeClass("dirty");+ $fileEdit.data("contentChanged", false);+ }+ });+ gitplex.commons.codemirror.setMode(cm, filePath);++ if (mark)+ gitplex.commons.codemirror.mark(cm, mark);+ }++ if ($edit.is(":visible")) {+ $body.css("overflow", "hidden");+ $edit.outerHeight($body.height());+ $edit.outerWidth($body.width());+ if (cm.getOption("fullScreen"))+ cm.setOption("fullScreen", false);+ cm.setSize($edit.width(), $edit.height());+ if (initState)+ gitplex.commons.codemirror.initState(cm, viewState);+ } else {+ $body.css("overflow", "auto");+ }+ });+ },+ save: function(containerId) {+ gitplex.fileedit.selectTab($("#" + containerId + ">.file-edit>.head>.save"));+ },+ preview: function(containerId) {+ gitplex.fileedit.selectTab($("#" + containerId + ">.file-edit>.head>.preview"));+ },+ setMode: function(containerId, filePath) {+ var cm = $("#"+ containerId + ">.file-edit>.body>div.edit>.CodeMirror")[0].CodeMirror;+ gitplex.commons.codemirror.setMode(cm, filePath);+ },+ mark: function(containerId, mark) {+ var cm = $("#"+ containerId + ">.file-edit>.body>div.edit>.CodeMirror")[0].CodeMirror;+ if (cm.oldDocValue) {+ var dmp = new diff_match_patch();+ var diffs = dmp.diff_main(cm.oldDocValue, cm.doc.getValue());++ var beginLine = mark.beginLine, beginChar = mark.beginChar;+ var endLine = mark.endLine, endChar = mark.endChar;+ var newBeginLine, newBeginChar, newEndLine, newEndChar;+ var oldLine = oldChar = newLine = newChar = 0;+ for (var i=0; i<diffs.length; i++) {+ var diff = diffs[i];+ var chars = diff[1];+ var quit = false;+ for (var j=0; j<chars.length; j++) {+ var char = chars[j];+ if (diff[0] == -1) {+ if (char == '\n') {+ oldLine++;+ oldChar = 0;+ } else {+ oldChar++;+ }+ } else if (diff[0] == 1) {+ if (char == '\n') {+ newLine++;+ newChar = 0;+ } else {+ newChar++;+ }+ } else {+ if (oldLine == beginLine && oldChar == beginChar) {+ newBeginLine = newLine;+ newBeginChar = newChar;+ }+ if (char == '\n') {+ oldLine++;+ oldChar = 0;+ newLine++;+ newChar = 0;+ } else {+ oldChar++;+ newChar++;+ }+ if (oldLine == endLine && oldChar == endChar) {+ newEndLine = newLine;+ newEndChar = newChar;+ }+ }+ if (oldLine>beginLine && newBeginLine==undefined || oldLine>endLine) {+ quit = true;+ break;+ }+ }+ if (quit)+ break;+ }+ }++ if (newBeginLine!=undefined && newEndLine!=undefined) {+ var newMark = {+ beginLine: newBeginLine,+ beginChar: newBeginChar,+ endLine: newEndLine,+ endChar: newEndChar+ };+ gitplex.commons.codemirror.mark(cm, newMark);+ }+ },+ selectTab: function($tab) {+ var $active = $tab.parent().find("a.tab.active");+ $active[0].hideableVisible = $(".hideable").is(":visible");+ $active.removeClass("active");+ $tab.addClass("active");+ if ($tab[0].hideableVisible != undefined) {+ if ($tab[0].hideableVisible)+ $(".hideable").show();+ else+ $(".hideable").hide();+ }+ var $fileEdit = $tab.closest(".file-edit");+ var $body = $fileEdit.find(">.body");++ var $cm = $fileEdit.find(">.body>div.edit>.CodeMirror");+ if ($cm.is(":visible")) {+ var cm = $cm[0].CodeMirror;+ $fileEdit.find(">.body>div.save").data("viewState", gitplex.commons.codemirror.getViewState(cm));+ }++ $body.find(">div").hide();+ if ($tab.hasClass("edit")) {+ $body.find(">div.edit").show().find(">.CodeMirror")[0].CodeMirror.focus();+ } else if ($tab.hasClass("preview")) {+ $body.find(">div.preview").show();+ } else {+ $body.find(">div.save").show();+ $body.find(">div.save>.edit-save").trigger("contentEdit", [$fileEdit.data("contentChanged")]);+ }+ $(window).resize();+ },+ onIndentTypeChange: function(containerId, indentType) {+ var cm = $("#"+ containerId + ">.file-edit>.body>div.edit>.CodeMirror")[0].CodeMirror;+ cm.setOption("indentWithTabs", indentType == "Tabs");+ },+ onLineWrapModeChange: function(containerId, lineWrapMode) {+ var cm = $("#"+ containerId + ">.file-edit>.body>div.edit>.CodeMirror")[0].CodeMirror;+ cm.setOption("lineWrapping", lineWrapMode == "Soft wrap");+ },+ onTabSizeChange: function(containerId, tabSize) {+ var cm = $("#"+ containerId + ">.file-edit>.body>div.edit>.CodeMirror")[0].CodeMirror;+ cm.setOption("tabSize", tabSize);+ }+};diff --git a/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/filelist/file-list.js b/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/filelist/file-list.jsindex 15913b1924..fbf987f2eb 100644--- a/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/filelist/file-list.js+++ b/gitplex.web/src/main/java/com/gitplex/web/component/depotfile/filelist/file-list.js@@ -8,7 +8,7 @@ gitplex.filelist = {$fileList.outerWidth(width);$fileList.outerHeight(height);- var viewState = pmease.commons.history.getViewState();+ var viewState = gitplex.commons.history.getViewState();if (viewState && viewState.scroll) {$fileList.scrollLeft(viewState.scroll.left);$fileList.scrollTop(viewState.scroll.top);@@ -17,7 +17,7 @@ gitplex.filelist = {$fileList.on("storeViewState", function(e) {e.stopPropagation();- pmease.commons.history.setViewState({scroll:{left: $fileList.scrollLeft(), top: $fileList.scrollTop()}});+ gitplex.commons.history.setViewState({scroll:{left: $fileList.scrollLeft(), top: $fileList.scrollTop()}});});$.ajax({diff --git a/gitplex.web/src/main/java/com/gitplex/web/component/diff/blob/text/text-diff.js b/gitplex.web/src/main/java/com/gitplex/web/component/diff/blob/text/text-diff.jsindex e6b8499c3b..f58addc089 100644--- a/gitplex.web/src/main/java/com/gitplex/web/component/diff/blob/text/text-diff.js+++ b/gitplex.web/src/main/java/com/gitplex/web/component/diff/blob/text/text-diff.js@@ -1,913 +1,913 @@-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, blameMessageCallback,- markSupport, mark, openComment, oldComments, newComments, dirtyContainerId, doclink) {- var $container = $("#" + containerId);- $container.data("dirtyContainerId", dirtyContainerId);- $container.data("callback", callback);- $container.data("blameMessageCallback", blameMessageCallback);- $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("blameMessageCallback")(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);- },- 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, blameMessageCallback,+ markSupport, mark, openComment, oldComments, newComments, dirtyContainerId, doclink) {+ var $container = $("#" + containerId);+ $container.data("dirtyContainerId", dirtyContainerId);+ $container.data("callback", callback);+ $container.data("blameMessageCallback", blameMessageCallback);+ $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 && !gitplex.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("blameMessageCallback")(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);+ },+ 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);+ }+};