diff options
Diffstat (limited to 'public/ng/js/gogs.js')
-rw-r--r-- | public/ng/js/gogs.js | 89 |
1 files changed, 86 insertions, 3 deletions
diff --git a/public/ng/js/gogs.js b/public/ng/js/gogs.js index ff38bda9..a6b9753e 100644 --- a/public/ng/js/gogs.js +++ b/public/ng/js/gogs.js @@ -202,6 +202,84 @@ var Gogs = {}; }).trigger('hashchange'); }; + // Render diff view. + Gogs.renderDiffView = function () { + function selectRange($list, $select, $from) { + $list.removeClass('active'); + $list.parents('tr').removeClass('end-selected-line'); + $list.parents('tr').find('td').removeClass('selected-line'); + if ($from) { + var expr = new RegExp(/diff-(\d+)L(\d+)/); + var selectMatches = $select.attr('rel').match(expr) + var fromMatches = $from.attr('rel').match(expr) + var a = parseInt(selectMatches[2]); + var b = parseInt(fromMatches[2]); + var linesIntToStr = {}; + linesIntToStr[a] = selectMatches[2]; + linesIntToStr[b] = fromMatches[2]; + + var c; + if (a != b) { + if (a > b) { + c = a; + a = b; + b = c; + } + $('[rel=diff-'+fromMatches[1]+'L' + linesIntToStr[b] + ']').parents('tr').next().addClass('end-selected-line'); + var $selectedLines = $('[rel=diff-'+fromMatches[1]+'L' + linesIntToStr[a] + ']').parents('tr').nextUntil('.end-selected-line').andSelf(); + $selectedLines.find('td.lines-num > span').addClass('active') + $selectedLines.find('td').addClass('selected-line'); + $.changeHash('#diff-'+fromMatches[1]+'L' + linesIntToStr[a] + '-L' + linesIntToStr[b]); + return + } + } + $select.addClass('active'); + $select.parents('tr').find('td').addClass('selected-line'); + $.changeHash('#' + $select.attr('rel')); + } + + $(document).on('click', '.code-diff .lines-num span', function (e) { + var $select = $(this); + var $list = $select.parent().siblings('.lines-code').parents().find('td.lines-num > span'); + selectRange( + $list, + $list.filter('[rel=' + $select.attr('rel') + ']'), + (e.shiftKey && $list.filter('.active').length ? $list.filter('.active').eq(0) : null) + ); + $.deSelect(); + }); + + $('.code-diff .lines-code > pre').each(function () { + var $pre = $(this); + var $lineCode = $pre.parent(); + var $lineNums = $lineCode.siblings('.lines-num'); + if ($lineNums.length > 0) { + var nums = $pre.find('ol.linenums > li').length; + for (var i = 1; i <= nums; i++) { + $lineNums.append('<span id="L' + i + '" rel="L' + i + '">' + i + '</span>'); + } + } + }); + + $(window).on('hashchange', function (e) { + var m = window.location.hash.match(/^#diff-(\d+)(L\d+)\-(L\d+)$/); + var $list = $('.code-diff td.lines-num > span'); + var $first; + if (m) { + $first = $list.filter('[rel=diff-' + m[1] + m[2] + ']'); + selectRange($list, $first, $list.filter('[rel=diff-' + m[1] + m[3] + ']')); + $("html, body").scrollTop($first.offset().top - 200); + return; + } + m = window.location.hash.match(/^#diff-(\d+)(L\d+)$/); + if (m) { + $first = $list.filter('[rel=diff-' + m[1] + m[2] + ']'); + selectRange($list, $first); + $("html, body").scrollTop($first.offset().top - 200); + } + }).trigger('hashchange'); + }; + // Search users by keyword. Gogs.searchUsers = function (val, $target) { var notEmpty = function (str) { @@ -287,7 +365,12 @@ var Gogs = {}; function initCore() { Gogs.renderMarkdown(); - Gogs.renderCodeView(); + + if ($('.code-diff').length == 0) { + Gogs.renderCodeView(); + } else { + Gogs.renderDiffView(); + } // Switch list. $('.js-tab-nav').click(function (e) { @@ -508,7 +591,7 @@ function initRepoSetting() { $ul.toggleShow(); } }).next().next().find('ul').on("click", 'li', function () { - $('#repo-collaborator').val($(this).text()); + $('#repo-collaborator').val($(this).find('.username').text()); $ul.toggleHide(); }); } @@ -608,7 +691,7 @@ function initTeamMembersList() { $ul.toggleShow(); } }).next().next().find('ul').on("click", 'li', function () { - $('#org-team-members-add').val($(this).text()); + $('#org-team-members-add').val($(this).find('.username').text()); $ul.toggleHide(); }); } |