aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndy Green <andy@warmcat.com>2018-06-23 06:07:45 +0800
committerAndy Green <andy@warmcat.com>2018-06-29 14:30:23 +0800
commit1915feb07b7460009fce4a5ac97b2aa45b45c338 (patch)
tree18ad0ccc5b5ea039a3c12326aeaf0285d6dec2bd
parentcgit.js: line range highlight: introduce javascript (diff)
downloadcgit-1915feb07b7460009fce4a5ac97b2aa45b45c338.tar.xz
cgit-1915feb07b7460009fce4a5ac97b2aa45b45c338.zip
cgit.js: line range highlight: make responsive to url changes
Browser default interpretation of the # part of the URL is to try to match it to an element ID one time at page load. Subsequent modifications in the URL bar are ignored, even if you hit enter there. This patch makes the line range highlight able to clean up after itself and be reapplied, and has the highlight function listen for hashchange events and re-interpret the # part when they occur. Now if you edit the URL and press enter, any existing highlight is removed, the # part reinterpreted and the new highlight applied automatically. This is particularly useful if you edit the # link with the intention to show a range before copying the URL. Clicking on the line number, which changes the URL bar to have a corresponding #n part, also triggers hashchange and makes the clicked line be highlit immediately. Signed-off-by: Andy Green <andy@warmcat.com>
-rw-r--r--cgit.js17
1 files changed, 17 insertions, 0 deletions
diff --git a/cgit.js b/cgit.js
index 7c034e3..6cc27c1 100644
--- a/cgit.js
+++ b/cgit.js
@@ -33,6 +33,18 @@ function line_range_highlight()
{
var h = window.location.hash, l1 = 0, l2 = 0, e, t;
+ e = document.getElementById("cgit-line-range");
+ if (e) {
+ l1 = e.l1;
+ while (l1 <= e.l2) {
+ var e1;
+ e1 = document.getElementById('n' + l1++);
+ e1.style.backgroundColor = null;
+ }
+
+ e.remove();
+ }
+
l1 = parseInt(h.substring(2));
if (!l1)
return;
@@ -56,6 +68,7 @@ function line_range_highlight()
de.className = "selected-lines";
de.style.bottom = e.style.bottom;
de.style.top = collect_offsetTop(e) + 'px';
+ de.id = "cgit-line-range";
de.l1 = l1;
de.l2 = l2;
@@ -89,4 +102,8 @@ window.addEventListener("load", function() {
line_range_highlight();
}, false);
+window.addEventListener("hashchange", function() {
+ line_range_highlight();
+}, false);
+
})();