UNPKG

@agile-ts/multieditor

Version:

Simple Form Manager for UI-Frameworks

257 lines (254 loc) 7.89 kB
import { defineConfig } from '@agile-ts/utils'; import { Item } from '../item.js'; import { updateNestedProperty } from '../utils.js'; import { Validator } from '../validator/validator.js'; var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; class Multieditor { constructor(config, agileInstance) { this.isModified = false; this.isValid = false; this.submitted = false; this.fixedProperties = []; this.editableProperties = []; this.data = {}; this.agileInstance = () => agileInstance; let _config = typeof config === "function" ? config(this) : config; _config = defineConfig(_config, { key: void 0, fixedProperties: [], editableProperties: Object.keys(_config.initialData), validationSchema: {}, computeMethods: {}, reValidateMode: "onSubmit", toValidate: "editable" }); this._key = _config.key; this.onSubmit = _config.onSubmit; this.fixedProperties = _config.fixedProperties; this.editableProperties = _config.editableProperties; this.config = { reValidateMode: _config.reValidateMode, toValidate: _config.toValidate }; const formattedValidators = {}; Object.keys(_config.validationSchema).forEach((key) => { const validationMethod = _config.validationSchema[key]; if (validationMethod instanceof Validator) { if (validationMethod.key == null) validationMethod.key = key; formattedValidators[key] = validationMethod; } else { formattedValidators[key] = new Validator({ key }).addValidationMethod(validationMethod); } }); for (const key in _config.initialData) { const data = _config.initialData[key]; const item = new Item(this, data, { key, canBeEdited: this.editableProperties.includes(key), validator: formattedValidators[key] }); if (Object.prototype.hasOwnProperty.call(_config.computeMethods, key)) item.computeValue(_config.computeMethods[key]); this.data[key] = item; item.validate(); } } set key(value) { this._key = value; } get key() { return this._key; } get deps() { const deps = []; for (const key in this.data) { const item = this.data[key]; deps.push(item.observers["value"]); deps.push(item.status.observers["value"]); } return deps; } itemDeps(itemKey) { const deps = []; const item = this.getItem(itemKey); if (item) { deps.push(item.observers["value"]); deps.push(item.status.observers["value"]); } return deps; } setValue(itemKey, value, config = {}) { config = defineConfig(config, { background: true }); const path = itemKey.toString().split("."); const item = this.getItem(path.shift()); if (item == null) return this; if (path.length > 0) { item.set(updateNestedProperty(item.nextStateValue, path, value), config); } else { item.set(value, config); } return this; } setInitialValue(itemKey, value, config = {}) { config = defineConfig(config, { background: true, reset: true }); const path = itemKey.toString().split("."); const item = this.getItem(path.shift()); if (item == null) return this; if (path.length > 0) { item.initialStateValue = updateNestedProperty(item.initialStateValue, path, value); } else { item.initialStateValue = value; } if (config.reset) { item.reset(config); } return this; } submit() { return __async(this, arguments, function* (config = {}) { config = defineConfig(config, { assignToInitial: true, onSubmitConfig: void 0 }); for (const key in this.data) { const item = this.data[key]; if (this.canAssignStatusToItemOnSubmit(item)) { item.status.config.display = true; item.status.ingest({ force: true }); } } this.submitted = true; if (!this.isValid) return false; const preparedData = {}; for (const key in this.data) { const item = this.data[key]; if (item.isSet && item.config.canBeEdited) { preparedData[key] = item.value; if (config.assignToInitial) this.setInitialValue(key, item.value); } } for (const key of this.fixedProperties) { const item = this.getItem(key); if (!item) continue; preparedData[key] = item.value; } return yield this.onSubmit(preparedData, config.onSubmitConfig); }); } reset() { for (const key in this.data) this.data[key].reset(); this.isModified = false; this.submitted = false; return this; } setStatus(itemKey, type, message) { const item = this.getItem(itemKey); if (item == null) return this; item.status.set({ type, message }, { waitForTracking: true }); return this; } resetStatus(itemKey) { const item = this.getItem(itemKey); if (item == null || item.status == null) return this; item.status.set(null); return this; } getStatus(itemKey) { var _a; return ((_a = this.getItem(itemKey)) == null ? void 0 : _a.status.value) || null; } getItem(itemKey) { return this.data[itemKey]; } getItemValue(itemKey) { var _a; return (_a = this.getItem(itemKey)) == null ? void 0 : _a.value; } getItemInitialValue(itemKey) { var _a; return (_a = this.getItem(itemKey)) == null ? void 0 : _a.initialStateValue; } areModified(itemKeys) { let _isModified = false; for (const key of itemKeys) { const item = this.getItem(key); if (item == null) continue; _isModified = _isModified || item.isSet; } return _isModified; } recomputeModifiedState() { this.isModified = this.areModified(this.editableProperties); return this; } recomputeValidatedState(config = {}) { config = defineConfig(config, { validate: true }); let isValid = true; for (const key in this.data) { const item = this.data[key]; if (config.validate) item.validate(); if (!item.config.canBeEdited && this.config.toValidate === "editable") continue; isValid = item.isValid && isValid; } this.isValid = isValid; return isValid; } validate() { return this.recomputeValidatedState({ validate: true }); } canAssignStatusToItemOnChange(item) { return (this.config.reValidateMode === "onChange" || this.config.reValidateMode === "afterFirstSubmit" && this.submitted) && (this.config.toValidate === "all" || this.config.toValidate === "editable" && item.config.canBeEdited || false); } canAssignStatusToItemOnSubmit(item) { return (this.config.reValidateMode === "onSubmit" || this.config.reValidateMode === "afterFirstSubmit" && !this.submitted || this.config.reValidateMode === "onChange" && !item.status.config.display) && (this.config.toValidate === "all" || this.config.toValidate === "editable" && item.config.canBeEdited || false); } canAssignStatusToItemOnBlur(item) { return this.config.reValidateMode === "onBlur" && (this.config.toValidate === "all" || this.config.toValidate === "editable" && item.config.canBeEdited || false); } } export { Multieditor };