Convert et2_number to TS

This commit is contained in:
Hadi Nategh 2020-01-23 14:25:02 +01:00
parent 605a587bca
commit 00086e37b2
2 changed files with 305 additions and 153 deletions

View File

@ -1,3 +1,4 @@
"use strict";
/** /**
* EGroupware eTemplate2 - JS Number object * EGroupware eTemplate2 - JS Number object
* *
@ -6,170 +7,135 @@
* @subpackage api * @subpackage api
* @link http://www.egroupware.org * @link http://www.egroupware.org
* @author Nathan Gray * @author Nathan Gray
* @copyright Nathan Gray 2011
* @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_widget_textbox; et2_widget_textbox;
*/ */
var et2_widget_textbox_1 = require("./et2_widget_textbox");
var et2_core_widget_1 = require("./et2_core_widget");
var et2_core_inheritance_1 = require("./et2_core_inheritance");
/** /**
* Class which implements the "int" and textbox type=float XET-Tags * Class which implements the "int" and textbox type=float XET-Tags
* *
* @augments et2_textbox * @augments et2_textbox
*/ */
var et2_number = (function(){ "use strict"; return et2_textbox.extend( var et2_number = /** @class */ (function (_super_1) {
{ __extends(et2_number, _super_1);
attributes: { /**
"value": { * Constructor
"type": "float" *
}, * @memberOf et2_number
// Override default width, numbers are usually shorter */
"size": { function et2_number(_parent, _attrs, _child) {
"default": 5 var _this = _super_1.call(this, _parent, _attrs, et2_core_inheritance_1.ClassWithAttributes.extendAttributes(et2_number._attributes, _child || {})) || this;
}, _this.min = null;
"min": { _this.max = null;
"name": "Minimum", return _this;
"type": "integer", }
"default": et2_no_init, et2_number.prototype.transformAttributes = function (_attrs) {
"description": "Minimum allowed value" _super_1.prototype.transformAttributes.call(this, _attrs);
}, if (typeof _attrs.validator == 'undefined') {
"max": { _attrs.validator = _attrs.type == 'float' ? '/^-?[0-9]*[,.]?[0-9]*$/' : '/^-?[0-9]*$/';
"name": "Maximum", }
"type": "integer", };
"default": et2_no_init, /**
"description": "Maximum allowed value" * Clientside validation using regular expression in "validator" attribute
}, *
"precision": { * @param {array} _messages
// TODO: Implement this in some nice way other than HTML5's step attribute */
"name": "Precision", et2_number.prototype.isValid = function (_messages) {
"type": "integer", var ok = true;
"default": et2_no_init, // if we have a html5 validation error, show it, as this.input.val() will be empty!
"description": "Allowed precision - # of decimal places", if (this.input && this.input[0] && this.input[0].validationMessage && !this.input[0].validity.stepMismatch) {
"ignore": true _messages.push(this.input[0].validationMessage);
} ok = false;
}, }
return _super_1.prototype.isValid.call(this, _messages) && ok;
/** };
* Constructor et2_number.prototype.createInputWidget = function () {
* this.input = jQuery(document.createElement("input"));
* @memberOf et2_number this.input.attr("type", "number");
*/ this.input.addClass("et2_textbox");
init: function() { // bind invalid event to change, to trigger our validation
this._super.apply(this, arguments); this.input.on('invalid', jQuery.proxy(this.change, this));
}, if (this.options.onkeypress && typeof this.options.onkeypress == 'function') {
var self = this;
transformAttributes: function(_attrs) { this.input.keypress(function (_ev) {
this._super.apply(this, arguments); return self.options.onkeypress.call(this, _ev, self);
});
if (typeof _attrs.validator == 'undefined') }
{ this.setDOMNode(this.input[0]);
_attrs.validator = _attrs.type == 'float' ? '/^-?[0-9]*[,.]?[0-9]*$/' : '/^-?[0-9]*$/'; };
} /**
}, * Set input widget size
*
/** * Overwritten from et2_textbox as input type=number seems to ignore size,
* Clientside validation using regular expression in "validator" attribute * therefore we set width in em instead, if not et2_fullWidth given.
* *
* @param {array} _messages * @param _size Rather arbitrary size units, approximately characters
*/ */
isValid: function(_messages) et2_number.prototype.set_size = function (_size) {
{ if (typeof _size != 'undefined' && _size != this.input.attr("size")) {
var ok = true; this.size = _size;
// if we have a html5 validation error, show it, as this.input.val() will be empty! this.input.attr("size", this.size);
if (this.input && this.input[0] && this.input[0].validationMessage && !this.input[0].validity.stepMismatch) if (typeof this.options.class == 'undefined' || this.options.class.search('et2_fullWidth') == -1) {
{ this.input.css('width', _size + 'em');
_messages.push(this.input[0].validationMessage); }
ok = false; }
} };
return this._super.apply(this, arguments) && ok; et2_number.prototype.set_min = function (_value) {
}, this.min = _value;
if (this.min == null) {
createInputWidget: function() { this.input.removeAttr("min");
this.input = jQuery(document.createElement("input")); }
this.input.attr("type", "number"); else {
this.input.addClass("et2_textbox"); this.input.attr("min", this.min);
// bind invalid event to change, to trigger our validation }
this.input.on('invalid', jQuery.proxy(this.change, this)); };
if (this.options.onkeypress && typeof this.options.onkeypress == 'function') et2_number.prototype.set_max = function (_value) {
{ this.max = _value;
var self = this; if (this.max == null) {
this.input.keypress(function(_ev) this.input.removeAttr("max");
{ }
return self.options.onkeypress.call(this, _ev, self); else {
}); this.input.attr("max", this.max);
} }
this.setDOMNode(this.input[0]); };
}, return et2_number;
}(et2_widget_textbox_1.et2_textbox));
/** et2_core_widget_1.et2_register_widget(et2_number, ["int", "integer", "float"]);
* Set input widget size
*
* Overwritten from et2_textbox as input type=number seems to ignore size,
* therefore we set width in em instead, if not et2_fullWidth given.
*
* @param _size Rather arbitrary size units, approximately characters
*/
set_size: function(_size) {
if (typeof _size != 'undefined' && _size != this.input.attr("size"))
{
this.size = _size;
this.input.attr("size", this.size);
if (typeof this.options.class == 'undefined' || this.options.class.search('et2_fullWidth') == -1)
{
this.input.css('width', _size+'em');
}
}
},
set_min: function(_value) {
this.min = _value;
if(this.min == null) {
this.input.removeAttr("min");
} else {
this.input.attr("min",this.min);
}
},
set_max: function(_value) {
this.max = _value;
if(this.max == null) {
this.input.removeAttr("max");
} else {
this.input.attr("max",this.max);
}
}
});}).call(this);
et2_register_widget(et2_number, ["int", "integer", "float"]);
/** /**
* Extend read-only to tell it to ignore special attributes, which * Extend read-only to tell it to ignore special attributes, which
* would cause warnings otherwise * would cause warnings otherwise
* @augments et2_textbox_ro * @augments et2_textbox_ro
* @class * @class
*/ */
var et2_number_ro = (function(){ "use strict"; return et2_textbox_ro.extend( var et2_number_ro = /** @class */ (function (_super_1) {
{ __extends(et2_number_ro, _super_1);
attributes: { function et2_number_ro() {
min: { ignore: true}, return _super_1 !== null && _super_1.apply(this, arguments) || this;
max: { ignore: true}, }
precision: { et2_number_ro.prototype.set_value = function (_value) {
name: "Precision", if (typeof this.options.precision != 'undefined' && "" + _value != "") {
type: "integer", _value = parseFloat(_value).toFixed(this.options.precision);
default: et2_no_init, }
description: "Allowed precision - # of decimal places", this._super.call(this, _value);
ignore: true };
}, return et2_number_ro;
value: { type: "float" } }(et2_textbox_ro));
}, et2_core_widget_1.et2_register_widget(et2_number_ro, ["int_ro", "integer_ro", "float_ro"]);
set_value: function(_value) //# sourceMappingURL=et2_widget_number.js.map
{
if (typeof this.options.precision != 'undefined' && ""+_value != "")
{
_value = parseFloat(_value).toFixed(this.options.precision);
}
this._super.call(this, _value);
}
});}).call(this);
et2_register_widget(et2_number_ro, ["int_ro", "integer_ro", "float_ro"]);

View File

@ -0,0 +1,186 @@
/**
* EGroupware eTemplate2 - JS Number object
*
* @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_widget_textbox;
*/
import {et2_textbox} from "./et2_widget_textbox";
import {et2_register_widget, WidgetConfig} from "./et2_core_widget";
import {ClassWithAttributes} from "./et2_core_inheritance";
/**
* Class which implements the "int" and textbox type=float XET-Tags
*
* @augments et2_textbox
*/
class et2_number extends et2_textbox
{
static readonly _attributes: {
"value": {
"type": "float"
},
// Override default width, numbers are usually shorter
"size": {
"default": 5
},
"min": {
"name": "Minimum",
"type": "integer",
"default": et2_no_init,
"description": "Minimum allowed value"
},
"max": {
"name": "Maximum",
"type": "integer",
"default": et2_no_init,
"description": "Maximum allowed value"
},
"precision": {
// TODO: Implement this in some nice way other than HTML5's step attribute
"name": "Precision",
"type": "integer",
"default": et2_no_init,
"description": "Allowed precision - # of decimal places",
"ignore": true
}
};
min : number = null;
max : number = null;
/**
* Constructor
*
* @memberOf et2_number
*/
constructor(_parent?, _attrs? : WidgetConfig, _child? : object) {
super(_parent, _attrs, ClassWithAttributes.extendAttributes(et2_number._attributes, _child || {}));
}
transformAttributes(_attrs)
{
super.transformAttributes(_attrs);
if (typeof _attrs.validator == 'undefined')
{
_attrs.validator = _attrs.type == 'float' ? '/^-?[0-9]*[,.]?[0-9]*$/' : '/^-?[0-9]*$/';
}
}
/**
* Clientside validation using regular expression in "validator" attribute
*
* @param {array} _messages
*/
isValid(_messages)
{
let ok = true;
// if we have a html5 validation error, show it, as this.input.val() will be empty!
if (this.input && this.input[0] && this.input[0].validationMessage && !this.input[0].validity.stepMismatch)
{
_messages.push(this.input[0].validationMessage);
ok = false;
}
return super.isValid(_messages) && ok;
}
createInputWidget()
{
this.input = jQuery(document.createElement("input"));
this.input.attr("type", "number");
this.input.addClass("et2_textbox");
// bind invalid event to change, to trigger our validation
this.input.on('invalid', jQuery.proxy(this.change, this));
if (this.options.onkeypress && typeof this.options.onkeypress == 'function')
{
var self = this;
this.input.keypress(function(_ev)
{
return self.options.onkeypress.call(this, _ev, self);
});
}
this.setDOMNode(this.input[0]);
}
/**
* Set input widget size
*
* Overwritten from et2_textbox as input type=number seems to ignore size,
* therefore we set width in em instead, if not et2_fullWidth given.
*
* @param _size Rather arbitrary size units, approximately characters
*/
set_size(_size)
{
if (typeof _size != 'undefined' && _size != this.input.attr("size"))
{
this.size = _size;
this.input.attr("size", this.size);
if (typeof this.options.class == 'undefined' || this.options.class.search('et2_fullWidth') == -1)
{
this.input.css('width', _size+'em');
}
}
}
set_min(_value)
{
this.min = _value;
if(this.min == null) {
this.input.removeAttr("min");
} else {
this.input.attr("min",this.min);
}
}
set_max(_value)
{
this.max = _value;
if(this.max == null) {
this.input.removeAttr("max");
} else {
this.input.attr("max",this.max);
}
}
}
et2_register_widget(et2_number, ["int", "integer", "float"]);
/**
* Extend read-only to tell it to ignore special attributes, which
* would cause warnings otherwise
* @augments et2_textbox_ro
* @class
*/
class et2_number_ro extends et2_textbox_ro
{
static readonly _attributes: {
min: { ignore: true},
max: { ignore: true},
precision: {
name: "Precision",
type: "integer",
default: et2_no_init,
description: "Allowed precision - # of decimal places",
ignore: true
},
value: { type: "float" }
};
set_value(_value)
{
if (typeof this.options.precision != 'undefined' && ""+_value != "")
{
_value = parseFloat(_value).toFixed(this.options.precision);
}
this._super.call(this, _value);
}
}
et2_register_widget(et2_number_ro, ["int_ro", "integer_ro", "float_ro"]);