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';
8 * Generic IDL class editor page.
13 <ng-container *ngIf="idlClass">
14 <eg-title i18n-prefix prefix="{{classLabel}} Administration">
16 <eg-staff-banner bannerText="{{classLabel}} Configuration" i18n-bannerText>
18 <eg-admin-page persistKeyPfx="{{persistKeyPfx}}" idlClass="{{idlClass}}"
19 configLinkBasePath="{{configLinkBasePath}}"
20 fieldOrder="{{fieldOrder}}"
21 readonlyFields="{{readonlyFields}}"
22 [defaultNewRecord]="defaultNewRecordIdl"
23 [disableOrgFilter]="disableOrgFilter"></eg-admin-page>
28 export class BasicAdminPageComponent implements OnInit {
32 persistKeyPfx: string;
35 defaultNewRecordIdl: IdlObject;
36 configLinkBasePath = '/staff/admin';
38 // Tell the admin page to disable and hide the automagic org unit filter
39 disableOrgFilter: boolean;
41 private getParams$: Observable<ParamMap>;
42 private getRouteData$: Observable<any>;
43 private getParentUrl$: Observable<any>;
45 private schema: string;
46 private table: string;
47 private defaultNewRecord: Record<string, any>;
50 private route: ActivatedRoute,
51 private idl: IdlService
56 this.getParams$ = this.route.paramMap
58 this.schema = params.get('schema');
59 this.table = params.get('table');
62 this.getRouteData$ = this.route.data
63 .pipe(tap(routeData => {
64 const data = routeData[0];
67 // Schema and table can both be passed
68 // by either param or data
70 this.schema = data['schema'];
73 this.table = data['table'];
75 this.disableOrgFilter = data['disableOrgFilter'];
76 this.fieldOrder = data['fieldOrder'];
77 this.readonlyFields = data['readonlyFields'];
78 this.defaultNewRecord = data['defaultNewRecord'];
83 this.getParentUrl$ = this.route.parent.url
84 .pipe(tap(parentUrl => {
85 // Set the prefix to "server", "local", "workstation",
86 // extracted from the URL path.
87 // For admin pages that use none of these, avoid setting
88 // the prefix because that will cause it to double-up.
89 // e.g. eg.grid.acq.acq.cancel_reason
90 this.persistKeyPfx = this.route.snapshot.parent.url[0].path;
91 const selfPrefixers = ['acq', 'action_trigger', 'booking'];
92 if (selfPrefixers.indexOf(this.persistKeyPfx) > -1) {
93 // selfPrefixers, unlike 'server', 'local', and
94 // 'workstation', are the root of the path.
95 this.persistKeyPfx = '';
97 this.configLinkBasePath += '/' + this.persistKeyPfx;
101 this.getParentUrl$.subscribe();
102 this.getParams$.pipe(
103 switchMap(() => this.getRouteData$)
105 const fullTable = this.schema + '.' + this.table;
107 Object.keys(this.idl.classes).forEach(class_ => {
108 const classDef = this.idl.classes[class_];
109 if (classDef.table === fullTable) {
110 this.idlClass = class_;
111 this.classLabel = classDef.label;
115 if (!this.idlClass) {
116 throw new Error('Unable to find IDL class for table ' + fullTable);
119 if (this.defaultNewRecord) {
120 const record = this.idl.create(this.idlClass);
121 // Call IDL setter for each field that has a default value
122 Object.keys(this.defaultNewRecord).forEach(key => {
123 if (typeof (record[key]) === 'function') {
124 record[key].apply(record, [this.defaultNewRecord[key]]);
126 console.warn('Unknown field "' + key + '" in defaultNewRecord for table ' + fullTable);
129 this.defaultNewRecordIdl = record;