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