Added et2_createWidget function and got et2_selectbox_ro working correctly with predefined options and non-string option-entries.

This commit is contained in:
Andreas Stöckel 2011-08-23 09:45:45 +00:00
parent d9ab0a9ce8
commit cb27424f5b
4 changed files with 92 additions and 7 deletions

View File

@ -16,6 +16,7 @@
/*egw:uses
lib/tooltip;
jquery.jquery;
et2_xml;
et2_DOMWidget;
et2_inputWidget;
*/
@ -71,7 +72,10 @@ var et2_selectbox = et2_inputWidget.extend({
this.input = null;
},
transformAttributes: function(_attrs) {
this._super.apply(this, arguments);
// Try to find the options inside the "sel-options" array
_attrs["select_options"] = this.getArrayMgr("sel_options").getValueForID(this.id);
@ -144,10 +148,8 @@ var et2_selectbox = et2_inputWidget.extend({
attrs["label"] = _options[key]
}
// Add all other important options to the attributes
et2_option.prototype.generateAttributeSet(attrs);
new et2_option(root, attrs);
// Create the widget and add it as a child
this.addChild(et2_createWidget("option", attrs));
}
}
});
@ -167,6 +169,7 @@ var et2_selectbox_ro = et2_selectbox.extend({
this._super.apply(this, arguments);
this.supportedWidgetClasses = [];
this.optionValues = {};
},
createInputWidget: function() {
@ -176,13 +179,40 @@ var et2_selectbox_ro = et2_selectbox.extend({
this.setDOMNode(this.span[0]);
},
loadFromXML: function(_node) {
// Read the option-tags
var options = et2_directChildrenByTagName(_node, "options");
for (var i = 0; i < options.length; i++)
{
this.optionValues[et2_readAttrWithDefault(options[i], "value", 0)] =
{
"label": options[i].textContent,
"title": et2_readAttrWithDefault(options[i], "title", "")
}
}
},
set_select_options: function(_options) {
this.select_options = _options;
for (var key in _options)
{
this.optionValues[key] = _options[key];
}
},
set_value: function(_value) {
this.value = _value;
this.span.text(this.select_options[_value]);
var option = this.optionValues[_value];
if (option instanceof Object)
{
this.span.text(option.label);
this.set_statustext(option.title);
}
else
{
this.span.text(option);
}
}
});

View File

@ -48,6 +48,53 @@ function et2_register_widget(_constructor, _types)
}
}
/**
* Creates a widget registered for the given tag-name. If "readonly" is listed
* inside the attributes, et2_createWidget will try to use the "_ro" type of the
* widget.
*
* @param _name is the name of the widget with which it is registered. If the
* widget is not found, an et2_placeholder will be created.
* @param _attrs is an associative array with attributes. If not passed, it will
* default to true.
* @param _parent is the parent to which the element will be attached. If _parent
* is not passed, it will default to null. Then you have to attach the element
* to a parent using the addChild or insertChild method.
*/
function et2_createWidget(_name, _attrs, _parent)
{
if (typeof _attrs == "undefined")
{
_attrs = {};
}
if (typeof _parent == "undefined")
{
_parent = null;
}
// Parse the "readonly" and "type" flag for this element here, as they
// determine which constructor is used
var nodeName = _attrs["type"] = _name;
var readonly = _attrs["readonly"] =
typeof _attrs["readonly"] == "undefined" ? false : _attrs["readonly"];
// Get the constructor - if the widget is readonly, use the special "_ro"
// constructor if it is available
var constructor = typeof et2_registry[nodeName] == "undefined" ?
et2_placeholder : et2_registry[nodeName];
if (readonly && typeof et2_registry[nodeName + "_ro"] != "undefined")
{
constructor = et2_registry[nodeName + "_ro"];
}
// Do an sanity check for the attributes
constructor.prototype.generateAttributeSet(_attrs);
// Create the new widget and return it
return new constructor(_parent, _attrs);
}
/**
* The et2 widget base class.
*/

View File

@ -9,7 +9,7 @@ var timesheet_data = {
"ts_quantity":"2.5",
"ts_unitprice":null,
"cat_id":null,
"ts_owner":"5",
"ts_owner":"6",
"ts_modified":1307039479,
"ts_modifier":"5",
"pl_id":null,

View File

@ -260,3 +260,11 @@ table.et2_grid {
/* border: 1px dashed silver;*/
}
/**
* Create some spacing for widgets inside labels
*/
label input, label span, label div, label select, label textarea {
margin-left: 1ex;
margin-right: 1ex;
}