chiark / gitweb /
doxygen: hide the search by going back only when it makes sense.
authorVladimír Vondruš <mosra@centrum.cz>
Tue, 1 Jan 2019 23:59:18 +0000 (00:59 +0100)
committerVladimír Vondruš <mosra@centrum.cz>
Wed, 2 Jan 2019 21:07:30 +0000 (22:07 +0100)
If we landed directly on an URL with #search, going back would mean the
browser is redirected back to the referrer page, the Speed Dial or
whatever. Do not want. In this case we simply replace the history state
without the #search hash -- there definitely wasn't any other hash we
could return to.

doxygen/search.js

index 05bda57f710d644e808581487cae1b2f8ef268a5..330351a7fd3a29ec3326c966d084c6e0bee5ed81 100644 (file)
@@ -40,6 +40,10 @@ var Search = {
        only after mouse moves */
     mouseMovedSinceLastRender: false,
 
+    /* Whether we can go back in history in order to hide the search box or
+       not. We can't do that if we arrived directly on #search from outside. */
+    canGoBackToHideSearch: false,
+
     init: function(buffer, maxResults) {
         let view = new DataView(buffer);
 
@@ -512,6 +516,7 @@ function updateForSearchVisible() {
 /* istanbul ignore next */
 function showSearch() {
     window.location.hash = '#search';
+    Search.canGoBackToHideSearch = true;
 
     updateForSearchVisible();
     return false;
@@ -519,8 +524,17 @@ function showSearch() {
 
 /* istanbul ignore next */
 function hideSearch() {
-    /* Go back to the previous state (that removes the #search hash) */
-    window.history.back();
+    /* If the search box was opened using showSearch(), we can go back in the
+       history. Otherwise (for example when we landed to #search from a
+       bookmark or another server), going back would not do the right thing and
+       in that case we simply replace the current history state. */
+    if(Search.canGoBackToHideSearch) {
+        Search.canGoBackToHideSearch = false;
+        window.history.back();
+    } else {
+        window.location.hash = '#!';
+        window.history.replaceState('', '', window.location.pathname);
+    }
 
     /* Restore scrollbar, prevent page layout jumps */
     document.body.style.overflow = 'auto';