@paperbits/forms
Version:
Paperbits components for form builder.
55 lines (46 loc) • 2.07 kB
text/typescript
import { UrlInput } from "./urlInput";
import { ViewModelBinder } from "@paperbits/common/widgets";
import { UrlInputModel } from "../urlInputModel";
import { EventManager, Events } from "@paperbits/common/events";
import { StyleCompiler } from "@paperbits/common/styles";
import { Bag } from "@paperbits/common";
import { IWidgetBinding } from "@paperbits/common/editing";
import { ComponentFlow } from "@paperbits/common/components";
export class UrlInputViewModelBinder implements ViewModelBinder<UrlInputModel, UrlInput> {
constructor(
private readonly eventManager: EventManager,
private readonly styleCompiler: StyleCompiler
) { }
public async modelToViewModel(model: UrlInputModel, viewModel?: UrlInput, bindingContext?: Bag<any>): Promise<UrlInput> {
if (!viewModel) {
viewModel = new UrlInput();
}
viewModel.label(model.label);
viewModel.name(model.name);
viewModel.value(model.value);
viewModel.readonly(model.readonly);
viewModel.required(model.required);
viewModel.maxLength(model.maxLength);
viewModel.placeholder(model.placeholder);
if (model.styles) {
viewModel.styles(await this.styleCompiler.getStyleModelAsync(model.styles, bindingContext?.styleManager));
}
const binding: IWidgetBinding<UrlInputModel, UrlInput> = {
displayName: "URL input",
layer: bindingContext?.layer,
model: model,
draggable: true,
flow: ComponentFlow.Block,
editor: "url-input-editor",
applyChanges: async () => {
await this.modelToViewModel(model, viewModel, bindingContext);
this.eventManager.dispatchEvent(Events.ContentUpdate);
}
};
viewModel["widgetBinding"] = binding;
return viewModel;
}
public canHandleModel(model: UrlInputModel): boolean {
return model instanceof UrlInputModel;
}
}