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