]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/server/perm-group-map-dialog.component.ts
LP#1803790: apply alphabetization fix to Angular admin splash pages
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / server / perm-group-map-dialog.component.ts
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';
8
9 @Component({
10   selector: 'eg-perm-group-map-dialog',
11   templateUrl: './perm-group-map-dialog.component.html'
12 })
13
14 /**
15  * Ask the user which part is the lead part then merge others parts in.
16  */
17 export class PermGroupMapDialogComponent
18     extends DialogComponent implements OnInit {
19
20     @Input() permGroup: IdlObject;
21
22     @Input() permissions: IdlObject[];
23
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[];
27
28     @Input() orgDepths: number[];
29
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>;
33
34     // Permissions the user may apply to the current group.
35     trimmedPerms: IdlObject[];
36
37     depth: number;
38     grantable: boolean;
39     perm: number;
40
41     constructor(
42         private idl: IdlService,
43         private pcrud: PcrudService,
44         private modal: NgbModal) {
45         super(modal);
46     }
47
48     ngOnInit() {
49         this.depth = 0;
50         this.grantable = false;
51
52         this.permissions = this.permissions
53             .sort((a, b) => a.code() < b.code() ? -1 : 1);
54
55         this.onOpen$.subscribe(() => this.trimPermissions());
56
57
58         this.permEntries = (term: string) => {
59             if (term === null || term === undefined) { return empty(); }
60             term = ('' + term).toLowerCase();
61
62             // Find entries whose code or description match the search term
63
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()});
69                 }
70             });
71
72             return from(entries);
73         };
74     }
75
76     trimPermissions() {
77         this.trimmedPerms = [];
78
79         this.permissions.forEach(p => {
80
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()) {
86                     return;
87                 }
88             }
89
90             this.trimmedPerms.push(p);
91         });
92     }
93
94     create() {
95         const map = this.idl.create('pgpm');
96
97         map.grp(this.permGroup.id());
98         map.perm(this.perm);
99         map.grantable(this.grantable ? 't' : 'f');
100         map.depth(this.depth);
101
102         this.pcrud.create(map).subscribe(
103             newMap => this.close(newMap),
104             err => throwError(err)
105         );
106     }
107 }
108
109