import { Bag } from "@paperbits/common"; import { EventManager } from "@paperbits/common/events"; import { IWidgetBinding } from "@paperbits/common/editing"; import { widgetName, widgetDisplayName, widgetEditorSelector } from "../constants"; import { ReportsByApiViewModel } from "./reportsByApiViewModel"; import { ViewModelBinder } from "@paperbits/common/widgets"; import { ReportsByApiModel } from "../reportsByApiModel"; /** * This class describes how the model needs to be presented (as a view model) * in a specific UI framework. */ export class ReportsByApiViewModelBinder implements ViewModelBinder { constructor(private readonly eventManager: EventManager) { } public async updateViewModel(model: ReportsByApiModel, viewModel: ReportsByApiViewModel): Promise { viewModel.runtimeConfig(JSON.stringify({ pageSize: model.pageSize, customFilterStartTime: model.customFilterStartTime, customFilterEndTime: model.customFilterEndTime, })); } public async modelToViewModel(model: ReportsByApiModel, viewModel?: ReportsByApiViewModel, bindingContext?: Bag): Promise { if (!viewModel) { viewModel = new ReportsByApiViewModel(); const binding: IWidgetBinding = { name: widgetName, displayName: widgetDisplayName, readonly: bindingContext ? bindingContext.readonly : false, model: model, editor: widgetEditorSelector, draggable: true, applyChanges: async () => { await this.updateViewModel(model, viewModel); this.eventManager.dispatchEvent("onContentUpdate"); } }; viewModel["widgetBinding"] = binding; } this.updateViewModel(model, viewModel); return viewModel; } public canHandleModel(model: ReportsByApiModel): boolean { return model instanceof ReportsByApiModel; } }