2010-09-27 01:33:49 +02:00
|
|
|
/**
|
|
|
|
* Javascript functions for the frontend
|
2010-09-28 18:17:45 +02:00
|
|
|
*
|
2010-09-27 01:33:49 +02:00
|
|
|
* @author Florian Ziegler <fz@f10-home.de>
|
|
|
|
* @author Justin Otherguy <justin@justinotherguy.org>
|
|
|
|
* @author Steffen Vogel <info@steffenvogel.de>
|
|
|
|
* @copyright Copyright (c) 2010, The volkszaehler.org project
|
|
|
|
* @package default
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
|
|
*/
|
2010-06-06 16:05:46 +02:00
|
|
|
/*
|
2010-09-27 01:33:49 +02:00
|
|
|
* This file is part of volkzaehler.org
|
2010-09-28 18:17:45 +02:00
|
|
|
*
|
|
|
|
* volkzaehler.org 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 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* volkzaehler.org 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
|
|
|
|
* volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
|
2010-09-27 01:33:49 +02:00
|
|
|
*/
|
2010-06-06 16:05:46 +02:00
|
|
|
|
2010-09-29 21:59:49 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Frontend related functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refresh plot with new data
|
|
|
|
*/
|
2010-10-01 11:23:31 +02:00
|
|
|
function refreshWindow() {
|
|
|
|
if ($('input[name=refresh]').attr('checked')) {
|
|
|
|
loadData();
|
2010-09-28 18:17:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-25 18:18:08 +02:00
|
|
|
function moveWindow(mode) {
|
2010-09-28 15:44:28 +02:00
|
|
|
delta = myWindowEnd - myWindowStart;
|
2010-09-25 18:18:08 +02:00
|
|
|
|
|
|
|
if(mode == 'last')
|
|
|
|
myWindowEnd = (new Date()).getTime();
|
2010-09-28 15:44:28 +02:00
|
|
|
myWindowStart = myWindowEnd - delta;
|
2010-09-25 18:18:08 +02:00
|
|
|
if(mode == 'back') {
|
2010-09-28 15:44:28 +02:00
|
|
|
myWindowStart -= delta;
|
|
|
|
myWindowEnd -= delta;
|
2010-09-25 18:18:08 +02:00
|
|
|
}
|
|
|
|
if(mode == 'forward') {
|
2010-09-28 15:44:28 +02:00
|
|
|
myWindowStart += delta;
|
|
|
|
myWindowEnd += delta;
|
2010-09-25 18:18:08 +02:00
|
|
|
}
|
|
|
|
|
2010-10-01 11:23:31 +02:00
|
|
|
loadData();
|
2010-09-25 18:18:08 +02:00
|
|
|
}
|
|
|
|
|
2010-10-01 11:23:31 +02:00
|
|
|
//load json data with given time window
|
|
|
|
function loadData() {
|
|
|
|
eachRecursive(entities, function(entity, parent) {
|
|
|
|
if (entity.active && entity.type != 'group') {
|
|
|
|
$.getJSON(backendUrl + '/data/' + entity.uuid + '.json', { from: myWindowStart, to: myWindowEnd, tuples: tuples }, ajaxWait(function(json) {
|
|
|
|
entity.data = json.data[0]; // TODO filter for correct uuid
|
|
|
|
}, showChart, 'data'));
|
|
|
|
}
|
2010-09-25 18:18:08 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function showChart() {
|
|
|
|
var jqData = new Array();
|
|
|
|
|
2010-10-01 11:23:31 +02:00
|
|
|
eachRecursive(entities, function(entity, parent) {
|
|
|
|
jqData.push(entity.data.tuples);
|
2010-09-28 15:44:28 +02:00
|
|
|
});
|
2010-09-25 18:18:08 +02:00
|
|
|
|
2010-09-28 18:17:45 +02:00
|
|
|
// TODO read docs
|
2010-09-28 15:44:28 +02:00
|
|
|
chart = $.jqplot('plot', jqData, jqOptions);
|
|
|
|
chart.replot({
|
|
|
|
clear: true,
|
|
|
|
resetAxes: true
|
|
|
|
});
|
2010-09-29 21:59:49 +02:00
|
|
|
}
|
|
|
|
|
2010-10-01 23:06:40 +02:00
|
|
|
/*
|
|
|
|
* Cookie & UUID related functions
|
|
|
|
*/
|
|
|
|
function getUUIDs() {
|
|
|
|
if ($.getCookie('uuids')) {
|
|
|
|
return JSON.parse($.getCookie('uuids'));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return new Array;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addUUID(uuid) {
|
|
|
|
if (!uuids.contains(uuid)) {
|
|
|
|
uuids.push(uuid);
|
|
|
|
$.setCookie('uuids', JSON.stringify(uuids));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeUUID(uuid) {
|
|
|
|
if (uuids.contains(uuid)) {
|
|
|
|
uuids.filter(function(value) {
|
|
|
|
return value != uuid;
|
|
|
|
});
|
|
|
|
$.setCookie('uuids', JSON.stringify(uuids));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-29 21:59:49 +02:00
|
|
|
/*
|
|
|
|
* Entity list related functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-10-01 11:23:31 +02:00
|
|
|
* Get all entity information from backend
|
2010-09-29 21:59:49 +02:00
|
|
|
*/
|
2010-10-01 11:23:31 +02:00
|
|
|
function loadEntities(uuids) {
|
2010-09-29 21:59:49 +02:00
|
|
|
$.each(uuids, function(index, value) {
|
2010-10-01 11:23:31 +02:00
|
|
|
$.getJSON(backendUrl + '/entity/' + value + '.json', ajaxWait(function(json) {
|
|
|
|
entities.push(json.entity);
|
|
|
|
}, showEntities, 'information'));
|
2010-09-29 21:59:49 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create nested entity list
|
|
|
|
* @param data
|
|
|
|
*/
|
2010-10-01 11:23:31 +02:00
|
|
|
function showEntities() {
|
2010-09-29 21:59:49 +02:00
|
|
|
$('#entities tbody').empty();
|
|
|
|
|
2010-10-01 11:23:31 +02:00
|
|
|
var i = 0;
|
|
|
|
|
|
|
|
eachRecursive(entities, function(entity, parent) {
|
|
|
|
entity.active = true; // TODO active by default or via backend property?
|
|
|
|
entity.color = colors[i++%colors.length];
|
2010-09-29 21:59:49 +02:00
|
|
|
|
2010-10-01 11:23:31 +02:00
|
|
|
$('#entities tbody').append(
|
|
|
|
$('<tr>')
|
|
|
|
.addClass((parent) ? 'child-of-entity-' + parent.uuid : '')
|
|
|
|
.attr('id', 'entity-' + entity.uuid)
|
|
|
|
.append(
|
|
|
|
$('<td>').append(
|
|
|
|
$('<span>')
|
|
|
|
.addClass((entity.type == 'group') ? 'group' : 'channel')
|
|
|
|
.attr('title', entity.uuid)
|
|
|
|
.text(entity.title)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.append($('<td>').text(entity.type))
|
|
|
|
.append($('<td>') // operations
|
|
|
|
.append($('<input>')
|
|
|
|
.attr('type', 'image')
|
|
|
|
.attr('src', 'images/delete.png')
|
|
|
|
.attr('alt', 'delete')
|
2010-10-01 23:06:40 +02:00
|
|
|
.bind('click', entity, function(event) { removeUUID(event.data.uuid); })
|
2010-10-01 11:23:31 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
.append($('<td>')
|
|
|
|
.append($('<div>')
|
|
|
|
.css('background-color', entity.color)
|
|
|
|
.addClass('indicator')
|
|
|
|
.append($('<input>')
|
|
|
|
.attr('type', 'checkbox')
|
|
|
|
.attr('checked', entity.active)
|
|
|
|
.bind('change', entity, function(event) {
|
|
|
|
event.data.active = $(this).attr('checked');
|
|
|
|
loadData();
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2010-09-29 21:59:49 +02:00
|
|
|
});
|
|
|
|
|
2010-10-01 11:23:31 +02:00
|
|
|
// http://ludo.cubicphuse.nl/jquery-plugins/treeTable/doc/index.html
|
|
|
|
$('#entities table').treeTable();
|
2010-09-29 21:59:49 +02:00
|
|
|
|
2010-10-01 11:23:31 +02:00
|
|
|
// load data and show plot
|
|
|
|
loadData();
|
2010-09-29 21:59:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* General helper functions
|
|
|
|
*/
|
|
|
|
|
2010-10-01 11:23:31 +02:00
|
|
|
function ajaxWait(callback, finished, identifier) {
|
|
|
|
if (!ajaxWait.counter) { ajaxWait.counter = new Array(); }
|
|
|
|
if (!ajaxWait.counter[identifier]) { ajaxWait.counter[identifier] = 0; }
|
2010-09-29 21:59:49 +02:00
|
|
|
|
|
|
|
ajaxWait.counter[identifier]++;
|
|
|
|
|
|
|
|
return function (data, textStatus) {
|
2010-10-01 11:23:31 +02:00
|
|
|
callback(data, textStatus);
|
2010-09-29 21:59:49 +02:00
|
|
|
|
|
|
|
if (!--ajaxWait.counter[identifier]) {
|
2010-10-01 11:23:31 +02:00
|
|
|
finished();
|
2010-09-29 21:59:49 +02:00
|
|
|
}
|
|
|
|
};
|
2010-10-01 11:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function eachRecursive(array, callback, parent) {
|
|
|
|
$.each(array, function(index, value) {
|
|
|
|
callback(value, parent);
|
|
|
|
|
|
|
|
if (value.children) { // has children?
|
|
|
|
eachRecursive(value.children, callback, value); // call recursive
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Array.prototype.contains = function(needle) {
|
|
|
|
for (var i=0; i<this.length; i++) {
|
|
|
|
if (this[i] == needle) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
Array.prototype.diff = function(compare) {
|
|
|
|
return this.filter(function(elem) {
|
|
|
|
return !compare.contains(elem);
|
|
|
|
});
|
|
|
|
};
|