Browse Source

Fix some ComponentData collection bugs

pull/6419/head
mohamedsalem401 2 years ago
parent
commit
04f72fbaf3
  1. 107
      packages/core/src/data_sources/model/data_collection/ComponentDataCollection.ts

107
packages/core/src/data_sources/model/data_collection/ComponentDataCollection.ts

@ -1,4 +1,4 @@
import { isArray } from 'underscore'; import { bindAll, isArray } from 'underscore';
import { ObjectAny } from '../../../common'; import { ObjectAny } from '../../../common';
import Component from '../../../dom_components/model/Component'; import Component from '../../../dom_components/model/Component';
import { ComponentDefinition, ComponentOptions } from '../../../dom_components/model/types'; import { ComponentDefinition, ComponentOptions } from '../../../dom_components/model/types';
@ -8,22 +8,30 @@ import DataResolverListener from '../DataResolverListener';
import DataSource from '../DataSource'; import DataSource from '../DataSource';
import DataVariable, { DataVariableProps, DataVariableType } from '../DataVariable'; import DataVariable, { DataVariableProps, DataVariableType } from '../DataVariable';
import { isDataVariable } from '../utils'; import { isDataVariable } from '../utils';
import { DataCollectionType, keyCollectionDefinition, keyCollectionsStateMap, keyIsCollectionItem } from './constants'; import {
DataCollectionType,
keyCollectionDefinition,
keyCollectionsStateMap,
keyIsCollectionItem
} from './constants';
import { import {
ComponentDataCollectionProps, ComponentDataCollectionProps,
DataCollectionConfig, DataCollectionConfig,
DataCollectionDataSource, DataCollectionDataSource,
DataCollectionProps, DataCollectionProps,
DataCollectionState, DataCollectionState,
DataCollectionStateMap, DataCollectionStateMap
} from './types'; } from './types';
import { getSymbolsToUpdate } from '../../../dom_components/model/SymbolUtils'; import { getSymbolsToUpdate } from '../../../dom_components/model/SymbolUtils';
import { StyleProps, UpdateStyleOptions } from '../../../domain_abstract/model/StyleableModel'; import { StyleProps, UpdateStyleOptions } from '../../../domain_abstract/model/StyleableModel';
import { updateFromWatcher } from '../../../dom_components/model/ComponentDataResolverWatchers';
export default class ComponentDataCollection extends Component { export default class ComponentDataCollection extends Component {
dataSourceWatcher?: DataResolverListener;
constructor(props: ComponentDataCollectionProps, opt: ComponentOptions) { constructor(props: ComponentDataCollectionProps, opt: ComponentOptions) {
const collectionDef = props[keyCollectionDefinition]; const collectionDef = props[keyCollectionDefinition];
// If we are cloning, leave setting the collection items to the main symbol collection
if (opt.forCloning) { if (opt.forCloning) {
return super(props as any, opt) as unknown as ComponentDataCollection; return super(props as any, opt) as unknown as ComponentDataCollection;
} }
@ -37,16 +45,22 @@ export default class ComponentDataCollection extends Component {
return cmp; return cmp;
} }
this.rebuildChildrenFromCollection();
bindAll(this, 'rebuildChildrenFromCollection');
this.listenTo(this, `change:${keyCollectionDefinition}`, this.rebuildChildrenFromCollection);
this.listenToDataSource();
return cmp; return cmp;
} }
getItemsCount(): number { getItemsCount() {
const items = this.getDataSourceItems(); const items = this.getDataSourceItems();
const startIndex = Math.max(0, this.collectionConfig.startIndex || 0); const startIndex = Math.max(0, this.getConfigStartIndex() || 0);
const configEndIndex = this.getConfigStartIndex() || Number.MAX_VALUE; const configEndIndex = this.getConfigEndIndex() || Number.MAX_VALUE;
const endIndex = Math.min(items.length - 1, configEndIndex); const endIndex = Math.min(items.length - 1, configEndIndex);
return endIndex - startIndex + 1; const count = endIndex - startIndex + 1;
return Math.max(0, count);
} }
getConfigStartIndex() { getConfigStartIndex() {
@ -58,27 +72,19 @@ export default class ComponentDataCollection extends Component {
} }
getComponentDef(): ComponentDefinition { getComponentDef(): ComponentDefinition {
const componentDef = this.getFirstChildJSON(); return this.getFirstChildJSON();
return componentDef;
} }
getDataSource(): DataCollectionDataSource { getDataSource(): DataCollectionDataSource {
const collectionDef = this.collectionDef; return this.collectionDef?.collectionConfig?.dataSource;
return collectionDef?.collectionConfig?.dataSource;
} }
getCollectionId(): string { getCollectionId(): string {
const collectionDef = this.collectionDef; return this.collectionDef?.collectionConfig?.collectionId;
return collectionDef?.collectionConfig?.collectionId;
} }
setComponentDef(componentDef: ComponentDefinition) { setComponentDef(componentDef: ComponentDefinition) {
const collectionDef = { ...this.collectionDef }; this.set(keyCollectionDefinition, { ...this.collectionDef, componentDef });
collectionDef.componentDef = componentDef;
this.set(keyCollectionDefinition, collectionDef);
} }
setStartIndex(startIndex: number) { setStartIndex(startIndex: number) {
@ -86,40 +92,29 @@ export default class ComponentDataCollection extends Component {
this.em.logError('Start index should be greater than or equal to 0'); this.em.logError('Start index should be greater than or equal to 0');
return; return;
} }
this.set(keyCollectionDefinition, {
const newCollectionConfig = { ...this.collectionConfig }; ...this.collectionDef,
newCollectionConfig.startIndex = startIndex; collectionConfig: { ...this.collectionConfig, startIndex }
});
const collectionDef = { ...this.collectionDef, collectionConfig: newCollectionConfig };
this.set(keyCollectionDefinition, collectionDef);
} }
setEndIndex(endIndex: number) { setEndIndex(endIndex: number) {
const collectionConfig = { ...this.collectionConfig }; this.set(keyCollectionDefinition, {
collectionConfig.endIndex = endIndex; ...this.collectionDef,
collectionConfig: { ...this.collectionConfig, endIndex }
const collectionDef = { ...this.collectionDef, collectionConfig }; });
this.set(keyCollectionDefinition, collectionDef);
} }
setDataSource(dataSource: DataCollectionDataSource) { setDataSource(dataSource: DataCollectionDataSource) {
const collectionConfig = { ...this.collectionConfig }; this.set(keyCollectionDefinition, {
collectionConfig.dataSource = dataSource; ...this.collectionDef,
collectionConfig: { ...this.collectionConfig, dataSource }
const collectionDef = { ...this.collectionDef, collectionConfig }; });
this.set(keyCollectionDefinition, collectionDef);
} }
private getDataSourceItems() { private getDataSourceItems() {
const collectionDef = this.collectionDef; return this.collectionDef?.collectionConfig ?
const collectionConfig = collectionDef?.collectionConfig; getDataSourceItems(this.collectionConfig.dataSource, this.em) : [];
if (!collectionConfig) {
return [];
}
const items = getDataSourceItems(collectionConfig.dataSource, this.em);
return items;
} }
private getCollectionStateMap() { private getCollectionStateMap() {
@ -143,13 +138,28 @@ export default class ComponentDataCollection extends Component {
const firstChildJSON = firstChild ? serialize(firstChild) : this.collectionDef.componentDef; const firstChildJSON = firstChild ? serialize(firstChild) : this.collectionDef.componentDef;
delete firstChildJSON?.draggable; delete firstChildJSON?.draggable;
delete firstChildJSON?.removable; delete firstChildJSON?.removable;
return firstChildJSON; return firstChildJSON;
} }
private listenToDataSource() {
const { em } = this;
const path = this.collectionDataSource?.path;
if (!path) return;
this.dataSourceWatcher = new DataResolverListener({
em,
resolver: new DataVariable({ type: DataVariableType, path }, { em }),
onUpdate: this.rebuildChildrenFromCollection
});
}
private rebuildChildrenFromCollection() {
this.components().reset(this.getCollectionItems(), updateFromWatcher as any);
}
getCollectionItems() { getCollectionItems() {
const { componentDef, collectionConfig } = this.collectionDef; const { componentDef, collectionConfig } = this.collectionDef;
const result = validateCollectionConfig(collectionConfig, componentDef, this.em); const result = validateCollectionConfig(collectionConfig, componentDef, this.em);
if (!result) { if (!result) {
return []; return [];
} }
@ -182,7 +192,6 @@ export default class ComponentDataCollection extends Component {
this.em.logError( this.em.logError(
`The collection ID "${collectionId}" already exists in the parent collection state. Overriding it is not allowed.`, `The collection ID "${collectionId}" already exists in the parent collection state. Overriding it is not allowed.`,
); );
return []; return [];
} }
@ -208,7 +217,6 @@ export default class ComponentDataCollection extends Component {
const instance = symbolMain!.clone({ symbol: true }); const instance = symbolMain!.clone({ symbol: true });
!isFirstItem && instance.set('locked', true); !isFirstItem && instance.set('locked', true);
setCollectionStateMapAndPropagate(collectionsStateMap, collectionId)(instance); setCollectionStateMapAndPropagate(collectionsStateMap, collectionId)(instance);
components.push(instance); components.push(instance);
} }
@ -224,7 +232,6 @@ export default class ComponentDataCollection extends Component {
json[keyCollectionDefinition].componentDef = this.getFirstChildJSON(); json[keyCollectionDefinition].componentDef = this.getFirstChildJSON();
delete json.components; delete json.components;
delete json.droppable; delete json.droppable;
return json; return json;
} }
} }
@ -240,7 +247,6 @@ function setCollectionStateMapAndPropagate(collectionsStateMap: DataCollectionSt
const listenerKey = `_hasAddListener${collectionId ? `_${collectionId}` : ''}`; const listenerKey = `_hasAddListener${collectionId ? `_${collectionId}` : ''}`;
const cmps = cmp.components(); const cmps = cmp.components();
// Add the 'add' listener if not already in the listeners array
if (!cmp.collectionStateListeners.includes(listenerKey)) { if (!cmp.collectionStateListeners.includes(listenerKey)) {
cmp.listenTo(cmps, 'add', addListener); cmp.listenTo(cmps, 'add', addListener);
cmp.collectionStateListeners.push(listenerKey); cmp.collectionStateListeners.push(listenerKey);
@ -360,7 +366,6 @@ function getDataSourceItems(dataSource: DataCollectionDataSource, em: EditorMode
const id = dataSource.path; const id = dataSource.path;
items = listDataSourceVariables(id, em); items = listDataSourceVariables(id, em);
} else { } else {
// Path points to a record in the data source
items = em.DataSources.getValue(dataSource.path, []); items = em.DataSources.getValue(dataSource.path, []);
} }
break; break;

Loading…
Cancel
Save