egroupware/api/js/etemplate/Et2InputWidget/Et2InputWidget.ts

115 lines
2.2 KiB
TypeScript
Raw Normal View History

import {et2_IInput, et2_IInputNode} from "../et2_core_interfaces";
/**
* This mixin will allow any LitElement to become an Et2InputWidget
*
* Usage:
* export class Et2Button extends Et2InputWidget(Et2Widget(LitWidget)) {...}
*/
type Constructor<T = {}> = new (...args : any[]) => T;
export const Et2InputWidget = <T extends Constructor>(superClass : T) =>
{
class Et2InputWidgetClass extends superClass implements et2_IInput, et2_IInputNode
{
label : string = '';
protected value : string | number | Object;
protected _oldValue : string | number | Object;
/** WebComponent **/
static get properties()
{
return {
...super.properties,
// readOnly is what the property is in Lion, readonly is the attribute
readOnly: {
type: Boolean,
attribute: 'readonly',
reflect: true,
},
};
}
constructor()
{
super();
}
set_value(new_value)
{
this.value = new_value;
}
get_value()
{
return this.getValue();
}
getValue()
{
return typeof this.serializedValue !== "undefined" ? this.serializedValue : this.modalValue;
}
isDirty()
{
let value = this.getValue();
if(typeof value !== typeof this._oldValue)
{
return true;
}
if(this._oldValue === value)
{
return false;
}
switch(typeof this._oldValue)
{
case "object":
if(typeof this._oldValue.length !== "undefined" &&
this._oldValue.length !== value.length
)
{
return true;
}
for(let key in this._oldValue)
{
if(this._oldValue[key] !== value[key])
{
return true;
}
}
return false;
default:
return this._oldValue != value;
}
}
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;
}
getInputNode()
{
// From LionInput
return this._inputNode;
}
};
return Et2InputWidgetClass as unknown as Constructor<et2_IInput> & T;
}