UNPKG

@vaadin/hilla-lit-form

Version:

Hilla form utils

172 lines 8.48 kB
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _BinderRoot_defaultValue, _BinderRoot_value, _BinderRoot_emptyValue, _BinderRoot_submitting, _BinderRoot_validating, _BinderRoot_validationRequest, _BinderRoot_config, _BinderRoot_validations, _BinderRoot_context; import { EndpointValidationError } from '@vaadin/hilla-frontend/EndpointErrors.js'; import { _clearValidation, _setErrorsWithDescendants, _update, BinderNode, CHANGED } from './BinderNode.js'; import { getDefaultFieldStrategy } from './Field.js'; import { _parent, createDetachedModel, } from './Models.js'; import { runValidator, ServerValidator, ValidationError, } from './Validation.js'; export class BinderRoot extends BinderNode { constructor(Model, config) { super(createDetachedModel(Model)); _BinderRoot_defaultValue.set(this, void 0); _BinderRoot_value.set(this, void 0); _BinderRoot_emptyValue.set(this, void 0); _BinderRoot_submitting.set(this, false); _BinderRoot_validating.set(this, false); _BinderRoot_validationRequest.set(this, void 0); _BinderRoot_config.set(this, void 0); _BinderRoot_validations.set(this, new Map()); _BinderRoot_context.set(this, this); this.model[_parent] = this; __classPrivateFieldSet(this, _BinderRoot_context, config?.context ?? this, "f"); __classPrivateFieldSet(this, _BinderRoot_config, config, "f"); this.initializeValue(true); __classPrivateFieldSet(this, _BinderRoot_emptyValue, this.value, "f"); } get defaultValue() { return __classPrivateFieldGet(this, _BinderRoot_defaultValue, "f"); } set defaultValue(newValue) { __classPrivateFieldSet(this, _BinderRoot_defaultValue, newValue, "f"); this.dispatchEvent(CHANGED); } get binder() { return this; } get value() { return __classPrivateFieldGet(this, _BinderRoot_value, "f"); } set value(newValue) { if (newValue === __classPrivateFieldGet(this, _BinderRoot_value, "f")) { return; } const oldValue = __classPrivateFieldGet(this, _BinderRoot_value, "f"); __classPrivateFieldSet(this, _BinderRoot_value, newValue, "f"); this[_update](oldValue); } get submitting() { return __classPrivateFieldGet(this, _BinderRoot_submitting, "f"); } get validating() { return __classPrivateFieldGet(this, _BinderRoot_validating, "f"); } read(value) { if (value === undefined || value === null) { this.clear(); return; } this.defaultValue = value; if (this.value && this[_clearValidation]() && this.value === value) { this[_update](this.value); } this.value = this.defaultValue; } reset() { this.read(__classPrivateFieldGet(this, _BinderRoot_defaultValue, "f")); } clear() { this.read(__classPrivateFieldGet(this, _BinderRoot_emptyValue, "f")); } async submit() { const onSubmit = __classPrivateFieldGet(this, _BinderRoot_config, "f")?.onSubmit; if (onSubmit) { return this.submitTo(onSubmit); } return undefined; } async submitTo(endpointMethod) { const errors = await this.validate(); if (errors.length) { throw new ValidationError(errors); } __classPrivateFieldSet(this, _BinderRoot_submitting, true, "f"); this[_update](this.value); this.dispatchEvent(CHANGED); try { return await endpointMethod.call(__classPrivateFieldGet(this, _BinderRoot_context, "f"), this.value); } catch (error) { if (error instanceof EndpointValidationError && error.validationErrorData.length) { const valueErrors = []; error.validationErrorData.forEach((data) => { const res = /Object of type '(.+)' has invalid property '(.+)' with value '(.+)', validation error: '(.+)'/u.exec(data.message); const [property, value, message] = res ? res.splice(2) : [data.parameterName ?? '', undefined, data.message]; valueErrors.push({ message, property: property.replace(/\[(\d+)\]/gu, '.$1'), validator: new ServerValidator(message), value, validatorMessage: data.validatorMessage, }); }); this[_setErrorsWithDescendants](valueErrors); throw new ValidationError(valueErrors); } throw error; } finally { __classPrivateFieldSet(this, _BinderRoot_submitting, false, "f"); this.defaultValue = this.value; this[_update](this.value); } } async requestValidation(model, validator) { let modelValidations; if (__classPrivateFieldGet(this, _BinderRoot_validations, "f").has(model)) { modelValidations = __classPrivateFieldGet(this, _BinderRoot_validations, "f").get(model); } else { modelValidations = new Map(); __classPrivateFieldGet(this, _BinderRoot_validations, "f").set(model, modelValidations); } await this.performValidation(); if (modelValidations.has(validator)) { return modelValidations.get(validator); } const promise = runValidator(model, validator, this.constructor.interpolateMessageCallback); modelValidations.set(validator, promise); const valueErrors = await promise; modelValidations.delete(validator); if (modelValidations.size === 0) { __classPrivateFieldGet(this, _BinderRoot_validations, "f").delete(model); } if (__classPrivateFieldGet(this, _BinderRoot_validations, "f").size === 0) { this.completeValidation(); } return valueErrors; } getFieldStrategy(elm, model) { return getDefaultFieldStrategy(elm, model); } performValidation() { if (!__classPrivateFieldGet(this, _BinderRoot_validationRequest, "f")) { __classPrivateFieldSet(this, _BinderRoot_validating, true, "f"); this.dispatchEvent(CHANGED); __classPrivateFieldSet(this, _BinderRoot_validationRequest, Promise.resolve().then(() => { __classPrivateFieldSet(this, _BinderRoot_validationRequest, undefined, "f"); }), "f"); } return __classPrivateFieldGet(this, _BinderRoot_validationRequest, "f"); } completeValidation() { __classPrivateFieldSet(this, _BinderRoot_validating, false, "f"); this.dispatchEvent(CHANGED); } [(_BinderRoot_defaultValue = new WeakMap(), _BinderRoot_value = new WeakMap(), _BinderRoot_emptyValue = new WeakMap(), _BinderRoot_submitting = new WeakMap(), _BinderRoot_validating = new WeakMap(), _BinderRoot_validationRequest = new WeakMap(), _BinderRoot_config = new WeakMap(), _BinderRoot_validations = new WeakMap(), _BinderRoot_context = new WeakMap(), _update)](oldValue) { __classPrivateFieldGet(this, _BinderRoot_config, "f")?.onChange?.call(__classPrivateFieldGet(this, _BinderRoot_context, "f"), oldValue); this.dispatchEvent(CHANGED); } } //# sourceMappingURL=BinderRoot.js.map