// Collectd-web - base.js
// Copyright (C) 2009-2010 Kenneth Belitzky
//
// This program 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; either version 2 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
/**
* Global vars
*/
var $selected_host = '';
var $selected_plugin = '';
var $graph_json = null;
var window_top = 0;
var previous_window_top = 0;
// error free logger taken from:
// http://goo.gl/yG9Gb
var Fb = {}; //An empty object literal for holding the function
Fb.log = function(obj, consoleMethod) {
if (window.console && window.console.firebug && window.console.firebug.replace(/^\s\s*/, '').replace(/\s\s*$/, '') !== '') {
if (typeof consoleMethod === "string" && typeof console[consoleMethod] === "function") {
console[consoleMethod](obj);
} else {
console.log(obj);
}
}
}
var server_tz = 0;
/**
* Returns a current Date object, representing server time.
*/
function server_now() {
var d = new Date(),
local_time = d.getTime(),
local_offset = d.getTimezoneOffset() * 60000,
utc = local_time + local_offset,
server = utc + (3600000 * server_tz);
return new Date(server);
}
/*
* checks to see if the number is negative
*
*/
function is_neg(string) {
var numericExpression = /^-[0-9]+$/;
if(string.match(numericExpression)) {
return true;
} else {
return false;
}
}
/**
* Get the id of the container for the selected element
*
* @param {Object}
* elem
*/
function get_container(elem) {
return $(elem).parent().parent().parent().attr('id');
}
var graph_def_values = [];
function get_gmt(offset) {
off = '';
if (offset == 0) {
off = '';
} else if (offset > 0) {
off = '+' + offset
} else {
off = offset
}
return offset
}
function get_url_params(in_url) {
var params = {};
if (in_url) {
var nurl = in_url.replace(/.*\?(.*?)/, "$1");
variables = nurl.split(";");
for (i = 0; i < variables.length; i++) {
separ = variables[i].split("=");
params[separ[0]] = separ[1];
}
}
Fb.log(params,"info");
return params;
}
function get_timespan_start (timespan) {
var out_date = server_now();
switch (timespan) {
case "hour":
out_date.add(-1).hours();
break;
case "day":
out_date.add(-1).days();
break;
case "week":
out_date.add(-1).weeks();
break;
case "month":
out_date.add(-1).months();
break;
case "year":
out_date.add(-1).years();
break;
}
return out_date;
}
/**
* Build up and url with the given parameters
* @param original_url string with the original url
* @param params hash with all parameters to add to the url
* @return string
*/
function build_url(original_url, new_params) {
Fb.log(new_params, "warning");
params = get_url_params(original_url);
$.extend(params, new_params);
var url = original_url.split('?')[0] + '?';
for (key in params) {
if (params[key] != '' || params[key] == 0) {
url += key + '=' + params[key] + ';';
}
}
Fb.log(url,"info");
return url;
}
function hide_toolbar_items () {
$('.toolbar-item').fadeOut();
}
/**
* default control container
*
* @param {Object}
* txt
*/
function control_container(txt) {
return '
' + txt + '
';
}
/**
* return html structure for the available plugins menu
* @param host
* @param plugins
* @return
*/
function create_plugin_menu(host, plugins) {
var tpl = '
Available Plugins
';
tpl += '
';
for (var p = 0; p < plugins.length; p++) {
tpl += '