vzlogger/htdocs/frontend/javascripts/uuid.js

83 lines
2.1 KiB
JavaScript
Raw Normal View History

/**
* UUID handling
*
* @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
/**
* Add given UUID and update cookie
*/
2010-10-04 21:10:58 +02:00
vz.uuids.add = function(uuid) {
if (this.validate(uuid)) {
if (!this.contains(uuid)) {
this.push(uuid);
2010-12-10 22:13:19 +01:00
this.save();
2010-10-04 21:10:58 +02:00
}
else {
2010-12-10 22:13:19 +01:00
throw new Exception('UUIDException', 'UUID already added: ' + uuid);
2010-10-04 21:10:58 +02:00
}
}
2010-10-04 21:10:58 +02:00
else {
2010-10-19 19:57:45 +02:00
throw new Exception('UUIDException', 'Invalid UUID');
}
2010-10-04 21:10:58 +02:00
};
2010-10-04 22:37:29 +02:00
/**
* Remove UUID and update cookie
*/
2010-10-04 21:10:58 +02:00
vz.uuids.remove = function(uuid) {
if (this.contains(uuid)) {
this.splice(this.indexOf(uuid), 1); // remove uuid from array
2010-12-10 22:13:19 +01:00
this.save();
}
2010-10-04 21:10:58 +02:00
else {
2010-10-19 19:57:45 +02:00
throw new Exception('UUIDException', 'UUID unkown: ' + uuid);
2010-10-04 21:10:58 +02:00
}
};
2010-10-04 22:37:29 +02:00
/**
* Validate UUID
*/
2010-10-04 21:10:58 +02:00
vz.uuids.validate = function(uuid) {
return uuid.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}$/);
2010-10-19 19:57:45 +02:00
};
/**
* Save uuids as cookie
*/
vz.uuids.save = function() {
$.setCookie('vz_uuids', this.join(';'));
};
/**
* Load uuids from cookie
*/
vz.uuids.load = function() {
var cookie = $.getCookie('vz_uuids');
if (cookie) {
cookie.split(';').each(function(index, uuid) {
vz.uuids.add(uuid);
});
}
};