Convert et2_widget_entry to TS

This commit is contained in:
Hadi Nategh 2020-02-10 15:29:27 +01:00
parent b98ce00079
commit 44ed664f7a
2 changed files with 324 additions and 153 deletions

View File

@ -1,3 +1,4 @@
"use strict";
/* /*
* Egroupware etemplate2 JS Entry widget * Egroupware etemplate2 JS Entry widget
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
@ -5,14 +6,27 @@
* @subpackage api * @subpackage api
* @link http://www.egroupware.org * @link http://www.egroupware.org
* @author Nathan Gray * @author Nathan Gray
* @version $Id$
*/ */
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
/*egw:uses /*egw:uses
et2_core_valueWidget; et2_core_valueWidget;
*/ */
var et2_core_widget_1 = require("./et2_core_widget");
var et2_core_valueWidget_1 = require("./et2_core_valueWidget");
var et2_core_inheritance_1 = require("./et2_core_inheritance");
/** /**
* A widget to display a value from an entry * A widget to display a value from an entry
* *
@ -27,151 +41,129 @@
* *
* @augments et2_valueWidget * @augments et2_valueWidget
*/ */
var et2_entry = (function(){ "use strict"; return et2_valueWidget.extend( var et2_entry = /** @class */ (function (_super) {
{ __extends(et2_entry, _super);
attributes: { function et2_entry(_parent, _attrs, _child) {
field: { var _this =
'name': 'Fields', // Call the inherited constructor
'description': 'Which entry field to display, or "sum" to add up the alternate_fields', _super.call(this, _parent, _attrs, et2_core_inheritance_1.ClassWithAttributes.extendAttributes(et2_entry._attributes, _child || {})) || this;
'type': 'string' _this.legacyOptions = ["field", "compare", "alternate_fields"];
}, _this.widget = null;
compare: { // Often the ID conflicts, so check prefix
name: 'Compare', if (_attrs.id && _attrs.id.indexOf(et2_entry.prefix) < 0) {
description: 'if given, the selected field is compared with its value and an X is printed on equality, nothing otherwise', _attrs.id = et2_entry.prefix + _attrs.id;
default: et2_no_init, }
type: 'string' var value = _attrs.value;
}, _this = _super.call(this, _parent, _attrs, et2_core_inheritance_1.ClassWithAttributes.extendAttributes(et2_entry._attributes, _child || {})) || this;
alternate_fields: { // Save value from parsing, but only if set
name: 'Alternate fields', if (value) {
description: 'colon (:) separated list of alternative fields. The first non-empty one is used if the selected field is empty, (-) used for subtraction', _this.options.value = value;
type: 'string', }
default: et2_no_init _this.widget = null;
}, _this.setDOMNode(document.createElement('span'));
precision: { return _this;
name: 'Decimals to be shown', }
description: 'Specifies the number of decimals for sum of alternates, the default is 2', et2_entry.prototype.loadFromXML = function (_node) {
type: 'string', // Load the nodes as usual
default: '2' _super.prototype.loadFromXML.call(this, _node);
}, // Do the magic
regex: { this.loadField();
name: 'Regular expression pattern', };
description: 'Only used server-side in a preg_replace with regex_replace to modify the value', /**
default: et2_no_init, * Initialize widget for entry field
type: 'string' */
}, et2_entry.prototype.loadField = function () {
regex_replace: { // Create widget of correct type
name: 'Regular expression replacement pattern', var attrs = {
description: 'Only used server-side in a preg_replace with regex to modify the value', id: this.id + (this.options.field ? '[' + this.options.field + ']' : ''),
default: et2_no_init, type: 'label',
type: 'string' readonly: this.options.readonly
}, };
value: { var modifications = this.getArrayMgr("modifications");
type: 'any' if (modifications && this.options.field) {
}, jQuery.extend(attrs, modifications.getEntry(attrs.id));
readonly: { }
default: true // Supress labels on templates
} if (attrs.type == 'template' && this.options.label) {
}, this.egw().debug('log', "Surpressed label on <" + this.getType() + ' label="' + this.options.label + '" id="' + this.id + '"...>');
this.options.label = '';
legacyOptions: ["field","compare","alternate_fields"], }
var widget = et2_createWidget(attrs.type, attrs, this);
prefix: '~', // If value is not set, etemplate takes care of everything
// If value was set, find the record explicitly.
/** if (typeof this.options.value == 'string') {
* Constructor widget.options.value = this.getArrayMgr('content').getEntry(this.id + '[' + this.options.field + ']') ||
* this.getRoot().getArrayMgr('content').getEntry(et2_entry.prefix + this.options.value + '[' + this.options.field + ']');
* @memberOf et2_customfields_list }
*/ else if (this.options.field && this.options.value && this.options.value[this.options.field]) {
init: function(parent, attrs) { widget.options.value = this.options.value[this.options.field];
// Often the ID conflicts, so check prefix }
if(attrs.id && attrs.id.indexOf(this.prefix) < 0) if (this.options.compare) {
{ widget.options.value = widget.options.value == this.options.compare ? 'X' : '';
attrs.id = this.prefix + attrs.id; }
} if (this.options.alternate_fields) {
var value = attrs.value; var sum = 0;
var fields = this.options.alternate_fields.split(':');
this._super.apply(this, arguments); for (var i = 0; i < fields.length; i++) {
var negate = (fields[i][0] == "-");
// Save value from parsing, but only if set var value = this.getArrayMgr('content').getEntry(fields[i].replace('-', ''));
if(value) sum += typeof value === 'undefined' ? 0 : (parseFloat(value) * (negate ? -1 : 1));
{ if (value && this.options.field !== 'sum') {
this.options.value = value; widget.options.value = value;
} break;
}
this.widget = null; }
this.setDOMNode(document.createElement('span')); if (this.options.field == 'sum') {
}, if (this.options.precision && jQuery.isNumeric(sum))
sum = parseFloat(sum).toFixed(this.options.precision);
loadFromXML: function(_node) { widget.options.value = sum;
// Load the nodes as usual }
this._super.apply(this, arguments); }
};
// Do the magic et2_entry._attributes = {
this.loadField(); field: {
}, 'name': 'Fields',
'description': 'Which entry field to display, or "sum" to add up the alternate_fields',
/** 'type': 'string'
* Initialize widget for entry field },
*/ compare: {
loadField: function() { name: 'Compare',
// Create widget of correct type description: 'if given, the selected field is compared with its value and an X is printed on equality, nothing otherwise',
var attrs = { default: et2_no_init,
id: this.id + (this.options.field ? '[' +this.options.field+']' : ''), type: 'string'
type: 'label', },
readonly: this.options.readonly alternate_fields: {
}; name: 'Alternate fields',
var modifications = this.getArrayMgr("modifications"); description: 'colon (:) separated list of alternative fields. The first non-empty one is used if the selected field is empty, (-) used for subtraction',
if(modifications && this.options.field) type: 'string',
{ default: et2_no_init
jQuery.extend(attrs, modifications.getEntry(attrs.id)); },
} precision: {
name: 'Decimals to be shown',
// Supress labels on templates description: 'Specifies the number of decimals for sum of alternates, the default is 2',
if(attrs.type == 'template' && this.options.label) type: 'string',
{ default: '2'
this.egw().debug('log', "Surpressed label on <" + this._type + ' label="' + this.options.label + '" id="' + this.id + '"...>'); },
this.options.label = ''; regex: {
} name: 'Regular expression pattern',
var widget = et2_createWidget(attrs.type, attrs, this); description: 'Only used server-side in a preg_replace with regex_replace to modify the value',
default: et2_no_init,
// If value is not set, etemplate takes care of everything type: 'string'
// If value was set, find the record explicitly. },
if(typeof this.options.value == 'string') regex_replace: {
{ name: 'Regular expression replacement pattern',
widget.options.value = this.getArrayMgr('content').getEntry(this.id+'['+this.options.field+']') || description: 'Only used server-side in a preg_replace with regex to modify the value',
this.getRoot().getArrayMgr('content').getEntry(this.prefix+this.options.value + '['+this.options.field+']'); default: et2_no_init,
} type: 'string'
else if (this.options.field && this.options.value && this.options.value[this.options.field]) },
{ value: {
widget.options.value = this.options.value[this.options.field]; type: 'any'
} },
if(this.options.compare) readonly: {
{ default: true
widget.options.value = widget.options.value == this.options.compare ? 'X' : ''; }
} };
if(this.options.alternate_fields) return et2_entry;
{ }(et2_core_valueWidget_1.et2_valueWidget));
var sum = 0; et2_core_widget_1.et2_register_widget(et2_entry, ["entry", 'contact-value', 'contact-account', 'contact-template', 'infolog-value', 'tracker-value', 'records-value']);
var fields = this.options.alternate_fields.split(':'); //# sourceMappingURL=et2_widget_entry.js.map
for(var i = 0; i < fields.length; i++)
{
var negate = (fields[i][0] == "-");
var value = this.getArrayMgr('content').getEntry(fields[i].replace('-',''))
sum += typeof value === 'undefined' ? 0 : (parseFloat(value) * (negate ? -1 : 1));
if(value && this.options.field !== 'sum')
{
widget.options.value = value;
break;
}
}
if(this.options.field == 'sum')
{
if (this.options.precision && jQuery.isNumeric(sum)) sum = parseFloat(sum).toFixed(this.options.precision);
widget.options.value = sum;
}
}
}
});}).call(this);
et2_register_widget(et2_entry, ["entry", 'contact-value', 'contact-account', 'contact-template', 'infolog-value','tracker-value','records-value']);

View File

@ -0,0 +1,179 @@
/*
* 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
*/
/*egw:uses
et2_core_valueWidget;
*/
import {et2_register_widget, WidgetConfig} from "./et2_core_widget";
import {et2_valueWidget} from "./et2_core_valueWidget";
import {ClassWithAttributes} from "./et2_core_inheritance";
/**
* 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
*/
class et2_entry extends et2_valueWidget
{
static readonly _attributes : any = {
field: {
'name': 'Fields',
'description': 'Which entry field to display, or "sum" to add up the alternate_fields',
'type': 'string'
},
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, (-) used for subtraction',
type: 'string',
default: et2_no_init
},
precision: {
name: 'Decimals to be shown',
description: 'Specifies the number of decimals for sum of alternates, the default is 2',
type: 'string',
default: '2'
},
regex: {
name: 'Regular expression pattern',
description: 'Only used server-side in a preg_replace with regex_replace to modify the value',
default: et2_no_init,
type: 'string'
},
regex_replace: {
name: 'Regular expression replacement pattern',
description: 'Only used server-side in a preg_replace with regex to modify the value',
default: et2_no_init,
type: 'string'
},
value: {
type: 'any'
},
readonly: {
default: true
}
};
legacyOptions : string[] = ["field","compare","alternate_fields"];
static readonly prefix: '~';
protected widget = null;
constructor(_parent, _attrs? : WidgetConfig, _child? : object)
{
// Call the inherited constructor
super(_parent, _attrs, ClassWithAttributes.extendAttributes(et2_entry._attributes, _child || {}));
// Often the ID conflicts, so check prefix
if(_attrs.id && _attrs.id.indexOf(et2_entry.prefix) < 0)
{
_attrs.id = et2_entry.prefix + _attrs.id;
}
let value = _attrs.value;
super(_parent, _attrs, ClassWithAttributes.extendAttributes(et2_entry._attributes, _child || {}));
// Save value from parsing, but only if set
if(value)
{
this.options.value = value;
}
this.widget = null;
this.setDOMNode(document.createElement('span'));
}
loadFromXML(_node)
{
// Load the nodes as usual
super.loadFromXML(_node);
// Do the magic
this.loadField();
}
/**
* Initialize widget for entry field
*/
loadField()
{
// Create widget of correct type
let attrs = {
id: this.id + (this.options.field ? '[' +this.options.field+']' : ''),
type: 'label',
readonly: this.options.readonly
};
let modifications = this.getArrayMgr("modifications");
if(modifications && this.options.field)
{
jQuery.extend(attrs, modifications.getEntry(attrs.id));
}
// Supress labels on templates
if(attrs.type == 'template' && this.options.label)
{
this.egw().debug('log', "Surpressed label on <" + this.getType() + ' label="' + this.options.label + '" id="' + this.id + '"...>');
this.options.label = '';
}
let widget = et2_createWidget(attrs.type, attrs, this);
// 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.getArrayMgr('content').getEntry(this.id+'['+this.options.field+']') ||
this.getRoot().getArrayMgr('content').getEntry(et2_entry.prefix+this.options.value + '['+this.options.field+']');
}
else if (this.options.field && this.options.value && this.options.value[this.options.field])
{
widget.options.value = this.options.value[this.options.field];
}
if(this.options.compare)
{
widget.options.value = widget.options.value == this.options.compare ? 'X' : '';
}
if(this.options.alternate_fields)
{
let sum : number | string = 0;
let fields = this.options.alternate_fields.split(':');
for(let i = 0; i < fields.length; i++)
{
let negate = (fields[i][0] == "-");
let value = this.getArrayMgr('content').getEntry(fields[i].replace('-',''));
sum += typeof value === 'undefined' ? 0 : (parseFloat(value) * (negate ? -1 : 1));
if(value && this.options.field !== 'sum')
{
widget.options.value = value;
break;
}
}
if(this.options.field == 'sum')
{
if (this.options.precision && jQuery.isNumeric(sum)) sum = parseFloat(<string><unknown>sum).toFixed(this.options.precision);
widget.options.value = sum;
}
}
}
}
et2_register_widget(et2_entry, ["entry", 'contact-value', 'contact-account', 'contact-template', 'infolog-value','tracker-value','records-value']);