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="{{recordLabel || classLabel}} Administration">
16 <eg-staff-banner bannerText="{{recordLabel || classLabel}} Configuration" i18n-bannerText>
18 <eg-admin-page persistKeyPfx="{{persistKeyPfx}}" idlClass="{{idlClass}}"
19 configLinkBasePath="{{configLinkBasePath}}"
20 fieldOrder="{{fieldOrder}}"
21 readonlyFields="{{readonlyFields}}"
22 recordLabel="{{recordLabel}}"
23 orgDefaultAllowed="{{orgDefaultAllowed}}"
24 [hideClearFilters]="hideClearFilters"
25 [defaultNewRecord]="defaultNewRecordIdl"
26 [enableUndelete]="enableUndelete"
27 [disableOrgFilter]="disableOrgFilter"></eg-admin-page>
32 export class BasicAdminPageComponent implements OnInit {
36 persistKeyPfx: string;
40 orgDefaultAllowed = '';
41 hideClearFilters: boolean;
42 defaultNewRecordIdl: IdlObject;
43 configLinkBasePath = '/staff/admin';
45 // Tell the admin page to disable and hide the automagic org unit filter
46 disableOrgFilter: boolean;
48 enableUndelete: boolean;
50 private getParams$: Observable<ParamMap>;
51 private getRouteData$: Observable<any>;
52 private getParentUrl$: Observable<any>;
54 private schema: string;
55 private table: string;
56 private defaultNewRecord: Record<string, any>;
59 private route: ActivatedRoute,
60 private idl: IdlService
65 this.getParams$ = this.route.paramMap
67 this.schema = params.get('schema');
68 this.table = params.get('table');
71 this.getRouteData$ = this.route.data
72 .pipe(tap(routeData => {
73 const data = routeData[0];
76 // Schema and table can both be passed
77 // by either param or data
79 this.schema = data['schema'];
82 this.table = data['table'];
84 this.disableOrgFilter = data['disableOrgFilter'];
85 this.enableUndelete = data['enableUndelete'];
86 this.fieldOrder = data['fieldOrder'];
87 this.readonlyFields = data['readonlyFields'];
88 this.recordLabel = data['recordLabel'];
89 this.orgDefaultAllowed = data['orgDefaultAllowed'];
90 this.hideClearFilters = data['hideClearFilters'];
91 this.defaultNewRecord = data['defaultNewRecord'];
96 this.getParentUrl$ = this.route.parent.url
97 .pipe(tap(parentUrl => {
98 // Set the prefix to "server", "local", "workstation",
99 // extracted from the URL path.
100 // For admin pages that use none of these, avoid setting
101 // the prefix because that will cause it to double-up.
102 // e.g. eg.grid.acq.acq.cancel_reason
103 this.persistKeyPfx = this.route.snapshot.parent.url[0].path;
104 const selfPrefixers = ['acq', 'action_trigger', 'booking'];
105 if (selfPrefixers.indexOf(this.persistKeyPfx) > -1) {
106 // selfPrefixers, unlike 'server', 'local', and
107 // 'workstation', are the root of the path.
108 this.persistKeyPfx = '';
110 this.configLinkBasePath += '/' + this.persistKeyPfx;
114 this.getParentUrl$.subscribe();
115 this.getParams$.pipe(
116 switchMap(() => this.getRouteData$)
118 const fullTable = this.schema + '.' + this.table;
120 Object.keys(this.idl.classes).forEach(class_ => {
121 const classDef = this.idl.classes[class_];
122 if (classDef.table === fullTable) {
123 this.idlClass = class_;
124 this.classLabel = classDef.label;
128 if (!this.idlClass) {
129 throw new Error('Unable to find IDL class for table ' + fullTable);
132 if (this.defaultNewRecord) {
133 const record = this.idl.create(this.idlClass);
134 // Call IDL setter for each field that has a default value
135 Object.keys(this.defaultNewRecord).forEach(key => {
136 if (typeof (record[key]) === 'function') {
137 record[key].apply(record, [this.defaultNewRecord[key]]);
139 console.warn('Unknown field "' + key + '" in defaultNewRecord for table ' + fullTable);
142 this.defaultNewRecordIdl = record;