From 767ad2314525231f0f1e144e391c82f837580612 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Mon, 4 Oct 2010 21:10:58 +0200 Subject: [PATCH] made javascript code object-oriented --- backend/lib/Util/UUID.php | 20 +-- frontend/index.html | 11 +- frontend/javascripts/backend.js | 258 ------------------------------- frontend/javascripts/entity.js | 134 ++++++++++++++++ frontend/javascripts/frontend.js | 141 ++++++++++++++--- frontend/javascripts/helper.js | 64 ++------ frontend/javascripts/init.js | 36 +++-- frontend/javascripts/property.js | 96 ++++++++++++ frontend/javascripts/uuid.js | 67 ++++---- 9 files changed, 423 insertions(+), 404 deletions(-) delete mode 100644 frontend/javascripts/backend.js create mode 100644 frontend/javascripts/entity.js create mode 100644 frontend/javascripts/property.js diff --git a/backend/lib/Util/UUID.php b/backend/lib/Util/UUID.php index 7ed154f..d42b356 100644 --- a/backend/lib/Util/UUID.php +++ b/backend/lib/Util/UUID.php @@ -116,26 +116,12 @@ class UUID { } /** - * Performant validation of UUID's - * - * Replaces preg_match('/[a-f0-9\-]{36}/', $uuid); + * Validation of UUID's * * @param string $uuid - * @param boolen $short whether to allow abbreviated form of UUID's or not */ - public static function validate($uuid, $short = FALSE) { - $len = strlen($uuid); - - for ($i = 0; $i < $len; $i++) { - $char = $uuid[$i]; - $ord = ord($char); - - if (($ord > 57 || $ord < 48) && ($ord > 70 || $ord < 65) && ($ord > 102 || $ord < 97) && $ord != 45) { - return FALSE; // char not allowed - } - } - - return ($short) ? $len <= 36 : $len == 36; // check for strlen + public static function validate($uuid) { + return (boolean) preg_match('/^[0-9a-zA-Z]{8}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{12}$/', $uuid); } /** diff --git a/frontend/index.html b/frontend/index.html index 92b1c4c..32add7a 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -8,6 +8,9 @@ + + + @@ -15,12 +18,10 @@ - - - - - + + + diff --git a/frontend/javascripts/backend.js b/frontend/javascripts/backend.js deleted file mode 100644 index 5982f71..0000000 --- a/frontend/javascripts/backend.js +++ /dev/null @@ -1,258 +0,0 @@ -/** - * Backend related javascript code - * - * @author Florian Ziegler - * @author Justin Otherguy - * @author Steffen Vogel - * @copyright Copyright (c) 2010, The volkszaehler.org project - * @package default - * @license http://opensource.org/licenses/gpl-license.php GNU Public License - */ -/* - * This file is part of volkzaehler.org - * - * 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 . - */ - -/** - * Get all entity information from backend - */ -function loadEntities() { - $.each(vz.uuids, function(index, value) { - $.getJSON(vz.options.backendUrl + '/entity/' + value + '.json', ajaxWait(function(json) { - vz.entities.push(json.entity); - }, showEntities, 'information')); - }); -} - -/** - * Create nested entity list - * @param data - */ -function showEntities() { - $('#entities tbody').empty(); - - var i = 0; - eachRecursive(vz.entities, function(entity, parent) { - entity.active = true; // TODO active by default or via backend property? - entity.color = vz.options.plot.colors[i++ % vz.options.plot.colors.length]; - - var row = $('') - .addClass((parent) ? 'child-of-entity-' + parent.uuid : '') - .attr('id', 'entity-' + entity.uuid) - .append($('') - .css('background-color', entity.color) - .css('width', 19) - .append($('') - .attr('type', 'checkbox') - .attr('checked', entity.active) - .bind('change', entity, function(event) { - event.data.active = $(this).attr('checked'); - loadData(); - }) - ) - ) - .append($('') - .css('width', 20) - ) - .append($('') - .append($('') - .text(entity.title) - .addClass('indicator') - .addClass((entity.type == 'group') ? 'group' : 'channel') - ) - ) - .append($('').text(entity.type)) - .append($('')) // min - .append($('')) // max - .append($('')) // avg - .append($('') // operations - .addClass('ops') - .append($('') - .attr('type', 'image') - .attr('src', 'images/information.png') - .attr('alt', 'details') - .bind('click', entity, function(event) { showEntityDetails(event.data); }) - ) - ); - - if (parent == null) { - $('td.ops', row).prepend($('') - .attr('type', 'image') - .attr('src', 'images/delete.png') - .attr('alt', 'delete') - .bind('click', entity, function(event) { - removeUUID(event.data.uuid); - loadEntities(); - }) - ); - } - - $('#entities tbody').append(row); - }); - - // http://ludo.cubicphuse.nl/jquery-plugins/treeTable/doc/index.html - $('#entities table').treeTable({ - treeColumn: 2, - clickableNodeNames: true - }); - - // load data and show plot - loadData(); -} - -/** - * Show and edit entity details - * @param entity - */ -function showEntityDetails(entity) { - var properties = $(''); - - $.each(entity, function(key, value) { - properties.append($('') - .append($('') - .addClass('required') - .append($('
') - .addClass('key') - .text(key) - ) - .append($('') - .addClass('value') - .text(value) - ) - ); - }); - - $('
') - .addClass('details') - .append(properties) - .dialog({ - title: 'Entity Details', - width: 450 - }); -} - -function validateEntity(entity) { - var def = getDefinition(vz.definitions.entities, entity.type); - - $.each(def.required, function(index, property) { - var property = getDefinition(vz.definitions.properties, property); - if (!validateProperty(property, form.elements[property.name].value)) { - alert('Error: invalid property: ' + property.name + ' = ' + form.elements[property.name].value); - return false; - } - }); - - $.each(entity.optional, function(index, property) { - var property = getDefinition(properties, property); - }); - - return true; -} - -function validateProperty(property, value) { - switch (property.type) { - case 'string': - case 'text': - // TODO check pattern - // TODO check string length - return true; - - case 'float': - // TODO check format - // TODO check min/max - return true; - - case 'integer': - // TODO check format - // TODO check min/max - return true; - - case 'boolean': - return value == '1' || value == ''; - - case 'multiple': - return $.inArray(value, property.options); - - default: - alert('Error: unknown property!'); - } -} - -/** - * Show from for new Channel - * - * @param type - * @return - */ -function getEntityDOM(type) { - $('#properties').empty(); - var entity = getDefinition(entities, type); - - $.each(entity.required, function(index, property) { - var property = getDefinition(properties, property); - - if (property) { - $('#properties') - .append($('
') - .append($('').append(getPropertyDOM(property))) - .append($('').text('(*)')) - ); - } - }); - - // TODO optional properties -} - -function getPropertyDOM(property) { - switch (property.type) { - case 'string': - case 'float': - case 'integer': - return $('') - .attr('type', 'text') - .attr('name=', property.name) - .attr('maxlength', (property.type == 'string') ? property.max : 0); - - case 'text': - return $('