LP 1857351: refactor basic admin component
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / basic-admin-page.component.ts
1 import {Component, OnInit} from '@angular/core';
2 import {ActivatedRoute, ParamMap} from '@angular/router';
3 import {IdlService} from '@eg/core/idl.service';
4 import {Observable} from 'rxjs';
5 import {tap, switchMap} from 'rxjs/operators';
6
7 /**
8  * Generic IDL class editor page.
9  */
10
11 @Component({
12     template: `
13       <ng-container *ngIf="idlClass">
14       <eg-title i18n-prefix prefix="{{classLabel}} Administration">
15       </eg-title>
16       <eg-staff-banner bannerText="{{classLabel}} Configuration" i18n-bannerText>
17       </eg-staff-banner>
18       <eg-admin-page persistKeyPfx="{{persistKeyPfx}}" idlClass="{{idlClass}}"
19         configLinkBasePath="{{configLinkBasePath}}"
20         readonlyFields="{{readonlyFields}}"
21         [disableOrgFilter]="disableOrgFilter"></eg-admin-page>
22       </ng-container>
23     `
24 })
25
26 export class BasicAdminPageComponent implements OnInit {
27
28     idlClass: string;
29     classLabel: string;
30     persistKeyPfx: string;
31     readonlyFields = '';
32     configLinkBasePath = '/staff/admin';
33
34     // Tell the admin page to disable and hide the automagic org unit filter
35     disableOrgFilter: boolean;
36
37     private getParams$: Observable<ParamMap>;
38     private getRouteData$: Observable<any>;
39     private getParentUrl$: Observable<any>;
40
41     private schema: string;
42     private table: string;
43
44     constructor(
45         private route: ActivatedRoute,
46         private idl: IdlService
47     ) {
48     }
49
50     ngOnInit() {
51         this.getParams$ = this.route.paramMap
52             .pipe(tap(params => {
53                 this.schema = params.get('schema');
54                 this.table = params.get('table');
55             }));
56
57         this.getRouteData$ = this.route.data
58             .pipe(tap(routeData => {
59                 const data = routeData[0];
60
61                 if (data) {
62                     // Schema and table can both be passed
63                     // by either param or data
64                     if (!this.schema) {
65                         this.schema = data['schema'];
66                     }
67                     if (!this.table) {
68                         this.table = data['table'];
69                     }
70                 this.disableOrgFilter = data['disableOrgFilter'];
71                 this.readonlyFields = data['readonlyFields'];
72                 }
73
74             }));
75
76         this.getParentUrl$ = this.route.parent.url
77             .pipe(tap(parentUrl => {
78                 // Set the prefix to "server", "local", "workstation",
79                 // extracted from the URL path.
80                 // For admin pages that use none of these, avoid setting
81                 // the prefix because that will cause it to double-up.
82                 // e.g. eg.grid.acq.acq.cancel_reason
83                 this.persistKeyPfx = this.route.snapshot.parent.url[0].path;
84                 const selfPrefixers = ['acq', 'action_trigger', 'booking'];
85                 if (selfPrefixers.indexOf(this.persistKeyPfx) > -1) {
86                     // selfPrefixers, unlike 'server', 'local', and
87                     // 'workstation', are the root of the path.
88                     this.persistKeyPfx = '';
89                 } else {
90                     this.configLinkBasePath += '/' + this.persistKeyPfx;
91                 }
92             }));
93
94         this.getParentUrl$.subscribe();
95         this.getParams$.pipe(
96             switchMap(() => this.getRouteData$)
97         ).subscribe(() => {
98             const fullTable = this.schema + '.' + this.table;
99
100             Object.keys(this.idl.classes).forEach(class_ => {
101                 const classDef = this.idl.classes[class_];
102                 if (classDef.table === fullTable) {
103                     this.idlClass = class_;
104                     this.classLabel = classDef.label;
105                 }
106             });
107
108             if (!this.idlClass) {
109                 throw new Error('Unable to find IDL class for table ' + fullTable);
110             }
111         });
112     }
113
114 }
115