Browse Source

Add component dynamic variables serialization

pull/6351/head
mohamedsalem401 1 year ago
parent
commit
d1876f6877
  1. 2
      packages/core/src/data_sources/model/DataVariableListenerManager.ts
  2. 5
      packages/core/src/dom_components/model/Component.ts
  3. 21
      packages/core/src/dom_components/model/ComponentDynamicValueListener.ts
  4. 25
      packages/core/src/dom_components/model/DynamicValueWatcher.ts

2
packages/core/src/data_sources/model/DataVariableListenerManager.ts

@ -16,7 +16,7 @@ export interface DynamicVariableListenerManagerOptions {
export default class DynamicVariableListenerManager { export default class DynamicVariableListenerManager {
private dataListeners: DataVariableListener[] = []; private dataListeners: DataVariableListener[] = [];
private em: EditorModel; private em: EditorModel;
private dynamicVariable: DynamicValue; dynamicVariable: DynamicValue;
private updateValueFromDynamicVariable: (value: any) => void; private updateValueFromDynamicVariable: (value: any) => void;
private model = new Model(); private model = new Model();

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

@ -1560,8 +1560,9 @@ export default class Component extends StyleableModel<ComponentProperties> {
* @private * @private
*/ */
toJSON(opts: ObjectAny = {}): ComponentDefinition { toJSON(opts: ObjectAny = {}): ComponentDefinition {
const obj = Model.prototype.toJSON.call(this, opts); let obj = Model.prototype.toJSON.call(this, opts);
obj.attributes = this.getAttributes(); obj.attributes = this.componentDVListener.getAttributesDefsOrValues(this.getAttributes());
obj = { ...obj, ...this.componentDVListener.getDynamicPropsDefs() };
delete obj.attributes.class; delete obj.attributes.class;
delete obj.toolbar; delete obj.toolbar;
delete obj.status; delete obj.status;

21
packages/core/src/dom_components/model/ComponentDynamicValueListener.ts

@ -20,10 +20,7 @@ export class ComponentDynamicValueListener {
}, em); }, em);
} }
static evaluateComponentDef( static evaluateComponentDef(values: ObjectAny, em: EditorModel) {
values: ObjectAny,
em: EditorModel,
) {
const props = DynamicValueWatcher.getStaticValues(values, em); const props = DynamicValueWatcher.getStaticValues(values, em);
props.attributes = DynamicValueWatcher.getStaticValues(props.attributes, em); props.attributes = DynamicValueWatcher.getStaticValues(props.attributes, em);
@ -39,16 +36,24 @@ export class ComponentDynamicValueListener {
this.propertyWatchClass.watchDynamicValue(props); this.propertyWatchClass.watchDynamicValue(props);
} }
getDynamicPropsDefs() {
return this.propertyWatchClass.getAllSerializableValues();
}
setAttributes(attributes: ObjectAny) { setAttributes(attributes: ObjectAny) {
this.attributeWatchClass.removeListeners(); this.attributeWatchClass.removeListeners();
this.attributeWatchClass.watchDynamicValue(attributes); this.attributeWatchClass.watchDynamicValue(attributes);
} }
removeAttributes(attrArr: string[]) {
this.attributeWatchClass.removeListeners(attrArr);
}
watchAttributes(attributes: ObjectAny) { watchAttributes(attributes: ObjectAny) {
this.attributeWatchClass.watchDynamicValue(attributes); this.attributeWatchClass.watchDynamicValue(attributes);
} }
removeAttributes(attributes: string[]) {
this.attributeWatchClass.removeListeners(attributes);
}
getAttributesDefsOrValues(attributes: ObjectAny) {
return this.attributeWatchClass.getSerializableValues(attributes);
}
} }

25
packages/core/src/dom_components/model/DynamicValueWatcher.ts

@ -77,4 +77,29 @@ export class DynamicValueWatcher {
const propsKeys = keys ? keys : Object.keys(this.dynamicVariableListeners); const propsKeys = keys ? keys : Object.keys(this.dynamicVariableListeners);
propsKeys.forEach((key) => this.dynamicVariableListeners[key].destroy()); propsKeys.forEach((key) => this.dynamicVariableListeners[key].destroy());
} }
getSerializableValues(values: ObjectAny) {
const serializableValues = { ...values };
const propsKeys = Object.keys(serializableValues);
for (let index = 0; index < propsKeys.length; index++) {
const key = propsKeys[index];
if (this.dynamicVariableListeners[key]) {
serializableValues[key] = this.dynamicVariableListeners[key].dynamicVariable.toJSON();
continue;
}
}
return serializableValues;
}
getAllSerializableValues() {
const serializableValues: ObjectAny = {};
const propsKeys = Object.keys(this.dynamicVariableListeners);
for (let index = 0; index < propsKeys.length; index++) {
const key = propsKeys[index];
serializableValues[key] = this.dynamicVariableListeners[key].dynamicVariable.toJSON();
}
return serializableValues;
}
} }

Loading…
Cancel
Save