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