2011-08-18 19:34:01 +02:00
|
|
|
/**
|
2013-04-13 21:00:13 +02:00
|
|
|
* EGroupware eTemplate2 - JS Radiobox object
|
2011-08-18 19:34:01 +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 Nathan Gray
|
|
|
|
* @copyright Nathan Gray 2011
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*egw:uses
|
|
|
|
jquery.jquery;
|
2011-08-24 12:18:07 +02:00
|
|
|
et2_core_inputWidget;
|
2011-08-18 19:34:01 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class which implements the "radiobox" XET-Tag
|
2013-04-13 21:00:13 +02:00
|
|
|
*
|
|
|
|
* @augments et2_inputWidget
|
2011-08-18 19:34:01 +02:00
|
|
|
*/
|
2013-04-13 21:00:13 +02:00
|
|
|
var et2_radiobox = et2_inputWidget.extend(
|
|
|
|
{
|
2011-08-18 19:34:01 +02:00
|
|
|
attributes: {
|
|
|
|
"set_value": {
|
|
|
|
"name": "Set value",
|
|
|
|
"type": "string",
|
|
|
|
"default": "true",
|
|
|
|
"description": "Value when selected"
|
|
|
|
},
|
|
|
|
"ro_true": {
|
|
|
|
"name": "Read only selected",
|
|
|
|
"type": "string",
|
|
|
|
"default": "x",
|
|
|
|
"description": "What should be displayed when readonly and selected"
|
|
|
|
},
|
|
|
|
"ro_false": {
|
|
|
|
"name": "Read only unselected",
|
|
|
|
"type": "string",
|
|
|
|
"default": "",
|
|
|
|
"description": "What should be displayed when readonly and not selected"
|
|
|
|
}
|
|
|
|
},
|
2012-05-08 22:25:56 +02:00
|
|
|
|
|
|
|
legacyOptions: ["set_value", "ro_true", "ro_false"],
|
2011-08-18 19:34:01 +02:00
|
|
|
|
2013-04-13 21:00:13 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @memberOf et2_radiobox
|
|
|
|
*/
|
2011-08-24 12:05:52 +02:00
|
|
|
init: function() {
|
2011-08-18 19:34:01 +02:00
|
|
|
this._super.apply(this, arguments);
|
|
|
|
|
|
|
|
this.input = null;
|
|
|
|
this.id = "";
|
|
|
|
|
|
|
|
this.createInputWidget();
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
createInputWidget: function() {
|
2012-03-23 19:57:13 +01:00
|
|
|
this.input = $j(document.createElement("input"))
|
|
|
|
.val(this.options.set_value)
|
|
|
|
.attr("type", "radio");
|
2011-08-18 19:34:01 +02:00
|
|
|
|
|
|
|
this.input.addClass("et2_radiobox");
|
|
|
|
|
|
|
|
this.setDOMNode(this.input[0]);
|
|
|
|
},
|
|
|
|
|
2012-03-23 19:57:13 +01:00
|
|
|
set_name: function(_name) {
|
|
|
|
if(_name.substr(_name.length-2) != "[]")
|
|
|
|
{
|
|
|
|
_name += "[]";
|
|
|
|
}
|
|
|
|
this.input.attr("name", _name);
|
|
|
|
},
|
|
|
|
|
2013-06-18 17:08:50 +02:00
|
|
|
/**
|
|
|
|
* Default for radio buttons is label after button
|
|
|
|
*
|
|
|
|
* @param _label String New label for radio button. Use %s to locate the radio button somewhere else in the label
|
|
|
|
*/
|
|
|
|
set_label: function(_label) {
|
|
|
|
if(_label.length > 0 && _label.indexOf('%s')==-1)
|
|
|
|
{
|
|
|
|
_label = '%s'+_label;
|
|
|
|
}
|
|
|
|
this._super.apply(this, [_label]);
|
|
|
|
},
|
|
|
|
|
2011-08-18 19:34:01 +02:00
|
|
|
/**
|
|
|
|
* Override default to match against set/unset value
|
|
|
|
*/
|
|
|
|
set_value: function(_value) {
|
2012-03-23 19:57:13 +01:00
|
|
|
if(_value == this.options.set_value) {
|
|
|
|
this.input.attr("checked", "checked");
|
|
|
|
} else {
|
|
|
|
this.input.removeAttr("checked");
|
2011-08-18 19:34:01 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override default to return unchecked value
|
|
|
|
*/
|
|
|
|
getValue: function() {
|
2012-03-23 19:57:13 +01:00
|
|
|
if(jQuery("input:checked", this._parent.getDOMNode()).val() == this.options.set_value) {
|
|
|
|
return this.options.set_value;
|
2011-08-18 19:34:01 +02:00
|
|
|
}
|
2012-03-23 19:57:13 +01:00
|
|
|
return null;
|
2011-08-18 19:34:01 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
et2_register_widget(et2_radiobox, ["radio"]);
|
|
|
|
|
2013-04-13 21:00:13 +02:00
|
|
|
/**
|
|
|
|
* @augments et2_valueWidget
|
|
|
|
*/
|
|
|
|
var et2_radiobox_ro = et2_valueWidget.extend([et2_IDetachedDOM],
|
|
|
|
{
|
2012-03-23 19:57:13 +01:00
|
|
|
attributes: {
|
|
|
|
"set_value": {
|
|
|
|
"name": "Set value",
|
|
|
|
"type": "string",
|
|
|
|
"default": "true",
|
|
|
|
"description": "Value when selected"
|
|
|
|
},
|
|
|
|
"ro_true": {
|
|
|
|
"name": "Read only selected",
|
|
|
|
"type": "string",
|
|
|
|
"default": "x",
|
|
|
|
"description": "What should be displayed when readonly and selected"
|
|
|
|
},
|
|
|
|
"ro_false": {
|
|
|
|
"name": "Read only unselected",
|
|
|
|
"type": "string",
|
|
|
|
"default": "",
|
|
|
|
"description": "What should be displayed when readonly and not selected"
|
|
|
|
},
|
|
|
|
"label": {
|
|
|
|
"name": "Label",
|
|
|
|
"default": "",
|
|
|
|
"type": "string",
|
|
|
|
}
|
|
|
|
},
|
2013-04-13 21:00:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @memberOf et2_radiobox_ro
|
|
|
|
*/
|
2012-03-23 19:57:13 +01:00
|
|
|
init: function() {
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
|
|
|
|
this.value = "";
|
|
|
|
this.span = $j(document.createElement("span"))
|
|
|
|
.addClass("et2_radiobox");
|
|
|
|
|
|
|
|
this.setDOMNode(this.span[0]);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override default to match against set/unset value
|
|
|
|
*/
|
|
|
|
set_value: function(_value) {
|
|
|
|
if(_value == this.options.set_value) {
|
|
|
|
this.span.text(this.options.ro_true);
|
|
|
|
} else {
|
|
|
|
this.span.text(this.options.ro_false);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Code for implementing et2_IDetachedDOM
|
|
|
|
*/
|
|
|
|
getDetachedAttributes: function(_attrs)
|
|
|
|
{
|
|
|
|
// Show label in nextmatch instead of just x
|
|
|
|
this.options.ro_true = this.options.label;
|
|
|
|
_attrs.push("value");
|
|
|
|
},
|
|
|
|
|
|
|
|
getDetachedNodes: function()
|
|
|
|
{
|
|
|
|
return [this.span[0]];
|
|
|
|
},
|
|
|
|
|
|
|
|
setDetachedAttributes: function(_nodes, _values)
|
|
|
|
{
|
|
|
|
this.span = jQuery(_nodes[0]);
|
|
|
|
this.set_value(_values["value"]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
et2_register_widget(et2_radiobox_ro, ["radio_ro"]);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A group of radio buttons
|
2013-04-13 21:00:13 +02:00
|
|
|
*
|
|
|
|
* @augments et2_box
|
2012-03-23 19:57:13 +01:00
|
|
|
*/
|
2013-04-13 21:00:13 +02:00
|
|
|
var et2_radioGroup = et2_box.extend(
|
|
|
|
{
|
2012-03-23 19:57:13 +01:00
|
|
|
attributes: {
|
|
|
|
"value": {
|
|
|
|
"name": "Value",
|
|
|
|
"type": "string",
|
|
|
|
"default": "true",
|
|
|
|
"description": "Value for each radio button"
|
|
|
|
},
|
|
|
|
"ro_true": {
|
|
|
|
"name": "Read only selected",
|
|
|
|
"type": "string",
|
|
|
|
"default": "x",
|
|
|
|
"description": "What should be displayed when readonly and selected"
|
|
|
|
},
|
|
|
|
"ro_false": {
|
|
|
|
"name": "Read only unselected",
|
|
|
|
"type": "string",
|
|
|
|
"default": "",
|
|
|
|
"description": "What should be displayed when readonly and not selected"
|
|
|
|
},
|
|
|
|
"options": {
|
|
|
|
"name": "Radio options",
|
|
|
|
"type": "any",
|
|
|
|
"default": {},
|
|
|
|
"description": "Options for radio buttons. Should be {value: label, ...}"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
createNamespace: false,
|
|
|
|
|
2013-04-13 21:00:13 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param parent
|
|
|
|
* @param attrs
|
|
|
|
* @memberOf et2_radioGroup
|
|
|
|
*/
|
2012-03-23 19:57:13 +01:00
|
|
|
init: function(parent, attrs) {
|
|
|
|
attrs.type = "vbox";
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
set_value: function(_value) {
|
|
|
|
this.value = _value;
|
|
|
|
for (var i = 0; i < this._children.length; i++)
|
|
|
|
{
|
|
|
|
var radio = this._children[i];
|
|
|
|
radio.set_value(_value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a bunch of radio buttons
|
|
|
|
* Options should be {value: label, ...}
|
|
|
|
*/
|
|
|
|
set_options: function(_options) {
|
|
|
|
for(var key in _options)
|
|
|
|
{
|
|
|
|
var attrs = {
|
|
|
|
// Add index so radios work properly
|
|
|
|
"id": (this.options.readonly ? this.id : this.id + "[" + "]"),
|
|
|
|
set_value: key,
|
|
|
|
label: _options[key],
|
|
|
|
ro_true: this.options.ro_true,
|
|
|
|
ro_false: this.options.ro_false,
|
|
|
|
readonly: this.options.readonly
|
2013-04-13 21:00:13 +02:00
|
|
|
};
|
2012-03-23 19:57:13 +01:00
|
|
|
var radio = et2_createWidget("radio", attrs, this);
|
|
|
|
// radio.set_name(this.id);
|
|
|
|
}
|
|
|
|
this.set_value(this.value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// No such tag as 'radiogroup', but it needs something
|
|
|
|
et2_register_widget(et2_radioGroup, ["radiogroup"]);
|