2011-08-10 16:36:31 +02:00
/ * *
* eGroupWare eTemplate2 - JS Widget base class
*
* @ license http : //opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @ package etemplate
* @ subpackage api
* @ link http : //www.egroupware.org
* @ author Andreas Stöckel
* @ copyright Stylite 2011
2011-08-16 15:12:39 +02:00
* @ version $Id$
2011-08-10 16:36:31 +02:00
* /
"use strict" ;
/ * e g w : u s e s
jquery . jquery ;
2011-08-16 14:31:18 +02:00
et2 _valueWidget ;
2011-08-10 16:36:31 +02:00
* /
/ * *
* Interface for all widgets which support returning a value
* /
var et2 _IInput = new Interface ( {
/ * *
* getValue has to return the value of the input widget
* /
getValue : function ( ) { } ,
/ * *
* Is dirty returns true if the value of the widget has changed since it
* was loaded .
* /
isDirty : function ( ) { } ,
/ * *
* Causes the dirty flag to be reseted .
* /
resetDirty : function ( ) { }
} ) ;
/ * *
* et2 _inputWidget derrives from et2 _simpleWidget and implements the IInput
* interface . When derriving from this class , call setDOMNode with an input
* DOMNode .
* /
2011-08-16 14:31:18 +02:00
var et2 _inputWidget = et2 _valueWidget . extend ( et2 _IInput , {
2011-08-10 16:36:31 +02:00
2011-08-16 23:18:26 +02:00
attributes : {
"required" : {
"name" : "Required" ,
"default" : false ,
"type" : "boolean" ,
"description" : "If required, the user must enter a value before the form can be submitted"
2011-08-18 00:56:49 +02:00
} ,
"label" : {
"name" : "Label" ,
"default" : "" ,
"type" : "string" ,
"description" : "The label is displayed by default in front (for radiobuttons behind) each widget (if not empty). If you want to specify a different position, use a '%s' in the label, which gets replaced by the widget itself. Eg. '%s Name' to have the label Name behind a checkbox. 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)."
2011-08-16 23:18:26 +02:00
}
} ,
2011-08-21 17:22:00 +02:00
2011-08-10 16:36:31 +02:00
init : function ( ) {
this . _super . apply ( this , arguments ) ;
this . _oldValue = "" ;
2011-08-21 17:22:00 +02:00
this . _labelContainer = null ;
this . _widgetPlaceholder = null ;
} ,
destroy : function ( ) {
this . _super . apply ( this , arguments ) ;
this . _labelContainer = null ;
this . _widgetPlaceholder = null ;
2011-08-10 16:36:31 +02:00
} ,
2011-08-17 23:36:08 +02:00
attachToDOM : function ( ) {
2011-08-21 17:22:00 +02:00
if ( this . _labelContainer )
{
// Get the DOM Node without the labelContainer - that's why null is
// passed here.
var node = this . getDOMNode ( null ) ;
if ( node )
{
// Recreate the widget placeholder and return, as the set_label
// function will call attachToDOM again
if ( ! this . _widgetPlaceholder )
{
this . set _label ( this . label ) ;
return ;
}
// Insert the widget at the position of the placeholder
this . _labelContainer . replaceChild ( node , this . _widgetPlaceholder ) ;
// Set the widgetPlaceholder to null
this . _widgetPlaceholder = null ;
}
}
2011-08-17 23:36:08 +02:00
this . _super . apply ( this , arguments ) ;
$j ( this . getInputNode ( ) ) . attr ( "novalidate" , "novalidate" ) ; // Stop browser from getting involved
$j ( this . getInputNode ( ) ) . validator ( ) ;
} ,
2011-08-19 18:00:44 +02:00
2011-08-17 23:36:08 +02:00
detatchFromDOM : function ( ) {
if ( this . getInputNode ( ) ) {
$j ( this . getInputNode ( ) ) . data ( "validator" ) . destroy ( ) ;
}
this . _super . apply ( this , arguments ) ;
} ,
2011-08-10 16:36:31 +02:00
set _value : function ( _value ) {
this . _oldValue = _value ;
2011-08-11 15:53:35 +02:00
var node = this . getInputNode ( ) ;
if ( node )
2011-08-10 16:36:31 +02:00
{
2011-08-11 15:53:35 +02:00
$j ( node ) . val ( _value ) ;
2011-08-10 16:36:31 +02:00
}
} ,
2011-08-10 19:44:22 +02:00
set _id : function ( _value ) {
2011-08-12 17:26:08 +02:00
this . id = _value ;
2011-08-15 16:52:45 +02:00
// Set the id of the _input_ node (in contrast to the default
// implementation, which sets the base node)
2011-08-12 17:26:08 +02:00
var node = this . getInputNode ( ) ;
if ( node )
{
if ( _value != "" )
{
node . setAttribute ( "id" , _value ) ;
2011-08-17 23:36:08 +02:00
node . setAttribute ( "name" , _value ) ;
2011-08-12 17:26:08 +02:00
}
else
{
node . removeAttribute ( "id" ) ;
2011-08-17 23:36:08 +02:00
node . removeAttribute ( "name" ) ;
2011-08-12 17:26:08 +02:00
}
}
2011-08-10 19:44:22 +02:00
} ,
2011-08-18 00:56:49 +02:00
2011-08-21 17:22:00 +02:00
set _label : function ( _value ) {
// Copy the given value
this . label = _value ;
// Detach the current element from the DOM
var attached = this . isAttached ( ) ;
if ( attached )
2011-08-18 00:56:49 +02:00
{
2011-08-21 17:22:00 +02:00
this . detatchFromDOM ( ) ;
}
if ( _value )
{
// Create the label container if it didn't exist yet
if ( this . _labelContainer == null )
{
this . _labelContainer = document . createElement ( "span" ) ;
}
// Clear the label container.
for ( ; this . _labelContainer . childNodes . length > 0 ; )
{
this . _labelContainer . removeChild ( this . _labelContainer . childNodes [ 0 ] ) ;
}
// Create the placeholder element
this . _widgetPlaceholder = document . createElement ( "span" ) ;
// Split the label at the "%s"
var parts = et2 _csvSplit ( _value , 2 , "%s" ) ;
// Create the content of the label container
for ( var i = 0 ; i < parts . length ; i ++ )
{
if ( parts [ i ] )
{
this . _labelContainer . appendChild ( document . createTextNode ( parts [ i ] ) ) ;
}
if ( i == 0 )
{
this . _labelContainer . appendChild ( this . _widgetPlaceholder ) ;
}
}
}
else
{
this . _labelContainer = null ;
this . _widgetPlaceholder = null ;
}
// Attach the current element to the DOM again
if ( attached )
{
this . attachToDOM ( ) ;
2011-08-18 00:56:49 +02:00
}
} ,
2011-08-16 19:02:09 +02:00
set _required : function ( _value ) {
var node = this . getInputNode ( ) ;
if ( node )
{
if ( _value ) {
$j ( node ) . attr ( "required" , "required" ) ;
} else {
node . removeAttribute ( "required" ) ;
}
}
2011-08-16 23:18:26 +02:00
2011-08-16 19:02:09 +02:00
} ,
2011-08-10 19:44:22 +02:00
2011-08-21 17:22:00 +02:00
getDOMNode : function ( _sender ) {
if ( _sender == this && this . _labelContainer )
{
return this . _labelContainer ;
}
return this . _super . apply ( this , arguments ) ;
2011-08-10 16:36:31 +02:00
} ,
2011-08-11 15:53:35 +02:00
getInputNode : function ( ) {
return this . node ;
} ,
2011-08-21 17:22:00 +02:00
get _value : function ( ) {
return this . getValue ( ) ;
} ,
2011-08-10 16:36:31 +02:00
getValue : function ( ) {
2011-08-11 15:53:35 +02:00
var node = this . getInputNode ( ) ;
if ( node )
2011-08-10 16:36:31 +02:00
{
2011-08-12 14:15:44 +02:00
var val = $j ( node ) . val ( ) ;
return val ;
2011-08-10 16:36:31 +02:00
}
return this . _oldValue ;
} ,
isDirty : function ( ) {
return this . _oldValue != this . getValue ( ) ;
} ,
resetDirty : function ( ) {
this . _oldValue = this . getValue ( ) ;
}
} ) ;