d47d92a94cf7a9157b05a0e370056608d5f9c047
[working/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} from '@angular/router';
3 import {IdlService} from '@eg/core/idl.service';
4
5 /**
6  * Generic IDL class editor page.
7  */
8
9 @Component({
10     template: `
11       <eg-staff-banner bannerText="{{classLabel}} Configuration" i18n-bannerText>
12       </eg-staff-banner>
13       <eg-admin-page persistKeyPfx="{{persistKeyPfx}}" idlClass="{{idlClass}}" readonlyFields="{{readonlyFields}}"></eg-admin-page>
14     `
15 })
16
17 export class BasicAdminPageComponent implements OnInit {
18
19     idlClass: string;
20     classLabel: string;
21     persistKeyPfx: string;
22     readonlyFields = '';
23
24     constructor(
25         private route: ActivatedRoute,
26         private idl: IdlService
27     ) {
28     }
29
30     ngOnInit() {
31         let schema = this.route.snapshot.paramMap.get('schema');
32         if (!schema) {
33             // Allow callers to pass the schema via static route data
34             const data = this.route.snapshot.data[0];
35             if (data) { schema = data.schema; }
36         }
37         let table = this.route.snapshot.paramMap.get('table');
38         if (!table) {
39             const data = this.route.snapshot.data[0];
40             if (data) { table = data.table; }
41         }
42         const fullTable = schema + '.' + table;
43
44         // Set the prefix to "server", "local", "workstation",
45         // extracted from the URL path.
46         // For admin pages that use none of these, avoid setting
47         // the prefix because that will cause it to double-up.
48         // e.g. eg.grid.acq.acq.cancel_reason
49         this.persistKeyPfx = this.route.snapshot.parent.url[0].path;
50         const selfPrefixers = ['acq', 'booking'];
51         if (selfPrefixers.indexOf(this.persistKeyPfx) > -1) {
52             // ACQ is a special case, because unlike 'server', 'local',
53             // 'workstation', the schema ('acq') is the root of the path.
54             this.persistKeyPfx = '';
55         }
56
57         // Pass the readonlyFields param if available
58         if (this.route.snapshot.data &&
59             this.route.snapshot.data.length &&
60             this.route.snapshot.data[0].readonlyFields) {
61             this.readonlyFields = this.route.snapshot.data[0].readonlyFields;
62         }
63
64
65         Object.keys(this.idl.classes).forEach(class_ => {
66             const classDef = this.idl.classes[class_];
67             if (classDef.table === fullTable) {
68                 this.idlClass = class_;
69                 this.classLabel = classDef.label;
70             }
71         });
72
73         if (!this.idlClass) {
74             throw new Error('Unable to find IDL class for table ' + fullTable);
75         }
76     }
77 }
78
79