2022-02-24 23:52:45 +01:00
|
|
|
import {et2_IInput, et2_IInputNode, et2_ISubmitListener} from "../et2_core_interfaces";
|
2021-08-26 20:59:13 +02:00
|
|
|
import {Et2Widget} from "../Et2Widget/Et2Widget";
|
2022-02-25 18:21:16 +01:00
|
|
|
import {css, dedupeMixin, PropertyValues} from "@lion/core";
|
2022-02-24 23:52:45 +01:00
|
|
|
import {ManualMessage} from "../Validators/ManualMessage";
|
|
|
|
import {Required} from "../Validators/Required";
|
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;
|
|
|
|
|
2021-12-10 19:15:02 +01:00
|
|
|
public set_readonly(boolean) : void;
|
|
|
|
|
2021-08-27 19:21:40 +02:00
|
|
|
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) =>
|
|
|
|
{
|
2022-02-24 23:52:45 +01:00
|
|
|
class Et2InputWidgetClass extends Et2Widget(superclass) implements et2_IInput, et2_IInputNode, et2_ISubmitListener
|
2021-08-27 19:21:40 +02:00
|
|
|
{
|
2021-08-25 19:32:15 +02:00
|
|
|
protected _oldValue : string | number | Object;
|
2021-10-13 15:36:33 +02:00
|
|
|
protected node : HTMLElement;
|
2021-08-25 19:32:15 +02:00
|
|
|
|
|
|
|
/** WebComponent **/
|
2021-08-27 19:21:40 +02:00
|
|
|
static get styles()
|
|
|
|
{
|
|
|
|
return [
|
2022-02-25 18:21:16 +01:00
|
|
|
super.styles,
|
|
|
|
css`
|
|
|
|
/* Needed so required can show through */
|
|
|
|
::slotted(input), input {
|
|
|
|
background-color: transparent;
|
|
|
|
}`
|
2021-08-27 19:21:40 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
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-09-16 21:35:41 +02:00
|
|
|
// readonly is what is in the templates
|
|
|
|
// I put this in here so loadWebComponent finds it when it tries to set it from the template
|
|
|
|
readonly: {
|
|
|
|
type: Boolean
|
2022-01-07 00:22:55 +01:00
|
|
|
},
|
|
|
|
|
2022-02-25 18:21:16 +01:00
|
|
|
required: {
|
2022-02-24 23:52:45 +01:00
|
|
|
type: Boolean,
|
|
|
|
reflect: true
|
|
|
|
},
|
2022-01-07 00:22:55 +01:00
|
|
|
onchange: {
|
|
|
|
type: Function
|
|
|
|
},
|
2021-08-25 19:32:15 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-05 14:22:45 +01:00
|
|
|
/**
|
|
|
|
* List of properties that get translated
|
|
|
|
* Done separately to not interfere with properties - if we re-define label property,
|
|
|
|
* labels go missing.
|
|
|
|
* @returns object
|
|
|
|
*/
|
|
|
|
static get translate()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
...super.translate,
|
|
|
|
placeholder: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 11:12:04 +01:00
|
|
|
/**
|
|
|
|
* Compatibility for deprecated name "needed"
|
|
|
|
*
|
|
|
|
* @deprecated use required instead
|
|
|
|
* @param val
|
|
|
|
*/
|
|
|
|
set needed(val : boolean)
|
|
|
|
{
|
|
|
|
this.required = val;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Compatibility for deprecated name "needed"
|
|
|
|
*
|
|
|
|
* @deprecated use required instead
|
|
|
|
*/
|
|
|
|
get needed()
|
|
|
|
{
|
|
|
|
return this.required;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2021-12-07 21:35:25 +01:00
|
|
|
|
2021-10-13 15:36:33 +02:00
|
|
|
connectedCallback()
|
|
|
|
{
|
|
|
|
super.connectedCallback();
|
2022-03-01 23:15:24 +01:00
|
|
|
this._oldChange = this._oldChange.bind(this);
|
|
|
|
this.addEventListener("change", this._oldChange);
|
2021-10-13 15:36:33 +02:00
|
|
|
this.node = this.getInputNode();
|
|
|
|
}
|
2021-12-07 21:35:25 +01:00
|
|
|
|
2022-03-01 23:15:24 +01:00
|
|
|
disconnectedCallback()
|
|
|
|
{
|
|
|
|
super.disconnectedCallback();
|
|
|
|
this.removeEventListener("change", this._oldChange);
|
|
|
|
}
|
|
|
|
|
2022-02-24 23:52:45 +01:00
|
|
|
/**
|
|
|
|
* A property has changed, and we want to make adjustments to other things
|
|
|
|
* based on that
|
|
|
|
*
|
|
|
|
* @param {import('@lion/core').PropertyValues } changedProperties
|
|
|
|
*/
|
|
|
|
updated(changedProperties : PropertyValues)
|
|
|
|
{
|
|
|
|
super.updated(changedProperties);
|
|
|
|
|
2022-02-28 11:12:04 +01:00
|
|
|
// required changed, add / remove validator
|
2022-02-25 18:21:16 +01:00
|
|
|
if(changedProperties.has('required'))
|
2022-02-24 23:52:45 +01:00
|
|
|
{
|
|
|
|
// Remove class
|
|
|
|
this.classList.remove("et2_required")
|
|
|
|
// Remove all existing Required validators (avoids duplicates)
|
|
|
|
this.validators = (this.validators || []).filter((validator) => validator instanceof Required)
|
2022-02-25 18:21:16 +01:00
|
|
|
if(this.required)
|
2022-02-24 23:52:45 +01:00
|
|
|
{
|
|
|
|
this.validators.push(new Required());
|
|
|
|
this.classList.add("et2_required");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 00:22:55 +01:00
|
|
|
/**
|
|
|
|
* Change handler calling custom handler set via onchange attribute
|
|
|
|
*
|
|
|
|
* @param _ev
|
|
|
|
* @returns
|
|
|
|
*/
|
2022-03-01 23:15:24 +01:00
|
|
|
_oldChange(_ev : Event) : boolean
|
2022-01-07 00:22:55 +01:00
|
|
|
{
|
|
|
|
if(typeof this.onchange == 'function')
|
|
|
|
{
|
|
|
|
// Make sure function gets a reference to the widget, splice it in as 2. argument if not
|
|
|
|
let args = Array.prototype.slice.call(arguments);
|
|
|
|
if(args.indexOf(this) == -1)
|
|
|
|
{
|
|
|
|
args.splice(1, 0, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.onchange(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-25 19:32:15 +02:00
|
|
|
set_value(new_value)
|
|
|
|
{
|
2022-01-18 22:13:25 +01:00
|
|
|
this.value = new_value;
|
2022-03-24 16:46:27 +01:00
|
|
|
|
|
|
|
if(typeof this._callParser == "function")
|
|
|
|
{
|
|
|
|
this.modelValue = this._callParser(new_value);
|
|
|
|
}
|
2021-08-25 19:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get_value()
|
|
|
|
{
|
|
|
|
return this.getValue();
|
|
|
|
}
|
|
|
|
|
2021-12-07 21:35:25 +01:00
|
|
|
set_readonly(new_value)
|
|
|
|
{
|
|
|
|
this.readonly = this.readOnly = new_value;
|
|
|
|
}
|
|
|
|
|
2021-08-25 19:32:15 +02:00
|
|
|
getValue()
|
|
|
|
{
|
2022-02-21 19:58:56 +01:00
|
|
|
return this.readOnly ? null : this.value;
|
2021-08-25 19:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2022-02-24 23:52:45 +01:00
|
|
|
/**
|
|
|
|
* Used by etemplate2 to determine if we can submit or not
|
|
|
|
*
|
|
|
|
* @param messages
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2021-08-25 19:32:15 +02:00
|
|
|
isValid(messages)
|
|
|
|
{
|
|
|
|
var ok = true;
|
2022-02-24 23:52:45 +01:00
|
|
|
debugger;
|
2021-08-25 19:32:15 +02:00
|
|
|
|
|
|
|
// Check for required
|
2022-02-28 11:12:04 +01:00
|
|
|
if(this.required && !this.readonly && !this.disabled &&
|
2021-08-25 19:32:15 +02:00
|
|
|
(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;
|
|
|
|
}
|
2022-02-21 15:56:30 +01:00
|
|
|
|
2022-02-24 18:35:53 +01:00
|
|
|
transformAttributes(attrs)
|
|
|
|
{
|
|
|
|
super.transformAttributes(attrs);
|
2022-02-25 18:21:16 +01:00
|
|
|
|
2022-02-24 18:35:53 +01:00
|
|
|
// Check whether an validation error entry exists
|
|
|
|
if(this.id && this.getArrayMgr("validation_errors"))
|
|
|
|
{
|
|
|
|
let val = this.getArrayMgr("validation_errors").getEntry(this.id);
|
|
|
|
if(val)
|
|
|
|
{
|
|
|
|
this.set_validation_error(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 00:55:55 +01:00
|
|
|
set_validation_error(err : string | false)
|
2022-02-21 15:56:30 +01:00
|
|
|
{
|
2022-02-24 18:35:53 +01:00
|
|
|
// ToDo - implement Lion validators properly, most likely by adding to this.validators
|
|
|
|
|
2022-03-02 00:55:55 +01:00
|
|
|
if(err === false)
|
|
|
|
{
|
|
|
|
// Remove all Manual validators
|
|
|
|
this.validators = (this.validators || []).filter((validator) => validator instanceof ManualMessage)
|
|
|
|
return;
|
|
|
|
}
|
2022-02-24 18:35:53 +01:00
|
|
|
// Need to change interaction state so messages show up
|
2022-02-25 18:30:55 +01:00
|
|
|
// submitted is a little heavy-handed, especially on first load, but it works
|
|
|
|
this.submitted = true;
|
2022-03-02 00:55:55 +01:00
|
|
|
|
2022-02-24 18:35:53 +01:00
|
|
|
// Add validator
|
|
|
|
this.validators.push(new ManualMessage(err));
|
|
|
|
// Force a validate - not needed normally, but if you call set_validation_error() manually,
|
|
|
|
// it won't show up without validate()
|
|
|
|
this.validate();
|
2022-02-21 15:56:30 +01:00
|
|
|
}
|
2022-02-24 23:52:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
async submit(_values) : Promise<boolean>
|
|
|
|
{
|
|
|
|
this.submitted = true;
|
|
|
|
|
|
|
|
// If using Lion validators, run them now
|
|
|
|
if(this.validate)
|
|
|
|
{
|
|
|
|
// Force update now
|
|
|
|
this.validate(true);
|
|
|
|
await this.validateComplete;
|
|
|
|
|
|
|
|
return (this.hasFeedbackFor || []).indexOf("error") == -1;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
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);
|