2020-01-21 14:18:15 +01:00
|
|
|
/**
|
|
|
|
* EGroupware eTemplate2 - JS Widget base class
|
|
|
|
*
|
|
|
|
* @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
|
2020-01-21 14:18:15 +01:00
|
|
|
* @author Andreas Stöckel
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*egw:uses
|
|
|
|
/vendor/bower-asset/jquery/dist/jquery.js;
|
|
|
|
et2_core_interfaces;
|
|
|
|
et2_core_valueWidget;
|
|
|
|
*/
|
|
|
|
|
2021-06-07 17:33:53 +02:00
|
|
|
import {et2_no_init} from "./et2_core_common";
|
2021-09-02 18:52:10 +02:00
|
|
|
import {ClassWithAttributes} from "./et2_core_inheritance";
|
|
|
|
import {et2_widget, WidgetConfig} from "./et2_core_widget";
|
|
|
|
import {et2_valueWidget} from './et2_core_valueWidget'
|
|
|
|
import {et2_IInput, et2_IInputNode, et2_ISubmitListener} from "./et2_core_interfaces";
|
2021-06-07 17:33:53 +02:00
|
|
|
import {et2_compileLegacyJS} from "./et2_core_legacyJSFunctions";
|
|
|
|
// fixing circular dependencies by only importing the type (not in compiled .js)
|
|
|
|
import type {et2_tabbox} from "./et2_widget_tabs";
|
2020-01-21 14:18:15 +01:00
|
|
|
|
2021-09-02 18:52:10 +02:00
|
|
|
export interface et2_input
|
|
|
|
{
|
|
|
|
getInputNode(): HTMLInputElement | HTMLElement;
|
2021-06-07 17:33:53 +02:00
|
|
|
}
|
2021-09-02 18:52:10 +02:00
|
|
|
|
2020-01-21 14:18:15 +01:00
|
|
|
/**
|
|
|
|
* et2_inputWidget derrives from et2_simpleWidget and implements the IInput
|
|
|
|
* interface. When derriving from this class, call setDOMNode with an input
|
|
|
|
* DOMNode.
|
|
|
|
*/
|
2021-06-07 17:33:53 +02:00
|
|
|
export class et2_inputWidget extends et2_valueWidget implements et2_IInput, et2_ISubmitListener, et2_input
|
2020-01-21 14:18:15 +01:00
|
|
|
{
|
2021-09-02 18:52:10 +02:00
|
|
|
static readonly _attributes: any = {
|
2020-01-21 14:18:15 +01:00
|
|
|
"needed": {
|
2021-09-02 18:52:10 +02:00
|
|
|
"name": "Required",
|
2020-01-21 14:18:15 +01:00
|
|
|
"default": false,
|
|
|
|
"type": "boolean",
|
|
|
|
"description": "If required, the user must enter a value before the form can be submitted"
|
|
|
|
},
|
|
|
|
"onchange": {
|
|
|
|
"name": "onchange",
|
|
|
|
"type": "js",
|
|
|
|
"default": et2_no_init,
|
|
|
|
"description": "JS code which is executed when the value changes."
|
|
|
|
},
|
|
|
|
"onfocus": {
|
|
|
|
"name": "onfocus",
|
|
|
|
"type": "js",
|
|
|
|
"default": et2_no_init,
|
|
|
|
"description": "JS code which get executed when wiget receives focus."
|
|
|
|
},
|
|
|
|
"validation_error": {
|
|
|
|
"name": "Validation Error",
|
|
|
|
"type": "string",
|
|
|
|
"default": et2_no_init,
|
|
|
|
"description": "Used internally to store the validation error that came from the server."
|
|
|
|
},
|
|
|
|
"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."
|
|
|
|
},
|
|
|
|
readonly: {
|
|
|
|
name: "readonly",
|
|
|
|
type: "boolean",
|
|
|
|
"default": false,
|
|
|
|
description: "Does NOT allow user to enter data, just displays existing data"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 15:54:26 +01:00
|
|
|
protected _oldValue: any;
|
2020-01-21 14:18:15 +01:00
|
|
|
onchange: Function;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2021-09-02 18:52:10 +02:00
|
|
|
constructor(_parent, _attrs?: WidgetConfig, _child?: object)
|
2020-01-21 14:18:15 +01:00
|
|
|
{
|
|
|
|
// Call the inherited constructor
|
2020-01-22 18:38:33 +01:00
|
|
|
super(_parent, _attrs, ClassWithAttributes.extendAttributes(et2_inputWidget._attributes, _child || {}));
|
2020-01-21 14:18:15 +01:00
|
|
|
|
|
|
|
// mark value as not initialised, so set_value can determine if it is necessary to trigger change event
|
|
|
|
this._oldValue = et2_no_init;
|
|
|
|
this._labelContainer = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy()
|
|
|
|
{
|
|
|
|
var node = this.getInputNode();
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
jQuery(node).unbind("change.et2_inputWidget");
|
|
|
|
jQuery(node).unbind("focus");
|
|
|
|
}
|
|
|
|
|
|
|
|
super.destroy();
|
|
|
|
|
|
|
|
this._labelContainer = null;
|
|
|
|
}
|
|
|
|
|
2020-06-19 21:27:41 +02:00
|
|
|
/**
|
|
|
|
* Make sure dirty flag is properly set
|
|
|
|
*/
|
2021-09-02 18:52:10 +02:00
|
|
|
doLoadingFinished(): boolean | JQueryPromise<unknown>
|
2020-06-19 21:27:41 +02:00
|
|
|
{
|
|
|
|
let result = super.doLoadingFinished();
|
|
|
|
|
|
|
|
this.resetDirty();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-01-21 14:18:15 +01:00
|
|
|
/**
|
|
|
|
* Load the validation errors from the server
|
|
|
|
*
|
|
|
|
* @param {object} _attrs
|
|
|
|
*/
|
|
|
|
transformAttributes(_attrs)
|
|
|
|
{
|
|
|
|
super.transformAttributes(_attrs);
|
|
|
|
|
|
|
|
// Check whether an validation error entry exists
|
|
|
|
if (this.id && this.getArrayMgr("validation_errors"))
|
|
|
|
{
|
|
|
|
var val = this.getArrayMgr("validation_errors").getEntry(this.id);
|
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
_attrs["validation_error"] = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
attachToDOM()
|
|
|
|
{
|
|
|
|
var node = this.getInputNode();
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
jQuery(node)
|
|
|
|
.off('.et2_inputWidget')
|
2021-09-02 18:52:10 +02:00
|
|
|
.bind("change.et2_inputWidget", this, function (e)
|
|
|
|
{
|
2020-01-21 14:18:15 +01:00
|
|
|
e.data.change.call(e.data, this);
|
|
|
|
})
|
2021-09-02 18:52:10 +02:00
|
|
|
.bind("focus.et2_inputWidget", this, function (e)
|
|
|
|
{
|
2020-01-21 14:18:15 +01:00
|
|
|
e.data.focus.call(e.data, this);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.attachToDOM();
|
|
|
|
|
|
|
|
// jQuery(this.getInputNode()).attr("novalidate","novalidate"); // Stop browser from getting involved
|
|
|
|
// jQuery(this.getInputNode()).validator();
|
|
|
|
}
|
|
|
|
|
|
|
|
detatchFromDOM()
|
|
|
|
{
|
|
|
|
// if(this.getInputNode()) {
|
|
|
|
// jQuery(this.getInputNode()).data("validator").destroy();
|
|
|
|
// }
|
|
|
|
super.detachFromDOM();
|
|
|
|
}
|
|
|
|
|
2020-01-22 20:12:36 +01:00
|
|
|
change(_node, _widget?, _value?)
|
2020-01-21 14:18:15 +01:00
|
|
|
{
|
|
|
|
var messages = [];
|
|
|
|
var valid = this.isValid(messages);
|
|
|
|
|
|
|
|
// Passing false will clear any set messages
|
|
|
|
this.set_validation_error(valid ? false : messages);
|
|
|
|
|
|
|
|
if (valid && this.onchange)
|
|
|
|
{
|
2021-09-02 18:52:10 +02:00
|
|
|
if (typeof this.onchange == 'function')
|
2020-01-21 14:18:15 +01:00
|
|
|
{
|
|
|
|
// Make sure function gets a reference to the widget
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
2021-09-02 18:52:10 +02:00
|
|
|
if (args.indexOf(this) == -1) args.push(this);
|
2020-01-21 14:18:15 +01:00
|
|
|
|
|
|
|
return this.onchange.apply(this, args);
|
2021-09-02 18:52:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-21 14:18:15 +01:00
|
|
|
return (et2_compileLegacyJS(this.options.onchange, this, _node))();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
focus(_node)
|
|
|
|
{
|
2021-09-02 18:52:10 +02:00
|
|
|
if (typeof this.options.onfocus == 'function')
|
2020-01-21 14:18:15 +01:00
|
|
|
{
|
|
|
|
// Make sure function gets a reference to the widget
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
2021-09-02 18:52:10 +02:00
|
|
|
if (args.indexOf(this) == -1) args.push(this);
|
2020-01-21 14:18:15 +01:00
|
|
|
|
|
|
|
return this.options.onfocus.apply(this, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set value of widget and trigger for real changes a change event
|
|
|
|
*
|
|
|
|
* First initialisation (_oldValue === et2_no_init) is NOT considered a change!
|
|
|
|
*
|
|
|
|
* @param {string} _value value to set
|
|
|
|
*/
|
2021-09-02 18:52:10 +02:00
|
|
|
set_value(_value: any | null)
|
2020-01-21 14:18:15 +01:00
|
|
|
{
|
|
|
|
var node = this.getInputNode();
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
jQuery(node).val(_value);
|
2021-09-02 18:52:10 +02:00
|
|
|
if (this.isAttached() && this._oldValue !== et2_no_init && this._oldValue !== _value)
|
2020-01-21 14:18:15 +01:00
|
|
|
{
|
|
|
|
jQuery(node).change();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._oldValue = _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_id(_value)
|
|
|
|
{
|
|
|
|
this.id = _value;
|
2021-09-02 18:52:10 +02:00
|
|
|
this.dom_id = _value && this.getInstanceManager() ? this.getInstanceManager().uniqueId + '_' + this.id : _value;
|
2020-01-21 14:18:15 +01:00
|
|
|
|
|
|
|
// Set the id of the _input_ node (in contrast to the default
|
|
|
|
// implementation, which sets the base node)
|
|
|
|
var node = this.getInputNode();
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
// Unique ID to prevent DOM collisions across multiple templates
|
|
|
|
if (_value != "")
|
|
|
|
{
|
|
|
|
node.setAttribute("id", this.dom_id);
|
|
|
|
node.setAttribute("name", _value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node.removeAttribute("id");
|
|
|
|
node.removeAttribute("name");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set_needed(_value)
|
|
|
|
{
|
|
|
|
var node = this.getInputNode();
|
|
|
|
if (node)
|
|
|
|
{
|
2021-09-02 18:52:10 +02:00
|
|
|
if (_value && !this.options.readonly)
|
|
|
|
{
|
2020-01-21 14:18:15 +01:00
|
|
|
jQuery(node).attr("required", "required");
|
2021-09-02 18:52:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-21 14:18:15 +01:00
|
|
|
node.removeAttribute("required");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set_validation_error(_value)
|
|
|
|
{
|
|
|
|
var node = this.getInputNode();
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
if (_value === false)
|
|
|
|
{
|
|
|
|
this.hideMessage();
|
|
|
|
jQuery(node).removeClass("invalid");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.showMessage(_value, "validation_error");
|
|
|
|
jQuery(node).addClass("invalid");
|
|
|
|
|
|
|
|
// If on a tab, switch to that tab so user can see it
|
2021-09-02 18:52:10 +02:00
|
|
|
let widget: et2_widget = this;
|
|
|
|
while (widget.getParent() && widget.getType() != 'tabbox')
|
2020-01-21 14:18:15 +01:00
|
|
|
{
|
|
|
|
widget = widget.getParent();
|
|
|
|
}
|
|
|
|
if (widget.getType() == 'tabbox') (<et2_tabbox><unknown>widget).activateTab(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set tab index
|
|
|
|
*
|
|
|
|
* @param {number} index
|
|
|
|
*/
|
|
|
|
set_tabindex(index)
|
|
|
|
{
|
|
|
|
jQuery(this.getInputNode()).attr("tabindex", index);
|
|
|
|
}
|
|
|
|
|
|
|
|
getInputNode()
|
|
|
|
{
|
|
|
|
return this.node;
|
|
|
|
}
|
|
|
|
|
|
|
|
get_value()
|
|
|
|
{
|
|
|
|
return this.getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
getValue()
|
|
|
|
{
|
|
|
|
var node = this.getInputNode();
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
var val = jQuery(node).val();
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._oldValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
isDirty()
|
|
|
|
{
|
2020-06-19 21:27:41 +02:00
|
|
|
let value = this.getValue();
|
2021-09-02 18:52:10 +02:00
|
|
|
if (typeof value !== typeof this._oldValue)
|
2020-06-19 21:27:41 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2021-09-02 18:52:10 +02:00
|
|
|
if (this._oldValue === value)
|
2020-06-19 21:27:41 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-02 18:52:10 +02:00
|
|
|
switch (typeof this._oldValue)
|
2020-06-19 21:27:41 +02:00
|
|
|
{
|
|
|
|
case "object":
|
2021-09-02 18:52:10 +02:00
|
|
|
if (typeof this._oldValue.length !== "undefined" &&
|
2020-06-24 17:40:35 +02:00
|
|
|
this._oldValue.length !== value.length
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2021-09-02 18:52:10 +02:00
|
|
|
for (let key in this._oldValue)
|
2020-06-19 21:27:41 +02:00
|
|
|
{
|
2021-09-02 18:52:10 +02:00
|
|
|
if (this._oldValue[key] !== value[key]) return true;
|
2020-06-19 21:27:41 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return this._oldValue != value;
|
|
|
|
}
|
2020-01-21 14:18:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resetDirty()
|
|
|
|
{
|
|
|
|
this._oldValue = this.getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
isValid(messages)
|
|
|
|
{
|
|
|
|
var ok = true;
|
|
|
|
|
|
|
|
// Check for required
|
|
|
|
if (this.options && this.options.needed && !this.options.readonly && !this.disabled &&
|
|
|
|
(this.getValue() == null || this.getValue().valueOf() == ''))
|
|
|
|
{
|
|
|
|
messages.push(this.egw().lang('Field must not be empty !!!'));
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called whenever the template gets submitted. We return false if the widget
|
|
|
|
* is not valid, which cancels the submission.
|
|
|
|
*
|
|
|
|
* @param _values contains the values which will be sent to the server.
|
|
|
|
* Listeners may change these values before they get submitted.
|
|
|
|
*/
|
|
|
|
submit(_values)
|
|
|
|
{
|
|
|
|
var messages = [];
|
|
|
|
var valid = this.isValid(messages);
|
|
|
|
|
|
|
|
// Passing false will clear any set messages
|
|
|
|
this.set_validation_error(valid ? false : messages);
|
|
|
|
return valid;
|
|
|
|
}
|
2021-09-02 18:52:10 +02:00
|
|
|
}
|