@paperbits/core
Version:
Paperbits core components.
55 lines (46 loc) • 1.9 kB
text/typescript
import * as ko from "knockout";
import { IContextCommand, ViewManager, ViewManagerMode } from "@paperbits/common/ui";
interface CommandConfig {
element: HTMLElement;
command: IContextCommand;
}
export class ContextualCommandBindingHandler {
constructor(viewManager: ViewManager) {
ko.bindingHandlers["contextualCommand"] = {
init(element: HTMLElement, valueAccessor: () => CommandConfig): void {
const config = valueAccessor();
const bindings = {
background: {
color: config.command.color
}
};
if (config.command.component) {
bindings["balloon"] = {
component: config.command.component,
onOpen: () => {
viewManager.pauseContextualCommands();
if (!!config.command.doNotClearSelection) {
return;
}
viewManager.clearSelection();
},
onClose: () => {
viewManager.resumeContextualCommands();
}
};
}
else if (config.command.callback) {
bindings["click"] = config.command.callback;
}
if (config.command.position) {
bindings["stickTo"] = {
target: config.element,
position: config.command.position
};
}
bindings["tooltip"] = config.command.tooltip;
ko.applyBindingsToNode(element, bindings, null);
}
};
}
}