mirror of https://github.com/artf/grapesjs.git
10 changed files with 203 additions and 18 deletions
@ -0,0 +1,53 @@ |
|||
import Component, { keyCollectionsStateMap, keySymbolOvrd } from '../../../dom_components/model/Component'; |
|||
import { ComponentOptions, ComponentProperties } from '../../../dom_components/model/types'; |
|||
import { toLowerCase } from '../../../utils/mixins'; |
|||
import DataCollectionVariable from './DataCollectionVariable'; |
|||
import { CollectionVariableType, keyInnerCollectionState } from './constants'; |
|||
import { DataCollectionStateMap, DataCollectionVariableDefinition } from './types'; |
|||
|
|||
export default class ComponentDataCollectionVariable extends Component { |
|||
datacollectionVariable: DataCollectionVariable; |
|||
|
|||
get defaults() { |
|||
return { |
|||
// @ts-ignore
|
|||
...super.defaults, |
|||
type: CollectionVariableType, |
|||
variableType: '', |
|||
path: '', |
|||
collectionName: keyInnerCollectionState, |
|||
}; |
|||
} |
|||
|
|||
constructor(props: DataCollectionVariableDefinition & ComponentProperties, opt: ComponentOptions) { |
|||
super(props, opt); |
|||
const em = opt.em; |
|||
const { type, variableType, path, collectionName } = props; |
|||
|
|||
this.datacollectionVariable = new DataCollectionVariable( |
|||
{ type, variableType, path, collectionName }, |
|||
{ |
|||
em, |
|||
collectionsStateMap: this.get(keyCollectionsStateMap), |
|||
}, |
|||
); |
|||
|
|||
this.listenTo(this, `change:${keyCollectionsStateMap}`, this.handleCollectionsMapStateUpdate); |
|||
} |
|||
|
|||
private handleCollectionsMapStateUpdate(m: any, v: DataCollectionStateMap, opts = {}) { |
|||
this.datacollectionVariable.updateCollectionsStateMap(v); |
|||
} |
|||
|
|||
getDataValue() { |
|||
return this.datacollectionVariable.getDataValue(); |
|||
} |
|||
|
|||
getInnerHTML() { |
|||
return this.getDataValue(); |
|||
} |
|||
|
|||
static isComponent(el: HTMLElement) { |
|||
return toLowerCase(el.tagName) === CollectionVariableType; |
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
import ComponentView from '../../../dom_components/view/ComponentView'; |
|||
import DynamicVariableListenerManager from '../DataVariableListenerManager'; |
|||
import ComponentDataCollectionVariable from './ComponentDataCollectionVariable'; |
|||
|
|||
export default class ComponentDataCollectionVariableView extends ComponentView<ComponentDataCollectionVariable> { |
|||
collectionVariableListener?: DynamicVariableListenerManager; |
|||
|
|||
initialize(opt = {}) { |
|||
super.initialize(opt); |
|||
this.postRender(); |
|||
this.collectionVariableListener = new DynamicVariableListenerManager({ |
|||
em: this.em!, |
|||
dataVariable: this.model.datacollectionVariable, |
|||
updateValueFromDataVariable: this.postRender, |
|||
}); |
|||
} |
|||
|
|||
postRender() { |
|||
const { model, el } = this; |
|||
el.innerHTML = model.datacollectionVariable.getDataValue(); |
|||
|
|||
super.postRender(); |
|||
} |
|||
} |
|||
@ -0,0 +1,69 @@ |
|||
import { Component, DataRecord, DataSource, DataSourceManager, Editor } from '../../../../../src'; |
|||
import { DataVariableType } from '../../../../../src/data_sources/model/DataVariable'; |
|||
import { |
|||
CollectionComponentType, |
|||
CollectionVariableType, |
|||
} from '../../../../../src/data_sources/model/data_collection/constants'; |
|||
import { DataCollectionStateVariableType } from '../../../../../src/data_sources/model/data_collection/types'; |
|||
import EditorModel from '../../../../../src/editor/model/Editor'; |
|||
import { setupTestEditor } from '../../../../common'; |
|||
|
|||
describe('Collection component', () => { |
|||
let em: EditorModel; |
|||
let editor: Editor; |
|||
let dsm: DataSourceManager; |
|||
let dataSource: DataSource; |
|||
let wrapper: Component; |
|||
let firstRecord: DataRecord; |
|||
let secondRecord: DataRecord; |
|||
|
|||
beforeEach(() => { |
|||
({ em, editor, dsm } = setupTestEditor()); |
|||
wrapper = em.getWrapper()!; |
|||
dataSource = dsm.add({ |
|||
id: 'my_data_source_id', |
|||
records: [ |
|||
{ id: 'user1', user: 'user1', age: '12' }, |
|||
{ id: 'user2', user: 'user2', age: '14' }, |
|||
{ id: 'user3', user: 'user3', age: '16' }, |
|||
], |
|||
}); |
|||
|
|||
firstRecord = dataSource.getRecord('user1')!; |
|||
secondRecord = dataSource.getRecord('user2')!; |
|||
}); |
|||
|
|||
afterEach(() => { |
|||
em.destroy(); |
|||
}); |
|||
|
|||
test('Collection variable components', async () => { |
|||
const cmp = wrapper.components({ |
|||
type: CollectionComponentType, |
|||
collectionDefinition: { |
|||
block: { |
|||
type: 'default', |
|||
components: [ |
|||
{ |
|||
type: CollectionVariableType, |
|||
variableType: DataCollectionStateVariableType.currentItem, |
|||
path: 'user', |
|||
}, |
|||
], |
|||
}, |
|||
config: { |
|||
dataSource: { |
|||
type: DataVariableType, |
|||
path: 'my_data_source_id', |
|||
}, |
|||
}, |
|||
}, |
|||
})[0]; |
|||
|
|||
const firstGrandchild = cmp.components().at(0).components().at(0); |
|||
expect(firstGrandchild.getInnerHTML()).toContain('user1'); |
|||
|
|||
const secondGrandchild = cmp.components().at(1).components().at(0); |
|||
expect(secondGrandchild.getInnerHTML()).toContain('user2'); |
|||
}); |
|||
}); |
|||
Loading…
Reference in new issue