Skip to content

Commit 754e2f4

Browse files
authored
Fixes and improvements for datasources (#6649)
* Improve data source TS * Skip running updateSymbolOverride on non-symbol components * Refactor data types * Update default root resolver item * Resolve direct data sources in ComponentWrapper * Add updateDebug event * Fix data resolver update logic * Fix id removal in ComponentDataCollection and tests * Clone CSS rules pasted cross components * Fix attributes/styles collide in data sources
1 parent 0c33465 commit 754e2f4

36 files changed

+515
-201
lines changed

packages/core/src/css_composer/model/CssRule.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ export default class CssRule extends StyleableModel<CssRuleProperties> {
129129
this.on('change', this.__onChange);
130130
}
131131

132-
__onChange(m: CssRule, opts: any) {
132+
__onChange(rule: CssRule, options: any) {
133133
const { em } = this;
134134
const changed = this.changedAttributes();
135-
changed && !isEmptyObj(changed) && em?.changesUp(opts);
135+
changed && !isEmptyObj(changed) && em?.changesUp(options, { rule, changed, options });
136136
}
137137

138138
clone(): typeof this {

packages/core/src/data_sources/index.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,34 @@
2121
* @module DataSources
2222
*/
2323

24+
import { Events } from 'backbone';
2425
import { isEmpty } from 'underscore';
2526
import { ItemManagerModule, ModuleConfig } from '../abstract/Module';
2627
import { AddOptions, collectionEvents, ObjectAny, RemoveOptions } from '../common';
2728
import EditorModel from '../editor/model/Editor';
2829
import { get, set, stringToPath } from '../utils/mixins';
2930
import defConfig, { DataSourcesConfig } from './config/config';
31+
import { AnyTypeOperation } from './model/conditional_variables/operators/AnyTypeOperator';
32+
import { BooleanOperation } from './model/conditional_variables/operators/BooleanOperator';
33+
import { NumberOperation } from './model/conditional_variables/operators/NumberOperator';
34+
import { StringOperation } from './model/conditional_variables/operators/StringOperator';
35+
import { DataCollectionStateType } from './model/data_collection/types';
3036
import DataRecord from './model/DataRecord';
3137
import DataSource from './model/DataSource';
3238
import DataSources from './model/DataSources';
33-
import { DataSourcesEvents, DataSourceProps, DataRecordProps } from './types';
34-
import { Events } from 'backbone';
39+
import { DataComponentTypes, DataRecordProps, DataSourceProps, DataSourcesEvents } from './types';
3540

3641
export default class DataSourceManager extends ItemManagerModule<DataSourcesConfig & ModuleConfig, DataSources> {
3742
storageKey = 'dataSources';
3843
events = DataSourcesEvents;
44+
dataComponentTypes = DataComponentTypes;
45+
dataCollectionStateTypes = DataCollectionStateType;
46+
dataOperationTypes = {
47+
any: AnyTypeOperation,
48+
boolean: BooleanOperation,
49+
number: NumberOperation,
50+
string: StringOperation,
51+
};
3952
destroy(): void {}
4053

4154
constructor(em: EditorModel) {
@@ -74,6 +87,16 @@ export default class DataSourceManager extends ItemManagerModule<DataSourcesConf
7487
return this.all.get(id);
7588
}
7689

90+
/**
91+
* Return all data sources.
92+
* @returns {Array<[DataSource]>}
93+
* @example
94+
* const ds = dsm.getAll();
95+
*/
96+
getAll() {
97+
return [...this.all.models];
98+
}
99+
77100
/**
78101
* Get value from data sources by path.
79102
* @param {String} path Path to value.
@@ -211,7 +234,10 @@ export default class DataSourceManager extends ItemManagerModule<DataSourcesConf
211234

212235
postLoad() {
213236
const { em, all } = this;
214-
em.listenTo(all, collectionEvents, (m, c, o) => em.changesUp(o || c));
237+
em.listenTo(all, collectionEvents, (dataSource, c, o) => {
238+
const options = o || c;
239+
em.changesUp(options, { dataSource, options });
240+
});
215241
this.em.UndoManager.add(all);
216242
}
217243
}

packages/core/src/data_sources/model/ComponentWithCollectionsState.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ export default class ComponentWithCollectionsState<DataResolverType> extends Com
8383
}
8484

8585
protected getDataSourceItems(): DataSourceRecords {
86-
const dataSourceProps = this.dataSourceProps;
86+
const { dataSourceProps } = this;
8787
if (!dataSourceProps) return [];
88+
8889
const items = this.listDataSourceItems(dataSourceProps);
8990
if (items && isArray(items)) {
9091
return items;

packages/core/src/data_sources/model/ComponentWithDataResolver.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,6 @@ export abstract class ComponentWithDataResolver<T extends DataResolverProps> ext
5555
}
5656

5757
protected listenToPropsChange() {
58-
this.listenTo(
59-
this.dataResolver,
60-
'change',
61-
(() => {
62-
this.__changesUp({ m: this });
63-
}).bind(this),
64-
);
65-
6658
this.on('change:dataResolver', () => {
6759
// @ts-ignore
6860
this.dataResolver.set(this.get('dataResolver'));

packages/core/src/data_sources/model/DataSource.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,8 @@ export default class DataSource<DRProps extends DataRecordProps = DataRecordProp
316316
return this.schema[fieldKey];
317317
}
318318

319-
private handleChanges(m: any, c: any, o: any) {
320-
this.em.changesUp(o || c);
319+
private handleChanges(dataRecord: any, c: any, o: any) {
320+
const options = o || c;
321+
this.em.changesUp(options, { dataRecord, options });
321322
}
322323
}

packages/core/src/data_sources/model/DataVariable.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Model } from '../../common';
22
import { keyRootData } from '../../dom_components/constants';
33
import EditorModel from '../../editor/model/Editor';
4+
import { DataComponentTypes } from '../types';
45
import { isDataVariable } from '../utils';
56
import {
67
DataCollectionStateMap,
@@ -9,7 +10,7 @@ import {
910
RootDataType,
1011
} from './data_collection/types';
1112

12-
export const DataVariableType = 'data-variable' as const;
13+
export const DataVariableType = DataComponentTypes.variable as const;
1314

1415
export interface DataVariableProps {
1516
type?: typeof DataVariableType;

packages/core/src/data_sources/model/conditional_variables/ComponentDataCondition.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Component from '../../../dom_components/model/Component';
12
import {
23
ComponentAddType,
34
ComponentDefinitionDefined,
@@ -6,19 +7,15 @@ import {
67
ToHTMLOptions,
78
} from '../../../dom_components/model/types';
89
import { toLowerCase } from '../../../utils/mixins';
10+
import { DataComponentTypes, DataResolver } from '../../types';
11+
import { ComponentWithDataResolver } from '../ComponentWithDataResolver';
12+
import { DataCollectionStateMap } from '../data_collection/types';
913
import { DataCondition, DataConditionProps, DataConditionType } from './DataCondition';
1014
import { ConditionProps } from './DataConditionEvaluator';
1115
import { StringOperation } from './operators/StringOperator';
12-
import { DataConditionIfTrueType, DataConditionIfFalseType } from './constants';
13-
import { ComponentWithDataResolver } from '../ComponentWithDataResolver';
14-
import Component from '../../../dom_components/model/Component';
15-
import { DataResolver } from '../../types';
16-
import { DataCollectionStateMap } from '../data_collection/types';
17-
18-
export type DataConditionDisplayType = typeof DataConditionIfTrueType | typeof DataConditionIfFalseType;
1916

2017
export interface ComponentDataConditionProps extends ComponentProperties {
21-
type: typeof DataConditionType;
18+
type: DataComponentTypes.condition;
2219
dataResolver: DataConditionProps;
2320
}
2421

@@ -28,7 +25,7 @@ export default class ComponentDataCondition extends ComponentWithDataResolver<Da
2825
// @ts-ignore
2926
...super.defaults,
3027
droppable: false,
31-
type: DataConditionType,
28+
type: DataComponentTypes.condition,
3229
dataResolver: {
3330
condition: {
3431
left: '',
@@ -38,10 +35,10 @@ export default class ComponentDataCondition extends ComponentWithDataResolver<Da
3835
},
3936
components: [
4037
{
41-
type: DataConditionIfTrueType,
38+
type: DataComponentTypes.conditionTrue,
4239
},
4340
{
44-
type: DataConditionIfFalseType,
41+
type: DataComponentTypes.conditionFalse,
4542
},
4643
],
4744
};

packages/core/src/data_sources/model/conditional_variables/constants.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

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

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
import { isArray, size } from 'underscore';
1+
import { isArray } from 'underscore';
22
import { ObjectAny } from '../../../common';
33
import Component, { keySymbol } from '../../../dom_components/model/Component';
4+
import { keyDataValues, updateFromWatcher } from '../../../dom_components/model/ModelDataResolverWatchers';
5+
import { detachSymbolInstance, getSymbolInstances } from '../../../dom_components/model/SymbolUtils';
46
import { ComponentAddType, ComponentDefinitionDefined, ComponentOptions } from '../../../dom_components/model/types';
57
import EditorModel from '../../../editor/model/Editor';
68
import { toLowerCase } from '../../../utils/mixins';
9+
import { DataComponentTypes } from '../../types';
10+
import ComponentWithCollectionsState, { DataVariableMap } from '../ComponentWithCollectionsState';
711
import DataResolverListener from '../DataResolverListener';
812
import { DataVariableProps } from '../DataVariable';
9-
import { DataCollectionItemType, DataCollectionType, keyCollectionDefinition } from './constants';
13+
import { keyCollectionDefinition } from './constants';
1014
import {
1115
ComponentDataCollectionProps,
1216
DataCollectionDataSource,
1317
DataCollectionProps,
1418
DataCollectionStateMap,
1519
} from './types';
16-
import { detachSymbolInstance, getSymbolInstances } from '../../../dom_components/model/SymbolUtils';
17-
import { keyDataValues, updateFromWatcher } from '../../../dom_components/model/ModelDataResolverWatchers';
18-
import ComponentWithCollectionsState, { DataVariableMap } from '../ComponentWithCollectionsState';
1920

2021
const AvoidStoreOptions = { avoidStore: true, partial: true };
2122

@@ -28,10 +29,10 @@ export default class ComponentDataCollection extends ComponentWithCollectionsSta
2829
...super.defaults,
2930
droppable: false,
3031
dataResolver: {},
31-
type: DataCollectionType,
32+
type: DataComponentTypes.collection,
3233
components: [
3334
{
34-
type: DataCollectionItemType,
35+
type: DataComponentTypes.collectionItem,
3536
},
3637
],
3738
};
@@ -281,32 +282,22 @@ export default class ComponentDataCollection extends ComponentWithCollectionsSta
281282
}
282283

283284
private ensureFirstChild() {
284-
const dataConditionItemModel = this.em.Components.getType(DataCollectionItemType)!.model;
285-
286-
return (
287-
this.firstChild ||
288-
new dataConditionItemModel(
289-
{
290-
type: DataCollectionItemType,
291-
},
292-
this.opt,
293-
)
294-
);
285+
const dataConditionItemModel = this.em.Components.getType(DataComponentTypes.collectionItem)!.model;
286+
return this.firstChild || new dataConditionItemModel({ type: DataComponentTypes.collectionItem }, this.opt);
295287
}
296288

297289
private get collectionId() {
298290
return this.dataResolverProps?.collectionId ?? '';
299291
}
300292

301293
static isComponent(el: HTMLElement) {
302-
return toLowerCase(el.tagName) === DataCollectionType;
294+
return toLowerCase(el.tagName) === DataComponentTypes.collection;
303295
}
304296

305297
toJSON(opts?: ObjectAny) {
306298
const json = super.toJSON.call(this, opts) as ComponentDataCollectionProps;
307299
delete json.droppable;
308300
delete json[keySymbol];
309-
delete json.attributes?.id;
310301

311302
const firstChild = this.firstChild as any;
312303
return { ...json, components: [firstChild] };
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
export const DataCollectionType = 'data-collection';
2-
export const DataCollectionItemType = 'data-collection-item';
31
export const keyCollectionDefinition = 'dataResolver';

0 commit comments

Comments
 (0)