UNPKG

property-manager

Version:

make it easier to manage the properties/attributes of your class.

165 lines (129 loc) 4.46 kB
import {defineProperty, isFunction} from 'util-ex' import {extend, getPrototypeOf} from 'inherits-ex' import Properties from './properties/index.js' import PropertyManager from './abstract.js' import _defineProperties from './properties/define-properties' import ArrayPropertyManager from './array.js'; function _toPlainType(type) { if (type === String) return 'string'; if (type === Number) return 'number'; if (type === Boolean) return 'boolean'; if (type === Array) return 'array'; if (type === Object) return 'object'; return undefined; } function _generateSchema(managerInstance) { const attrs = managerInstance.$attributes; if (!attrs) return {}; const schema = { type: 'object', properties: {}, required: [], }; for (const key in attrs.properties) { const propDef = attrs.properties[key]; const propValue = managerInstance[key]; let propSchema = {}; if (propValue instanceof ArrayPropertyManager) { // Case 1: Array of items propSchema.type = 'array'; const itemType = propValue.$type; if (itemType && itemType.prototype instanceof PropertyManager) { // Array of nested objects propSchema.items = _generateSchema(new itemType()); } else { // Array of primitive types propSchema.items = { type: _toPlainType(itemType) }; } } else if (propValue instanceof PropertyManager) { // Case 2: Nested object propSchema = _generateSchema(propValue); } else { // Case 3: Primitive type propSchema.type = _toPlainType(propDef.type); } // Assign common properties if (propDef.value !== undefined) { propSchema.default = propDef.value; } if (propDef.required) { schema.required.push(key); } // You can add more mappings here, e.g., minLength, maxLength, etc. if (propDef.title) propSchema.title = propDef.title; if (propDef.description) propSchema.description = propDef.description; schema.properties[key] = propSchema; } if (schema.required.length === 0) { delete schema.required; } return schema; } function _generateUiSchema(managerInstance) { const attrs = managerInstance.$attributes; if (!attrs) return {}; const uiSchema = attrs.uiSchema || {}; for (const key in attrs.properties) { const propValue = managerInstance[key]; if (propValue instanceof PropertyManager) { const nestedUiSchema = _generateUiSchema(propValue); if (Object.keys(nestedUiSchema).length > 0) { uiSchema[key] = { ...uiSchema[key], ...nestedUiSchema }; } } } return uiSchema; } export function AdvancePropertyManager() { if (arguments.length) {this.initialize.apply(this, arguments)} } // merge the methods on the PropertyManager.prototype. extend(AdvancePropertyManager, PropertyManager); AdvancePropertyManager.getProperties = function getProperties() { const result = {}; const attrs = this.prototype.$attributes if (attrs) { for (const k in attrs) { const v = attrs[k]; result[k] = v; } } return result; } AdvancePropertyManager.prototype.getProperties = function getProperties() { return this.$attributes; } AdvancePropertyManager.prototype.defineProperties = function defineProperties(aProperties, recreate = false) { const vAttrs = _defineProperties(this, aProperties, recreate); vAttrs.initializeTo(this); return this; } AdvancePropertyManager.prototype.assignPropertyTo = function assignPropertyTo(dest, src, name, value, attrs, options) { if (!attrs) { attrs = this.getProperties(); } attrs.assignPropertyTo(dest, src, name, value, options); } // --- Gemini Modification Start --- AdvancePropertyManager.prototype.getSchema = function getSchema() { return _generateSchema(this); }; AdvancePropertyManager.prototype.getUiSchema = function getUiSchema() { return _generateUiSchema(this); }; AdvancePropertyManager.prototype.toRjsf = function toRjsf() { return { schema: this.getSchema(), uiSchema: this.getUiSchema(), }; }; // --- Gemini Modification End --- AdvancePropertyManager.prototype.$attributes = null; AdvancePropertyManager.prototype.SMART_ASSIGN = Properties.SMART_ASSIGN; defineProperty(AdvancePropertyManager, '$attributes', undefined, { get() { return getPrototypeOf(this).$attributes; } }); AdvancePropertyManager.defineProperties = _defineProperties; export default AdvancePropertyManager