2011-08-04 21:08:50 +02:00
|
|
|
/**
|
2014-03-20 10:40:37 +01:00
|
|
|
* EGroupware eTemplate2 - JS code for implementing inheritance with attributes
|
2011-08-04 21:08:50 +02:00
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Andreas Stöckel
|
|
|
|
* @copyright Stylite 2011
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
2011-08-07 15:43:46 +02:00
|
|
|
"use strict";
|
|
|
|
|
2011-08-10 16:36:31 +02:00
|
|
|
/*egw:uses
|
2011-08-24 12:18:07 +02:00
|
|
|
et2_core_common;
|
2014-03-20 10:40:37 +01:00
|
|
|
egw_inheritance;
|
2011-08-10 16:36:31 +02:00
|
|
|
*/
|
|
|
|
|
2014-03-20 10:40:37 +01:00
|
|
|
var ClassWithAttributes = Class.extend(
|
|
|
|
{
|
2011-08-12 17:35:27 +02:00
|
|
|
/**
|
|
|
|
* Returns the value of the given attribute. If the property does not
|
|
|
|
* exist, an error message is issued.
|
2014-03-20 10:40:37 +01:00
|
|
|
*
|
|
|
|
* @param {string} _name
|
|
|
|
* @return {*}
|
2011-08-12 17:35:27 +02:00
|
|
|
*/
|
2014-03-20 10:40:37 +01:00
|
|
|
getAttribute: function(_name) {
|
2011-08-12 17:35:27 +02:00
|
|
|
if (typeof this.attributes[_name] != "undefined" &&
|
|
|
|
!this.attributes[_name].ignore)
|
|
|
|
{
|
|
|
|
if (typeof this["get_" + _name] == "function")
|
|
|
|
{
|
|
|
|
return this["get_" + _name]();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return this[_name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-05 14:07:38 +01:00
|
|
|
egw.debug("error", this, "Attribute '" + _name + "' does not exist!");
|
2011-08-12 17:35:27 +02:00
|
|
|
}
|
2014-03-20 10:40:37 +01:00
|
|
|
},
|
2011-08-12 17:35:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The setAttribute function sets the attribute with the given name to
|
|
|
|
* the given value. _override defines, whether this[_name] will be set,
|
|
|
|
* if this key already exists. _override defaults to true. A warning
|
|
|
|
* is issued if the attribute does not exist.
|
2014-03-20 10:40:37 +01:00
|
|
|
*
|
|
|
|
* @param {string} _name
|
|
|
|
* @param {*} _value
|
|
|
|
* @param {boolean} _override
|
2011-08-12 17:35:27 +02:00
|
|
|
*/
|
2014-03-20 10:40:37 +01:00
|
|
|
setAttribute: function(_name, _value, _override) {
|
2011-08-12 17:35:27 +02:00
|
|
|
if (typeof this.attributes[_name] != "undefined")
|
|
|
|
{
|
|
|
|
if (!this.attributes[_name].ignore)
|
|
|
|
{
|
|
|
|
if (typeof _override == "undefined")
|
|
|
|
{
|
|
|
|
_override = true;
|
|
|
|
}
|
|
|
|
|
2011-08-16 15:45:41 +02:00
|
|
|
var val = et2_checkType(_value, this.attributes[_name].type,
|
2013-10-09 16:35:03 +02:00
|
|
|
_name, this);
|
2011-08-12 17:35:27 +02:00
|
|
|
|
|
|
|
if (typeof this["set_" + _name] == "function")
|
|
|
|
{
|
|
|
|
this["set_" + _name](val);
|
|
|
|
}
|
|
|
|
else if (_override || typeof this[_name] == "undefined")
|
|
|
|
{
|
|
|
|
this[_name] = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-05 14:07:38 +01:00
|
|
|
egw.debug("warn", this, "Attribute '" + _name + "' does not exist!");
|
2011-08-12 17:35:27 +02:00
|
|
|
}
|
2014-03-20 10:40:37 +01:00
|
|
|
},
|
2011-08-12 17:35:27 +02:00
|
|
|
|
2011-08-19 18:00:44 +02:00
|
|
|
/**
|
|
|
|
* generateAttributeSet sanitizes the given associative array of attributes
|
|
|
|
* (by passing each entry to "et2_checkType" and checking for existance of
|
|
|
|
* the attribute) and adds the default values to the associative array.
|
|
|
|
*
|
2014-03-20 10:40:37 +01:00
|
|
|
* @param {object} _attrs is the associative array containing the attributes.
|
2011-08-19 18:00:44 +02:00
|
|
|
*/
|
2014-03-20 10:40:37 +01:00
|
|
|
generateAttributeSet: function(_attrs) {
|
2011-08-19 18:00:44 +02:00
|
|
|
|
|
|
|
// Sanity check and validation
|
|
|
|
for (var key in _attrs)
|
|
|
|
{
|
|
|
|
if (typeof this.attributes[key] != "undefined")
|
|
|
|
{
|
|
|
|
if (!this.attributes[key].ignore)
|
|
|
|
{
|
|
|
|
_attrs[key] = et2_checkType(_attrs[key], this.attributes[key].type,
|
2013-10-09 16:35:03 +02:00
|
|
|
key, this);
|
2011-08-19 18:00:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Key does not exist - delete it and issue a warning
|
|
|
|
delete(_attrs[key]);
|
2012-03-05 14:07:38 +01:00
|
|
|
egw.debug("warn", this, "Attribute '" + key +
|
2011-09-15 19:49:03 +02:00
|
|
|
"' does not exist in " + _attrs.type+"!");
|
2011-08-19 18:00:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Include default values or already set values for this attribute
|
|
|
|
for (var key in this.attributes)
|
|
|
|
{
|
|
|
|
if (typeof _attrs[key] == "undefined")
|
|
|
|
{
|
|
|
|
var _default = this.attributes[key]["default"];
|
|
|
|
if (_default == et2_no_init)
|
|
|
|
{
|
|
|
|
_default = undefined;
|
|
|
|
}
|
|
|
|
|
2014-05-06 22:05:08 +02:00
|
|
|
_attrs[key] = _default;
|
2011-08-19 18:00:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _attrs;
|
2014-03-20 10:40:37 +01:00
|
|
|
},
|
2011-08-19 18:00:44 +02:00
|
|
|
|
2011-08-12 17:35:27 +02:00
|
|
|
/**
|
|
|
|
* The initAttributes function sets the attributes to their default
|
|
|
|
* values. The attributes are not overwritten, which means, that the
|
|
|
|
* default is only set, if either a setter exists or this[propName] does
|
|
|
|
* not exist yet.
|
2014-03-20 10:40:37 +01:00
|
|
|
*
|
|
|
|
* @param {object} _attrs is the associative array containing the attributes.
|
2011-08-12 17:35:27 +02:00
|
|
|
*/
|
2014-03-20 10:40:37 +01:00
|
|
|
initAttributes: function(_attrs) {
|
2011-08-19 18:00:44 +02:00
|
|
|
for (var key in _attrs)
|
2011-08-12 17:35:27 +02:00
|
|
|
{
|
2011-08-25 22:09:45 +02:00
|
|
|
if (typeof this.attributes[key] != "undefined" && !this.attributes[key].ignore && !(_attrs[key] == undefined))
|
2011-08-12 17:35:27 +02:00
|
|
|
{
|
2011-08-19 18:00:44 +02:00
|
|
|
this.setAttribute(key, _attrs[key], false);
|
2011-08-12 17:35:27 +02:00
|
|
|
}
|
|
|
|
}
|
2014-03-20 10:40:37 +01:00
|
|
|
},
|
2011-08-12 17:35:27 +02:00
|
|
|
|
2014-03-20 10:40:37 +01:00
|
|
|
_validate_attributes: function(attributes)
|
|
|
|
{
|
|
|
|
// Validate the attributes
|
|
|
|
for (var key in attributes)
|
2011-08-12 17:35:27 +02:00
|
|
|
{
|
2014-03-20 10:40:37 +01:00
|
|
|
et2_validateAttrib(key, attributes[key]);
|
2011-08-12 17:35:27 +02:00
|
|
|
}
|
2014-03-20 10:40:37 +01:00
|
|
|
}
|
|
|
|
});
|