[checkmk-commits] Check_MK Git: check_mk: Customized graph viewport is now maintained during view updates

Lars Michelsen lm at mathias-kettner.de
Fri Apr 17 15:59:55 CEST 2015


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

Author: Lars Michelsen <lm at mathias-kettner.de>
Date:   Fri Apr 17 15:59:47 2015 +0200

Customized graph viewport is now maintained during view updates

---

 web/htdocs/htmllib.py    |    3 +-
 web/htdocs/js/checkmk.js |  176 +++++++++++++++++++++++++++-------------------
 web/htdocs/valuespec.py  |    2 +-
 3 files changed, 105 insertions(+), 76 deletions(-)

diff --git a/web/htdocs/htmllib.py b/web/htdocs/htmllib.py
index 956eaa6..0f5d08f 100644
--- a/web/htdocs/htmllib.py
+++ b/web/htdocs/htmllib.py
@@ -959,8 +959,7 @@ class html:
                 encoded_vars[k] = v
 
             self.popup_trigger(
-                '<img class=statusicon src="images/status_add_dashlet.png" '
-                'title="%s"></a></div>\n' % _("Add this view to..."),
+                '<img class=statusicon src="images/status_add_dashlet.png" title="%s">\n' % _("Add this view to..."),
                 'add_visual', 'add_visual', data='[\'%s\', %s, {\'name\': \'%s\'}]' %
                                                     (mode_name, self.attrencode(repr(encoded_vars)),
                                                      self.var('view_name')))
diff --git a/web/htdocs/js/checkmk.js b/web/htdocs/js/checkmk.js
index 3c149ae..d759765 100644
--- a/web/htdocs/js/checkmk.js
+++ b/web/htdocs/js/checkmk.js
@@ -197,6 +197,34 @@ function rad(g) {
     return (g * 360 / 100 * Math.PI) / 180;
 }
 
+
+// simple implementation of function default arguments when
+// using objects as function parameters. Example:
+// function xxx(args) {
+//     args = merge_args({
+//         'arg2': 'default_val',
+//     });
+// }
+// xxx({
+//   'arg1': 'val1',
+//   'arg3': 'val3',
+// })
+function merge_args()
+{
+    var defaults = arguments[0];
+    var args = arguments[1] || {};
+
+    for (var name in args)
+        defaults[name] = args[name];
+
+    return defaults;
+}
+
+function has_graphing()
+{
+    return typeof g_graphs !== 'undefined';
+}
+
 //#.
 //#   .-Events-------------------------------------------------------------.
 //#   |                    _____                 _                         |
@@ -373,37 +401,43 @@ if (navigator.appVersion.indexOf("MSIE 7.") != -1)
 //#   | AJAX call related functions                                        |
 //#   '--------------------------------------------------------------------'
 
-function get_url(url, handler, data, errorHandler, addAjaxId)
+function call_ajax(url, args)
 {
-    if (window.XMLHttpRequest) {
-        var AJAX = new XMLHttpRequest();
-    } else {
-        var AJAX = new ActiveXObject("Microsoft.XMLHTTP");
-    }
-
-    var addAjaxId = (typeof addAjaxId === "undefined") ? true : addAjaxId;
+    args = merge_args({
+        add_ajax_id      : true,
+        response_handler : null,
+        error_handler    : null,
+        handler_data     : null,
+        method           : "GET",
+        post_data        : null,
+        sync             : false
+    }, args);
+
+    var AJAX = window.XMLHttpRequest ? new XMLHttpRequest()
+                                     : new ActiveXObject("Microsoft.XMLHTTP");
+    if (!AJAX)
+        return null;
 
     // Dynamic part to prevent caching
-    var dyn = '';
-    if (addAjaxId) {
-        dyn = "_ajaxid="+Math.floor(Date.parse(new Date()) / 1000);
-        if (url.indexOf('\?') !== -1) {
-            dyn = "&"+dyn;
-        } else {
-            dyn = "?"+dyn;
-        }
+    if (args.add_ajax_id) {
+        url += url.indexOf('\?') !== -1 ? "&" : "?";
+        url += "_ajaxid="+Math.floor(Date.parse(new Date()) / 1000);
     }
 
-    if (!AJAX) {
-        return null;
+    AJAX.open(args.method, url, !args.sync);
+
+    if (args.method == "POST") {
+        AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
+        AJAX.setRequestHeader("Content-length", args.post_data.length);
+        AJAX.setRequestHeader("Connection", "close");
     }
 
-    AJAX.open("GET", url + dyn, true);
-    if (typeof handler === 'function') {
+    if (!args.sync) {
         AJAX.onreadystatechange = function() {
             if (AJAX && AJAX.readyState == 4) {
                 if (AJAX.status == 200) {
-                    handler(data, AJAX.responseText);
+                    if (args.response_handler)
+                        args.response_handler(args.handler_data, AJAX.responseText);
                 }
                 else if (AJAX.status == 401) {
                     // This is reached when someone is not authenticated anymore
@@ -416,64 +450,41 @@ function get_url(url, handler, data, errorHandler, addAjaxId)
                         document.location.reload();
                 }
                 else {
-                    if (typeof errorHandler !== 'undefined')
-                        errorHandler(data, AJAX.status);
+                    if (args.error_handler)
+                        args.error_handler(args.handler_data, AJAX.status);
                 }
             }
         }
     }
 
-    AJAX.send(null);
+    AJAX.send(args.post_data);
     return AJAX;
 }
 
-function get_url_sync(url)
+function get_url(url, handler, data, errorHandler, addAjaxId)
 {
-    if (window.XMLHttpRequest) {
-        var AJAX = new XMLHttpRequest();
-    } else {
-        var AJAX = new ActiveXObject("Microsoft.XMLHTTP");
-    }
+    call_ajax(url, {
+        response_handler : handler,
+        handler_data     : data,
+        error_handler    : errorHandler,
+        add_ajax_id      : addAjaxId
+    });
+}
 
-    AJAX.open("GET", url, false);
-    AJAX.send(null);
+function get_url_sync(url)
+{
+    var AJAX = call_ajax(url, {sync:true});
     return AJAX.responseText;
 }
 
-function post_url(url, params) {
-    if (window.XMLHttpRequest) {
-        var AJAX = new XMLHttpRequest();
-    } else {
-        var AJAX = new ActiveXObject("Microsoft.XMLHTTP");
-    }
-
-    AJAX.open("POST", url);
-
-    AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
-    AJAX.setRequestHeader("Content-length", params.length);
-    AJAX.setRequestHeader("Connection", "close");
-
-    AJAX.onreadystatechange = function() {
-        if (AJAX && AJAX.readyState == 4) {
-            if (AJAX.status == 401) {
-                // This is reached when someone is not authenticated anymore
-                // but has some webservices running which are still fetching
-                // infos via AJAX. Reload the whole frameset or only the
-                // single page in that case.
-                if(top)
-                    top.location.reload();
-                else
-                    document.location.reload();
-            }
-            else if (AJAX.status != 200) {
-                alert('Error ' + AJAX.status + ' during POST to URL ' + url);
-            }
-        }
-    }
-    AJAX.send(params);
+function post_url(url, params)
+{
+    call_ajax(url, {
+        method: "POST",
+        post_data: params
+    });
 }
 
-
 function bulkUpdateContents(ids, codes)
 {
     var codes = eval(codes);
@@ -601,7 +612,8 @@ function makeuri(addvars, url) {
 // GUI styling
 // ----------------------------------------------------------------------------
 
-function update_togglebutton(id, enabled) {
+function update_togglebutton(id, enabled)
+{
     var on  = document.getElementById(id + '_on');
     var off = document.getElementById(id + '_off');
     if (!on || !off)
@@ -991,7 +1003,7 @@ function handle_content_reload(_unused, code) {
     schedule_reload();
 }
 
-function handle_content_reload_error(data, status_code)
+function handle_content_reload_error(_unused, status_code)
 {
     if(!g_reload_error) {
         var o = document.getElementById('data_container');
@@ -1004,6 +1016,24 @@ function handle_content_reload_error(data, status_code)
     schedule_reload();
 }
 
+function get_content_reload_data()
+{
+    var data = {};
+
+    if (has_graphing())
+        data = merge_args(data, get_modified_graph_contextes());
+
+    // now urlencode the data
+    var params = [];
+    for (var name in data)
+        params.push(name + '=' + encodeURIComponent(data[name]));
+
+    if (params)
+        return params.join('&');
+    else
+        return null;
+}
+
 function do_reload(url)
 {
     // Nicht mehr die ganze Seite neu laden, wenn es ein DIV "data_container" gibt.
@@ -1029,15 +1059,13 @@ function do_reload(url)
             else
                 display_options += opts[i];
         }
-        opts = null;
 
         // Add optional display_options if not defined in original display_options
-        var opts = [ 'w' ];
+        opts = [ 'w' ];
         for (var i = 0; i < opts.length; i++) {
             if (display_options.indexOf(opts[i].toUpperCase()) == -1)
                 display_options += opts[i];
         }
-        opts = null;
 
         var params = {'_display_options': display_options};
         var real_display_options = getUrlParam('display_options');
@@ -1046,10 +1074,12 @@ function do_reload(url)
 
         params['_do_actions'] = getUrlParam('_do_actions')
 
-        var url = makeuri(params);
-        display_options = null;
-        get_url(url, handle_content_reload, '', handle_content_reload_error);
-        url = null;
+        call_ajax(makeuri(params), {
+            response_handler : handle_content_reload,
+            error_handler    : handle_content_reload_error,
+            method           : 'POST',
+            post_data        : get_content_reload_data()
+        });
     }
 }
 
diff --git a/web/htdocs/valuespec.py b/web/htdocs/valuespec.py
index cec47d9..d2b73fd 100644
--- a/web/htdocs/valuespec.py
+++ b/web/htdocs/valuespec.py
@@ -1919,7 +1919,7 @@ class AbsoluteDate(ValueSpec):
 
         if self._include_time:
             if self._show_titles:
-                html.write('<td>&nbsp</td><td>')
+                html.write('<td> </td><td>')
             else:
                 html.write(" ")
             html.number_input(varprefix + "_hour", hour, size=2)



More information about the checkmk-commits mailing list