1
Fork 0

clean highlightSourceLines code

This commit is contained in:
Guillaume Gomez 2019-11-04 13:14:36 +01:00
parent cba9368537
commit 1c78af7de2

View file

@ -163,15 +163,39 @@ function getSearchElement() {
var main = document.getElementById("main"); var main = document.getElementById("main");
function highlightSourceLines(ev) { function onHashChange(ev) {
// If we're in mobile mode, we should add the sidebar in any case. // If we're in mobile mode, we should hide the sidebar in any case.
hideSidebar(); hideSidebar();
var elem; var match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
var search = getSearchElement();
var i, from, to, match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
if (match) { if (match) {
from = parseInt(match[1], 10); return highlightSourceLines(match, ev);
to = from; }
var search = getSearchElement();
if (ev !== null && search && !hasClass(search, "hidden") && ev.newURL) {
addClass(search, "hidden");
removeClass(main, "hidden");
var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1);
if (browserSupportsHistoryApi()) {
history.replaceState(hash, "", "?search=#" + hash);
}
var elem = document.getElementById(hash);
if (elem) {
elem.scrollIntoView();
}
}
}
function highlightSourceLines(match, ev) {
if (typeof match === "undefined") {
// If we're in mobile mode, we should hide the sidebar in any case.
hideSidebar();
match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
}
if (!match) {
return;
}
var from = parseInt(match[1], 10);
var to = from;
if (typeof match[2] !== "undefined") { if (typeof match[2] !== "undefined") {
to = parseInt(match[2], 10); to = parseInt(match[2], 10);
} }
@ -180,11 +204,11 @@ function getSearchElement() {
to = from; to = from;
from = tmp; from = tmp;
} }
elem = document.getElementById(from); var elem = document.getElementById(from);
if (!elem) { if (!elem) {
return; return;
} }
if (ev === null) { if (!ev) {
var x = document.getElementById(from); var x = document.getElementById(from);
if (x) { if (x) {
x.scrollIntoView(); x.scrollIntoView();
@ -195,25 +219,13 @@ function getSearchElement() {
removeClass(i_e, "line-highlighted"); removeClass(i_e, "line-highlighted");
}); });
}); });
for (i = from; i <= to; ++i) { for (var i = from; i <= to; ++i) {
elem = document.getElementById(i); elem = document.getElementById(i);
if (!elem) { if (!elem) {
break; break;
} }
addClass(elem, "line-highlighted"); addClass(elem, "line-highlighted");
} }
} else if (ev !== null && search && !hasClass(search, "hidden") && ev.newURL) {
addClass(search, "hidden");
removeClass(main, "hidden");
var hash = ev.newURL.slice(ev.newURL.indexOf("#") + 1);
if (browserSupportsHistoryApi()) {
history.replaceState(hash, "", "?search=#" + hash);
}
elem = document.getElementById(hash);
if (elem) {
elem.scrollIntoView();
}
}
} }
function expandSection(id) { function expandSection(id) {
@ -234,8 +246,8 @@ function getSearchElement() {
} }
} }
highlightSourceLines(null); highlightSourceLines();
window.onhashchange = highlightSourceLines; window.onhashchange = onHashChange;
// Gets the human-readable string for the virtual-key code of the // Gets the human-readable string for the virtual-key code of the
// given KeyboardEvent, ev. // given KeyboardEvent, ev.
@ -358,7 +370,7 @@ function getSearchElement() {
var set_fragment = function(name) { var set_fragment = function(name) {
if (browserSupportsHistoryApi()) { if (browserSupportsHistoryApi()) {
history.replaceState(null, null, "#" + name); history.replaceState(null, null, "#" + name);
highlightSourceLines(null); highlightSourceLines();
} else { } else {
location.replace("#" + name); location.replace("#" + name);
} }