2
* Copyright (c) 2010 Richard Wall <richard (at) the-moon.net>
2
* Copyright (c) Richard Wall
3
3
* See LICENSE for details.
5
5
* Wrappers and convenience fuctions for working with the javascriptRRD, jQuery,
338
338
* @param rrd {Object} A javascriptrrd.RRDFile
339
339
* @param unit {String} The unit symbol for this data series
340
* @param transformer {Function} A callable which performs a
341
* tranfsformation of the values returned from the RRD file.
341
jarmon.RrdQuery = function(rrd, unit) {
343
jarmon.RrdQuery = function(rrd, unit, transformer) {
343
345
this.unit = unit;
346
if(typeof(transformer) !== 'undefined') {
347
this.transformer = transformer;
349
this.transformer = function(v) {return v;};
346
353
jarmon.RrdQuery.prototype.getData = function(startTimeJs, endTimeJs,
444
451
var timestamp = startRowTime;
445
452
for(i=startRowIndex; i<endRowIndex; i++) {
446
val = rra.getEl(i, dsIndex);
453
val = this.transformer(rra.getEl(i, dsIndex));
447
454
flotData.push([timestamp*1000.0, val]);
448
455
timestamp += step;
480
487
* @param unit {String} The unit suffix of this data eg 'bit/sec'
481
488
* @param downloader {Function} A callable which returns a Deferred and calls
482
489
* back with a javascriptrrd.BinaryFile when it has downloaded.
490
* @param transformer {Function} A callable which performs a
491
* tranfsformation of the values returned from the RRD file.
484
jarmon.RrdQueryRemote = function(url, unit, downloader) {
493
jarmon.RrdQueryRemote = function(url, unit, downloader, transformer) {
486
495
this.unit = unit;
487
this.downloader = downloader || jarmon.downloadBinary;
496
if(typeof(downloader) == 'undefined') {
497
this.downloader = jarmon.downloadBinary;
499
this.downloader = downloader;
501
this.transformer = transformer;
488
503
this.lastUpdate = 0;
489
504
this._download = null;
694
709
var label = recipe.data[j][2];
695
710
var unit = recipe.data[j][3];
711
var transformer = recipe.data[j][4];
697
713
if(typeof(dataDict[rrd]) === 'undefined') {
698
714
dataDict[rrd] = new jarmon.RrdQueryRemote(
699
rrd, unit, this.downloader);
715
rrd, unit, this.downloader, transformer);
701
717
this.addData(label, new jarmon.RrdQueryDsProxy(dataDict[rrd], ds));
1178
1194
this.placeholders = [];
1180
1196
this.$tabBar = $('<ul/>', {'class': 'css-tabs'}).appendTo($tpl);
1182
// Icon and hidden input box for adding new tabs. See event handlers below.
1183
this.$newTabControls = $('<li/>', {
1184
'class': 'newTabControls',
1185
'title': 'Add new tab'
1187
$('<img/>', {src: 'assets/icons/next.gif'}),
1188
$('<input/>', {'type': 'text'}).hide()
1189
).appendTo(this.$tabBar);
1191
1197
this.$tabPanels = $('<div/>', {'class': 'css-panes charts'}).appendTo($tpl);
1192
1198
var tabName, $tabPanel, placeNames;
1193
1199
for(var i=0; i<recipe.length; i++) {
1207
// Show the new tab name input box when the user clicks the new tab icon
1208
$('ul.css-tabs > li.newTabControls > img', $tpl[0]).live(
1211
$(this).hide().siblings().show().focus();
1215
// When the "new" tab input loses focus, use its value to create a new
1217
// XXX: Due to event bubbling, this event seems to be triggered twice, but
1218
// only when the input is forcefully blurred by the "keypress" event handler
1219
// below. To prevent two tabs, we blank the input field value. Tried
1220
// preventing event bubbling, but there seems to be some subtle difference
1221
// with the use of jquery live event handlers.
1222
$('ul.css-tabs > li.newTabControls > input', $tpl[0]).live(
1226
var self = e.data.self;
1227
var value = this.value;
1229
$(this).hide().siblings().show();
1233
self.$tabBar.data("tabs").click(value);
1238
// Unfocus the input element when return key is pressed. Triggers a
1239
// blur event which then replaces the input with a tab
1240
$('ul.css-tabs > li > input', $tpl[0]).live(
1243
if(e.which === 13) {
1249
// Show tab name input box when tab is double clicked.
1250
$('ul.css-tabs > li > a', $tpl[0]).live(
1254
var $originalLink = $(this);
1255
var $input = $('<input/>', {
1256
'value': $originalLink.text(),
1257
'name': 'editTabTitle',
1260
$originalLink.replaceWith($input);
1265
// Handle the updating of the tab when its name is edited.
1266
$('ul.css-tabs > li > input[name=editTabTitle]', $tpl[0]).live(
1270
var self = e.data.self;
1271
$(this).replaceWith(
1273
href: ['#', this.value].join('')
1277
self.$tabBar.data("tabs").click(this.value);
1281
$('input[name=add_new_chart]', $tpl[0]).live(
1290
1214
jarmon.TabbedInterface.prototype.newTab = function(tabName) {
1294
1218
).appendTo(this.$tabBar);
1295
1219
var $placeholder = $('<div/>');
1296
1220
// Add tab panel
1299
$('<div/>', {'class': 'tab-controls'}).append(
1302
value: 'Add new chart',
1303
name: 'add_new_chart'
1306
).appendTo(this.$tabPanels);
1221
$('<div/>').append($placeholder).appendTo(this.$tabPanels);
1308
1223
return $placeholder;
1311
1226
jarmon.TabbedInterface.prototype.setup = function() {
1312
this.$newTabControls.remove();
1313
1227
// Destroy then re-initialise the jquerytools tabs plugin
1314
1228
var api = this.$tabBar.data("tabs");
1318
1232
this.$tabBar.tabs(this.$tabPanels.children('div'));
1319
this.$newTabControls.appendTo(this.$tabBar);