908dadf1da3fb96146b428cad4708e3087fc9990
[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
45         // Set the prefix to "server", "local", "workstation",
46         // extracted from the URL path.
47         this.persistKeyPfx = this.route.snapshot.parent.url[0].path;
48         if (this.persistKeyPfx === 'acq') {
49             // ACQ is a special case, becaus unlike 'server', 'local',
50             // 'workstation', the schema ('acq') is the root of the path.
51             this.persistKeyPfx = '';
52         }
53
54         // Pass the readonlyFields param if available
55         if (this.route.snapshot.data[0].readonlyFields) {
56             this.readonlyFields = this.route.snapshot.data[0].readonlyFields;
57         }
58
59
60         Object.keys(this.idl.classes).forEach(class_ => {
61             const classDef = this.idl.classes[class_];
62             if (classDef.table === fullTable) {
63                 this.idlClass = class_;
64                 this.classLabel = classDef.label;
65             }
66         });
67
68         if (!this.idlClass) {
69             throw new Error('Unable to find IDL class for table ' + fullTable);
70         }
71     }
72 }
73
74