Browse Source

Refactor and format

pull/6359/head
mohamedsalem401 1 year ago
parent
commit
ed8ecbca93
  1. 62
      packages/core/src/data_sources/model/collection_component/CollectionComponent.ts
  2. 4
      packages/core/src/data_sources/model/collection_component/CollectionComponentView.ts
  3. 28
      packages/core/src/dom_components/model/Component.ts

62
packages/core/src/data_sources/model/collection_component/CollectionComponent.ts

@ -1,4 +1,4 @@
import { DataVariableType } from './../DataVariable';
import { DataVariableDefinition, DataVariableType } from './../DataVariable';
import { isArray } from 'underscore';
import Component from '../../../dom_components/model/Component';
import { ComponentDefinition, ComponentOptions, ComponentProperties } from '../../../dom_components/model/types';
@ -16,16 +16,13 @@ type CollectionVariable = {
path?: string;
};
type CollectionDataSource =
| any[]
| { type: typeof DataVariableType; path: string }
| CollectionVariable;
type CollectionDataSource = any[] | DataVariableDefinition | CollectionVariable;
type CollectionConfig = {
start_index?: number;
end_index?: number | ConditionDefinition;
dataSource: CollectionDataSource;
}
};
type CollectionState = {
current_index: number;
@ -35,18 +32,18 @@ type CollectionState = {
collection_name?: string;
total_items: number;
remaining_items: number;
}
};
type CollectionsStateMap = {
[key: string]: CollectionState;
}
};
type CollectionDefinition = {
type: typeof CollectionVariableType;
collection_name?: string;
config: CollectionConfig;
block: ComponentDefinition;
}
};
export const collectionDefinitionKey = 'collectionDefinition';
export const collectionsStateKey = 'collectionsItems';
@ -144,13 +141,18 @@ function listDataSourceVariables(dataSource_id: string, em: EditorModel) {
const records = em.DataSources.getValue(dataSource_id, []);
const keys = Object.keys(records);
return keys.map(key => ({
return keys.map((key) => ({
type: DataVariableType,
path: dataSource_id + '.' + key,
}));
}
function resolveComponent(symbol: Component, block: ComponentDefinition, collectionsStateMap: CollectionsStateMap, em: EditorModel) {
function resolveComponent(
symbol: Component,
block: ComponentDefinition,
collectionsStateMap: CollectionsStateMap,
em: EditorModel,
) {
const instance = em.Components.addSymbol(symbol);
const { resolvedCollectionValues: overrideKeys } = resolveBlockValues(collectionsStateMap, block);
Object.keys(overrideKeys).length && instance!.setSymbolOverride(Object.keys(overrideKeys));
@ -166,7 +168,7 @@ function resolveComponent(symbol: Component, block: ComponentDefinition, collect
const componentJSON = instance!.toJSON();
const componentDefinition: ComponentDefinition = {
...componentJSON,
components: children
components: children,
};
return componentDefinition;
@ -189,33 +191,35 @@ function resolveBlockValues(collectionsStateMap: CollectionsStateMap, block: Obj
const {
variable_type,
collection_name = 'innerMostCollectionItem',
path = ''
path = '',
} = blockValue as CollectionVariable;
const collectionItem = collectionsStateMap[collection_name];
if (!collectionItem) {
throw new Error(
`Collection not found: ${collection_name}`
);
throw new Error(`Collection not found: ${collection_name}`);
}
if (!variable_type) {
throw new Error(
`Missing collection variable type for collection: ${collection_name}`
);
throw new Error(`Missing collection variable type for collection: ${collection_name}`);
}
clonedBlock[key] = resolveCurrentItem(variable_type, collectionItem, path);
hasCollectionVariable = true;
} else if (Array.isArray(blockValue)) {
clonedBlock[key] = blockValue.map((arrayItem: any) => {
const { clonedBlock, resolvedCollectionValues: itemOverrideKeys } = resolveBlockValues(collectionsStateMap, arrayItem)
const { clonedBlock, resolvedCollectionValues: itemOverrideKeys } = resolveBlockValues(
collectionsStateMap,
arrayItem,
);
if (!isEmptyObject(itemOverrideKeys)) {
hasCollectionVariable = true;
}
return typeof arrayItem === 'object' ? clonedBlock : arrayItem
return typeof arrayItem === 'object' ? clonedBlock : arrayItem;
});
} else {
const { clonedBlock, resolvedCollectionValues: itemOverrideKeys } = resolveBlockValues(collectionsStateMap, blockValue);
const { clonedBlock, resolvedCollectionValues: itemOverrideKeys } = resolveBlockValues(
collectionsStateMap,
blockValue,
);
clonedBlock[key] = clonedBlock;
if (!isEmptyObject(itemOverrideKeys)) {
@ -224,7 +228,7 @@ function resolveBlockValues(collectionsStateMap: CollectionsStateMap, block: Obj
}
if (hasCollectionVariable && key !== 'components') {
resolvedCollectionValues[key] = clonedBlock[key]
resolvedCollectionValues[key] = clonedBlock[key];
}
}
}
@ -233,12 +237,15 @@ function resolveBlockValues(collectionsStateMap: CollectionsStateMap, block: Obj
return { clonedBlock, resolvedCollectionValues };
}
function resolveCurrentItem(variableType: CollectionVariable['variable_type'], collectionItem: CollectionState, path: string) {
function resolveCurrentItem(
variableType: CollectionVariable['variable_type'],
collectionItem: CollectionState,
path: string,
) {
const valueIsDataVariable = collectionItem.current_item?.type === DataVariableType;
if (variableType === 'current_item' && valueIsDataVariable) {
const resolvedPath = collectionItem.current_item.path
? `${collectionItem.current_item.path}.${path}`
: path;
const currentItem_path = collectionItem.current_item.path;
const resolvedPath = currentItem_path ? `${currentItem_path}.${path}` : path;
return {
...collectionItem.current_item,
path: resolvedPath,
@ -247,7 +254,6 @@ function resolveCurrentItem(variableType: CollectionVariable['variable_type'], c
return collectionItem[variableType];
}
function isEmptyObject(itemOverrideKeys: ObjectAny) {
return Object.keys(itemOverrideKeys).length === 0;
}

4
packages/core/src/data_sources/model/collection_component/CollectionComponentView.ts

@ -1,4 +1,4 @@
import ComponentView from "../../../dom_components/view/ComponentView";
import ComponentView from '../../../dom_components/view/ComponentView';
import CollectionComponent from './CollectionComponent';
export default class CollectionComponentView extends ComponentView<CollectionComponent> { }
export default class CollectionComponentView extends ComponentView<CollectionComponent> {}

28
packages/core/src/dom_components/model/Component.ts

@ -56,9 +56,9 @@ import { ConditionalVariableType, DataCondition } from '../../data_sources/model
import { isDynamicValue, isDynamicValueDefinition } from '../../data_sources/model/utils';
import { DynamicValueDefinition } from '../../data_sources/types';
export interface IComponent extends ExtractMethods<Component> { }
export interface IComponent extends ExtractMethods<Component> {}
export interface SetAttrOptions extends SetOptions, UpdateStyleOptions { }
export interface SetAttrOptions extends SetOptions, UpdateStyleOptions {}
const escapeRegExp = (str: string) => {
return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
@ -225,12 +225,12 @@ export default class Component extends StyleableModel<ComponentProperties> {
return this.frame?.getPage();
}
preInit() { }
preInit() {}
/**
* Hook method, called once the model is created
*/
init() { }
init() {}
/**
* Hook method, called when the model has been updated (eg. updated some model's property)
@ -238,12 +238,12 @@ export default class Component extends StyleableModel<ComponentProperties> {
* @param {*} value Property value, if triggered after some property update
* @param {*} previous Property previous value, if triggered after some property update
*/
updated(property: string, value: any, previous: any) { }
updated(property: string, value: any, previous: any) {}
/**
* Hook method, called once the model has been removed
*/
removed() { }
removed() {}
em!: EditorModel;
opt!: ComponentOptions;
@ -263,22 +263,22 @@ export default class Component extends StyleableModel<ComponentProperties> {
constructor(props: ComponentProperties = {}, opt: ComponentOptions) {
if (Array.isArray(props['components'])) {
props['components']?.map(component => {
props['components']?.map((component) => {
return {
...component,
collectionsItems: {
...props.componentCollectionKey
}
}
})
...props.componentCollectionKey,
},
};
});
} else if (typeof props['components'] === 'object') {
props['components'] = {
...props['components'],
// @ts-ignore
collectionsItems: {
...props.componentCollectionKey
}
}
...props.componentCollectionKey,
},
};
}
super(props, opt);

Loading…
Cancel
Save