diff --git a/dist/js/context/index.d.ts b/dist/js/context/index.d.ts index d948761d..1a1ae105 100644 --- a/dist/js/context/index.d.ts +++ b/dist/js/context/index.d.ts @@ -1,6 +1,2 @@ -import { JSONSchemaFormDataProvider } from "./json_schema_provider"; -import { ApplicationContextMixin, JobContextMixin, MaterialContextMixin, MaterialsContextMixin, MaterialsSetContextMixin, MethodDataContextMixin, WorkflowContextMixin } from "./mixins"; import { JobContextPickKeysForMixin, WorkflowContextPickKeysForMixin } from "./pickers"; -import { ContextProvider } from "./provider"; -import { ContextProviderRegistryContainer, createAndPatchRegistry, extendAndPatchRegistry } from "./registry"; -export { ContextProvider, ContextProviderRegistryContainer, extendAndPatchRegistry, createAndPatchRegistry, JobContextPickKeysForMixin, JSONSchemaFormDataProvider, WorkflowContextPickKeysForMixin, ApplicationContextMixin, MaterialContextMixin, MaterialsContextMixin, MaterialsSetContextMixin, MethodDataContextMixin, JobContextMixin, WorkflowContextMixin, }; +export { JobContextPickKeysForMixin, WorkflowContextPickKeysForMixin }; diff --git a/dist/js/context/index.js b/dist/js/context/index.js index ff9df80b..40362a12 100644 --- a/dist/js/context/index.js +++ b/dist/js/context/index.js @@ -1,22 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.WorkflowContextMixin = exports.JobContextMixin = exports.MethodDataContextMixin = exports.MaterialsSetContextMixin = exports.MaterialsContextMixin = exports.MaterialContextMixin = exports.ApplicationContextMixin = exports.WorkflowContextPickKeysForMixin = exports.JSONSchemaFormDataProvider = exports.JobContextPickKeysForMixin = exports.createAndPatchRegistry = exports.extendAndPatchRegistry = exports.ContextProviderRegistryContainer = exports.ContextProvider = void 0; -const json_schema_provider_1 = require("./json_schema_provider"); -Object.defineProperty(exports, "JSONSchemaFormDataProvider", { enumerable: true, get: function () { return json_schema_provider_1.JSONSchemaFormDataProvider; } }); -const mixins_1 = require("./mixins"); -Object.defineProperty(exports, "ApplicationContextMixin", { enumerable: true, get: function () { return mixins_1.ApplicationContextMixin; } }); -Object.defineProperty(exports, "JobContextMixin", { enumerable: true, get: function () { return mixins_1.JobContextMixin; } }); -Object.defineProperty(exports, "MaterialContextMixin", { enumerable: true, get: function () { return mixins_1.MaterialContextMixin; } }); -Object.defineProperty(exports, "MaterialsContextMixin", { enumerable: true, get: function () { return mixins_1.MaterialsContextMixin; } }); -Object.defineProperty(exports, "MaterialsSetContextMixin", { enumerable: true, get: function () { return mixins_1.MaterialsSetContextMixin; } }); -Object.defineProperty(exports, "MethodDataContextMixin", { enumerable: true, get: function () { return mixins_1.MethodDataContextMixin; } }); -Object.defineProperty(exports, "WorkflowContextMixin", { enumerable: true, get: function () { return mixins_1.WorkflowContextMixin; } }); +exports.WorkflowContextPickKeysForMixin = exports.JobContextPickKeysForMixin = void 0; const pickers_1 = require("./pickers"); Object.defineProperty(exports, "JobContextPickKeysForMixin", { enumerable: true, get: function () { return pickers_1.JobContextPickKeysForMixin; } }); Object.defineProperty(exports, "WorkflowContextPickKeysForMixin", { enumerable: true, get: function () { return pickers_1.WorkflowContextPickKeysForMixin; } }); -const provider_1 = require("./provider"); -Object.defineProperty(exports, "ContextProvider", { enumerable: true, get: function () { return provider_1.ContextProvider; } }); -const registry_1 = require("./registry"); -Object.defineProperty(exports, "ContextProviderRegistryContainer", { enumerable: true, get: function () { return registry_1.ContextProviderRegistryContainer; } }); -Object.defineProperty(exports, "createAndPatchRegistry", { enumerable: true, get: function () { return registry_1.createAndPatchRegistry; } }); -Object.defineProperty(exports, "extendAndPatchRegistry", { enumerable: true, get: function () { return registry_1.extendAndPatchRegistry; } }); diff --git a/dist/js/context/json_schema_provider.d.ts b/dist/js/context/json_schema_provider.d.ts deleted file mode 100644 index 7dc39b2a..00000000 --- a/dist/js/context/json_schema_provider.d.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { UiSchema } from "react-jsonschema-form"; -import { ContextProvider, ContextProviderConfig } from "./provider"; -interface JSONSchemaFormDataProviderConfig extends ContextProviderConfig { - isUsingJinjaVariables?: boolean; -} -/** - * @summary Provides jsonSchema and uiSchema for generating react-jsonschema-form - * See https://github.com/mozilla-services/react-jsonschema-form for Form UI. - * Form generation example: - * ``` - *
- * ``` - */ -declare class JSONSchemaFormDataProvider extends ContextProvider { - isUsingJinjaVariables: boolean; - constructor(config: JSONSchemaFormDataProviderConfig); - get jsonSchema(): void; - get uiSchema(): UiSchema; - get fields(): {}; - get defaultFieldStyles(): {}; - get uiSchemaStyled(): UiSchema; -} -export { JSONSchemaFormDataProvider }; diff --git a/dist/js/context/json_schema_provider.js b/dist/js/context/json_schema_provider.js deleted file mode 100644 index 67cd2e44..00000000 --- a/dist/js/context/json_schema_provider.js +++ /dev/null @@ -1,46 +0,0 @@ -"use strict"; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.JSONSchemaFormDataProvider = void 0; -const underscore_1 = __importDefault(require("underscore")); -const provider_1 = require("./provider"); -/** - * @summary Provides jsonSchema and uiSchema for generating react-jsonschema-form - * See https://github.com/mozilla-services/react-jsonschema-form for Form UI. - * Form generation example: - * ``` - * - * ``` - */ -class JSONSchemaFormDataProvider extends provider_1.ContextProvider { - constructor(config) { - super(config); - this.isUsingJinjaVariables = Boolean(config === null || config === void 0 ? void 0 : config.isUsingJinjaVariables); - } - get jsonSchema() { - throw new Error("Not implemented."); - } - get uiSchema() { - throw new Error("Not implemented."); - } - get fields() { - return {}; - } - get defaultFieldStyles() { - return {}; - } - get uiSchemaStyled() { - const schema = this.uiSchema; - // @ts-ignore - return underscore_1.default.each(schema, (v, k, l) => { - l[k] = { ...v, ...this.defaultFieldStyles }; - l[k].classNames = `${v.classNames || ""}`; - return null; - }); - } -} -exports.JSONSchemaFormDataProvider = JSONSchemaFormDataProvider; diff --git a/dist/js/context/mixins.d.ts b/dist/js/context/mixins.d.ts deleted file mode 100644 index 37ed8288..00000000 --- a/dist/js/context/mixins.d.ts +++ /dev/null @@ -1,77 +0,0 @@ -import { ApplicationSchemaBase, JobSchema, MaterialSchema, WorkflowSchema } from "@mat3ra/esse/dist/js/types"; -import { InMemoryEntity } from "../entity"; -import type { OrderedInMemoryEntityInSet } from "../entity/set/ordered/OrderedInMemoryEntityInSetMixin"; -type Constructor = new (...args: any[]) => T; -export declare function ApplicationContextMixin(superclass: T): { - new (...args: any): { - [x: string]: any; - _application: ApplicationSchemaBase; - readonly application: ApplicationSchemaBase; - }; -} & T; -type Material = InMemoryEntity & MaterialSchema & { - hash: string; -}; -export declare function MaterialContextMixin(superclass: T): { - new (...args: any): { - [x: string]: any; - _material: Material; - extraData?: { - materialHash: string; - }; - isEdited?: boolean; - readonly isEditedIsSetToFalseOnMaterialUpdate: boolean; - updateMaterialHash(): void; - readonly isMaterialCreatedDefault: boolean; - readonly isMaterialUpdated: boolean; - readonly material: Material; - }; -} & T; -export declare function MaterialsSetContextMixin(superclass: T): { - new (...params: any): { - [x: string]: any; - _materialsSet: any; - readonly materialsSet: any; - sortMaterialsByIndexInSet(materials?: OrderedInMemoryEntityInSet[]): { - getIndexByIdInOrderedSet(setId: string): number; - }[]; - }; -} & T; -export declare function MaterialsContextMixin(superclass: T): { - new (...params: any): { - [x: string]: any; - _materials: any; - readonly materials: any; - }; -} & T; -export declare function MethodDataContextMixin(superclass: T): { - new (...params: any): { - [x: string]: any; - _methodData: any; - isEdited: boolean; - methodDataHash?: string; - extraData?: { - methodDataHash?: string; - }; - _initMethodDataHash(): void; - readonly methodData: any; - readonly isMethodDataUpdated: boolean; - }; -} & T; -export declare function WorkflowContextMixin(superclass: T): { - new (...params: any): { - [x: string]: any; - _workflow: WorkflowSchema; - isEdited: boolean; - readonly workflow: WorkflowSchema; - }; -} & T; -export declare function JobContextMixin(superclass: T): { - new (...params: any): { - [x: string]: any; - _job: JobSchema; - isEdited: boolean; - readonly job: JobSchema; - }; -} & T; -export {}; diff --git a/dist/js/context/mixins.js b/dist/js/context/mixins.js deleted file mode 100644 index d653b26a..00000000 --- a/dist/js/context/mixins.js +++ /dev/null @@ -1,172 +0,0 @@ -"use strict"; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.ApplicationContextMixin = ApplicationContextMixin; -exports.MaterialContextMixin = MaterialContextMixin; -exports.MaterialsSetContextMixin = MaterialsSetContextMixin; -exports.MaterialsContextMixin = MaterialsContextMixin; -exports.MethodDataContextMixin = MethodDataContextMixin; -exports.WorkflowContextMixin = WorkflowContextMixin; -exports.JobContextMixin = JobContextMixin; -const crypto_js_1 = __importDefault(require("crypto-js")); -const utils_1 = require("../entity/set/ordered/utils"); -function ApplicationContextMixin(superclass) { - return class ApplicationContextMixin extends superclass { - constructor(...args) { - super(...args); - // @ts-ignore - if (!this.constructor.Application) { - throw Error("ApplicationContextMixin: Application is undefined"); - } - const config = args[0]; - this._application = - (config.context && config.context.application) || - // @ts-ignore - this.constructor.Application.createDefault(); - } - get application() { - return this._application; - } - }; -} -function MaterialContextMixin(superclass) { - return class MaterialContextMixin extends superclass { - constructor(...args) { - super(...args); - // @ts-ignore - if (!this.constructor.Material) { - throw Error("MaterialContextMixin: Material is undefined"); - } - const config = args[0]; - this._material = config.context && config.context.material; - // @ts-ignore - if (!this._material) - // @ts-ignore - this._material = this.constructor.Material.createDefault(); - this.updateMaterialHash(); - } - // eslint-disable-next-line class-methods-use-this - get isEditedIsSetToFalseOnMaterialUpdate() { - return false; - } - updateMaterialHash() { - if (this.isEditedIsSetToFalseOnMaterialUpdate) - this.isEdited = false; - this.extraData = { materialHash: this.material.hash }; - } - // Workaround: Material.createDefault() used to initiate workflow reducer and hence here too - // does not have an id. Here we catch when such material is used and avoid resetting isEdited - get isMaterialCreatedDefault() { - return !this.material.id; - } - get isMaterialUpdated() { - return Boolean(this.extraData && this.extraData.materialHash !== this.material.hash); - } - get material() { - return this._material; - } - }; -} -function MaterialsSetContextMixin(superclass) { - return class MaterialsSetContextMixin extends superclass { - constructor(...params) { - super(...params); - this._materialsSet = this.config.context && this.config.context.materialsSet; - } - get materialsSet() { - return this._materialsSet; - } - sortMaterialsByIndexInSet(materials = []) { - // DO NOT SORT IN PLACE AS IT CHANGES THE ORDER IN `this.materials` AND HAS SIDE EFFECTS (MaterialViewer). - return materials.concat().sort((a, b) => { - return (0, utils_1.compareEntitiesInOrderedSetForSorting)(a, b, this.materialsSet._id, false); - }); - } - }; -} -function MaterialsContextMixin(superclass) { - return class MaterialsContextMixin extends superclass { - constructor(...params) { - super(...params); - const materials = this.config.context && this.config.context.materials; - // @ts-ignore - if (!this.constructor.Material) { - throw Error("MaterialsContextMixin: Material is undefined"); - } - this._materials = - materials && materials.length - ? materials - : // @ts-ignore - [this.constructor.Material.createDefault()]; - } - get materials() { - return this._materials; - } - }; -} -function MethodDataContextMixin(superclass) { - return class extends superclass { - constructor(...params) { - super(...params); - const config = params[0]; - this._methodData = (config.context && config.context.methodData) || {}; - this.isEdited = Boolean(config.isEdited); - } - /* @summary Replace the logic in constructor with this in order to enable passing `methodDataHash` between - * subsequent initializations of the derived class. Not used at present and kept for the record. - */ - _initMethodDataHash() { - this.methodDataHash = crypto_js_1.default.MD5(JSON.stringify(this.methodData)).toString(); - this.extraData = { methodDataHash: this.methodDataHash }; - if (!this._methodData) { - this._methodData = {}; - this.isEdited = false; - // Commented out to reduce effect on performance. Uncomment for debugging purposes. - // TODO: remove on next refactoring or convert to log - // console.warn("MethodDataContextMixin: methodData is undefined or null"); - } - else if (this.isMethodDataUpdated) { - this.isEdited = false; - } - else { - // @ts-ignore - // eslint-disable-next-line no-undef - this.isEdited = config.isEdited; - } - } - get methodData() { - return this._methodData; - } - get isMethodDataUpdated() { - return Boolean(this.extraData && this.extraData.methodDataHash !== this.methodDataHash); - } - }; -} -function WorkflowContextMixin(superclass) { - return class extends superclass { - constructor(...params) { - super(...params); - const config = params[0]; - this._workflow = (config.context && config.context.workflow) || {}; - this.isEdited = false; // we always get the `defaultData` (recalculated from scratch, not persistent) - } - get workflow() { - return this._workflow; - } - }; -} -function JobContextMixin(superclass) { - return class extends superclass { - constructor(...params) { - super(...params); - const config = params[0]; - this._job = (config.context && config.context.job) || {}; - this.isEdited = false; // we always get the `defaultData` (recalculated from scratch, not persistent) - } - get job() { - return this._job; - } - }; -} diff --git a/dist/js/context/provider.d.ts b/dist/js/context/provider.d.ts deleted file mode 100644 index 39746105..00000000 --- a/dist/js/context/provider.d.ts +++ /dev/null @@ -1,49 +0,0 @@ -export interface ContextProviderConfig { - name: string; - domain?: string; - entityName?: string; - data?: object; - extraData?: object; - isEdited?: boolean; - context?: object; -} -export declare class ContextProvider { - config: ContextProviderConfig; - name: string; - domain?: string; - entityName?: string; - data?: object; - extraData?: object; - isEdited?: boolean; - constructor(config: ContextProviderConfig); - static getConstructorConfig(config: ContextProviderConfig): { - constructor: Function; - config: ContextProviderConfig; - }; - static createConfigFromContext(config: ContextProviderConfig): ContextProviderConfig & ({ - data: any; - extraData: any; - isEdited: any; - } | { - data?: undefined; - extraData?: undefined; - isEdited?: undefined; - }); - setIsEdited(isEdited: boolean): void; - getData(): void | object; - setData(data: object): void; - get defaultData(): void; - transformData(data: object): object; - yieldData(...transformDataArgs: any): { - [x: string]: boolean | object | undefined; - }; - yieldDataForRendering(): { - [x: string]: boolean | object | undefined; - }; - get extraDataKey(): string; - static getExtraDataKeyByName(name: string): string; - get isEditedKey(): string; - static getIsEditedKeyByName(name: string): string; - get isUnitContextProvider(): boolean; - get isSubworkflowContextProvider(): boolean; -} diff --git a/dist/js/context/provider.js b/dist/js/context/provider.js deleted file mode 100644 index 6a5bd15b..00000000 --- a/dist/js/context/provider.js +++ /dev/null @@ -1,110 +0,0 @@ -"use strict"; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.ContextProvider = void 0; -/* - * @summary This is a standalone class that contains "data" for a property with "name". Helps facilitate UI logic. - * Can be initialized from context when user edits are present: - * - user edits the corresponding property, eg. "kpath" - * - isKpathEdited is set to `true` - * - context property is updated for the parent entity (eg. Unit) in a way that persists in Redux state - * - new entity inherits the "data" through "context" field in config - * - `extraData` field is used to store any other data that should be passed from one instance of provider - * to next one, for example data about material to track when it is changed. - * @notes Should hold static data only (see `setData` method), no classes or functions - */ -const lodash_1 = __importDefault(require("lodash")); -const clone_1 = require("../utils/clone"); -class ContextProvider { - constructor(config) { - this.config = config; - this.name = config.name; // property name, ie. "kpath" - this.domain = config.domain || "default"; - // if context is passed inside config, treat it as additional config - // eslint-disable-next-line no-param-reassign - if (config.context) - config = ContextProvider.createConfigFromContext(config); - this.entityName = config.entityName || "unit"; // entity this provider yields data to, eg. "unit", "subworkflow" - this.data = config.data; // property data container - this.extraData = config.extraData; // property extraData container, used track changes to data, for example - this.isEdited = config.isEdited; // whether property was edited by user, available under `isEdited` key - this.setIsEdited = this.setIsEdited.bind(this); - this.getData = this.getData.bind(this); - this.setData = this.setData.bind(this); - this.transformData = this.transformData.bind(this); - this.yieldData = this.yieldData.bind(this); - } - static getConstructorConfig(config) { - return { - constructor: this.prototype.constructor, - config, - }; - } - static createConfigFromContext(config) { - const data = lodash_1.default.get(config.context, config.name); - const isEdited = lodash_1.default.get(config.context, this.getIsEditedKeyByName(config.name)); - const extraData = lodash_1.default.get(config.context, this.getExtraDataKeyByName(config.name)); - return Object.assign(config, data - ? { - data, - extraData, - isEdited, - } - : {}); - } - setIsEdited(isEdited) { - this.isEdited = isEdited; - } - getData() { - return this.isEdited ? this.data : this.defaultData; - } - setData(data) { - this.data = (0, clone_1.deepClone)(data); - } - // override in children - // eslint-disable-next-line class-methods-use-this - get defaultData() { - throw new Error("Not implemented."); - } - // override in children - // eslint-disable-next-line class-methods-use-this - transformData(data) { - return data; - } - // eslint-disable-next-line @typescript-eslint/no-explicit-any - yieldData(...transformDataArgs) { - const extraDataObject = this.extraData ? { [this.extraDataKey]: this.extraData } : {}; - return { - ...extraDataObject, - // @ts-ignore - [this.name]: this.transformData(this.getData(), ...transformDataArgs), - [this.isEditedKey]: this.isEdited, - }; - } - // override when this.data needs additional processing before making it available to rendering context - // used to calculate explicit points path, for example - yieldDataForRendering() { - return this.yieldData(); - } - get extraDataKey() { - return `${this.name}ExtraData`; - } - static getExtraDataKeyByName(name) { - return `${name}ExtraData`; - } - get isEditedKey() { - return `is${lodash_1.default.capitalize(this.name)}Edited`; - } - static getIsEditedKeyByName(name) { - return `is${lodash_1.default.capitalize(name)}Edited`; - } - get isUnitContextProvider() { - return this.entityName === "unit"; - } - get isSubworkflowContextProvider() { - return this.entityName === "subworkflow"; - } -} -exports.ContextProvider = ContextProvider; diff --git a/dist/js/context/registry.d.ts b/dist/js/context/registry.d.ts deleted file mode 100644 index 28fcc1b2..00000000 --- a/dist/js/context/registry.d.ts +++ /dev/null @@ -1,16 +0,0 @@ -export class ContextProviderRegistryContainer { - _providers: any[]; - set providers(p: any[]); - get providers(): any[]; - addProvider({ name, instance }: { - name: any; - instance: any; - }): void; - findProviderInstanceByName(name: any): any; - removeProvider(providerCls: any): void; - removeProviderByName(name: any): void; -} -export function extendAndPatchRegistry(registryContainer: ContextProviderRegistryContainer, classConfigMap: Object, classesToPatch?: Object, defaultSettings?: {}): ContextProviderRegistryContainer; -export function createAndPatchRegistry(classConfigMap: Object, classesToPatch?: { - Material: SpecificMockMaterial; -}, defaultSettings?: {}): ContextProviderRegistryContainer; diff --git a/dist/js/context/registry.js b/dist/js/context/registry.js deleted file mode 100644 index 4cf40090..00000000 --- a/dist/js/context/registry.js +++ /dev/null @@ -1,78 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.createAndPatchRegistry = exports.extendAndPatchRegistry = exports.ContextProviderRegistryContainer = void 0; -class ContextProviderRegistryContainer { - constructor() { - this._providers = []; - } - get providers() { - return this._providers; - } - set providers(p) { - this._providers = p; - } - addProvider({ name, instance }) { - this._providers.push({ - name, - instance, - }); - } - findProviderInstanceByName(name) { - const provider = this.providers.find((p) => p.name === name); - return provider && provider.instance; - } - removeProvider(providerCls) { - this.providers = this.providers.filter((p) => p.name === providerCls.name); - } - removeProviderByName(name) { - this.providers = this.providers.filter((p) => p.name === name); - } -} -exports.ContextProviderRegistryContainer = ContextProviderRegistryContainer; -/** Extends an existing context provider registry container and patches static class variables if applicable. - * - * @param {ContextProviderRegistryContainer} registryContainer - * @param {Object} classConfigMap - * @param {Object} classesToPatch - * @example - * const classConfigMap = { - * PlanewaveCutoffDataManager: { - * providerCls: PlanewaveCutoffsContextProvider, - * config: _makeImportant({ name: "cutoffs", entityName: "subworkflow" }) - * }, - * }; - * - */ -const extendAndPatchRegistry = (registryContainer, classConfigMap, classesToPatch = {}, defaultSettings = {}) => { - Object.entries(classConfigMap).forEach(([name, { providerCls, config }]) => { - Object.entries(classesToPatch).forEach(([clsName, cls]) => { - if (providerCls[clsName]) { - providerCls[clsName] = cls; - } - const providerDefaultSettings = defaultSettings[providerCls.name]; - if (providerDefaultSettings) { - Object.entries(providerDefaultSettings).forEach(([key, value]) => { - if (providerCls[key]) { - providerCls[key] = value; - } - }); - } - }); - registryContainer.addProvider({ - instance: providerCls.getConstructorConfig(config), - name, - }); - }); - return registryContainer; -}; -exports.extendAndPatchRegistry = extendAndPatchRegistry; -/** Creates a new context provider registry container and patches static class variables if applicable. - * - * @param {Object} classConfigMap - * @param {{Material: SpecificMockMaterial}} classesToPatch - */ -const createAndPatchRegistry = (classConfigMap, classesToPatch = {}, defaultSettings = {}) => { - const registryContainer = new ContextProviderRegistryContainer(); - return (0, exports.extendAndPatchRegistry)(registryContainer, classConfigMap, classesToPatch, defaultSettings); -}; -exports.createAndPatchRegistry = createAndPatchRegistry; diff --git a/dist/js/entity/mixins/RuntimeItemsMixin.d.ts b/dist/js/entity/mixins/RuntimeItemsMixin.d.ts new file mode 100644 index 00000000..91521250 --- /dev/null +++ b/dist/js/entity/mixins/RuntimeItemsMixin.d.ts @@ -0,0 +1,35 @@ +import type { NameResultSchema } from "@mat3ra/esse/dist/js/types"; +import type { Constructor } from "../../utils/types"; +import { InMemoryEntity } from "../in_memory"; +export declare enum ItemKey { + results = "results", + monitors = "monitors", + preProcessors = "preProcessors", + postProcessors = "postProcessors" +} +export type BaseRuntimeItemsInMemoryEntity = InMemoryEntity & { + _json: { + results?: NameResultSchema[]; + monitors?: NameResultSchema[]; + preProcessors?: NameResultSchema[]; + postProcessors?: NameResultSchema[]; + }; + defaultResults?: NameResultSchema[]; + defaultMonitors?: NameResultSchema[]; + defaultPreProcessors?: NameResultSchema[]; + defaultPostProcessors?: NameResultSchema[]; +}; +export declare function runtimeItemsMixin(item: BaseRuntimeItemsInMemoryEntity): void; +export type RuntimeItemsInMemoryEntity = { + results: NameResultSchema[]; + monitors: NameResultSchema[]; + preProcessors: NameResultSchema[]; + postProcessors: NameResultSchema[]; + hashObjectFromRuntimeItems: { + results: NameResultSchema[]; + preProcessors: NameResultSchema[]; + postProcessors: NameResultSchema[]; + }; +}; +export type RuntimeItemsInMemoryEntityConstructor = Constructor; +export default function RuntimeItemsMixin>(superclass: S): S & RuntimeItemsInMemoryEntityConstructor; diff --git a/dist/js/entity/mixins/RuntimeItemsMixin.js b/dist/js/entity/mixins/RuntimeItemsMixin.js new file mode 100644 index 00000000..1634358b --- /dev/null +++ b/dist/js/entity/mixins/RuntimeItemsMixin.js @@ -0,0 +1,54 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.ItemKey = void 0; +exports.runtimeItemsMixin = runtimeItemsMixin; +exports.default = RuntimeItemsMixin; +const object_1 = require("../../utils/object"); +var ItemKey; +(function (ItemKey) { + ItemKey["results"] = "results"; + ItemKey["monitors"] = "monitors"; + ItemKey["preProcessors"] = "preProcessors"; + ItemKey["postProcessors"] = "postProcessors"; +})(ItemKey || (exports.ItemKey = ItemKey = {})); +/* + * @summary Contains runtime items: results, monitors, pre/postProcessors + * Is meant to work with Entity, InMemoryEntity b/c of `prop` extraction from `_json`. + */ +function runtimeItemsMixin(item) { + // @ts-expect-error - this is a hack to get the properties of the item + const properties = { + get results() { + var _a; + return this.prop("results", (_a = this.defaultResults) !== null && _a !== void 0 ? _a : []).map(object_1.safeMakeObject); + }, + get monitors() { + var _a; + return this.prop("monitors", (_a = this.defaultMonitors) !== null && _a !== void 0 ? _a : []).map(object_1.safeMakeObject); + }, + get preProcessors() { + var _a; + // TODO: safeMakeObject could return null. Should we throw an error here? + return this.prop("preProcessors", (_a = this.defaultPreProcessors) !== null && _a !== void 0 ? _a : []).map(object_1.safeMakeObject); + }, + get postProcessors() { + var _a; + // TODO: safeMakeObject could return null. Should we throw an error here? + return this.prop("postProcessors", (_a = this.defaultPostProcessors) !== null && _a !== void 0 ? _a : []).map(object_1.safeMakeObject); + }, + get hashObjectFromRuntimeItems() { + return { + results: this.results, + preProcessors: this.preProcessors, + postProcessors: this.postProcessors, + }; + }, + }; + Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties)); +} +function RuntimeItemsMixin(superclass) { + class RuntimeItemsMixin extends superclass { + } + runtimeItemsMixin(RuntimeItemsMixin.prototype); + return RuntimeItemsMixin; +} diff --git a/dist/js/entity/mixins/runtime_items.d.ts b/dist/js/entity/mixins/runtime_items.d.ts index b644dc18..a5e844cb 100644 --- a/dist/js/entity/mixins/runtime_items.d.ts +++ b/dist/js/entity/mixins/runtime_items.d.ts @@ -1,65 +1,19 @@ import { AnyObject } from "@mat3ra/esse/dist/js/esse/types"; import { NameResultSchema, RuntimeItemSchema } from "@mat3ra/esse/dist/js/types"; +import type { Constructor } from "src/js/utils/types"; import { InMemoryEntityConstructor } from "../in_memory"; -export declare enum ItemKey { - results = "results", - monitors = "monitors", - preProcessors = "preProcessors", - postProcessors = "postProcessors" -} -export declare function RuntimeItemsMixin(superclass: T): { - new (...args: any[]): { - readonly results: NameResultSchema[]; - readonly monitors: NameResultSchema[]; - readonly preProcessors: NameResultSchema[]; - readonly postProcessors: NameResultSchema[]; - readonly defaultResults: NameResultSchema[]; - readonly defaultMonitors: NameResultSchema[]; - readonly defaultPreProcessors: NameResultSchema[]; - readonly defaultPostProcessors: NameResultSchema[]; - readonly hashObjectFromRuntimeItems: { - results: NameResultSchema[]; - preProcessors: NameResultSchema[]; - postProcessors: NameResultSchema[]; - }; - _json: AnyObject; - prop(name: string, defaultValue: T_1): T_1; - prop(name: string): T_1 | undefined; - setProp(name: string, value: unknown): void; - unsetProp(name: string): void; - setProps(json?: AnyObject): /*elided*/ any; - toJSON(exclude?: string[]): AnyObject; - toJSONSafe(exclude?: string[]): AnyObject; - toJSONQuick(exclude?: string[]): AnyObject; - clone(extraContext?: object): /*elided*/ any; - validate(): void; - clean(config: AnyObject): AnyObject; - isValid(): boolean; - readonly cls: string; - getClsName(): string; - getAsEntityReference(byIdOnly: true): { - _id: string; - }; - getAsEntityReference(byIdOnly?: false): Required; - getEntityByName(entities: import("../in_memory").InMemoryEntity[], entity: string, name: string): import("../in_memory").InMemoryEntity; - id: string; - _id: string; - schemaVersion: string; - systemName: string; - readonly slug: string; - readonly isSystemEntity: boolean; - }; -} & T; +import RuntimeItemsMixin, { type BaseRuntimeItemsInMemoryEntity, ItemKey } from "./RuntimeItemsMixin"; +export { RuntimeItemsMixin, ItemKey }; export interface RuntimeItemsUILogicJSON extends AnyObject { results?: NameResultSchema[]; monitors?: NameResultSchema[]; preProcessors?: NameResultSchema[]; postProcessors?: NameResultSchema[]; } -export declare function RuntimeItemsUILogicMixin(superclass: T): { +export declare function RuntimeItemsUILogicMixin>(superclass: T): { new (...params: any): { _json: RuntimeItemsUILogicJSON; - getDefaultsByKey(key: ItemKey): NameResultSchema[]; + getDefaultsByKey(key: ItemKey): NameResultSchema[] | undefined; setRuntimeItemsToDefaultValues(): void; /** * @summary Must pass config for subclasses to override and use initialization logic @@ -79,19 +33,6 @@ export declare function RuntimeItemsUILogicMixin(name: string, defaultValue: T_1): T_1; prop(name: string): T_1 | undefined; setProp(name: string, value: unknown): void; @@ -117,6 +58,19 @@ export declare function RuntimeItemsUILogicMixin(superclass: T): { diff --git a/dist/js/entity/mixins/runtime_items.js b/dist/js/entity/mixins/runtime_items.js index 0e381cc6..83e5fe83 100644 --- a/dist/js/entity/mixins/runtime_items.js +++ b/dist/js/entity/mixins/runtime_items.js @@ -1,66 +1,53 @@ "use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || (function () { + var ownKeys = function(o) { + ownKeys = Object.getOwnPropertyNames || function (o) { + var ar = []; + for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; + return ar; + }; + return ownKeys(o); + }; + return function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); + __setModuleDefault(result, mod); + return result; + }; +})(); Object.defineProperty(exports, "__esModule", { value: true }); -exports.ItemKey = void 0; -exports.RuntimeItemsMixin = RuntimeItemsMixin; +exports.ItemKey = exports.RuntimeItemsMixin = void 0; exports.RuntimeItemsUILogicMixin = RuntimeItemsUILogicMixin; exports.RuntimeItemsUIAllowedMixin = RuntimeItemsUIAllowedMixin; const object_1 = require("../../utils/object"); -var ItemKey; -(function (ItemKey) { - ItemKey["results"] = "results"; - ItemKey["monitors"] = "monitors"; - ItemKey["preProcessors"] = "preProcessors"; - ItemKey["postProcessors"] = "postProcessors"; -})(ItemKey || (exports.ItemKey = ItemKey = {})); -/* - * @summary Contains runtime items: results, monitors, pre/postProcessors - * Is meant to work with Entity, InMemoryEntity b/c of `prop` extraction from `_json`. - */ -function RuntimeItemsMixin(superclass) { - return class extends superclass { - get results() { - return this.prop("results", this.defaultResults).map(object_1.safeMakeObject); - } - get monitors() { - return this.prop("monitors", this.defaultMonitors).map(object_1.safeMakeObject); - } - get preProcessors() { - // TODO: safeMakeObject could return null. Should we throw an error here? - return this.prop("preProcessors", this.defaultPreProcessors).map(object_1.safeMakeObject); - } - get postProcessors() { - // TODO: safeMakeObject could return null. Should we throw an error here? - return this.prop("postProcessors", this.defaultPostProcessors).map(object_1.safeMakeObject); - } - get defaultResults() { - return []; - } - get defaultMonitors() { - return []; - } - get defaultPreProcessors() { - return []; - } - get defaultPostProcessors() { - return []; - } - get hashObjectFromRuntimeItems() { - return { - results: this.results, - preProcessors: this.preProcessors, - postProcessors: this.postProcessors, - }; - } - }; -} +const RuntimeItemsMixin_1 = __importStar(require("./RuntimeItemsMixin")); +exports.RuntimeItemsMixin = RuntimeItemsMixin_1.default; +Object.defineProperty(exports, "ItemKey", { enumerable: true, get: function () { return RuntimeItemsMixin_1.ItemKey; } }); const allKeys = [ - ItemKey.results, - ItemKey.monitors, - ItemKey.postProcessors, - ItemKey.preProcessors, + RuntimeItemsMixin_1.ItemKey.results, + RuntimeItemsMixin_1.ItemKey.monitors, + RuntimeItemsMixin_1.ItemKey.postProcessors, + RuntimeItemsMixin_1.ItemKey.preProcessors, ]; function RuntimeItemsUILogicMixin(superclass) { - return class extends RuntimeItemsMixin(superclass) { + return class extends (0, RuntimeItemsMixin_1.default)(superclass) { // eslint-disable-next-line @typescript-eslint/no-explicit-any constructor(...params) { super(...params); @@ -68,13 +55,13 @@ function RuntimeItemsUILogicMixin(superclass) { this._initRuntimeItems(allKeys, config); } getDefaultsByKey(key) { - if (key === ItemKey.results) { + if (key === RuntimeItemsMixin_1.ItemKey.results) { return this.defaultResults; } - if (key === ItemKey.monitors) { + if (key === RuntimeItemsMixin_1.ItemKey.monitors) { return this.defaultMonitors; } - if (key === ItemKey.preProcessors) { + if (key === RuntimeItemsMixin_1.ItemKey.preProcessors) { return this.defaultPreProcessors; } return this.defaultPostProcessors; @@ -98,7 +85,7 @@ function RuntimeItemsUILogicMixin(superclass) { }); } // eslint-disable-next-line default-param-last - _addRuntimeItem(key = ItemKey.results, config) { + _addRuntimeItem(key = RuntimeItemsMixin_1.ItemKey.results, config) { const runtimeItems = this._json[key]; if (!runtimeItems) { throw new Error("not found"); @@ -106,7 +93,7 @@ function RuntimeItemsUILogicMixin(superclass) { runtimeItems.push((0, object_1.safeMakeObject)(config)); } // eslint-disable-next-line default-param-last - _removeRuntimeItem(key = ItemKey.results, config) { + _removeRuntimeItem(key = RuntimeItemsMixin_1.ItemKey.results, config) { const newConfig = (0, object_1.safeMakeObject)(config); this._removeRuntimeItemByName(key, (newConfig === null || newConfig === void 0 ? void 0 : newConfig.name) || ""); } @@ -115,7 +102,7 @@ function RuntimeItemsUILogicMixin(superclass) { } _toggleRuntimeItem( // eslint-disable-next-line default-param-last - key = ItemKey.results, data, isAdding) { + key = RuntimeItemsMixin_1.ItemKey.results, data, isAdding) { if (isAdding) { this._addRuntimeItem(key, data); } @@ -124,16 +111,16 @@ function RuntimeItemsUILogicMixin(superclass) { } } toggleResult(data, isAdding) { - this._toggleRuntimeItem(ItemKey.results, data, isAdding); + this._toggleRuntimeItem(RuntimeItemsMixin_1.ItemKey.results, data, isAdding); } toggleMonitor(data, isAdding) { - this._toggleRuntimeItem(ItemKey.monitors, data, isAdding); + this._toggleRuntimeItem(RuntimeItemsMixin_1.ItemKey.monitors, data, isAdding); } togglePreProcessor(data, isAdding) { - this._toggleRuntimeItem(ItemKey.preProcessors, data, isAdding); + this._toggleRuntimeItem(RuntimeItemsMixin_1.ItemKey.preProcessors, data, isAdding); } togglePostProcessor(data, isAdding) { - this._toggleRuntimeItem(ItemKey.postProcessors, data, isAdding); + this._toggleRuntimeItem(RuntimeItemsMixin_1.ItemKey.postProcessors, data, isAdding); } get resultNames() { return this.results.map((r) => { diff --git a/dist/js/entity/other.d.ts b/dist/js/entity/other.d.ts index 7cdd7b15..b2e97b20 100644 --- a/dist/js/entity/other.d.ts +++ b/dist/js/entity/other.d.ts @@ -320,13 +320,13 @@ export declare const NamedDefaultableRepetitionRuntimeItemsImportantSettingsCont } & { new (...params: any): { _json: import("./mixins/runtime_items").RuntimeItemsUILogicJSON; - getDefaultsByKey(key: import("./mixins/runtime_items").ItemKey): import("@mat3ra/esse/dist/js/types").NameResultSchema[]; + getDefaultsByKey(key: import("./mixins/RuntimeItemsMixin").ItemKey): import("@mat3ra/esse/dist/js/types").NameResultSchema[] | undefined; setRuntimeItemsToDefaultValues(): void; - _initRuntimeItems(keys: import("./mixins/runtime_items").ItemKey[], _config: object): void; - _addRuntimeItem(key: import("./mixins/runtime_items").ItemKey | undefined, config: import("@mat3ra/esse/dist/js/types").RuntimeItemSchema): void; - _removeRuntimeItem(key: import("./mixins/runtime_items").ItemKey | undefined, config: import("@mat3ra/esse/dist/js/types").RuntimeItemSchema): void; - _removeRuntimeItemByName(key: import("./mixins/runtime_items").ItemKey, name: string): void; - _toggleRuntimeItem(key: import("./mixins/runtime_items").ItemKey | undefined, data: import("@mat3ra/esse/dist/js/types").RuntimeItemSchema, isAdding: boolean): void; + _initRuntimeItems(keys: import("./mixins/RuntimeItemsMixin").ItemKey[], _config: object): void; + _addRuntimeItem(key: import("./mixins/RuntimeItemsMixin").ItemKey | undefined, config: import("@mat3ra/esse/dist/js/types").RuntimeItemSchema): void; + _removeRuntimeItem(key: import("./mixins/RuntimeItemsMixin").ItemKey | undefined, config: import("@mat3ra/esse/dist/js/types").RuntimeItemSchema): void; + _removeRuntimeItemByName(key: import("./mixins/RuntimeItemsMixin").ItemKey, name: string): void; + _toggleRuntimeItem(key: import("./mixins/RuntimeItemsMixin").ItemKey | undefined, data: import("@mat3ra/esse/dist/js/types").RuntimeItemSchema, isAdding: boolean): void; toggleResult(data: import("@mat3ra/esse/dist/js/types").RuntimeItemSchema, isAdding: boolean): void; toggleMonitor(data: import("@mat3ra/esse/dist/js/types").RuntimeItemSchema, isAdding: boolean): void; togglePreProcessor(data: import("@mat3ra/esse/dist/js/types").RuntimeItemSchema, isAdding: boolean): void; @@ -336,19 +336,6 @@ export declare const NamedDefaultableRepetitionRuntimeItemsImportantSettingsCont readonly postProcessorNames: string[]; readonly preProcessorNames: string[]; getResultByName(name: string): import("@mat3ra/esse/dist/js/types").NameResultSchema | undefined; - readonly results: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; - readonly monitors: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; - readonly preProcessors: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; - readonly postProcessors: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; - readonly defaultResults: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; - readonly defaultMonitors: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; - readonly defaultPreProcessors: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; - readonly defaultPostProcessors: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; - readonly hashObjectFromRuntimeItems: { - results: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; - preProcessors: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; - postProcessors: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; - }; prop(name: string, defaultValue: T): T; prop(name: string): T | undefined; setProp(name: string, value: unknown): void; @@ -374,6 +361,19 @@ export declare const NamedDefaultableRepetitionRuntimeItemsImportantSettingsCont systemName: string; readonly slug: string; readonly isSystemEntity: boolean; + defaultResults?: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; + defaultMonitors?: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; + defaultPreProcessors?: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; + defaultPostProcessors?: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; + results: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; + monitors: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; + preProcessors: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; + postProcessors: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; + hashObjectFromRuntimeItems: { + results: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; + preProcessors: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; + postProcessors: import("@mat3ra/esse/dist/js/types").NameResultSchema[]; + }; }; } & { new (...args: any[]): { diff --git a/package.json b/package.json index c2fac1df..548b7f20 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,6 @@ "mathjs": "^5.10.3", "mixwith": "^0.1.1", "nunjucks": "^3.2.4", - "react-jsonschema-form": "^1.8.1", "semver": "^7.6.3", "underscore": "^1.13.7", "underscore.string": "^3.3.6", @@ -63,7 +62,6 @@ "@types/json-schema": "^7.0.15", "@types/mocha": "^10.0.8", "@types/node": "^20.16.10", - "@types/react-jsonschema-form": "^1.7.13", "@types/underscore": "^1.11.15", "@typescript-eslint/eslint-plugin": "^5.62.0", "@typescript-eslint/parser": "^5.62.0", diff --git a/src/js/context/index.ts b/src/js/context/index.ts index 1c59abec..a6cd4f1b 100644 --- a/src/js/context/index.ts +++ b/src/js/context/index.ts @@ -1,34 +1,3 @@ -import { JSONSchemaFormDataProvider } from "./json_schema_provider"; -import { - ApplicationContextMixin, - JobContextMixin, - MaterialContextMixin, - MaterialsContextMixin, - MaterialsSetContextMixin, - MethodDataContextMixin, - WorkflowContextMixin, -} from "./mixins"; import { JobContextPickKeysForMixin, WorkflowContextPickKeysForMixin } from "./pickers"; -import { ContextProvider } from "./provider"; -import { - ContextProviderRegistryContainer, - createAndPatchRegistry, - extendAndPatchRegistry, -} from "./registry"; -export { - ContextProvider, - ContextProviderRegistryContainer, - extendAndPatchRegistry, - createAndPatchRegistry, - JobContextPickKeysForMixin, - JSONSchemaFormDataProvider, - WorkflowContextPickKeysForMixin, - ApplicationContextMixin, - MaterialContextMixin, - MaterialsContextMixin, - MaterialsSetContextMixin, - MethodDataContextMixin, - JobContextMixin, - WorkflowContextMixin, -}; +export { JobContextPickKeysForMixin, WorkflowContextPickKeysForMixin }; diff --git a/src/js/context/json_schema_provider.ts b/src/js/context/json_schema_provider.ts deleted file mode 100644 index cf41d84c..00000000 --- a/src/js/context/json_schema_provider.ts +++ /dev/null @@ -1,56 +0,0 @@ -/* eslint-disable class-methods-use-this */ -import { UiSchema } from "react-jsonschema-form"; -import _ from "underscore"; - -import { ContextProvider, ContextProviderConfig } from "./provider"; - -interface JSONSchemaFormDataProviderConfig extends ContextProviderConfig { - isUsingJinjaVariables?: boolean; -} - -/** - * @summary Provides jsonSchema and uiSchema for generating react-jsonschema-form - * See https://github.com/mozilla-services/react-jsonschema-form for Form UI. - * Form generation example: - * ``` - * - * ``` - */ -class JSONSchemaFormDataProvider extends ContextProvider { - isUsingJinjaVariables: boolean; - - constructor(config: JSONSchemaFormDataProviderConfig) { - super(config); - this.isUsingJinjaVariables = Boolean(config?.isUsingJinjaVariables); - } - - get jsonSchema() { - throw new Error("Not implemented."); - } - - get uiSchema(): UiSchema { - throw new Error("Not implemented."); - } - - get fields() { - return {}; - } - - get defaultFieldStyles() { - return {}; - } - - get uiSchemaStyled(): UiSchema { - const schema = this.uiSchema; - // @ts-ignore - return _.each(schema, (v: UiSchema, k: string, l: UiSchema) => { - l[k] = { ...v, ...this.defaultFieldStyles }; - l[k].classNames = `${v.classNames || ""}`; - return null; - }); - } -} - -export { JSONSchemaFormDataProvider }; diff --git a/src/js/context/mixins.ts b/src/js/context/mixins.ts deleted file mode 100644 index 0cde0edf..00000000 --- a/src/js/context/mixins.ts +++ /dev/null @@ -1,236 +0,0 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ -import { - ApplicationSchemaBase, - JobSchema, - MaterialSchema, - WorkflowSchema, -} from "@mat3ra/esse/dist/js/types"; -import CryptoJS from "crypto-js"; - -import { InMemoryEntity } from "../entity"; -import { DefaultableMixin } from "../entity/mixins/props"; -import type { OrderedInMemoryEntityInSet } from "../entity/set/ordered/OrderedInMemoryEntityInSetMixin"; -import { compareEntitiesInOrderedSetForSorting } from "../entity/set/ordered/utils"; - -type Constructor = new (...args: any[]) => T; - -type Defaultable = ReturnType; - -export function ApplicationContextMixin(superclass: T) { - return class ApplicationContextMixin extends superclass { - _application: ApplicationSchemaBase; - - constructor(...args: any) { - super(...args); - // @ts-ignore - if (!this.constructor.Application) { - throw Error("ApplicationContextMixin: Application is undefined"); - } - const config = args[0]; - this._application = - (config.context && config.context.application) || - // @ts-ignore - (this.constructor.Application as Defaultable).createDefault(); - } - - get application() { - return this._application; - } - }; -} - -type Material = InMemoryEntity & - MaterialSchema & { - hash: string; - }; - -export function MaterialContextMixin(superclass: T) { - return class MaterialContextMixin extends superclass { - _material: Material; - - extraData?: { - materialHash: string; - }; - - declare isEdited?: boolean; - - constructor(...args: any) { - super(...args); - // @ts-ignore - if (!this.constructor.Material) { - throw Error("MaterialContextMixin: Material is undefined"); - } - const config = args[0]; - this._material = config.context && config.context.material; - // @ts-ignore - if (!this._material) - // @ts-ignore - this._material = (this.constructor.Material as Defaultable).createDefault(); - this.updateMaterialHash(); - } - - // eslint-disable-next-line class-methods-use-this - get isEditedIsSetToFalseOnMaterialUpdate() { - return false; - } - - updateMaterialHash() { - if (this.isEditedIsSetToFalseOnMaterialUpdate) this.isEdited = false; - this.extraData = { materialHash: this.material.hash }; - } - - // Workaround: Material.createDefault() used to initiate workflow reducer and hence here too - // does not have an id. Here we catch when such material is used and avoid resetting isEdited - get isMaterialCreatedDefault() { - return !this.material.id; - } - - get isMaterialUpdated() { - return Boolean(this.extraData && this.extraData.materialHash !== this.material.hash); - } - - get material() { - return this._material; - } - }; -} - -export function MaterialsSetContextMixin(superclass: T) { - return class MaterialsSetContextMixin extends superclass { - _materialsSet: any; - - constructor(...params: any) { - super(...params); - this._materialsSet = this.config.context && this.config.context.materialsSet; - } - - get materialsSet() { - return this._materialsSet; - } - - sortMaterialsByIndexInSet(materials: OrderedInMemoryEntityInSet[] = []) { - // DO NOT SORT IN PLACE AS IT CHANGES THE ORDER IN `this.materials` AND HAS SIDE EFFECTS (MaterialViewer). - return materials.concat().sort((a, b) => { - return compareEntitiesInOrderedSetForSorting(a, b, this.materialsSet._id, false); - }); - } - }; -} - -export function MaterialsContextMixin(superclass: T) { - return class MaterialsContextMixin extends superclass { - _materials: any; - - constructor(...params: any) { - super(...params); - const materials = this.config.context && this.config.context.materials; - // @ts-ignore - if (!this.constructor.Material) { - throw Error("MaterialsContextMixin: Material is undefined"); - } - this._materials = - materials && materials.length - ? materials - : // @ts-ignore - [this.constructor.Material.createDefault()]; - } - - get materials() { - return this._materials; - } - }; -} - -export function MethodDataContextMixin(superclass: T) { - return class extends superclass { - _methodData: any; - - isEdited: boolean; - - methodDataHash?: string; - - extraData?: { - methodDataHash?: string; - }; - - constructor(...params: any) { - super(...params); - - const config = params[0]; - - this._methodData = (config.context && config.context.methodData) || {}; - this.isEdited = Boolean(config.isEdited); - } - - /* @summary Replace the logic in constructor with this in order to enable passing `methodDataHash` between - * subsequent initializations of the derived class. Not used at present and kept for the record. - */ - _initMethodDataHash() { - this.methodDataHash = CryptoJS.MD5(JSON.stringify(this.methodData)).toString(); - this.extraData = { methodDataHash: this.methodDataHash }; - if (!this._methodData) { - this._methodData = {}; - this.isEdited = false; - // Commented out to reduce effect on performance. Uncomment for debugging purposes. - // TODO: remove on next refactoring or convert to log - // console.warn("MethodDataContextMixin: methodData is undefined or null"); - } else if (this.isMethodDataUpdated) { - this.isEdited = false; - } else { - // @ts-ignore - // eslint-disable-next-line no-undef - this.isEdited = config.isEdited; - } - } - - get methodData() { - return this._methodData; - } - - get isMethodDataUpdated() { - return Boolean(this.extraData && this.extraData.methodDataHash !== this.methodDataHash); - } - }; -} - -export function WorkflowContextMixin(superclass: T) { - return class extends superclass { - _workflow: WorkflowSchema; - - isEdited: boolean; - - constructor(...params: any) { - super(...params); - - const config = params[0]; - - this._workflow = (config.context && config.context.workflow) || {}; - this.isEdited = false; // we always get the `defaultData` (recalculated from scratch, not persistent) - } - - get workflow() { - return this._workflow; - } - }; -} - -export function JobContextMixin(superclass: T) { - return class extends superclass { - _job: JobSchema; - - isEdited: boolean; - - constructor(...params: any) { - super(...params); - - const config = params[0]; - - this._job = (config.context && config.context.job) || {}; - this.isEdited = false; // we always get the `defaultData` (recalculated from scratch, not persistent) - } - - get job() { - return this._job; - } - }; -} diff --git a/src/js/context/provider.ts b/src/js/context/provider.ts deleted file mode 100644 index 6a1345af..00000000 --- a/src/js/context/provider.ts +++ /dev/null @@ -1,149 +0,0 @@ -/* - * @summary This is a standalone class that contains "data" for a property with "name". Helps facilitate UI logic. - * Can be initialized from context when user edits are present: - * - user edits the corresponding property, eg. "kpath" - * - isKpathEdited is set to `true` - * - context property is updated for the parent entity (eg. Unit) in a way that persists in Redux state - * - new entity inherits the "data" through "context" field in config - * - `extraData` field is used to store any other data that should be passed from one instance of provider - * to next one, for example data about material to track when it is changed. - * @notes Should hold static data only (see `setData` method), no classes or functions - */ -import lodash from "lodash"; - -import { deepClone } from "../utils/clone"; - -export interface ContextProviderConfig { - name: string; - domain?: string; - entityName?: string; - data?: object; - extraData?: object; - isEdited?: boolean; - context?: object; -} - -export class ContextProvider { - config: ContextProviderConfig; - - name: string; - - domain?: string; - - entityName?: string; - - data?: object; - - extraData?: object; - - isEdited?: boolean; - - constructor(config: ContextProviderConfig) { - this.config = config; - this.name = config.name; // property name, ie. "kpath" - this.domain = config.domain || "default"; - - // if context is passed inside config, treat it as additional config - // eslint-disable-next-line no-param-reassign - if (config.context) config = ContextProvider.createConfigFromContext(config); - - this.entityName = config.entityName || "unit"; // entity this provider yields data to, eg. "unit", "subworkflow" - this.data = config.data; // property data container - this.extraData = config.extraData; // property extraData container, used track changes to data, for example - this.isEdited = config.isEdited; // whether property was edited by user, available under `isEdited` key - - this.setIsEdited = this.setIsEdited.bind(this); - this.getData = this.getData.bind(this); - this.setData = this.setData.bind(this); - this.transformData = this.transformData.bind(this); - this.yieldData = this.yieldData.bind(this); - } - - static getConstructorConfig(config: ContextProviderConfig) { - return { - constructor: this.prototype.constructor, - config, - }; - } - - static createConfigFromContext(config: ContextProviderConfig) { - const data = lodash.get(config.context, config.name); - const isEdited = lodash.get(config.context, this.getIsEditedKeyByName(config.name)); - const extraData = lodash.get(config.context, this.getExtraDataKeyByName(config.name)); - return Object.assign( - config, - data - ? { - data, - extraData, - isEdited, - } - : {}, - ); - } - - setIsEdited(isEdited: boolean) { - this.isEdited = isEdited; - } - - getData() { - return this.isEdited ? this.data : this.defaultData; - } - - setData(data: object) { - this.data = deepClone(data); - } - - // override in children - // eslint-disable-next-line class-methods-use-this - get defaultData() { - throw new Error("Not implemented."); - } - - // override in children - // eslint-disable-next-line class-methods-use-this - transformData(data: object) { - return data; - } - - // eslint-disable-next-line @typescript-eslint/no-explicit-any - yieldData(...transformDataArgs: any) { - const extraDataObject = this.extraData ? { [this.extraDataKey]: this.extraData } : {}; - return { - ...extraDataObject, - // @ts-ignore - [this.name]: this.transformData(this.getData(), ...transformDataArgs), - [this.isEditedKey]: this.isEdited, - }; - } - - // override when this.data needs additional processing before making it available to rendering context - // used to calculate explicit points path, for example - yieldDataForRendering() { - return this.yieldData(); - } - - get extraDataKey() { - return `${this.name}ExtraData`; - } - - static getExtraDataKeyByName(name: string) { - return `${name}ExtraData`; - } - - get isEditedKey() { - return `is${lodash.capitalize(this.name)}Edited`; - } - - static getIsEditedKeyByName(name: string) { - return `is${lodash.capitalize(name)}Edited`; - } - - get isUnitContextProvider() { - return this.entityName === "unit"; - } - - get isSubworkflowContextProvider() { - return this.entityName === "subworkflow"; - } -} diff --git a/src/js/context/registry.js b/src/js/context/registry.js deleted file mode 100644 index 804d3036..00000000 --- a/src/js/context/registry.js +++ /dev/null @@ -1,95 +0,0 @@ -export class ContextProviderRegistryContainer { - constructor() { - this._providers = []; - } - - get providers() { - return this._providers; - } - - set providers(p) { - this._providers = p; - } - - addProvider({ name, instance }) { - this._providers.push({ - name, - instance, - }); - } - - findProviderInstanceByName(name) { - const provider = this.providers.find((p) => p.name === name); - return provider && provider.instance; - } - - removeProvider(providerCls) { - this.providers = this.providers.filter((p) => p.name === providerCls.name); - } - - removeProviderByName(name) { - this.providers = this.providers.filter((p) => p.name === name); - } -} - -/** Extends an existing context provider registry container and patches static class variables if applicable. - * - * @param {ContextProviderRegistryContainer} registryContainer - * @param {Object} classConfigMap - * @param {Object} classesToPatch - * @example - * const classConfigMap = { - * PlanewaveCutoffDataManager: { - * providerCls: PlanewaveCutoffsContextProvider, - * config: _makeImportant({ name: "cutoffs", entityName: "subworkflow" }) - * }, - * }; - * - */ -export const extendAndPatchRegistry = ( - registryContainer, - classConfigMap, - classesToPatch = {}, - defaultSettings = {}, -) => { - Object.entries(classConfigMap).forEach(([name, { providerCls, config }]) => { - Object.entries(classesToPatch).forEach(([clsName, cls]) => { - if (providerCls[clsName]) { - providerCls[clsName] = cls; - } - const providerDefaultSettings = defaultSettings[providerCls.name]; - if (providerDefaultSettings) { - Object.entries(providerDefaultSettings).forEach(([key, value]) => { - if (providerCls[key]) { - providerCls[key] = value; - } - }); - } - }); - - registryContainer.addProvider({ - instance: providerCls.getConstructorConfig(config), - name, - }); - }); - return registryContainer; -}; - -/** Creates a new context provider registry container and patches static class variables if applicable. - * - * @param {Object} classConfigMap - * @param {{Material: SpecificMockMaterial}} classesToPatch - */ -export const createAndPatchRegistry = ( - classConfigMap, - classesToPatch = {}, - defaultSettings = {}, -) => { - const registryContainer = new ContextProviderRegistryContainer(); - return extendAndPatchRegistry( - registryContainer, - classConfigMap, - classesToPatch, - defaultSettings, - ); -}; diff --git a/src/js/entity/mixins/RuntimeItemsMixin.ts b/src/js/entity/mixins/RuntimeItemsMixin.ts new file mode 100644 index 00000000..6f1dfbf5 --- /dev/null +++ b/src/js/entity/mixins/RuntimeItemsMixin.ts @@ -0,0 +1,88 @@ +import type { NameResultSchema } from "@mat3ra/esse/dist/js/types"; + +import { safeMakeObject } from "../../utils/object"; +import type { Constructor } from "../../utils/types"; +import { InMemoryEntity } from "../in_memory"; + +export enum ItemKey { + results = "results", + monitors = "monitors", + preProcessors = "preProcessors", + postProcessors = "postProcessors", +} + +export type BaseRuntimeItemsInMemoryEntity = InMemoryEntity & { + _json: { + results?: NameResultSchema[]; + monitors?: NameResultSchema[]; + preProcessors?: NameResultSchema[]; + postProcessors?: NameResultSchema[]; + }; + defaultResults?: NameResultSchema[]; + defaultMonitors?: NameResultSchema[]; + defaultPreProcessors?: NameResultSchema[]; + defaultPostProcessors?: NameResultSchema[]; +}; + +/* + * @summary Contains runtime items: results, monitors, pre/postProcessors + * Is meant to work with Entity, InMemoryEntity b/c of `prop` extraction from `_json`. + */ +export function runtimeItemsMixin(item: BaseRuntimeItemsInMemoryEntity) { + // @ts-expect-error - this is a hack to get the properties of the item + const properties: BaseRuntimeItemsInMemoryEntity & RuntimeItemsInMemoryEntity = { + get results(): NameResultSchema[] { + return this.prop("results", this.defaultResults ?? []).map(safeMakeObject); + }, + + get monitors(): NameResultSchema[] { + return this.prop("monitors", this.defaultMonitors ?? []).map(safeMakeObject); + }, + + get preProcessors(): NameResultSchema[] { + // TODO: safeMakeObject could return null. Should we throw an error here? + return this.prop("preProcessors", this.defaultPreProcessors ?? []).map(safeMakeObject); + }, + + get postProcessors(): NameResultSchema[] { + // TODO: safeMakeObject could return null. Should we throw an error here? + return this.prop("postProcessors", this.defaultPostProcessors ?? []).map( + safeMakeObject, + ); + }, + + get hashObjectFromRuntimeItems() { + return { + results: this.results, + preProcessors: this.preProcessors, + postProcessors: this.postProcessors, + }; + }, + }; + + Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties)); +} + +export type RuntimeItemsInMemoryEntity = { + results: NameResultSchema[]; + monitors: NameResultSchema[]; + preProcessors: NameResultSchema[]; + postProcessors: NameResultSchema[]; + hashObjectFromRuntimeItems: { + results: NameResultSchema[]; + preProcessors: NameResultSchema[]; + postProcessors: NameResultSchema[]; + }; +}; + +export type RuntimeItemsInMemoryEntityConstructor = Constructor; + +export default function RuntimeItemsMixin>( + superclass: S, +) { + class RuntimeItemsMixin extends superclass {} + + runtimeItemsMixin(RuntimeItemsMixin.prototype); + + return RuntimeItemsMixin as S & RuntimeItemsInMemoryEntityConstructor; +} diff --git a/src/js/entity/mixins/runtime_items.ts b/src/js/entity/mixins/runtime_items.ts index 57ac579d..3ea5facb 100644 --- a/src/js/entity/mixins/runtime_items.ts +++ b/src/js/entity/mixins/runtime_items.ts @@ -1,66 +1,16 @@ /* eslint-disable class-methods-use-this */ import { AnyObject } from "@mat3ra/esse/dist/js/esse/types"; import { NameResultSchema, RuntimeItemSchema } from "@mat3ra/esse/dist/js/types"; +import type { Constructor } from "src/js/utils/types"; import { safeMakeObject } from "../../utils/object"; import { InMemoryEntityConstructor } from "../in_memory"; +import RuntimeItemsMixin, { + type BaseRuntimeItemsInMemoryEntity, + ItemKey, +} from "./RuntimeItemsMixin"; -export enum ItemKey { - results = "results", - monitors = "monitors", - preProcessors = "preProcessors", - postProcessors = "postProcessors", -} -/* - * @summary Contains runtime items: results, monitors, pre/postProcessors - * Is meant to work with Entity, InMemoryEntity b/c of `prop` extraction from `_json`. - */ - -export function RuntimeItemsMixin(superclass: T) { - return class extends superclass { - get results(): NameResultSchema[] { - return this.prop("results", this.defaultResults).map(safeMakeObject); - } - - get monitors(): NameResultSchema[] { - return this.prop("monitors", this.defaultMonitors).map(safeMakeObject); - } - - get preProcessors(): NameResultSchema[] { - // TODO: safeMakeObject could return null. Should we throw an error here? - return this.prop("preProcessors", this.defaultPreProcessors).map(safeMakeObject); - } - - get postProcessors(): NameResultSchema[] { - // TODO: safeMakeObject could return null. Should we throw an error here? - return this.prop("postProcessors", this.defaultPostProcessors).map(safeMakeObject); - } - - get defaultResults(): NameResultSchema[] { - return []; - } - - get defaultMonitors(): NameResultSchema[] { - return []; - } - - get defaultPreProcessors(): NameResultSchema[] { - return []; - } - - get defaultPostProcessors(): NameResultSchema[] { - return []; - } - - get hashObjectFromRuntimeItems() { - return { - results: this.results, - preProcessors: this.preProcessors, - postProcessors: this.postProcessors, - }; - } - }; -} +export { RuntimeItemsMixin, ItemKey }; export interface RuntimeItemsUILogicJSON extends AnyObject { results?: NameResultSchema[]; @@ -76,7 +26,9 @@ const allKeys: ItemKey[] = [ ItemKey.preProcessors, ]; -export function RuntimeItemsUILogicMixin(superclass: T) { +export function RuntimeItemsUILogicMixin>( + superclass: T, +) { return class extends RuntimeItemsMixin(superclass) { declare _json: RuntimeItemsUILogicJSON; diff --git a/tests/js/context.tests.ts b/tests/js/context.tests.ts deleted file mode 100644 index 47915821..00000000 --- a/tests/js/context.tests.ts +++ /dev/null @@ -1,102 +0,0 @@ -import { expect } from "chai"; - -import { - ApplicationContextMixin, - ContextProvider, - createAndPatchRegistry, - MaterialContextMixin, -} from "../../src/js/context"; -import { ContextProviderConfig } from "../../src/js/context/provider"; - -class MockMaterial { - static createDefault() { - return "defaultMockMaterial"; - } -} - -class SpecificMockMaterial { - static createDefault() { - return "defaultSpecificMockMaterial"; - } -} - -class MockApplication { - static createDefault() { - return "defaultMockApplication"; - } -} - -class SpecificMockApplication { - static createDefault() { - return "defaultSpecificMockApplication"; - } -} - -class ProviderEntity extends MaterialContextMixin(ApplicationContextMixin(ContextProvider)) { - static Material = MockMaterial; - - static Application = MockApplication; - - static setting = 10; -} - -class DerivedProviderEntity extends ProviderEntity { - static Material = SpecificMockMaterial; - - static Application = SpecificMockApplication; -} - -class ApplicationContextProvider extends ApplicationContextMixin(ContextProvider) { - static Application = SpecificMockApplication; -} - -describe("Material & Application ContextMixin", () => { - const config: ContextProviderConfig = { name: "test" }; - - it("uses static entity class", () => { - const provider = new ProviderEntity(config); - expect(provider.material).to.be.equal("defaultMockMaterial"); - expect(provider.application).to.be.equal("defaultMockApplication"); - }); - - it("uses static entity class from derived class", () => { - const provider = new DerivedProviderEntity(config); - expect(provider.material).to.be.equal("defaultSpecificMockMaterial"); - expect(provider.application).to.be.equal("defaultSpecificMockApplication"); - }); -}); - -describe("ContextProviderRegistryContainer", () => { - const classConfigObj = { - DataManager: { - providerCls: ProviderEntity, - config: { name: "example1", domain: "important" }, - }, - ApplicationDataManager: { - providerCls: ApplicationContextProvider, - config: { name: "example2", domain: "important" }, - }, - }; - - const defaultSettings = { - ProviderEntity: { - setting: 100, - }, - }; - - it("can be created and patched", () => { - const registry = createAndPatchRegistry( - classConfigObj, - { Material: SpecificMockMaterial }, - defaultSettings, - ); - - const _dataProvider = registry.findProviderInstanceByName("DataManager"); - const dataProvider = new _dataProvider.constructor(_dataProvider.config); - const _appProvider = registry.findProviderInstanceByName("ApplicationDataManager"); - const appProvider = new _appProvider.constructor(_appProvider.config); - expect(_dataProvider.constructor.setting).to.be.equal(100); - expect(dataProvider.material).to.be.equal("defaultSpecificMockMaterial"); - expect(appProvider.application).to.be.equal("defaultSpecificMockApplication"); - }); -}); diff --git a/tests/js/provider.tests.ts b/tests/js/provider.tests.ts deleted file mode 100644 index d72b3ad6..00000000 --- a/tests/js/provider.tests.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { expect } from "chai"; - -import { ContextProvider } from "../../src/js/context/provider"; - -describe("ContextProvider", () => { - const minimal = { name: "name" }; - const data = { a: "test" }; - - it("can be created", () => { - const provider = new ContextProvider(minimal); - // eslint-disable-next-line no-unused-expressions - expect(provider).to.exist; - }); - - it("sets and gets data", () => { - const provider = new ContextProvider(minimal); - provider.setData(data); - expect(() => provider.getData()).to.throw("Not implemented."); - provider.setIsEdited(true); - expect(JSON.stringify(provider.getData())).to.equal(JSON.stringify(data)); - expect(() => provider.defaultData).to.throw("Not implemented."); - }); - - // transform, yieldData, yieldDataForRendering -});