]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/basic-admin-page.component.ts
lp1857911 angularized stat cat admin interfaces
[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 {IdlObject, 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="{{recordLabel || classLabel}} Administration">
15       </eg-title>
16       <eg-staff-banner bannerText="{{recordLabel || classLabel}} Configuration" i18n-bannerText>
17       </eg-staff-banner>
18       <eg-admin-page persistKeyPfx="{{persistKeyPfx}}" idlClass="{{idlClass}}"
19         configLinkBasePath="{{configLinkBasePath}}"
20         fieldOrder="{{fieldOrder}}"
21         readonlyFields="{{readonlyFields}}"
22         recordLabel="{{recordLabel}}"
23         orgDefaultAllowed="{{orgDefaultAllowed}}"
24         [hideClearFilters]="hideClearFilters"
25         [defaultNewRecord]="defaultNewRecordIdl"
26         [enableUndelete]="enableUndelete"
27         [disableOrgFilter]="disableOrgFilter"></eg-admin-page>
28       </ng-container>
29     `
30 })
31
32 export class BasicAdminPageComponent implements OnInit {
33
34     idlClass: string;
35     classLabel: string;
36     persistKeyPfx: string;
37     fieldOrder = '';
38     readonlyFields = '';
39     recordLabel = '';
40     orgDefaultAllowed = '';
41     hideClearFilters: boolean;
42     defaultNewRecordIdl: IdlObject;
43     configLinkBasePath = '/staff/admin';
44
45     // Tell the admin page to disable and hide the automagic org unit filter
46     disableOrgFilter: boolean;
47
48     enableUndelete: boolean;
49
50     private getParams$: Observable<ParamMap>;
51     private getRouteData$: Observable<any>;
52     private getParentUrl$: Observable<any>;
53
54     private schema: string;
55     private table: string;
56     private defaultNewRecord: Record<string, any>;
57
58     constructor(
59         private route: ActivatedRoute,
60         private idl: IdlService
61     ) {
62     }
63
64     ngOnInit() {
65         this.getParams$ = this.route.paramMap
66             .pipe(tap(params => {
67                 this.schema = params.get('schema');
68                 this.table = params.get('table');
69             }));
70
71         this.getRouteData$ = this.route.data
72             .pipe(tap(routeData => {
73                 const data = routeData[0];
74
75                 if (data) {
76                     // Schema and table can both be passed
77                     // by either param or data
78                     if (!this.schema) {
79                         this.schema = data['schema'];
80                     }
81                     if (!this.table) {
82                         this.table = data['table'];
83                     }
84                     this.disableOrgFilter = data['disableOrgFilter'];
85                     this.enableUndelete = data['enableUndelete'];
86                     this.fieldOrder = data['fieldOrder'];
87                     this.readonlyFields = data['readonlyFields'];
88                     this.recordLabel = data['recordLabel'];
89                     this.orgDefaultAllowed = data['orgDefaultAllowed'];
90                     this.hideClearFilters = data['hideClearFilters'];
91                     this.defaultNewRecord = data['defaultNewRecord'];
92                 }
93
94             }));
95
96         this.getParentUrl$ = this.route.parent.url
97             .pipe(tap(parentUrl => {
98                 // Set the prefix to "server", "local", "workstation",
99                 // extracted from the URL path.
100                 // For admin pages that use none of these, avoid setting
101                 // the prefix because that will cause it to double-up.
102                 // e.g. eg.grid.acq.acq.cancel_reason
103                 this.persistKeyPfx = this.route.snapshot.parent.url[0].path;
104                 const selfPrefixers = ['acq', 'action_trigger', 'booking'];
105                 if (selfPrefixers.indexOf(this.persistKeyPfx) > -1) {
106                     // selfPrefixers, unlike 'server', 'local', and
107                     // 'workstation', are the root of the path.
108                     this.persistKeyPfx = '';
109                 } else {
110                     this.configLinkBasePath += '/' + this.persistKeyPfx;
111                 }
112             }));
113
114         this.getParentUrl$.subscribe();
115         this.getParams$.pipe(
116             switchMap(() => this.getRouteData$)
117         ).subscribe(() => {
118             const fullTable = this.schema + '.' + this.table;
119
120             Object.keys(this.idl.classes).forEach(class_ => {
121                 const classDef = this.idl.classes[class_];
122                 if (classDef.table === fullTable) {
123                     this.idlClass = class_;
124                     this.classLabel = classDef.label;
125                 }
126             });
127
128             if (!this.idlClass) {
129                 throw new Error('Unable to find IDL class for table ' + fullTable);
130             }
131
132             if (this.defaultNewRecord) {
133                 const record = this.idl.create(this.idlClass);
134                 // Call IDL setter for each field that has a default value
135                 Object.keys(this.defaultNewRecord).forEach(key => {
136                     if (typeof (record[key]) === 'function') {
137                         record[key].apply(record, [this.defaultNewRecord[key]]);
138                     } else {
139                         console.warn('Unknown field "' + key + '" in defaultNewRecord for table ' + fullTable);
140                     }
141                 });
142                 this.defaultNewRecordIdl = record;
143             }
144         });
145     }
146
147 }
148