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-25 15:35:53 +02:00
et2 _core _interfaces ;
2011-08-24 12:18:07 +02:00
et2 _core _valueWidget ;
2011-08-10 16:36:31 +02:00
* /
/ * *
* 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" ,
2011-08-23 19:05:05 +02:00
"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)." ,
"translate" : true
2011-08-22 16:38:05 +02:00
} ,
"onchange" : {
"name" : "onchange" ,
"type" : "js" ,
"description" : "JS code which is executed when the value changes."
2011-08-23 16:59:49 +02:00
} ,
"validation_error" : {
"name" : "Validation Error" ,
"type" : "string" ,
"default" : et2 _no _init ,
"description" : "Used internally to store the validation error that came from the server."
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 ;
} ,
destroy : function ( ) {
2011-08-22 16:38:05 +02:00
var node = this . getInputNode ( ) ;
if ( node )
{
$j ( node ) . unbind ( "change.et2_inputWidget" ) ;
}
2011-08-21 17:22:00 +02:00
this . _super . apply ( this , arguments ) ;
this . _labelContainer = null ;
2011-08-10 16:36:31 +02:00
} ,
2011-08-23 16:59:49 +02:00
/ * *
* Load the validation errors from the server
* /
transformAttributes : function ( _attrs ) {
this . _super . apply ( this , arguments ) ;
2011-08-21 17:22:00 +02:00
2011-08-23 16:59:49 +02:00
// Check whether an validation error entry exists
2011-09-26 18:01:42 +02:00
if ( this . id && this . getArrayMgr ( "validation_errors" ) )
2011-08-23 16:59:49 +02:00
{
2011-09-08 20:36:09 +02:00
var val = this . getArrayMgr ( "validation_errors" ) . getEntry ( this . id ) ;
2011-08-23 19:05:05 +02:00
if ( val )
{
_attrs [ "validation_error" ] = val ;
}
2011-08-21 17:22:00 +02:00
}
2011-08-23 16:59:49 +02:00
} ,
2011-08-21 17:22:00 +02:00
2011-08-23 16:59:49 +02:00
attachToDOM : function ( ) {
2011-08-22 16:38:05 +02:00
var node = this . getInputNode ( ) ;
if ( node )
{
$j ( node ) . bind ( "change.et2_inputWidget" , this , function ( e ) {
e . data . change ( this ) ;
} ) ;
}
2011-08-17 23:36:08 +02:00
this . _super . apply ( this , arguments ) ;
2011-08-23 16:59:49 +02:00
// $j(this.getInputNode()).attr("novalidate","novalidate"); // Stop browser from getting involved
// $j(this.getInputNode()).validator();
2011-08-17 23:36:08 +02:00
} ,
2011-08-19 18:00:44 +02:00
2011-08-17 23:36:08 +02:00
detatchFromDOM : function ( ) {
2011-08-23 16:59:49 +02:00
// if(this.getInputNode()) {
// $j(this.getInputNode()).data("validator").destroy();
// }
2011-08-17 23:36:08 +02:00
this . _super . apply ( this , arguments ) ;
} ,
2011-08-22 16:38:05 +02:00
change : function ( _node ) {
if ( this . onchange )
{
return this . onchange . apply ( _node ) ;
}
} ,
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 ) {
2011-08-23 16:59:49 +02:00
// Abort if ther was no change in the label
if ( _value == this . label )
2011-08-18 00:56:49 +02:00
{
2011-08-23 16:59:49 +02:00
return ;
2011-08-21 17:22:00 +02:00
}
if ( _value )
{
// Create the label container if it didn't exist yet
if ( this . _labelContainer == null )
{
2011-08-23 16:59:49 +02:00
this . _labelContainer = $j ( document . createElement ( "label" ) )
. addClass ( "et2_label" ) ;
this . getSurroundings ( ) . insertDOMNode ( this . _labelContainer [ 0 ] ) ;
2011-08-21 17:22:00 +02:00
}
// Clear the label container.
2011-08-23 16:59:49 +02:00
this . _labelContainer . empty ( ) ;
2011-08-21 17:22:00 +02:00
2011-08-23 16:59:49 +02:00
// Create the placeholder element and set it
var ph = document . createElement ( "span" ) ;
this . getSurroundings ( ) . setWidgetPlaceholder ( ph ) ;
2011-08-21 17:22:00 +02:00
// Split the label at the "%s"
var parts = et2 _csvSplit ( _value , 2 , "%s" ) ;
2011-08-23 16:59:49 +02:00
// Update the content of the label container
2011-08-21 17:22:00 +02:00
for ( var i = 0 ; i < parts . length ; i ++ )
{
if ( parts [ i ] )
{
2011-08-23 16:59:49 +02:00
this . _labelContainer . append ( document . createTextNode ( parts [ i ] ) ) ;
2011-08-21 17:22:00 +02:00
}
if ( i == 0 )
{
2011-08-23 16:59:49 +02:00
this . _labelContainer . append ( ph ) ;
2011-08-21 17:22:00 +02:00
}
}
}
else
{
2011-08-23 16:59:49 +02:00
// Delete the labelContainer from the surroundings object
if ( this . _labelContainer )
{
2011-10-12 18:37:56 +02:00
this . getSurroundings ( ) . removeDOMNode ( this . _labelContainer [ 0 ] ) ;
2011-08-23 16:59:49 +02:00
}
2011-08-21 17:22:00 +02:00
this . _labelContainer = null ;
}
2011-08-23 16:59:49 +02:00
// Update the surroundings in order to reflect the change in the label
this . getSurroundings ( ) . update ( ) ;
// Copy the given value
this . label = _value ;
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-23 16:59:49 +02:00
set _validation _error : function ( _value ) {
var node = this . getInputNode ( ) ;
if ( node )
2011-08-21 17:22:00 +02:00
{
2011-08-23 16:59:49 +02:00
if ( _value === false )
{
this . hideMessage ( ) ;
$j ( node ) . removeClass ( "invalid" ) ;
}
else
{
this . showMessage ( _value , "validation_error" ) ;
$j ( node ) . addClass ( "invalid" ) ;
}
2011-08-21 17:22:00 +02:00
}
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 ( ) ;
}
} ) ;