diff options
author | slene <vslene@gmail.com> | 2014-03-23 22:14:29 +0800 |
---|---|---|
committer | slene <vslene@gmail.com> | 2014-03-23 22:14:29 +0800 |
commit | f7f175a0793a53f3c50d20d89e324a610f94c442 (patch) | |
tree | 79c1f07e1fb5657cf3cb1e7f5d19b2cc55535c41 /public/js/app.js | |
parent | 809a4eab6774c265c8c07313fa9957c274e52aaf (diff) | |
download | gitea-f7f175a0793a53f3c50d20d89e324a610f94c442.tar.gz gitea-f7f175a0793a53f3c50d20d89e324a610f94c442.zip |
fix code select range
Diffstat (limited to 'public/js/app.js')
-rw-r--r-- | public/js/app.js | 83 |
1 files changed, 70 insertions, 13 deletions
diff --git a/public/js/app.js b/public/js/app.js index 3e995d5b0e..d5185580f6 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -31,6 +31,23 @@ var Gogits = { } }; return ajax(url, options); + }, + + changeHash: function(hash) { + if(history.pushState) { + history.pushState(null, null, hash); + } + else { + location.hash = hash; + } + }, + + deSelect: function() { + if(window.getSelection) { + window.getSelection().removeAllRanges(); + } else { + document.selection.empty(); + } } }); }(jQuery)); @@ -130,27 +147,67 @@ var Gogits = { } Gogits.renderCodeView = function () { + function selectRange($list, $select, $from){ + $list.removeClass('active'); + if($from){ + var a = parseInt($select.attr('rel').substr(1)); + var b = parseInt($from.attr('rel').substr(1)); + var c; + if(a != b){ + if(a > b){ + c = a; + a = b; + b = c; + } + var classes = []; + for(i = a; i <= b; i++) { + classes.push('.L'+i); + } + $list.filter(classes.join(',')).addClass('active'); + $.changeHash('#L' + a + '-' + 'L' + b); + return + } + } + $select.addClass('active'); + $.changeHash('#' + $select.attr('rel')); + } + + $(document).on('click', '.lines-num span', function (e) { + var $select = $(this); + var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li'); + selectRange($list, $list.filter('[rel='+$select.attr('rel')+']'), (e.shiftKey?$list.filter('.active').eq(0):null)); + $.deSelect(); + }); + $('.code-view .lines-code > pre').each(function(){ var $pre = $(this); - var $lineNums = $pre.parent().siblings('.lines-num'); + 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>'); + $lineNums.append('<span id="L' + i + '" rel="L' + i + '">' + i + '</span>'); } - - var last; - $(document).on('click', '.lines-num span', function () { - var $e = $(this); - if (last) { - last.removeClass('active'); - } - last = $e.parent().siblings('.lines-code').find('ol.linenums > ' + $e.attr('rel')); - last.addClass('active'); - window.location.href = '#' + $e.attr('id'); - }); } }); + + $(window).on('hashchange', function(e) { + var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/); + var $list = $('.code-view ol.linenums > li'); + if(m){ + var $first = $list.filter('.'+m[1]); + selectRange($list, $first, $list.filter('.'+m[2])); + $("html, body").scrollTop($first.offset().top-200); + console.log($first.offset().top); + return; + } + m = window.location.hash.match(/^#(L\d+)$/); + if(m){ + var $first = $list.filter('.'+m[1]); + selectRange($list, $first); + $("html, body").scrollTop($first.offset().top-200); + } + }).trigger('hashchange'); }; })(jQuery); |