2021-08-25 19:32:15 +02:00
|
|
|
import {et2_IInput, et2_IInputNode} from "../et2_core_interfaces";
|
2021-08-26 20:59:13 +02:00
|
|
|
import {Et2Widget} from "../Et2Widget/Et2Widget";
|
2021-08-27 08:13:36 +02:00
|
|
|
import {dedupeMixin} from "@lion/core";
|
2021-08-25 19:32:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This mixin will allow any LitElement to become an Et2InputWidget
|
|
|
|
*
|
|
|
|
* Usage:
|
2021-08-27 19:21:40 +02:00
|
|
|
* export class Et2Button extends Et2InputWidget(LitWidget)) {...}
|
2021-08-25 19:32:15 +02:00
|
|
|
*/
|
|
|
|
|
2021-08-27 19:21:40 +02:00
|
|
|
/**
|
|
|
|
* Need to define the interface first, to get around TypeScript issues with protected/public
|
|
|
|
* This must match the public API for Et2InputWidgetClass
|
|
|
|
* @see https://lit.dev/docs/composition/mixins/#typing-the-subclass
|
|
|
|
*/
|
|
|
|
export declare class Et2InputWidgetInterface
|
|
|
|
{
|
|
|
|
readOnly : boolean;
|
|
|
|
protected value : string | number | Object;
|
2021-08-26 20:59:13 +02:00
|
|
|
|
2021-08-27 19:21:40 +02:00
|
|
|
public set_value(any) : void;
|
|
|
|
|
|
|
|
public get_value() : any;
|
|
|
|
|
|
|
|
public getValue() : any;
|
|
|
|
|
|
|
|
public isDirty() : boolean;
|
2021-08-25 19:32:15 +02:00
|
|
|
|
2021-08-27 19:21:40 +02:00
|
|
|
public resetDirty() : void;
|
|
|
|
|
|
|
|
public isValid(messages : string[]) : boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Et2InputWidgetMixin = (superclass) =>
|
|
|
|
{
|
|
|
|
class Et2InputWidgetClass extends Et2Widget(superclass) implements et2_IInput, et2_IInputNode
|
|
|
|
{
|
2021-08-25 19:32:15 +02:00
|
|
|
protected value : string | number | Object;
|
|
|
|
protected _oldValue : string | number | Object;
|
|
|
|
|
|
|
|
/** WebComponent **/
|
2021-08-27 19:21:40 +02:00
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
...super.styles
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-08-25 19:32:15 +02:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-26 20:59:13 +02:00
|
|
|
constructor(...args : any[])
|
2021-08-25 19:32:15 +02:00
|
|
|
{
|
2021-08-26 20:59:13 +02:00
|
|
|
super(...args);
|
2021-08-25 19:32:15 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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":
|
2021-08-26 20:59:13 +02:00
|
|
|
if(Array.isArray(this._oldValue) &&
|
2021-08-25 19:32:15 +02:00
|
|
|
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;
|
|
|
|
}
|
2021-08-27 19:21:40 +02:00
|
|
|
}
|
2021-08-26 20:59:13 +02:00
|
|
|
|
2021-08-27 19:21:40 +02:00
|
|
|
return Et2InputWidgetClass;
|
|
|
|
}
|
|
|
|
export const Et2InputWidget = dedupeMixin(Et2InputWidgetMixin);
|