Fixed problem with id not being copied when cloning an template rewrote a part of the attribute merging system - now not only references of the attribute descriptors are copied between the instances.

This commit is contained in:
Andreas Stöckel 2011-08-10 16:23:02 +00:00
parent 67b05dc93c
commit 4f2eeaafe1
6 changed files with 60 additions and 26 deletions

View File

@ -29,7 +29,7 @@ if (typeof Array.prototype.indexOf == "undefined")
* ET2_DEBUGLEVEL specifies which messages are printed to the console. Decrease
* the value of ET2_DEBUGLEVEL to get less messages.
*/
var ET2_DEBUGLEVEL = 0;
var ET2_DEBUGLEVEL = 4;
function et2_debug(_level, _msg)
{
@ -158,6 +158,12 @@ function et2_checkType(_val, _type)
throw("Invalid type identifier supplied.");
}
/**
* If et2_no_init is set as default value, the initAttributes function will not
* try to initialize the attribute with the default value.
*/
var et2_no_init = new Object();
/**
* Validates the given attribute with the given id. The validation checks for
* the existance of a human name, a description, a type and a default value.

View File

@ -150,31 +150,47 @@
function addAttributeFunctions(prototype, _super)
{
var attributes = prototype.attributes;
function _copyMerge(_new, _old)
{
var result = {};
// Copy the new object
if (typeof _new != "undefined")
{
for (var key in _new)
{
result[key] = _new[key];
}
}
// Merge the old object
for (var key in _old)
{
if (typeof result[key] == "undefined")
{
result[key] = _old[key];
}
}
return result;
}
var attributes = {};
// Copy the old attributes
for (var key in prototype.attributes)
{
attributes[key] = _copyMerge({}, prototype.attributes[key]);
}
// Add the old attributes to the new ones. If the attributes already
// exist, they are merged.
for (var key in _super.attributes)
{
var attrib = _super.attributes[key];
var _old = _super.attributes[key];
var _new = {};
if (typeof attributes[key] == "undefined")
{
// In the case that the old attribute has no equivalent in the
// new class, simply create a reference to the old one.
attributes[key] = attrib;
}
else
{
// Otherwise merge the two attribute descriptors.
for (var key2 in attrib)
{
if (typeof attributes[key][key2] == "undefined")
{
attributes[key][key2] = attrib[key2];
}
}
}
attributes[key] = _copyMerge(attributes[key], _old);
}
// Validate the attributes
@ -183,6 +199,8 @@
et2_validateAttrib(key, attributes[key]);
}
prototype.attributes = attributes;
/**
* The initAttributes function sets the attributes to their default
* values. The attributes are not overwritten, which means, that the
@ -192,7 +210,7 @@
prototype.initAttributes = function() {
for (var key in this.attributes)
{
if (!this.attributes[key].ignore)
if (!this.attributes[key].ignore && this.attributes[key]["default"] !== et2_no_init)
{
this.setAttribute(key, this.attributes[key]["default"],
false);

View File

@ -50,7 +50,7 @@ var et2_inputWidget = et2_baseWidget.extend(et2_IInput, {
"name": "Value",
"description": "The value of the widget",
"type": "string",
"default": ""
"default": et2_no_init
}
},

View File

@ -115,10 +115,18 @@ var et2_template = et2_DOMWidget.extend({
// isProxied property to true
tmpl.makeProxied();
// Do not copy the id when cloning as this leads to infinit
// recursion
tmpl.attributes["id"].ignore = true;
// Create a clone of the template and add it as child of this
// template (done by passing "this" to the clone function)
this.proxiedTemplate = tmpl.clone(this);
// Reset the "ignore" flag and manually copy the id
tmpl.attributes["id"].ignore = false;
this.proxiedTemplate.id = tmpl.id;
// Disallow adding any new node to this template
this.supportedWidgetClasses = [];

View File

@ -35,6 +35,7 @@ var et2_textbox = et2_inputWidget.extend({
this._super.apply(this, arguments);
this.input = null;
this.id = "";
this.createInputWidget();
},

View File

@ -58,7 +58,7 @@ var et2_widget = Class.extend({
"id": {
"name": "ID",
"type": "string",
"description": "Unique identifier of the widget"
"description": "Unique identifier of the widget",
},
/**
@ -92,6 +92,8 @@ var et2_widget = Class.extend({
_type = "widget";
}
this.id = "";
// Copy the parent parameter and add this widget to its parent children
// list.
this._parent = _parent;
@ -161,7 +163,6 @@ var et2_widget = Class.extend({
},
assign: function(_obj) {
// Create a clone of all child elements of the given object
for (var i = 0; i < _obj._children.length; i++)
{
@ -171,7 +172,7 @@ var et2_widget = Class.extend({
// Copy all properties
for (var key in _obj.attributes)
{
if (!_obj.attributes[key].ignore && key != "id")
if (!_obj.attributes[key].ignore)
{
this.setAttribute(key, _obj.getAttribute(key));
}
@ -414,7 +415,7 @@ var et2_widget = Class.extend({
// corresponding setter function exists. If yes, it is called.
for (var key in this.attributes)
{
if (!this.attributes[key].ignore && key != "id")
if (!this.attributes[key].ignore)
{
this.setAttribute(key, this.getAttribute(key));
}