diff --git a/dxr/app.py b/dxr/app.py index 0dd7c5176..d108faf79 100644 --- a/dxr/app.py +++ b/dxr/app.py @@ -493,12 +493,23 @@ def _browse_file(tree, path, line_docs, file_doc, config, is_binary, :arg image_rev: revision number of a textual or binary image, for images displayed at a certain rev """ + def process_link_templates(sections): + """Look for {{line}} in the links of given sections, and duplicate them onto + a 'template' field. + """ + for section in sections: + for link in section['items']: + if '{{line}}' in link['href']: + link['template'] = link['href'] + link['href'] = link['href'].replace('{{line}}', '') + def sidebar_links(sections): """Return data structure to build nav sidebar from. :: [('Section Name', [{'icon': ..., 'title': ..., 'href': ...}])] """ + process_link_templates(sections) # Sort by order, resolving ties by section name: return sorted(sections, key=lambda section: (section['order'], section['heading'])) diff --git a/dxr/indexers.py b/dxr/indexers.py index 00e30b232..e5ca357e3 100644 --- a/dxr/indexers.py +++ b/dxr/indexers.py @@ -238,6 +238,9 @@ def links(self): (sort order, heading, [(icon, title, href), ...]) + File views will replace any {{line}} within the href with the + last-selected line number. + """ return [] diff --git a/dxr/static_unhashed/js/code-highlighter.js b/dxr/static_unhashed/js/code-highlighter.js index 370642ae8..7fe3cb38f 100644 --- a/dxr/static_unhashed/js/code-highlighter.js +++ b/dxr/static_unhashed/js/code-highlighter.js @@ -7,13 +7,15 @@ * 1) Multi-select highlight lines with shift key and update window.location.hash * 2) Multi-select highlight lines with command/control key and update window.location.hash * 3) Highlight lines when page loads, if window.location.hash exists - * In addition, we update the permalink link to keep it synchronized with window.location. + * In addition, we update the permalink and other nav links to keep them + * synchronized with window.location. */ $(function () { 'use strict'; var container = $('#line-numbers'), - permalink = $('.permalink'), // whenever we update window.location, update this href too + navlinks = $('.panel a'), // whenever we update window.location, maybe update these too + permalink = $('.permalink'), // a subset of navlinks, but it has more specific update rules lastModifierKey = null, // use this as a sort of canary/state indicator showing the last user action singleLinesArray = [], //track single highlighted lines here rangesArray = []; // track ranges of highlighted lines here @@ -86,6 +88,7 @@ $(function () { var selectedArray = generateSelectedArrays(); // generates sorted arrays var singleLinesArray = selectedArray[0]; var rangesArray = selectedArray[1]; + var firstNumber; // eliminate duplication for (s = 0; s < singleLinesArray.length; s++) { for (r = 0; r < rangesArray.length; r++) { @@ -103,22 +106,29 @@ $(function () { // if no singleLines left or range < singleLine add range to hash if ((r == rangesArray.length) || (singleLinesArray[s] < rangesArray[r][0])) { windowHash += singleLinesArray[s] + ','; + if (!firstNumber) { + firstNumber = singleLinesArray[s]; + } s++; } else if (( s == singleLinesArray.length) || (rangesArray[r][0] < singleLinesArray[s])) { windowHash += rangesArray[r][0] + '-' + rangesArray[r][1] + ','; + if (!firstNumber) { + firstNumber = rangesArray[r][0]; + } r++; } } if (windowHash) { windowHash = windowHash.replace(reCleanup, ''); - updateHash(windowHash); + updateHash(windowHash, firstNumber); } } - //update places where hash location is used: window, permalink - function updateHash(hash) { + //update places where hash location is used: window, permalink, other nav links + function updateHash(hash, lineNumber) { if (permalink.length > 0) updatePermalink(hash); + updateNavLinks(lineNumber); history.replaceState(null, '', hash); } @@ -132,6 +142,18 @@ $(function () { permalink.attr('href', permalink_href + windowHash); } + //replace any occurrence of {{line}} in hrefs with the last-selected line. + //unless the last-selected line is undefined, then remove {{line}} from the + //displayed url. + function updateNavLinks(lineNumber) { + navlinks.each(function() { + const $this = $(this); + if ($this.data('template')) { + $this.attr('href', $this.data('template').replace(/{{line}}/g, lineNumber || '')); + } + }); + } + //parse window.location.hash on new requests into two arrays //one of single lines and one multilines //use with singleLinesArray and rangesArray for adding/changing new highlights diff --git a/dxr/templates/file.html b/dxr/templates/file.html index 0cf7a39ff..336ff9042 100644 --- a/dxr/templates/file.html +++ b/dxr/templates/file.html @@ -37,7 +37,7 @@