Skip to content

Commit

Permalink
Made quicksearch JS code a dedicated module
Browse files Browse the repository at this point in the history
* Moved the needed utility functions to separate modules
  on the way.
* Separated the sidebar JS code from the rest by compiling
  a dedicated side_min.js out of side_index.js.
* Some linter cleanups and minor bug fixes on the way

Change-Id: Ia98dbf92ad9385990ddedb2b0aa1bc9955a20350
  • Loading branch information
LarsMichelsen committed Dec 20, 2018
1 parent 31c930e commit e3e78a9
Show file tree
Hide file tree
Showing 12 changed files with 296 additions and 193 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Expand Up @@ -13,7 +13,8 @@
"strict": 1,
"indent": [
"error",
4
4,
{ "SwitchCase": 1 }
],
"linebreak-style": [
"error",
Expand Down
3 changes: 3 additions & 0 deletions cmk/gui/htmllib.py
Expand Up @@ -1444,6 +1444,9 @@ def add_default_stylesheet(self, name):
if name not in self._default_stylesheets:
self._default_stylesheets.append(name)

def clear_default_javascript(self):
del self._default_javascripts[:]

def add_default_javascript(self, name):
if name not in self._default_javascripts:
self._default_javascripts.append(name)
Expand Down
7 changes: 5 additions & 2 deletions cmk/gui/plugins/sidebar/quicksearch.py
Expand Up @@ -58,11 +58,14 @@ def description(cls):
"<i>hg:</i> Hostgroup, <i>sg:</i> Servicegroup<br><i>ad:</i> Address, <i>al:</i> Alias, <i>tg:</i> Hosttag")

def show(self):
html.open_div(id_="mk_side_search", class_="content_center", onclick="mkSearchClose();")
html.open_div(
id_="mk_side_search", class_="content_center", onclick="cmk.quicksearch.close_popup();")
html.input(id_="mk_side_search_field", type_="text", name="search", autocomplete="off")
html.icon_button("#", _("Search"), "quicksearch", onclick="mkSearchButton();")
html.icon_button(
"#", _("Search"), "quicksearch", onclick="cmk.quicksearch.on_search_click();")
html.close_div()
html.div('', id_="mk_side_clear")
html.javascript("cmk.quicksearch.register_search_field('mk_side_search_field');")

@classmethod
def allowed_roles(cls):
Expand Down
4 changes: 3 additions & 1 deletion cmk/gui/sidebar.py
Expand Up @@ -339,7 +339,9 @@ def show(self):
interval = config.sidebar_notify_interval
else:
interval = 'null'
html.html_head(_("Check_MK Sidebar"), stylesheets=["sidebar", "status"])
html.clear_default_javascript()
html.html_head(
_("Check_MK Sidebar"), javascripts=["side"], stylesheets=["sidebar", "status"])
html.write('<body class="side')
if config.screenshotmode:
html.write(" screenshotmode")
Expand Down
135 changes: 3 additions & 132 deletions web/htdocs/js/checkmk.js
Expand Up @@ -173,28 +173,6 @@ function time() {
return (new Date()).getTime() / 1000;
}

// 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;
}

// Tells the caller whether or not there are graphs on the current page
function has_graphing()
{
Expand Down Expand Up @@ -468,116 +446,6 @@ if (!("lastElementChild" in document.documentElement)) {
//# | AJAX call related functions |
//# '--------------------------------------------------------------------'

function call_ajax(url, optional_args)
{
var args = merge_args({
add_ajax_id : true,
plain_error : false,
response_handler : null,
error_handler : null,
handler_data : null,
method : "GET",
post_data : null,
sync : false
}, optional_args);

var AJAX = window.XMLHttpRequest ? new XMLHttpRequest()
: new ActiveXObject("Microsoft.XMLHTTP");
if (!AJAX)
return null;

// Dynamic part to prevent caching
if (args.add_ajax_id) {
url += url.indexOf('\?') !== -1 ? "&" : "?";
url += "_ajaxid="+Math.floor(Date.parse(new Date()) / 1000);
}

if (args.plain_error) {
url += url.indexOf('\?') !== -1 ? "&" : "?";
url += "_plain_error=1";
}

try {
AJAX.open(args.method, url, !args.sync);
} catch (e) {
if (args.error_handler) {
args.error_handler(args.handler_data, null, e);
return null;
} else {
throw e;
}
}

if (args.method == "POST") {
AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
}

if (!args.sync) {
AJAX.onreadystatechange = function() {
if (AJAX && AJAX.readyState == 4) {
if (AJAX.status == 200) {
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
// 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 (args.error_handler)
args.error_handler(args.handler_data, AJAX.status, AJAX.statusText);
}
}
};
}

AJAX.send(args.post_data);
return AJAX;
}

function get_url(url, handler, data, errorHandler, addAjaxId)
{
var args = {
response_handler: handler
};

if (typeof data !== 'undefined')
args.handler_data = data;

if (typeof errorHandler !== 'undefined')
args.error_handler = errorHandler;

if (typeof addAjaxId !== 'undefined')
args.add_ajax_id = addAjaxId;

call_ajax(url, args);
}

function post_url(url, post_params, responseHandler, handler_data, errorHandler)
{
var args = {
method: "POST",
post_data: post_params
};

if (typeof responseHandler !== 'undefined') {
args.response_handler = responseHandler;
}

if (typeof handler_data !== 'undefined')
args.handler_data = handler_data;

if (typeof errorHandler !== 'undefined')
args.error_handler = errorHandler;

call_ajax(url, args);
}

function bulkUpdateContents(ids, codes)
{
Expand Down Expand Up @@ -929,6 +797,9 @@ function show_pnp_hover_graphs(url)

function handle_check_mk_hover_graphs_response(_unused, code)
{
if (!g_hover_menu)
return;

if (code.indexOf('pnp4nagios') !== -1) {
// fallback to pnp graph handling (received an URL with the previous ajax call)
show_pnp_hover_graphs(code);
Expand Down
16 changes: 10 additions & 6 deletions web/htdocs/js/index.js
Expand Up @@ -22,21 +22,25 @@
// to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
// Boston, MA 02110-1301 USA.

import * as ajax from "ajax";
import * as prediction from "prediction";

require("script-loader!./checkmk.js");
require("script-loader!./dashboard.js");
require("colorpicker");
var prediction = require("prediction");
require("script-loader!./search.js");
require("script-loader!./sidebar.js");
require("script-loader!./wato.js");

// TODO: Find a better solution for this CEE specific include
try {
require("script-loader!../../../enterprise/web/htdocs/js/graphs.js");
} catch(e) {}
} catch(e) {} // eslint-disable-line no-empty

module.exports = {
export default {
get_url: ajax.get_url,
post_url: ajax.post_url,
call_ajax: ajax.call_ajax,
cmk: {
prediction: prediction
prediction: prediction,
ajax: ajax,
}
};
135 changes: 135 additions & 0 deletions web/htdocs/js/modules/ajax.js
@@ -0,0 +1,135 @@
// +------------------------------------------------------------------+
// | ____ _ _ __ __ _ __ |
// | / ___| |__ ___ ___| | __ | \/ | |/ / |
// | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
// | | |___| | | | __/ (__| < | | | | . \ |
// | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
// | |
// | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
// +------------------------------------------------------------------+
//
// This file is part of Check_MK.
// The official homepage is at http://mathias-kettner.de/check_mk.
//
// check_mk is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation in version 2. check_mk is distributed
// in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
// out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public License for more de-
// ails. You should have received a copy of the GNU General Public
// License along with GNU Make; see the file COPYING. If not, write
// to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
// Boston, MA 02110-1301 USA.

import { merge_args } from "utils";

export function get_url(url, handler, data, errorHandler, addAjaxId)
{
var args = {
response_handler: handler
};

if (typeof data !== "undefined")
args.handler_data = data;

if (typeof errorHandler !== "undefined")
args.error_handler = errorHandler;

if (typeof addAjaxId !== "undefined")
args.add_ajax_id = addAjaxId;

call_ajax(url, args);
}

export function post_url(url, post_params, responseHandler, handler_data, errorHandler)
{
var args = {
method: "POST",
post_data: post_params
};

if (typeof responseHandler !== "undefined") {
args.response_handler = responseHandler;
}

if (typeof handler_data !== "undefined")
args.handler_data = handler_data;

if (typeof errorHandler !== "undefined")
args.error_handler = errorHandler;

call_ajax(url, args);
}

export function call_ajax(url, optional_args)
{
var args = merge_args({
add_ajax_id : true,
plain_error : false,
response_handler : null,
error_handler : null,
handler_data : null,
method : "GET",
post_data : null,
sync : false
}, optional_args);

var AJAX = window.XMLHttpRequest ? new window.XMLHttpRequest() : new window.ActiveXObject("Microsoft.XMLHTTP");
if (!AJAX)
return null;

// Dynamic part to prevent caching
if (args.add_ajax_id) {
url += url.indexOf("?") !== -1 ? "&" : "?";
url += "_ajaxid="+Math.floor(Date.parse(new Date()) / 1000);
}

if (args.plain_error) {
url += url.indexOf("?") !== -1 ? "&" : "?";
url += "_plain_error=1";
}

try {
AJAX.open(args.method, url, !args.sync);
} catch (e) {
if (args.error_handler) {
args.error_handler(args.handler_data, null, e);
return null;
} else {
throw e;
}
}

if (args.method == "POST") {
AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
}

if (!args.sync) {
AJAX.onreadystatechange = function() {
if (AJAX && AJAX.readyState == 4) {
if (AJAX.status == 200) {
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
// 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 (args.error_handler)
args.error_handler(args.handler_data, AJAX.status, AJAX.statusText);
}
}
};
}

AJAX.send(args.post_data);
return AJAX;
}

0 comments on commit e3e78a9

Please sign in to comment.