2011-08-16 19:02:09 +02:00
|
|
|
/**
|
2013-04-13 21:00:13 +02:00
|
|
|
* EGroupware eTemplate2 - JS Number object
|
2011-08-16 19:02:09 +02:00
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
2021-06-07 17:33:53 +02:00
|
|
|
* @link https://www.egroupware.org
|
2011-08-16 19:02:09 +02:00
|
|
|
* @author Nathan Gray
|
|
|
|
*/
|
|
|
|
/*egw:uses
|
2020-01-23 14:25:02 +01:00
|
|
|
et2_widget_textbox;
|
2011-08-16 19:02:09 +02:00
|
|
|
*/
|
2021-06-07 17:33:53 +02:00
|
|
|
import { et2_textbox, et2_textbox_ro } from "./et2_widget_textbox";
|
|
|
|
import { et2_register_widget } from "./et2_core_widget";
|
|
|
|
import { ClassWithAttributes } from "./et2_core_inheritance";
|
|
|
|
import { et2_no_init } from "./et2_core_common";
|
2011-08-16 19:02:09 +02:00
|
|
|
/**
|
|
|
|
* Class which implements the "int" and textbox type=float XET-Tags
|
2013-11-02 15:49:26 +01:00
|
|
|
*
|
2013-04-13 21:00:13 +02:00
|
|
|
* @augments et2_textbox
|
2013-11-02 15:49:26 +01:00
|
|
|
*/
|
2021-06-07 17:33:53 +02:00
|
|
|
export class et2_number extends et2_textbox {
|
2020-01-23 14:25:02 +01:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @memberOf et2_number
|
|
|
|
*/
|
2021-06-07 17:33:53 +02:00
|
|
|
constructor(_parent, _attrs, _child) {
|
|
|
|
super(_parent, _attrs, ClassWithAttributes.extendAttributes(et2_number._attributes, _child || {}));
|
|
|
|
this.min = null;
|
|
|
|
this.max = null;
|
|
|
|
this.step = null;
|
2020-01-23 14:25:02 +01:00
|
|
|
}
|
2021-06-07 17:33:53 +02:00
|
|
|
transformAttributes(_attrs) {
|
|
|
|
super.transformAttributes(_attrs);
|
2020-01-23 14:25:02 +01:00
|
|
|
if (typeof _attrs.validator == 'undefined') {
|
|
|
|
_attrs.validator = _attrs.type == 'float' ? '/^-?[0-9]*[,.]?[0-9]*$/' : '/^-?[0-9]*$/';
|
|
|
|
}
|
2021-06-07 17:33:53 +02:00
|
|
|
}
|
2020-01-23 14:25:02 +01:00
|
|
|
/**
|
|
|
|
* Clientside validation using regular expression in "validator" attribute
|
|
|
|
*
|
|
|
|
* @param {array} _messages
|
|
|
|
*/
|
2021-06-07 17:33:53 +02:00
|
|
|
isValid(_messages) {
|
|
|
|
let ok = true;
|
2020-01-23 14:25:02 +01:00
|
|
|
// 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;
|
|
|
|
}
|
2021-06-07 17:33:53 +02:00
|
|
|
return super.isValid(_messages) && ok;
|
|
|
|
}
|
|
|
|
createInputWidget() {
|
2020-01-23 14:25:02 +01:00
|
|
|
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]);
|
2021-06-07 17:33:53 +02:00
|
|
|
}
|
2020-01-23 14:25:02 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2021-06-07 17:33:53 +02:00
|
|
|
set_size(_size) {
|
2020-01-23 14:25:02 +01:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
2021-06-07 17:33:53 +02:00
|
|
|
}
|
|
|
|
set_min(_value) {
|
2020-01-23 14:25:02 +01:00
|
|
|
this.min = _value;
|
|
|
|
if (this.min == null) {
|
|
|
|
this.input.removeAttr("min");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.input.attr("min", this.min);
|
|
|
|
}
|
2021-06-07 17:33:53 +02:00
|
|
|
}
|
|
|
|
set_max(_value) {
|
2020-01-23 14:25:02 +01:00
|
|
|
this.max = _value;
|
|
|
|
if (this.max == null) {
|
|
|
|
this.input.removeAttr("max");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.input.attr("max", this.max);
|
|
|
|
}
|
2021-06-07 17:33:53 +02:00
|
|
|
}
|
|
|
|
set_step(_value) {
|
2020-07-06 18:24:07 +02:00
|
|
|
this.step = _value;
|
|
|
|
if (this.step == null) {
|
|
|
|
this.input.removeAttr("step");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.input.attr("step", this.step);
|
|
|
|
}
|
2021-06-07 17:33:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
et2_number._attributes = {
|
|
|
|
"value": {
|
|
|
|
"type": "float"
|
|
|
|
},
|
|
|
|
// Override default width, numbers are usually shorter
|
|
|
|
"size": {
|
|
|
|
"default": 5
|
|
|
|
},
|
|
|
|
"min": {
|
|
|
|
"name": "Minimum",
|
|
|
|
"type": "any",
|
|
|
|
"default": et2_no_init,
|
|
|
|
"description": "Minimum allowed value"
|
|
|
|
},
|
|
|
|
"max": {
|
|
|
|
"name": "Maximum",
|
|
|
|
"type": "any",
|
|
|
|
"default": et2_no_init,
|
|
|
|
"description": "Maximum allowed value"
|
|
|
|
},
|
|
|
|
"step": {
|
|
|
|
"name": "step value",
|
|
|
|
"type": "integer",
|
|
|
|
"default": et2_no_init,
|
|
|
|
"description": "Step attribute specifies the interval between legal numbers"
|
|
|
|
},
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
et2_register_widget(et2_number, ["int", "integer", "float"]);
|
2012-07-26 00:37:56 +02:00
|
|
|
/**
|
|
|
|
* Extend read-only to tell it to ignore special attributes, which
|
|
|
|
* would cause warnings otherwise
|
2013-04-13 21:00:13 +02:00
|
|
|
* @augments et2_textbox_ro
|
|
|
|
* @class
|
2012-07-26 00:37:56 +02:00
|
|
|
*/
|
2021-06-07 17:33:53 +02:00
|
|
|
export class et2_number_ro extends et2_textbox_ro {
|
|
|
|
set_value(_value) {
|
2020-01-23 14:25:02 +01:00
|
|
|
if (typeof this.options.precision != 'undefined' && "" + _value != "") {
|
|
|
|
_value = parseFloat(_value).toFixed(this.options.precision);
|
|
|
|
}
|
2021-06-07 17:33:53 +02:00
|
|
|
super.set_value(_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
et2_number_ro._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" }
|
|
|
|
};
|
|
|
|
et2_register_widget(et2_number_ro, ["int_ro", "integer_ro", "float_ro"]);
|
2020-01-23 14:25:02 +01:00
|
|
|
//# sourceMappingURL=et2_widget_number.js.map
|