namespace SmartBusiness.Web.Components { import IHistoryState = SVGLD.IHistoryState; import IEditorState = SVGLD.IEditorState; export class SVGLayoutDesigner extends Components.ComponentBase { public constructor(componentInfo: KnockoutComponentTypes.ComponentInfo, params: any) { super(componentInfo, params); } public GetEditorComponent() { const component = this.$component[0] .querySelector('iframe') .contentDocument .querySelector('.Editor'); if (component === undefined) { throw new Error('[SVGLD] Cannot hook the event because the editor is not yet open') } return component; } public GetRootComponent() { return this.$component[0] .querySelector('iframe') .contentDocument; } public GetCurrentHistoryState(callback: (state: IHistoryState) => void) { const component = this.GetEditorComponent(); const eventType = 'getCurrentHistoryState'; component.dispatchEvent(new CustomEvent(eventType)); this.AddEventListener(callback, eventType); } public GetEditorState(callback: (state: IEditorState) => void) { const component = this.GetEditorComponent(); const eventType = 'getEditorState'; component.dispatchEvent(new CustomEvent(eventType)); this.AddEventListener(callback, eventType); } public AppendNewHistoryState(historyState: SVGLD.IHistoryState, callback?: (state: IEditorState) => void) { const eventType = 'appendNewState'; this.GetEditorComponent().dispatchEvent(new CustomEvent(eventType, { detail: historyState })); this.AddEventListener(callback, eventType); } public AddContainer(index: number, type: string, parentId: string, callback?: (state: IEditorState) => void) { const detail = { index, type, parentId } const eventType = 'addContainer'; this.GetEditorComponent().dispatchEvent(new CustomEvent(eventType, { detail })); this.AddEventListener(callback, eventType); } public AddContainerToSelectedContainer(index: number, type: string, callback?: (state: IEditorState) => void) { const detail = { index, type } const eventType = 'addContainerToSelectedContainer'; this.GetEditorComponent().dispatchEvent(new CustomEvent(eventType, { detail })); this.AddEventListener(callback, eventType); } public AppendContainer(type: string, parentId: string, callback?: (state: IEditorState) => void) { const detail = { type, parentId } const eventType = 'appendContainer'; this.GetEditorComponent().dispatchEvent(new CustomEvent(eventType, { detail })); this.AddEventListener(callback, eventType); } public AppendContainerToSelectedContainer(type: string, callback?: (state: IEditorState) => void) { const detail = { type } const eventType = 'appendContainerToSelectedContainer'; this.GetEditorComponent().dispatchEvent(new CustomEvent(eventType, { detail })); this.AddEventListener(callback, eventType); } private AddEventListener(callback: (...args: any[]) => void, eventType: string) { const root = this.GetRootComponent(); const listener = (e: CustomEvent) => { e.target.removeEventListener(e.type, listener); callback && callback(e.detail); }; root.addEventListener(eventType, listener); } } ko.components.register('svg-layout-designer', { viewModel: { createViewModel: function (params, componentInfo) { return new SmartBusiness.Web.Components.SVGLayoutDesigner(componentInfo, params); } }, template: { element: 'svg-layout-designer' } }); }