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" ;
/ * e g w : u s e s
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
2014-02-13 10:50:49 +01:00
*
2013-10-09 18:22:35 +02:00
* A radio button belongs to same group by giving all buttons of a group same id !
2014-02-13 10:50:49 +01:00
*
2013-10-09 18:22:35 +02:00
* set _value iterates over all of them and ( un ) checks them depending on given value .
2014-02-13 10:50:49 +01:00
*
2013-04-13 21:00:13 +02:00
* @ augments et2 _inputWidget
2014-02-13 10:50:49 +01: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"
}
} ,
2014-02-13 10:50:49 +01:00
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
2014-02-13 10:50:49 +01:00
*
2013-04-13 21:00:13 +02:00
* @ 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 ] ) ;
} ,
2014-02-13 10:50:49 +01:00
2013-10-09 18:22:35 +02:00
/ * *
* Overwritten to set different DOM level ids by appending set _value
2014-02-13 10:50:49 +01:00
*
2013-10-09 18:22:35 +02:00
* @ param _id
* /
set _id : function ( _id )
{
this . _super . apply ( this , arguments ) ;
2011-08-18 19:34:01 +02:00
2013-10-09 18:22:35 +02:00
this . dom _id = this . dom _id . replace ( '[]' , '' ) + '-' + this . options . set _value ;
if ( this . input ) this . input . attr ( 'id' , this . dom _id ) ;
2012-03-23 19:57:13 +01:00
} ,
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
/ * *
2013-10-09 18:22:35 +02:00
* Override default to match against set / unset value AND iterate over all siblings with same id
2014-02-13 10:50:49 +01:00
*
* @ param { string } _value
2011-08-18 19:34:01 +02:00
* /
2014-02-13 10:50:49 +01:00
set _value : function ( _value )
2013-10-09 18:22:35 +02:00
{
this . getRoot ( ) . iterateOver ( function ( radio )
{
if ( radio . id == this . id )
{
radio . input . prop ( 'checked' , _value == radio . options . set _value ) ;
}
} , this , et2 _radiobox ) ;
2011-08-18 19:34:01 +02:00
} ,
/ * *
2014-02-13 10:50:49 +01:00
* Override default to iterate over all siblings with same id
*
* @ return { string }
2011-08-18 19:34:01 +02:00
* /
2014-02-13 10:50:49 +01:00
getValue : function ( )
{
var val = this . options . value ; // initial value, when form is loaded
this . getRoot ( ) . iterateOver ( function ( radio )
{
if ( radio . id == this . id && radio . input && radio . input . prop ( 'checked' ) )
{
val = radio . options . set _value ;
}
} , this , et2 _radiobox ) ;
2014-07-07 18:19:37 +02:00
return val == this . options . set _value ? this . options . set _value : null ;
2014-12-15 21:55:32 +01:00
} ,
/ * *
* Overridden from parent so if it ' s required , only 1 in a group needs a value
* /
isValid : function ( messages ) {
var ok = true ;
// Check for required
if ( this . options && this . options . needed && ! this . options . readonly && ( this . getValue ( ) == null || this . getValue ( ) . valueOf ( ) == '' ) )
{
if ( jQuery . isEmptyObject ( this . getInstanceManager ( ) . getValues ( this . getInstanceManager ( ) . widgetContainer ) [ this . id . replace ( '[]' , '' ) ] ) )
{
messages . push ( this . egw ( ) . lang ( 'Field must not be empty !!!' ) ) ;
ok = false ;
}
}
return ok ;
} ,
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
* /
2014-02-13 10:50:49 +01:00
var et2 _radiobox _ro = et2 _valueWidget . extend ( [ et2 _IDetachedDOM ] ,
2013-04-13 21:00:13 +02:00
{
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" : "" ,
2013-06-25 22:53:39 +02:00
"type" : "string"
2012-03-23 19:57:13 +01:00
}
} ,
2013-04-13 21:00:13 +02:00
2014-02-13 18:29:02 +01:00
legacyOptions : [ "set_value" , "ro_true" , "ro_false" ] ,
2013-04-13 21:00:13 +02:00
/ * *
* Constructor
2014-02-13 10:50:49 +01:00
*
2013-04-13 21:00:13 +02:00
* @ 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
2014-02-13 10:50:49 +01:00
*
* @ param { string } _value
2012-03-23 19:57:13 +01:00
* /
set _value : function ( _value ) {
2014-02-13 18:51:02 +01:00
this . value = _value ;
2012-03-23 19:57:13 +01:00
if ( _value == this . options . set _value ) {
this . span . text ( this . options . ro _true ) ;
} else {
this . span . text ( this . options . ro _false ) ;
}
} ,
2013-06-18 22:55:13 +02:00
2014-07-01 18:03:31 +02:00
set _label : function ( _label ) {
// no label for ro radio, we show label of checked option as content
} ,
2012-03-23 19:57:13 +01:00
/ * *
* Code for implementing et2 _IDetachedDOM
2014-02-13 10:50:49 +01:00
*
* @ param { array } _attrs
2012-03-23 19:57:13 +01:00
* /
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
2014-02-13 10:50:49 +01:00
*
2013-06-18 22:55:13 +02:00
* @ augments et2 _valueWidget
2014-02-13 10:50:49 +01:00
* /
var et2 _radioGroup = et2 _valueWidget . extend ( [ et2 _IDetachedDOM ] ,
2013-04-13 21:00:13 +02:00
{
2012-03-23 19:57:13 +01:00
attributes : {
2013-06-18 22:55:13 +02:00
"label" : {
2013-06-25 22:53:39 +02:00
"name" : "Label" ,
"default" : "" ,
"type" : "string" ,
"description" : "The label is displayed above the list of radio buttons. The label can contain variables, as descript for name. If the label starts with a '@' it is replaced by the value of the content-array at this index (with the '@'-removed and after expanding the variables)." ,
"translate" : true
} ,
2012-03-23 19:57:13 +01:00
"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, ...}"
2013-06-25 22:53:39 +02:00
} ,
2013-06-26 22:50:10 +02:00
"needed" : {
2013-06-25 22:53:39 +02:00
"name" : "Required" ,
"default" : false ,
"type" : "boolean" ,
"description" : "If required, the user must select one of the options before the form can be submitted"
2012-03-23 19:57:13 +01:00
}
} ,
createNamespace : false ,
2013-04-13 21:00:13 +02:00
/ * *
* Constructor
2014-02-13 10:50:49 +01:00
*
2013-04-13 21:00:13 +02:00
* @ param parent
* @ param attrs
* @ memberOf et2 _radioGroup
* /
2012-03-23 19:57:13 +01:00
init : function ( parent , attrs ) {
this . _super . apply ( this , arguments ) ;
2013-06-18 22:55:13 +02:00
this . node = $j ( document . createElement ( "div" ) )
. addClass ( "et2_vbox" )
. addClass ( "et2_box_widget" ) ;
2013-06-26 22:50:10 +02:00
if ( this . options . needed )
2013-06-25 22:53:39 +02:00
{
// This isn't strictly allowed, but it works
this . node . attr ( "required" , "required" ) ;
}
2013-06-18 22:55:13 +02:00
this . setDOMNode ( this . node [ 0 ] ) ;
// The supported widget classes array defines a whitelist for all widget
2013-06-25 22:53:39 +02:00
// classes or interfaces child widgets have to support.
this . supportedWidgetClasses = [ et2 _radiobox , et2 _radiobox _ro ] ;
2012-03-23 19:57:13 +01:00
} ,
set _value : function ( _value ) {
this . value = _value ;
for ( var i = 0 ; i < this . _children . length ; i ++ )
2013-06-25 22:53:39 +02:00
{
var radio = this . _children [ i ] ;
2012-03-23 19:57:13 +01:00
radio . set _value ( _value ) ;
2013-06-25 22:53:39 +02:00
}
2012-03-23 19:57:13 +01:00
} ,
2013-06-18 22:55:13 +02:00
getValue : function ( ) {
return jQuery ( "input:checked" , this . getDOMNode ( ) ) . val ( ) ;
} ,
2012-03-23 19:57:13 +01:00
/ * *
* Set a bunch of radio buttons
2014-02-13 10:50:49 +01:00
*
* @ param { object } _options object with value : label pairs
2012-03-23 19:57:13 +01:00
* /
set _options : function ( _options ) {
2014-06-30 21:34:53 +02:00
// Call the destructor of all children
for ( var i = this . _children . length - 1 ; i >= 0 ; i -- )
{
this . _children [ i ] . free ( ) ;
}
this . _children = [ ] ;
// create radio buttons for each option
2012-03-23 19:57:13 +01:00
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 ,
2014-01-11 17:40:45 +01:00
readonly : this . options . readonly
2013-04-13 21:00:13 +02:00
} ;
2014-01-11 17:40:45 +01:00
// Can't have a required readonly, it will warn & be removed later, so avoid the warning
if ( attrs . readonly === false )
{
attrs . needed = this . options . needed ;
}
2012-03-23 19:57:13 +01:00
var radio = et2 _createWidget ( "radio" , attrs , this ) ;
}
this . set _value ( this . value ) ;
2013-06-18 22:55:13 +02:00
} ,
/ * *
* Set a label on the group of radio buttons
2014-02-13 10:50:49 +01:00
*
* @ param { string } _value
2013-06-18 22:55:13 +02:00
* /
set _label : function ( _value ) {
// Abort if ther was no change in the label
if ( _value == this . label )
{
return ;
}
if ( _value )
{
// Create the label container if it didn't exist yet
if ( this . _labelContainer == null )
{
2014-02-13 10:50:49 +01:00
this . _labelContainer = $j ( document . createElement ( "label" ) ) ;
2013-06-18 22:55:13 +02:00
this . getSurroundings ( ) . insertDOMNode ( this . _labelContainer [ 0 ] ) ;
}
// Clear the label container.
this . _labelContainer . empty ( ) ;
// Create the placeholder element and set it
var ph = document . createElement ( "span" ) ;
this . getSurroundings ( ) . setWidgetPlaceholder ( ph ) ;
this . _labelContainer
. append ( document . createTextNode ( _value ) )
. append ( ph ) ;
}
else
{
// Delete the labelContainer from the surroundings object
if ( this . _labelContainer )
{
this . getSurroundings ( ) . removeDOMNode ( this . _labelContainer [ 0 ] ) ;
}
this . _labelContainer = null ;
}
2013-06-25 22:53:39 +02:00
} ,
2014-02-13 10:50:49 +01:00
2013-06-25 22:53:39 +02:00
/ * *
* Code for implementing et2 _IDetachedDOM
* This doesn ' t need to be implemented .
* Individual widgets are detected and handled by the grid , but the interface is needed for this to happen
2014-02-13 10:50:49 +01:00
*
* @ param { object } _attrs
2013-06-25 22:53:39 +02:00
* /
getDetachedAttributes : function ( _attrs )
{
} ,
getDetachedNodes : function ( )
{
return [ this . getDOMNode ( ) ] ;
} ,
setDetachedAttributes : function ( _nodes , _values )
{
2012-03-23 19:57:13 +01:00
}
2013-06-18 22:55:13 +02:00
2012-03-23 19:57:13 +01:00
} ) ;
// No such tag as 'radiogroup', but it needs something
et2 _register _widget ( et2 _radioGroup , [ "radiogroup" ] ) ;