[checkmk-commits] Check_MK Git: check_mk: OMG! Found a way to make F5 / browser reloads work as expected

Lars Michelsen lm at mathias-kettner.de
Wed Sep 18 10:07:23 CEST 2013


Module: check_mk
Branch: master
Commit: a0f74fe9c4c647012d4298bb8192330565ab9fcf
URL:    http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=a0f74fe9c4c647012d4298bb8192330565ab9fcf

Author: Lars Michelsen <lm at mathias-kettner.de>
Date:   Wed Sep 18 10:05:46 2013 +0200

OMG! Found a way to make F5 / browser reloads work as expected

This works at least in current firefox, chrome, safari. Cross fingers,
maybe this also works in current IE versions... Have not tested this
yet.

---

 ChangeLog                |    2 ++
 web/htdocs/js/sidebar.js |   43 ++++++++++++++++++++++++++++++++++++++-----
 web/htdocs/sidebar.py    |    3 ++-
 3 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index a2b71c0..895d1c3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -108,6 +108,8 @@
     * HTML notifications: Added time of state change
 
     Multisite:
+    * Finally good handling of F5 / browser reloads -> no page switching to
+      start page anymore (at least in modern browsers)
     * User accounts can now be locked after a specified amount of auth
       failures (lock_on_logon_failures can be set to a number of tries)
     * Column Perf-O-Meter is now sortable: it sorts after the *first*
diff --git a/web/htdocs/js/sidebar.js b/web/htdocs/js/sidebar.js
index 5385dac..5de212b 100644
--- a/web/htdocs/js/sidebar.js
+++ b/web/htdocs/js/sidebar.js
@@ -26,9 +26,8 @@ var browser         = navigator.userAgent.toLowerCase();
 var weAreIEF__k     = ((browser.indexOf("msie") != -1) && (browser.indexOf("opera") == -1));
 var weAreOpera      = browser.indexOf("opera") != -1;
 var weAreFirefox    = browser.indexOf("firefox") != -1 || browser.indexOf("namoroka") != -1;
-var contentLocation = null;
-if(contentFrameAccessible())
-    var contentLocation = parent.frames[1].document.location;
+var g_orig_title    = null;
+var g_content_loc   = null;
 
 //
 // Sidebar styling and scrolling stuff
@@ -360,6 +359,40 @@ function contentFrameAccessible() {
     }
 }
 
+function update_content_location() {
+    // init the original frameset title
+    if (g_orig_title == null)
+        g_orig_title = window.parent.document.title;
+
+    var content_frame = window.parent.frames[1];
+
+    // Change the title to add the right frame title to reflect the
+    // title of the content URL in the framesets title (window title or tab title)
+    if (content_frame.document.title != '') {
+        var page_title = g_orig_title + ' - ' + content_frame.document.title;
+    } else {
+        var page_title = g_orig_title;
+    }
+    window.parent.document.title = page_title;
+
+    // Construct the URL to be called on page reload
+    var parts = content_frame.location.pathname.split('/')
+    parts.pop();
+    var cmk_path = parts.join('/');
+    var rel_url = content_frame.location.pathname + content_frame.location.search + content_frame.location.hash
+    var index_url = cmk_path + '/index.py?start_url=' + rel_url
+
+    if (rel_url && rel_url != 'blank') {
+        // Update the URL to be called on reload, e.g. via F5, to make the
+        // frameset switch to exactly this URL
+        window.parent.history.replaceState({}, page_title, index_url);
+
+        // only update the internal flag var if the url was not blank and has been updated
+        //otherwise try again on next scheduler run
+        g_content_loc = parent.frames[1].document.location.href;
+    }
+}
+
 function debug(s) {
   window.parent.frames[1].document.write(s+'<br />');
 }
@@ -662,9 +695,9 @@ function sidebar_scheduler() {
 
     // Detect page changes and re-register the mousemove event handler
     // in the content frame. another bad hack ... narf
-    if (contentFrameAccessible() && contentLocation != parent.frames[1].document.location) {
+    if(contentFrameAccessible() && g_content_loc != parent.frames[1].document.location.href) {
         registerEdgeListeners(parent.frames[1]);
-        contentLocation = parent.frames[1].document.location;
+        update_content_location();
     }
     setTimeout(function(){sidebar_scheduler();}, 1000);
 }
diff --git a/web/htdocs/sidebar.py b/web/htdocs/sidebar.py
index 129ef6c..e653ee1 100644
--- a/web/htdocs/sidebar.py
+++ b/web/htdocs/sidebar.py
@@ -219,7 +219,8 @@ def page_side():
     html.write("refresh_snapins = %r;\n" % refresh_snapins)
     html.write("restart_snapins = %r;\n" % restart_snapins)
     html.write("sidebar_scheduler();\n")
-    html.write("window.onresize = function() { setSidebarHeight(); }\n")
+    html.write("window.onresize = function() { setSidebarHeight(); };\n")
+    html.write("if (contentFrameAccessible()) { update_content_location(); };\n")
     html.write("</script>\n")
 
     html.write("</body>\n</html>")



More information about the checkmk-commits mailing list