2011-08-10 16:36:31 +02:00
|
|
|
/**
|
2013-04-13 21:00:13 +02:00
|
|
|
* EGroupware eTemplate2 - JS Widget base class
|
2011-08-10 16:36:31 +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 Andreas Stöckel
|
|
|
|
* @copyright Stylite 2011
|
2011-08-16 15:12:39 +02:00
|
|
|
* @version $Id$
|
2011-08-10 16:36:31 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*egw:uses
|
|
|
|
jquery.jquery;
|
2011-08-25 15:35:53 +02:00
|
|
|
et2_core_interfaces;
|
2011-08-24 12:18:07 +02:00
|
|
|
et2_core_valueWidget;
|
2011-08-10 16:36:31 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* et2_inputWidget derrives from et2_simpleWidget and implements the IInput
|
|
|
|
* interface. When derriving from this class, call setDOMNode with an input
|
|
|
|
* DOMNode.
|
2013-12-08 23:56:23 +01:00
|
|
|
*
|
2013-04-13 21:00:13 +02:00
|
|
|
* @augments et2_valueWidget
|
2011-08-10 16:36:31 +02:00
|
|
|
*/
|
2013-12-08 23:56:23 +01:00
|
|
|
var et2_inputWidget = et2_valueWidget.extend([et2_IInput,et2_ISubmitListener],
|
2013-04-13 21:00:13 +02:00
|
|
|
{
|
2011-08-16 23:18:26 +02:00
|
|
|
attributes: {
|
2013-06-26 22:50:10 +02:00
|
|
|
"needed": {
|
2011-08-16 23:18:26 +02:00
|
|
|
"name": "Required",
|
|
|
|
"default": false,
|
|
|
|
"type": "boolean",
|
|
|
|
"description": "If required, the user must enter a value before the form can be submitted"
|
2011-08-18 00:56:49 +02:00
|
|
|
},
|
2011-08-22 16:38:05 +02:00
|
|
|
"onchange": {
|
|
|
|
"name": "onchange",
|
2013-10-09 16:35:03 +02:00
|
|
|
"type": "js",
|
2013-10-10 15:17:07 +02:00
|
|
|
"default": et2_no_init,
|
2011-08-22 16:38:05 +02:00
|
|
|
"description": "JS code which is executed when the value changes."
|
2011-08-23 16:59:49 +02:00
|
|
|
},
|
2013-10-09 11:05:30 +02:00
|
|
|
"onfocus": {
|
|
|
|
"name": "onfocus",
|
2013-10-09 16:35:03 +02:00
|
|
|
"type": "js",
|
2013-10-10 15:17:07 +02:00
|
|
|
"default": et2_no_init,
|
2013-10-09 11:05:30 +02:00
|
|
|
"description": "JS code which get executed when wiget receives focus."
|
|
|
|
},
|
2011-08-23 16:59:49 +02:00
|
|
|
"validation_error": {
|
|
|
|
"name": "Validation Error",
|
|
|
|
"type": "string",
|
|
|
|
"default": et2_no_init,
|
|
|
|
"description": "Used internally to store the validation error that came from the server."
|
2012-07-24 01:54:16 +02:00
|
|
|
},
|
|
|
|
"tabindex": {
|
|
|
|
"name": "Tab index",
|
|
|
|
"type": "integer",
|
|
|
|
"default": et2_no_init,
|
|
|
|
"description": "Specifies the tab order of a widget when the 'tab' button is used for navigating."
|
2011-08-16 23:18:26 +02:00
|
|
|
}
|
|
|
|
},
|
2011-08-21 17:22:00 +02:00
|
|
|
|
2013-04-13 21:00:13 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
2013-12-08 23:56:23 +01:00
|
|
|
*
|
2013-04-13 21:00:13 +02:00
|
|
|
* @memberOf et2_inputWidget
|
|
|
|
*/
|
2011-08-10 16:36:31 +02:00
|
|
|
init: function() {
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
|
|
|
|
this._oldValue = "";
|
2011-08-21 17:22:00 +02:00
|
|
|
this._labelContainer = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function() {
|
2011-08-22 16:38:05 +02:00
|
|
|
var node = this.getInputNode();
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
$j(node).unbind("change.et2_inputWidget");
|
2013-10-09 11:05:30 +02:00
|
|
|
$j(node).unbind("focus");
|
2011-08-22 16:38:05 +02:00
|
|
|
}
|
|
|
|
|
2011-08-21 17:22:00 +02:00
|
|
|
this._super.apply(this, arguments);
|
|
|
|
|
|
|
|
this._labelContainer = null;
|
2011-08-10 16:36:31 +02:00
|
|
|
},
|
|
|
|
|
2011-08-23 16:59:49 +02:00
|
|
|
/**
|
|
|
|
* Load the validation errors from the server
|
|
|
|
*/
|
|
|
|
transformAttributes: function(_attrs) {
|
|
|
|
this._super.apply(this, arguments);
|
2011-08-21 17:22:00 +02:00
|
|
|
|
2011-08-23 16:59:49 +02:00
|
|
|
// Check whether an validation error entry exists
|
2011-09-26 18:01:42 +02:00
|
|
|
if (this.id && this.getArrayMgr("validation_errors"))
|
2011-08-23 16:59:49 +02:00
|
|
|
{
|
2011-09-08 20:36:09 +02:00
|
|
|
var val = this.getArrayMgr("validation_errors").getEntry(this.id);
|
2011-08-23 19:05:05 +02:00
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
_attrs["validation_error"] = val;
|
|
|
|
}
|
2011-08-21 17:22:00 +02:00
|
|
|
}
|
2011-08-23 16:59:49 +02:00
|
|
|
},
|
2011-08-21 17:22:00 +02:00
|
|
|
|
2011-08-23 16:59:49 +02:00
|
|
|
attachToDOM: function() {
|
2011-08-22 16:38:05 +02:00
|
|
|
var node = this.getInputNode();
|
|
|
|
if (node)
|
|
|
|
{
|
2013-11-13 19:17:50 +01:00
|
|
|
$j(node)
|
|
|
|
.off('.et2_inputWidget')
|
|
|
|
.bind("change.et2_inputWidget", this, function(e) {
|
|
|
|
e.data.change.call(e.data, this);
|
|
|
|
})
|
|
|
|
.bind("focus.et2_inputWidget", this, function(e) {
|
|
|
|
e.data.focus.call(e.data, this);
|
|
|
|
});
|
2011-08-22 16:38:05 +02:00
|
|
|
}
|
|
|
|
|
2011-08-17 23:36:08 +02:00
|
|
|
this._super.apply(this,arguments);
|
2013-12-08 23:56:23 +01:00
|
|
|
|
2011-08-23 16:59:49 +02:00
|
|
|
// $j(this.getInputNode()).attr("novalidate","novalidate"); // Stop browser from getting involved
|
|
|
|
// $j(this.getInputNode()).validator();
|
2011-08-17 23:36:08 +02:00
|
|
|
},
|
2011-08-19 18:00:44 +02:00
|
|
|
|
2011-08-17 23:36:08 +02:00
|
|
|
detatchFromDOM: function() {
|
2011-08-23 16:59:49 +02:00
|
|
|
// if(this.getInputNode()) {
|
|
|
|
// $j(this.getInputNode()).data("validator").destroy();
|
|
|
|
// }
|
2011-08-17 23:36:08 +02:00
|
|
|
this._super.apply(this,arguments);
|
|
|
|
},
|
|
|
|
|
2011-08-22 16:38:05 +02:00
|
|
|
change: function(_node) {
|
2013-06-26 22:50:10 +02:00
|
|
|
var messages = [];
|
|
|
|
var valid = this.isValid(messages);
|
2013-12-08 23:56:23 +01:00
|
|
|
|
2013-06-26 22:50:10 +02:00
|
|
|
// Passing false will clear any set messages
|
|
|
|
this.set_validation_error(valid ? false : messages);
|
2013-10-01 19:03:03 +02:00
|
|
|
|
2013-06-26 22:50:10 +02:00
|
|
|
if (valid && this.onchange)
|
2011-08-22 16:38:05 +02:00
|
|
|
{
|
2013-10-01 19:03:03 +02:00
|
|
|
if(typeof this.onchange == 'function')
|
|
|
|
{
|
|
|
|
// Make sure function gets a reference to the widget
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
if(args.indexOf(this) == -1) args.push(this);
|
2013-12-08 23:56:23 +01:00
|
|
|
|
2013-10-01 19:03:03 +02:00
|
|
|
return this.onchange.apply(this, args);
|
|
|
|
} else {
|
|
|
|
return (et2_compileLegacyJS(this.options.onchange, this, _node))();
|
|
|
|
}
|
2011-08-22 16:38:05 +02:00
|
|
|
}
|
2013-06-26 22:50:10 +02:00
|
|
|
return valid;
|
2011-08-22 16:38:05 +02:00
|
|
|
},
|
2013-12-08 23:56:23 +01:00
|
|
|
|
|
|
|
focus: function(_node)
|
2013-10-09 11:05:30 +02:00
|
|
|
{
|
2013-10-09 16:35:03 +02:00
|
|
|
if(typeof this.options.onfocus == 'function')
|
2013-10-09 11:05:30 +02:00
|
|
|
{
|
2013-10-09 16:35:03 +02:00
|
|
|
// Make sure function gets a reference to the widget
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
if(args.indexOf(this) == -1) args.push(this);
|
2013-12-08 23:56:23 +01:00
|
|
|
|
2013-10-09 16:35:03 +02:00
|
|
|
return this.options.onfocus.apply(this, args);
|
2013-10-09 11:05:30 +02:00
|
|
|
}
|
|
|
|
},
|
2011-08-22 16:38:05 +02:00
|
|
|
|
2011-08-10 16:36:31 +02:00
|
|
|
set_value: function(_value) {
|
|
|
|
|
2011-08-11 15:53:35 +02:00
|
|
|
var node = this.getInputNode();
|
|
|
|
if (node)
|
2011-08-10 16:36:31 +02:00
|
|
|
{
|
2011-08-11 15:53:35 +02:00
|
|
|
$j(node).val(_value);
|
2013-10-04 12:07:34 +02:00
|
|
|
if(this.isAttached())
|
|
|
|
{
|
|
|
|
$j(node).change();
|
|
|
|
}
|
2011-08-10 16:36:31 +02:00
|
|
|
}
|
2013-10-04 12:07:34 +02:00
|
|
|
this._oldValue = _value;
|
2011-08-10 16:36:31 +02:00
|
|
|
},
|
|
|
|
|
2011-08-10 19:44:22 +02:00
|
|
|
set_id: function(_value) {
|
2011-08-12 17:26:08 +02:00
|
|
|
this.id = _value;
|
2013-12-08 23:56:23 +01:00
|
|
|
this.dom_id = _value && this.getInstanceManager() ? this.getInstanceManager().uniqueId+'_'+this.id : _value;
|
2011-08-12 17:26:08 +02:00
|
|
|
|
2011-08-15 16:52:45 +02:00
|
|
|
// Set the id of the _input_ node (in contrast to the default
|
|
|
|
// implementation, which sets the base node)
|
2011-08-12 17:26:08 +02:00
|
|
|
var node = this.getInputNode();
|
|
|
|
if (node)
|
|
|
|
{
|
2013-07-17 15:08:48 +02:00
|
|
|
// Unique ID to prevent DOM collisions across multiple templates
|
2011-08-12 17:26:08 +02:00
|
|
|
if (_value != "")
|
|
|
|
{
|
2013-08-23 16:10:37 +02:00
|
|
|
node.setAttribute("id", this.dom_id);
|
2011-08-17 23:36:08 +02:00
|
|
|
node.setAttribute("name", _value);
|
2011-08-12 17:26:08 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node.removeAttribute("id");
|
2011-08-17 23:36:08 +02:00
|
|
|
node.removeAttribute("name");
|
2011-08-12 17:26:08 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-10 19:44:22 +02:00
|
|
|
},
|
2011-08-18 00:56:49 +02:00
|
|
|
|
2013-06-26 22:50:10 +02:00
|
|
|
set_needed: function(_value) {
|
2011-08-16 19:02:09 +02:00
|
|
|
var node = this.getInputNode();
|
|
|
|
if (node)
|
|
|
|
{
|
2013-07-10 20:25:28 +02:00
|
|
|
if(_value && !this.options.readonly) {
|
2011-08-16 19:02:09 +02:00
|
|
|
$j(node).attr("required", "required");
|
|
|
|
} else {
|
|
|
|
node.removeAttribute("required");
|
|
|
|
}
|
|
|
|
}
|
2011-08-16 23:18:26 +02:00
|
|
|
|
2011-08-16 19:02:09 +02:00
|
|
|
},
|
2011-08-10 19:44:22 +02:00
|
|
|
|
2011-08-23 16:59:49 +02:00
|
|
|
set_validation_error: function(_value) {
|
|
|
|
var node = this.getInputNode();
|
|
|
|
if (node)
|
2011-08-21 17:22:00 +02:00
|
|
|
{
|
2011-08-23 16:59:49 +02:00
|
|
|
if (_value === false)
|
|
|
|
{
|
|
|
|
this.hideMessage();
|
|
|
|
$j(node).removeClass("invalid");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.showMessage(_value, "validation_error");
|
|
|
|
$j(node).addClass("invalid");
|
2013-12-08 23:56:23 +01:00
|
|
|
|
2013-07-08 23:55:38 +02:00
|
|
|
// If on a tab, switch to that tab so user can see it
|
|
|
|
var widget = this;
|
|
|
|
var tab = this;
|
|
|
|
while(widget._parent && widget._type !='tabbox')
|
|
|
|
{
|
|
|
|
tab = widget;
|
|
|
|
widget = widget._parent;
|
|
|
|
}
|
|
|
|
if(!widget || typeof widget.setActiveTab == 'undefined') return;
|
|
|
|
var index = widget._children.indexOf(tab);
|
|
|
|
widget.setActiveTab(index);
|
|
|
|
console.log(widget);
|
2011-08-23 16:59:49 +02:00
|
|
|
}
|
2011-08-21 17:22:00 +02:00
|
|
|
}
|
2011-08-10 16:36:31 +02:00
|
|
|
},
|
|
|
|
|
2012-07-24 01:54:16 +02:00
|
|
|
/**
|
|
|
|
* Set tab index
|
|
|
|
*/
|
|
|
|
set_tabindex: function(index) {
|
|
|
|
jQuery(this.getInputNode()).attr("tabindex", index);
|
|
|
|
},
|
|
|
|
|
2011-08-11 15:53:35 +02:00
|
|
|
getInputNode: function() {
|
|
|
|
return this.node;
|
|
|
|
},
|
|
|
|
|
2011-08-21 17:22:00 +02:00
|
|
|
get_value: function() {
|
|
|
|
return this.getValue();
|
|
|
|
},
|
|
|
|
|
2011-08-10 16:36:31 +02:00
|
|
|
getValue: function() {
|
2011-08-11 15:53:35 +02:00
|
|
|
var node = this.getInputNode();
|
|
|
|
if (node)
|
2011-08-10 16:36:31 +02:00
|
|
|
{
|
2011-08-12 14:15:44 +02:00
|
|
|
var val = $j(node).val();
|
|
|
|
|
|
|
|
return val;
|
2011-08-10 16:36:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._oldValue;
|
|
|
|
},
|
|
|
|
|
|
|
|
isDirty: function() {
|
|
|
|
return this._oldValue != this.getValue();
|
|
|
|
},
|
|
|
|
|
|
|
|
resetDirty: function() {
|
|
|
|
this._oldValue = this.getValue();
|
2013-06-26 22:50:10 +02:00
|
|
|
},
|
2013-12-08 23:56:23 +01:00
|
|
|
|
2013-06-26 22:50:10 +02:00
|
|
|
isValid: function(messages) {
|
|
|
|
var ok = true;
|
2013-12-08 23:56:23 +01:00
|
|
|
|
2013-06-26 22:50:10 +02:00
|
|
|
// Check for required
|
2013-10-07 18:11:25 +02:00
|
|
|
if(this.options && this.options.needed && !this.options.readonly && (this.getValue() == null || this.getValue().valueOf() == ''))
|
2013-06-26 22:50:10 +02:00
|
|
|
{
|
|
|
|
messages.push(this.egw().lang('input required'));
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
},
|
2011-08-10 16:36:31 +02:00
|
|
|
|
2013-06-26 22:50:10 +02:00
|
|
|
/**
|
|
|
|
* Called whenever the template gets submitted. We return false if the widget
|
|
|
|
* is not valid, which cancels the submission.
|
2013-12-08 23:56:23 +01:00
|
|
|
*
|
2013-06-26 22:50:10 +02:00
|
|
|
* @param _values contains the values which will be sent to the server.
|
|
|
|
* Listeners may change these values before they get submitted.
|
|
|
|
*/
|
|
|
|
submit: function(_values) {
|
|
|
|
var messages = [];
|
|
|
|
var valid = this.isValid(messages);
|
2013-12-08 23:56:23 +01:00
|
|
|
|
2013-06-26 22:50:10 +02:00
|
|
|
// Passing false will clear any set messages
|
|
|
|
this.set_validation_error(valid ? false : messages);
|
|
|
|
return valid;
|
|
|
|
}
|
2011-08-10 16:36:31 +02:00
|
|
|
});
|
|
|
|
|