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 [fieldOptions]="fieldOptions"
22 readonlyFields="{{readonlyFields}}"
23 recordLabel="{{recordLabel}}"
24 orgDefaultAllowed="{{orgDefaultAllowed}}"
25 orgFieldsDefaultingToContextOrg="{{orgFieldsDefaultingToContextOrg}}"
26 contextOrgSelectorPersistKey="{{contextOrgSelectorPersistKey}}"
27 [hideClearFilters]="hideClearFilters"
28 [defaultNewRecord]="defaultNewRecordIdl"
29 [enableUndelete]="enableUndelete"
30 [disableDelete]="disableDelete"
31 [deleteConfirmation]="deleteConfirmation"
32 [disableEdit]="disableEdit"
33 [disableOrgFilter]="disableOrgFilter"></eg-admin-page>
38 export class BasicAdminPageComponent implements OnInit {
42 persistKeyPfx: string;
45 contextOrgSelectorPersistKey = '';
48 orgDefaultAllowed = '';
49 orgFieldsDefaultingToContextOrg = '';
50 hideClearFilters: boolean;
51 defaultNewRecordIdl: IdlObject;
52 configLinkBasePath = '/staff/admin';
54 // Tell the admin page to disable and hide the automagic org unit filter
55 disableOrgFilter: boolean;
57 enableUndelete: boolean;
58 disableDelete: boolean;
59 deleteConfirmation: string;
62 private getParams$: Observable<ParamMap>;
63 private getRouteData$: Observable<any>;
64 private getParentUrl$: Observable<any>;
66 private schema: string;
67 private table: string;
68 private defaultNewRecord: Record<string, any>;
71 private route: ActivatedRoute,
72 private idl: IdlService
77 this.getParams$ = this.route.paramMap
79 this.schema = params.get('schema');
80 this.table = params.get('table');
83 this.getRouteData$ = this.route.data
84 .pipe(tap(routeData => {
85 const data = routeData[0];
88 // Schema and table can both be passed
89 // by either param or data
91 this.schema = data['schema'];
94 this.table = data['table'];
96 this.disableOrgFilter = data['disableOrgFilter'];
97 this.enableUndelete = data['enableUndelete'];
98 this.disableDelete = data['disableDelete'];
99 this.deleteConfirmation = data['deleteConfirmation'];
100 this.disableEdit = data['disableEdit'];
101 this.fieldOrder = data['fieldOrder'];
102 this.fieldOptions = data['fieldOptions'];
103 this.contextOrgSelectorPersistKey = data['contextOrgSelectorPersistKey'];
104 this.readonlyFields = data['readonlyFields'];
105 this.recordLabel = data['recordLabel'];
106 this.orgDefaultAllowed = data['orgDefaultAllowed'];
107 this.orgFieldsDefaultingToContextOrg = data['orgFieldsDefaultingToContextOrg'];
108 this.hideClearFilters = data['hideClearFilters'];
109 this.defaultNewRecord = data['defaultNewRecord'];
114 this.getParentUrl$ = this.route.parent.url
115 .pipe(tap(parentUrl => {
116 // Set the prefix to "server", "local", "workstation",
117 // extracted from the URL path.
118 // For admin pages that use none of these, avoid setting
119 // the prefix because that will cause it to double-up.
120 // e.g. eg.grid.acq.acq.cancel_reason
121 this.persistKeyPfx = this.route.snapshot.parent.url[0].path;
122 const selfPrefixers = ['acq', 'action_trigger', 'booking'];
123 if (selfPrefixers.indexOf(this.persistKeyPfx) > -1) {
124 // selfPrefixers, unlike 'server', 'local', and
125 // 'workstation', are the root of the path.
126 this.persistKeyPfx = '';
128 this.configLinkBasePath += '/' + this.persistKeyPfx;
132 this.getParentUrl$.subscribe();
133 this.getParams$.pipe(
134 switchMap(() => this.getRouteData$)
136 const fullTable = this.schema + '.' + this.table;
138 Object.keys(this.idl.classes).forEach(class_ => {
139 const classDef = this.idl.classes[class_];
140 if (classDef.table === fullTable) {
141 this.idlClass = class_;
142 this.classLabel = classDef.label;
146 if (!this.idlClass) {
147 throw new Error('Unable to find IDL class for table ' + fullTable);
150 if (this.defaultNewRecord) {
151 const record = this.idl.create(this.idlClass);
152 // Call IDL setter for each field that has a default value
153 Object.keys(this.defaultNewRecord).forEach(key => {
154 if (typeof (record[key]) === 'function') {
155 record[key].apply(record, [this.defaultNewRecord[key]]);
157 console.warn('Unknown field "' + key + '" in defaultNewRecord for table ' + fullTable);
160 this.defaultNewRecordIdl = record;