From 4f2eeaafe120f3b1c39f75043e125c43bb9a1dbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20St=C3=B6ckel?= Date: Wed, 10 Aug 2011 16:23:02 +0000 Subject: [PATCH] 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. --- etemplate/js/et2_common.js | 8 ++++- etemplate/js/et2_inheritance.js | 58 +++++++++++++++++++++------------ etemplate/js/et2_inputWidget.js | 2 +- etemplate/js/et2_template.js | 8 +++++ etemplate/js/et2_textbox.js | 1 + etemplate/js/et2_widget.js | 9 ++--- 6 files changed, 60 insertions(+), 26 deletions(-) diff --git a/etemplate/js/et2_common.js b/etemplate/js/et2_common.js index 51a19119c2..b85c64524b 100644 --- a/etemplate/js/et2_common.js +++ b/etemplate/js/et2_common.js @@ -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. diff --git a/etemplate/js/et2_inheritance.js b/etemplate/js/et2_inheritance.js index 9233805d4a..fd970e3865 100644 --- a/etemplate/js/et2_inheritance.js +++ b/etemplate/js/et2_inheritance.js @@ -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); diff --git a/etemplate/js/et2_inputWidget.js b/etemplate/js/et2_inputWidget.js index 6b2d541431..14260f50b0 100644 --- a/etemplate/js/et2_inputWidget.js +++ b/etemplate/js/et2_inputWidget.js @@ -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 } }, diff --git a/etemplate/js/et2_template.js b/etemplate/js/et2_template.js index a4ad2e270f..a9c25a9dd9 100644 --- a/etemplate/js/et2_template.js +++ b/etemplate/js/et2_template.js @@ -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 = []; diff --git a/etemplate/js/et2_textbox.js b/etemplate/js/et2_textbox.js index 6227716a00..6d22417e07 100644 --- a/etemplate/js/et2_textbox.js +++ b/etemplate/js/et2_textbox.js @@ -35,6 +35,7 @@ var et2_textbox = et2_inputWidget.extend({ this._super.apply(this, arguments); this.input = null; + this.id = ""; this.createInputWidget(); }, diff --git a/etemplate/js/et2_widget.js b/etemplate/js/et2_widget.js index 3426fc1a79..3a26511694 100644 --- a/etemplate/js/et2_widget.js +++ b/etemplate/js/et2_widget.js @@ -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)); }