vzlogger/htdocs/frontend/javascripts/property.js

107 lines
2.5 KiB
JavaScript
Raw Normal View History

2010-10-04 21:10:58 +02:00
/**
* Property handling & validation
*
* @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
*/
/*
* 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 <http://www.gnu.org/licenses/>.
*/
/**
2010-10-04 22:37:29 +02:00
* Property constructor
2010-10-04 21:10:58 +02:00
*/
2011-03-11 00:13:23 +01:00
var Property = function(json) {
$.extend(this, json);
2010-10-04 21:10:58 +02:00
};
2010-10-04 22:37:29 +02:00
/**
* Validate value
* @param value
* @return boolean
* @todo implement/test
*/
2010-10-04 21:10:58 +02:00
Property.prototype.validate = function(value) {
2011-03-11 00:13:23 +01:00
switch (this.type) {
2010-10-04 21:10:58 +02:00
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':
2011-03-11 00:13:23 +01:00
return this.options.contains(value);
2010-10-04 21:10:58 +02:00
default:
2011-03-11 00:13:23 +01:00
throw new Exception('EntityException', 'Unknown property');
2010-10-04 21:10:58 +02:00
}
};
2010-10-04 22:37:29 +02:00
/**
*
* @todo implement/test
*/
2011-03-11 00:13:23 +01:00
Property.prototype.getInput = function(value) {
switch (this.type) {
2010-10-04 21:10:58 +02:00
case 'string':
case 'float':
case 'integer':
return $('<input>')
.attr('type', 'text')
2011-03-11 00:13:23 +01:00
.attr('name=', this.name)
.attr('maxlength', (property.type == 'string') ? this.max : 0);
2010-10-04 21:10:58 +02:00
case 'text':
return $('<textarea>')
2011-03-11 00:13:23 +01:00
.attr('name', this.name);
2010-10-04 21:10:58 +02:00
case 'boolean':
return $('<input>')
.attr('type', 'checkbox')
2011-03-11 00:13:23 +01:00
.attr('name', this.name)
.attr('checked', true);
2010-10-04 21:10:58 +02:00
case 'multiple':
var dom = $('<select>').attr('name', property.name)
property.options.each(function(index, option) {
dom.append($('<option>')
.value(option)
.text(option)
);
});
return dom;
default:
2011-03-11 00:13:23 +01:00
throw new Exception('PropertyException', 'Unknown property');
2010-10-04 21:10:58 +02:00
}
2011-03-11 00:13:23 +01:00
};