|
|
@ -2,47 +2,59 @@ import { DataSourcesEvents, DataVariableListener } from '../types'; |
|
|
import { stringToPath } from '../../utils/mixins'; |
|
|
import { stringToPath } from '../../utils/mixins'; |
|
|
import { Model } from '../../common'; |
|
|
import { Model } from '../../common'; |
|
|
import EditorModel from '../../editor/model/Editor'; |
|
|
import EditorModel from '../../editor/model/Editor'; |
|
|
import DataVariable from './DataVariable'; |
|
|
import DataVariable, { DataVariableType } from './DataVariable'; |
|
|
import ComponentView from '../../dom_components/view/ComponentView'; |
|
|
import ComponentView from '../../dom_components/view/ComponentView'; |
|
|
import ComponentDataVariable from './ComponentDataVariable'; |
|
|
import ComponentDataVariable from './ComponentDataVariable'; |
|
|
|
|
|
|
|
|
export interface DataVariableListenerManagerOptions { |
|
|
export interface DynamicVariableListenerManagerOptions { |
|
|
model: Model | ComponentView; |
|
|
model: Model | ComponentView; |
|
|
em: EditorModel; |
|
|
em: EditorModel; |
|
|
dataVariable: DataVariable | ComponentDataVariable; |
|
|
dataVariable: DataVariable | ComponentDataVariable; |
|
|
updateValueFromDataVariable: (value: any) => void; |
|
|
updateValueFromDataVariable: (value: any) => void; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export default class DataVariableListenerManager { |
|
|
export default class DynamicVariableListenerManager { |
|
|
private dataListeners: DataVariableListener[] = []; |
|
|
private dataListeners: DataVariableListener[] = []; |
|
|
private em: EditorModel; |
|
|
private em: EditorModel; |
|
|
private model: Model | ComponentView; |
|
|
private model: Model | ComponentView; |
|
|
private dataVariable: DataVariable | ComponentDataVariable; |
|
|
private dynamicVariable: DataVariable | ComponentDataVariable; |
|
|
private updateValueFromDataVariable: (value: any) => void; |
|
|
private updateValueFromDynamicVariable: (value: any) => void; |
|
|
|
|
|
|
|
|
constructor(options: DataVariableListenerManagerOptions) { |
|
|
constructor(options: DynamicVariableListenerManagerOptions) { |
|
|
this.em = options.em; |
|
|
this.em = options.em; |
|
|
this.model = options.model; |
|
|
this.model = options.model; |
|
|
this.dataVariable = options.dataVariable; |
|
|
this.dynamicVariable = options.dataVariable; |
|
|
this.updateValueFromDataVariable = options.updateValueFromDataVariable; |
|
|
this.updateValueFromDynamicVariable = options.updateValueFromDataVariable; |
|
|
|
|
|
|
|
|
this.listenToDataVariable(); |
|
|
this.listenToDynamicVariable(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private onChange = () => { |
|
|
private onChange = () => { |
|
|
const value = this.dataVariable.getDataValue(); |
|
|
const value = this.dynamicVariable.getDataValue(); |
|
|
this.updateValueFromDataVariable(value); |
|
|
this.updateValueFromDynamicVariable(value); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
listenToDataVariable() { |
|
|
listenToDynamicVariable() { |
|
|
const { em, dataVariable, model } = this; |
|
|
const { em, dynamicVariable, model } = this; |
|
|
const { path } = dataVariable.attributes; |
|
|
|
|
|
const normPath = stringToPath(path || '').join('.'); |
|
|
|
|
|
const [ds, dr] = this.em.DataSources.fromPath(path); |
|
|
|
|
|
|
|
|
|
|
|
this.removeListeners(); |
|
|
this.removeListeners(); |
|
|
|
|
|
|
|
|
|
|
|
const type = dynamicVariable.get('type'); |
|
|
|
|
|
let dataListeners: DataVariableListener[] = []; |
|
|
|
|
|
switch (type) { |
|
|
|
|
|
case DataVariableType: |
|
|
|
|
|
dataListeners = this.listenToDataVariable(dynamicVariable, em); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
dataListeners.forEach((ls) => model.listenTo(ls.obj, ls.event, this.onChange)); |
|
|
|
|
|
|
|
|
|
|
|
this.dataListeners = dataListeners; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private listenToDataVariable(dataVariable: DataVariable | ComponentDataVariable, em: EditorModel) { |
|
|
const dataListeners: DataVariableListener[] = []; |
|
|
const dataListeners: DataVariableListener[] = []; |
|
|
|
|
|
const { path } = dataVariable.attributes; |
|
|
|
|
|
const normPath = stringToPath(path || '').join('.'); |
|
|
|
|
|
const [ds, dr] = this.em.DataSources.fromPath(path); |
|
|
ds && dataListeners.push({ obj: ds.records, event: 'add remove reset' }); |
|
|
ds && dataListeners.push({ obj: ds.records, event: 'add remove reset' }); |
|
|
dr && dataListeners.push({ obj: dr, event: 'change' }); |
|
|
dr && dataListeners.push({ obj: dr, event: 'change' }); |
|
|
dataListeners.push( |
|
|
dataListeners.push( |
|
|
@ -51,9 +63,7 @@ export default class DataVariableListenerManager { |
|
|
{ obj: em, event: `${DataSourcesEvents.path}:${normPath}` }, |
|
|
{ obj: em, event: `${DataSourcesEvents.path}:${normPath}` }, |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
dataListeners.forEach((ls) => model.listenTo(ls.obj, ls.event, this.onChange)); |
|
|
return dataListeners; |
|
|
|
|
|
|
|
|
this.dataListeners = dataListeners; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private removeListeners() { |
|
|
private removeListeners() { |
|
|
|