2014-09-30 23:37:45 +02:00
/ *
* Egroupware etemplate2 JS Entry widget
* @ 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
* @ version $Id$
* /
"use strict" ;
/ * e g w : u s e s
et2 _core _valueWidget ;
* /
/ * *
* A widget to display a value from an entry
*
* Since we have etemplate _widget _transformer , this client side widget exists
* mostly to resolve the problem where the ID for the entry widget is the same
* as the widget where you actually set the value , which prevents transformer
* from working .
*
* Server side will find the associated entry , and load it into ~ < entry _id > to
* avoid overwriting the widget with id = "entry_id" . This widget will reverse
* that , and the modifications from transformer will be applied .
*
* @ augments et2 _valueWidget
* /
var et2 _entry = et2 _valueWidget . extend (
{
attributes : {
field : {
'name' : 'Fields' ,
2014-10-01 18:49:26 +02:00
'description' : 'Which entry field to display, or "sum" to add up the alternate_fields' ,
2014-09-30 23:37:45 +02:00
'type' : 'string'
} ,
2014-10-01 18:49:26 +02:00
compare : {
name : 'Compare' ,
description : 'if given, the selected field is compared with its value and an X is printed on equality, nothing otherwise' ,
default : et2 _no _init ,
type : 'string'
} ,
alternate _fields : {
name : 'Alternate fields' ,
2016-01-08 14:35:35 +01:00
description : 'colon (:) separated list of alternative fields. The first non-empty one is used if the selected field is empty, (-) used for subtraction' ,
2014-10-01 18:49:26 +02:00
type : 'string' ,
default : et2 _no _init
} ,
2016-01-08 15:20:09 +01:00
precision : {
name : 'Decimals to be shown' ,
2016-01-08 15:52:51 +01:00
description : 'Specifies the number of decimals for sum of alternates, the default is 2' ,
2016-01-08 15:20:09 +01:00
type : 'string' ,
default : '2'
} ,
2014-09-30 23:37:45 +02:00
value : {
type : 'any'
} ,
readonly : {
default : true
}
} ,
2014-10-01 18:49:26 +02:00
legacyOptions : [ "field" , "compare" , "alternate_fields" ] ,
2014-09-30 23:37:45 +02:00
prefix : '~' ,
/ * *
* Constructor
*
* @ memberOf et2 _customfields _list
* /
init : function ( parent , attrs ) {
// Often the ID conflicts, so check prefix
2014-10-01 19:29:22 +02:00
if ( attrs . id && attrs . id . indexOf ( this . prefix ) < 0 )
2014-09-30 23:37:45 +02:00
{
attrs . id = this . prefix + attrs . id ;
}
2014-10-01 19:29:22 +02:00
var value = attrs . value ;
2014-09-30 23:37:45 +02:00
this . _super . apply ( this , arguments ) ;
2014-10-01 19:29:22 +02:00
// Save value from parsing, but only if set
if ( value )
{
this . options . value = value ;
}
2014-09-30 23:37:45 +02:00
this . widget = null ;
this . setDOMNode ( document . createElement ( 'span' ) ) ;
} ,
2015-08-20 16:57:18 +02:00
loadFromXML : function ( _node ) {
2014-09-30 23:37:45 +02:00
// Load the nodes as usual
this . _super . apply ( this , arguments ) ;
// Do the magic
this . loadField ( ) ;
} ,
/ * *
* Initialize widget for entry field
* /
loadField : function ( ) {
// Create widget of correct type
2015-01-14 21:10:34 +01:00
var attrs = {
id : this . id + '[' + this . options . field + ']' ,
type : 'label' ,
readonly : this . options . readonly
} ;
2014-09-30 23:37:45 +02:00
var modifications = this . getArrayMgr ( "modifications" ) ;
2014-10-01 18:49:26 +02:00
if ( modifications && this . options . field )
{
2015-01-14 21:10:34 +01:00
jQuery . extend ( attrs , modifications . getEntry ( attrs . id ) ) ;
2014-09-30 23:37:45 +02:00
}
2015-01-14 21:10:34 +01:00
2015-01-14 18:22:29 +01:00
// Supress labels on templates
if ( attrs . type == 'template' && this . options . label )
{
this . egw ( ) . debug ( 'log' , "Surpressed label on <" + this . _type + ' label="' + this . options . label + '" id="' + this . id + '"...>' ) ;
this . options . label = '' ;
}
2014-09-30 23:37:45 +02:00
var widget = et2 _createWidget ( attrs . type , attrs , this ) ;
2014-10-01 18:49:26 +02:00
2014-10-01 19:29:22 +02:00
// If value is not set, etemplate takes care of everything
// If value was set, find the record explicitly.
if ( typeof this . options . value == 'string' )
{
widget . options . value = this . getRoot ( ) . getArrayMgr ( 'content' ) . getEntry ( this . prefix + this . options . value + '[' + this . options . field + ']' ) ;
}
2014-10-01 18:49:26 +02:00
if ( this . options . compare )
{
2014-10-01 19:29:22 +02:00
widget . options . value = widget . options . value == this . options . compare ? 'X' : '' ;
2014-10-01 18:49:26 +02:00
}
if ( this . options . alternate _fields )
{
var sum = 0 ;
var fields = this . options . alternate _fields . split ( ':' ) ;
for ( var i = 0 ; i < fields . length ; i ++ )
{
2016-01-08 14:35:35 +01:00
var value = ( fields [ i ] [ 0 ] == "-" ) ? this . getArrayMgr ( 'content' ) . getEntry ( fields [ i ] . replace ( '-' , '' ) ) * - 1 :
this . getArrayMgr ( 'content' ) . getEntry ( fields [ i ] ) ;
2014-10-01 18:49:26 +02:00
sum += parseFloat ( value ) ;
if ( value && this . options . field !== 'sum' )
{
widget . options . value = value ;
break ;
}
}
if ( this . options . field == 'sum' )
{
2016-01-08 15:52:51 +01:00
if ( this . options . precision && jQuery . isNumeric ( sum ) ) sum = parseFloat ( sum ) . toFixed ( this . options . precision ) ;
2014-10-01 18:49:26 +02:00
widget . options . value = sum ;
}
}
2016-01-08 15:52:51 +01:00
2014-09-30 23:37:45 +02:00
}
} ) ;
2015-02-19 00:04:59 +01:00
et2 _register _widget ( et2 _entry , [ "entry" , 'contact-value' , 'contact-account' , 'contact-template' , 'infolog-value' , 'tracker-value' , 'records-value' ] ) ;