1 import {Component, Input, ViewChild, TemplateRef, OnInit} from '@angular/core';
2 import {Observable, from, empty, throwError} from 'rxjs';
3 import {DialogComponent} from '@eg/share/dialog/dialog.component';
4 import {IdlService, IdlObject} from '@eg/core/idl.service';
5 import {PcrudService} from '@eg/core/pcrud.service';
6 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
7 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
10 selector: 'eg-perm-group-map-dialog',
11 templateUrl: './perm-group-map-dialog.component.html'
15 * Ask the user which part is the lead part then merge others parts in.
17 export class PermGroupMapDialogComponent
18 extends DialogComponent implements OnInit {
20 @Input() permGroup: IdlObject;
22 @Input() permissions: IdlObject[];
24 // List of grp-perm-map objects that relate to the selected permission
25 // group or are linked to a parent group.
26 @Input() permMaps: IdlObject[];
28 @Input() orgDepths: number[];
30 // Note we have all of the permissions on hand, but rendering the
31 // full list of permissions can caus sluggishness. Render async instead.
32 permEntries: (term: string) => Observable<ComboboxEntry>;
34 // Permissions the user may apply to the current group.
35 trimmedPerms: IdlObject[];
42 private idl: IdlService,
43 private pcrud: PcrudService,
44 private modal: NgbModal) {
50 this.grantable = false;
52 this.permissions = this.permissions
53 .sort((a, b) => a.code() < b.code() ? -1 : 1);
55 this.onOpen$.subscribe(() => this.trimPermissions());
58 this.permEntries = (term: string) => {
59 if (term === null || term === undefined) { return empty(); }
60 term = ('' + term).toLowerCase();
62 // Find entries whose code or description match the search term
64 const entries: ComboboxEntry[] = [];
65 this.trimmedPerms.forEach(p => {
66 if (p.code().toLowerCase().includes(term) ||
67 p.description().toLowerCase().includes(term)) {
68 entries.push({id: p.id(), label: p.code()});
77 this.trimmedPerms = [];
79 this.permissions.forEach(p => {
81 // Prevent duplicate permissions, for-loop for early exit.
82 for (let idx = 0; idx < this.permMaps.length; idx++) {
83 const map = this.permMaps[idx];
84 if (map.perm().id() === p.id() &&
85 map.grp().id() === this.permGroup.id()) {
90 this.trimmedPerms.push(p);
95 const map = this.idl.create('pgpm');
97 map.grp(this.permGroup.id());
99 map.grantable(this.grantable ? 't' : 'f');
100 map.depth(this.depth);
102 this.pcrud.create(map).subscribe(
103 newMap => this.close(newMap),
104 err => throwError(err)