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

2
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'; import CollectionComponent from './CollectionComponent';
export default class CollectionComponentView extends ComponentView<CollectionComponent> {} export default class CollectionComponentView extends ComponentView<CollectionComponent> {}

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

@ -263,22 +263,22 @@ export default class Component extends StyleableModel<ComponentProperties> {
constructor(props: ComponentProperties = {}, opt: ComponentOptions) { constructor(props: ComponentProperties = {}, opt: ComponentOptions) {
if (Array.isArray(props['components'])) { if (Array.isArray(props['components'])) {
props['components']?.map(component => { props['components']?.map((component) => {
return { return {
...component, ...component,
collectionsItems: { collectionsItems: {
...props.componentCollectionKey ...props.componentCollectionKey,
} },
} };
}) });
} else if (typeof props['components'] === 'object') { } else if (typeof props['components'] === 'object') {
props['components'] = { props['components'] = {
...props['components'], ...props['components'],
// @ts-ignore // @ts-ignore
collectionsItems: { collectionsItems: {
...props.componentCollectionKey ...props.componentCollectionKey,
} },
} };
} }
super(props, opt); super(props, opt);

Loading…
Cancel
Save