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