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
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Nathan Gray
|
|
|
|
* @copyright Nathan Gray 2011
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*egw:uses
|
2011-08-24 12:18:07 +02:00
|
|
|
et2_widget_textbox;
|
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
|
|
|
*/
|
2013-04-13 21:00:13 +02:00
|
|
|
var et2_number = et2_textbox.extend(
|
|
|
|
{
|
2011-08-16 19:02:09 +02:00
|
|
|
attributes: {
|
2012-03-21 22:30:05 +01:00
|
|
|
"value": {
|
|
|
|
"type": "float"
|
|
|
|
},
|
2011-08-16 19:02:09 +02:00
|
|
|
// 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"
|
2011-09-01 01:37:30 +02:00
|
|
|
},
|
|
|
|
"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",
|
2013-11-02 15:49:26 +01:00
|
|
|
"ignore": true
|
2011-08-16 19:02:09 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-04-13 21:00:13 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
2013-11-02 15:49:26 +01:00
|
|
|
*
|
2013-04-13 21:00:13 +02:00
|
|
|
* @memberOf et2_number
|
|
|
|
*/
|
2011-08-24 12:05:52 +02:00
|
|
|
init: function() {
|
2011-08-16 19:02:09 +02:00
|
|
|
this._super.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
2014-06-05 17:48:29 +02:00
|
|
|
transformAttributes: function(_attrs) {
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
|
|
|
|
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: function(_messages)
|
|
|
|
{
|
|
|
|
var ok = true;
|
|
|
|
// if we have a html5 validation error, show it, as this.input.val() will be empty!
|
2014-06-23 16:31:47 +02:00
|
|
|
if (this.input && this.input[0] && this.input[0].validationMessage && !this.input[0].validity.stepMismatch)
|
2014-06-05 17:48:29 +02:00
|
|
|
{
|
|
|
|
_messages.push(this.input[0].validationMessage);
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
return this._super.apply(this, arguments) && ok;
|
|
|
|
},
|
|
|
|
|
2011-08-16 19:02:09 +02:00
|
|
|
createInputWidget: function() {
|
|
|
|
this.input = $j(document.createElement("input"));
|
|
|
|
this.input.attr("type", "number");
|
|
|
|
this.input.addClass("et2_textbox");
|
2014-06-05 17:48:29 +02:00
|
|
|
// bind invalid event to change, to trigger our validation
|
|
|
|
this.input.on('invalid', jQuery.proxy(this.change, this));
|
2011-08-16 19:02:09 +02:00
|
|
|
|
|
|
|
this.setDOMNode(this.input[0]);
|
|
|
|
},
|
|
|
|
|
2014-03-24 19:43:45 +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
|
|
|
|
*/
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-08-16 19:02:09 +02:00
|
|
|
set_min: function(_value) {
|
|
|
|
this.min = _value;
|
|
|
|
if(this.min == null) {
|
|
|
|
this.input.removeAttr("min");
|
|
|
|
} else {
|
|
|
|
this.input.attr("min",this.min);
|
|
|
|
}
|
2011-09-01 01:37:30 +02:00
|
|
|
},
|
|
|
|
set_max: function(_value) {
|
|
|
|
this.max = _value;
|
|
|
|
if(this.max == null) {
|
|
|
|
this.input.removeAttr("max");
|
|
|
|
} else {
|
|
|
|
this.input.attr("max",this.max);
|
|
|
|
}
|
2011-08-16 19:02:09 +02:00
|
|
|
}
|
|
|
|
});
|
2012-03-05 17:53:52 +01:00
|
|
|
et2_register_widget(et2_number, ["int", "integer", "float"]);
|
2011-08-16 19:02:09 +02:00
|
|
|
|
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
|
|
|
*/
|
2013-04-13 21:00:13 +02:00
|
|
|
var et2_number_ro = et2_textbox_ro.extend(
|
|
|
|
{
|
2012-07-26 00:37:56 +02:00
|
|
|
attributes: {
|
2013-11-02 15:49:26 +01:00
|
|
|
min: { ignore: true},
|
|
|
|
max: { ignore: true},
|
|
|
|
precision: { ignore: true},
|
|
|
|
value: { type: "float" }
|
2012-07-26 00:37:56 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
et2_register_widget(et2_number_ro, ["int_ro", "integer_ro", "float_ro"]);
|