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