@paperbits/core
Version:
Paperbits core components.
54 lines (43 loc) • 1.77 kB
text/typescript
import * as ko from "knockout";
import template from "./role-input.html";
import { Component, Event, OnMounted, Param } from "@paperbits/common/ko/decorators";
import { RoleModel, RoleService, BuiltInRoles } from "@paperbits/common/user";
export class RoleInput {
public readonly selectedRolesDisplay: ko.Observable<string>;
public readonly availableRoles: ko.ObservableArray<RoleModel>;
public readonly selectedRoles: ko.ObservableArray<RoleModel>;
constructor(private readonly roleService: RoleService) {
this.availableRoles = ko.observableArray();
this.selectedRoles = ko.observableArray();
this.selectedRolesDisplay = ko.observable();
}
public selection: string[];
public readonly onChange: (role: string[]) => void;
public async initialize(): Promise<void> {
const availableRoles = await this.roleService.getRoles();
this.availableRoles(availableRoles);
const selectedRoles = this.selection
? availableRoles.filter(x => this.selection.includes(x.key))
: [BuiltInRoles.everyone];
this.selectedRoles(selectedRoles);
this.updateDisplay(selectedRoles);
}
private updateDisplay(roles: RoleModel[]): string {
return this.selectedRolesDisplay(roles.map(x => x.name).sort().join(", "));
}
public onSelectionChange(roles: RoleModel[]): void {
this.updateDisplay(roles);
this.selectedRoles(roles);
this.selection = roles.map(role => role.key);
if (this.onChange) {
this.onChange(this.selection);
}
}
}