From ef88605caa8a0f9047edc7d46198912818fe8239 Mon Sep 17 00:00:00 2001 From: Daniel Dourvaris Date: Wed, 17 Oct 2018 13:40:45 +0200 Subject: [PATCH] diffs: fixed selecting active lines in both types of diffs. --- rhodecode/public/css/code-block.less | 5 +- rhodecode/public/js/src/rhodecode.js | 120 ++++++++++++++------------- 2 files changed, 67 insertions(+), 58 deletions(-) diff --git a/rhodecode/public/css/code-block.less b/rhodecode/public/css/code-block.less index 9ceb7dc9..375657d8 100644 --- a/rhodecode/public/css/code-block.less +++ b/rhodecode/public/css/code-block.less @@ -1132,6 +1132,9 @@ table.cb { .icon-comment { cursor: pointer; } + &.cb-line-selected { + background: @comment-highlight-color !important; + } &.cb-line-selected > div { display: block; background: @comment-highlight-color !important; @@ -1154,7 +1157,7 @@ table.cb { a::before { content: attr(data-line-no); } - &.cb-line-selected a { + &.cb-line-selected { background: @comment-highlight-color !important; } diff --git a/rhodecode/public/js/src/rhodecode.js b/rhodecode/public/js/src/rhodecode.js index 708407c2..b0ca809b 100644 --- a/rhodecode/public/js/src/rhodecode.js +++ b/rhodecode/public/js/src/rhodecode.js @@ -295,75 +295,81 @@ $(document).ready(function() { }); $('body').on('click', '.cb-lineno a', function(event) { + function sortNumber(a,b) { + return a - b; + } - function sortNumber(a,b) { - return a - b; - } + var lineNo = $(this).data('lineNo'); + var lineName = $(this).attr('name'); - var lineNo = $(this).data('lineNo'); - if (lineNo) { - - // on shift, we do a range selection, if we got previous line - var prevLine = $('.cb-line-selected a').data('lineNo'); - if (event.shiftKey && prevLine !== undefined) { - var prevLine = parseInt(prevLine); - var nextLine = parseInt(lineNo); - var pos = [prevLine, nextLine].sort(sortNumber); - var anchor = '#L{0}-{1}'.format(pos[0], pos[1]); - - } else { - - var nextLine = parseInt(lineNo); - var pos = [nextLine, nextLine]; - var anchor = '#L{0}'.format(pos[0]); - - } - // highlight - var range = []; - for (var i = pos[0]; i <= pos[1]; i++) { - range.push(i); - } - // clear selection - $('.cb-line-selected').removeClass('cb-line-selected'); - - $.each(range, function (i, lineNo) { - var line_td = $('td.cb-lineno#L' + lineNo); - if (line_td.length) { - line_td.addClass('cb-line-selected'); // line number td - line_td.prev().addClass('cb-line-selected'); // line data - line_td.next().addClass('cb-line-selected'); // line content - } - }); + if (lineNo) { + var prevLine = $('.cb-line-selected a').data('lineNo'); + // on shift, we do a range selection, if we got previous line + if (event.shiftKey && prevLine !== undefined) { + var prevLine = parseInt(prevLine); + var nextLine = parseInt(lineNo); + var pos = [prevLine, nextLine].sort(sortNumber); + var anchor = '#L{0}-{1}'.format(pos[0], pos[1]); + // single click } else { - if ($(this).attr('name') !== undefined) { - // clear selection - $('td.cb-line-selected').removeClass('cb-line-selected'); - var aEl = $(this).closest('td'); - aEl.addClass('cb-line-selected'); - aEl.next('td').addClass('cb-line-selected'); + var nextLine = parseInt(lineNo); + var pos = [nextLine, nextLine]; + var anchor = '#L{0}'.format(pos[0]); + + } + // highlight + var range = []; + for (var i = pos[0]; i <= pos[1]; i++) { + range.push(i); + } + // clear old selected lines + $('.cb-line-selected').removeClass('cb-line-selected'); + + $.each(range, function (i, lineNo) { + var line_td = $('td.cb-lineno#L' + lineNo); + + if (line_td.length) { + line_td.addClass('cb-line-selected'); // line number td + line_td.prev().addClass('cb-line-selected'); // line data + line_td.next().addClass('cb-line-selected'); // line content } + }); + + } else if (lineName !== undefined) { // lineName only occurs in diffs + // clear old selected lines + $('td.cb-line-selected').removeClass('cb-line-selected'); + var anchor = '#{0}'.format(lineName); + var diffmode = templateContext.session_attrs.diffmode || "sideside"; + + if (diffmode === "unified") { + $(this).closest('tr').find('td').addClass('cb-line-selected'); + } else { + var activeTd = $(this).closest('td'); + activeTd.addClass('cb-line-selected'); + activeTd.next('td').addClass('cb-line-selected'); } - // Replace URL without jumping to it if browser supports. - // Default otherwise - if (history.pushState && anchor !== undefined) { - var new_location = location.href.rstrip('#'); - if (location.hash) { - // location without hash - new_location = new_location.replace(location.hash, ""); - } + } - // Make new anchor url - new_location = new_location + anchor; - history.pushState(true, document.title, new_location); - - return false; + // Replace URL without jumping to it if browser supports. + // Default otherwise + if (history.pushState && anchor !== undefined) { + var new_location = location.href.rstrip('#'); + if (location.hash) { + // location without hash + new_location = new_location.replace(location.hash, ""); } - }); + // Make new anchor url + new_location = new_location + anchor; + history.pushState(true, document.title, new_location); + return false; + } + + }); $('.collapse_file').on('click', function(e) { e.stopPropagation();