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";
|
|
|
|
|
|
|
|
/*egw:uses
|
|
|
|
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',
|
|
|
|
description: 'colon (:) separated list of alternative fields. The first non-empty one is used if the selected field is empty',
|
|
|
|
type: 'string',
|
|
|
|
default: et2_no_init
|
|
|
|
},
|
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
|
|
|
|
|
|
|
// Doesn't really need a namespace, but this simplifies the sub-widgets
|
|
|
|
createNamespace: true,
|
|
|
|
|
|
|
|
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'));
|
|
|
|
},
|
|
|
|
|
|
|
|
loadFromXML: function(_node) {
|
|
|
|
// 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
|
2014-10-01 18:49:26 +02:00
|
|
|
var entry = {type: 'label'};
|
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)
|
|
|
|
{
|
|
|
|
jQuery.extend(entry, modifications.getEntry(this.options.field));
|
2014-09-30 23:37:45 +02:00
|
|
|
}
|
|
|
|
var attrs = {
|
|
|
|
id: this.options.field,
|
2014-10-01 00:08:57 +02:00
|
|
|
type: entry.type || 'label',
|
2014-09-30 23:37:45 +02:00
|
|
|
readonly: this.options.readonly
|
|
|
|
};
|
|
|
|
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++)
|
|
|
|
{
|
|
|
|
var value = this.getArrayMgr('content').getEntry(fields[i]);
|
|
|
|
sum += parseFloat(value);
|
|
|
|
if(value && this.options.field !== 'sum')
|
|
|
|
{
|
|
|
|
widget.options.value = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(this.options.field == 'sum')
|
|
|
|
{
|
|
|
|
widget.options.value = sum;
|
|
|
|
}
|
|
|
|
}
|
2014-09-30 23:37:45 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-10-01 00:08:57 +02:00
|
|
|
et2_register_widget(et2_entry, ["entry", 'contact-value', 'contact-account', 'contact-template', 'infolog-value','tracker-value']);
|