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}}" 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 [initialFilterValues]="initialFilterValues"
29 [defaultNewRecord]="defaultNewRecordIdl"
30 [enableUndelete]="enableUndelete"
31 [disableDelete]="disableDelete"
32 [deleteConfirmation]="deleteConfirmation"
33 [disableEdit]="disableEdit"
34 [disableOrgFilter]="disableOrgFilter"></eg-admin-page>
39 export class BasicAdminPageComponent implements OnInit {
43 persistKeyPfx: string;
46 contextOrgSelectorPersistKey = '';
49 orgDefaultAllowed = '';
50 orgFieldsDefaultingToContextOrg = '';
51 hideClearFilters: boolean;
52 initialFilterValues: {[field: string]: string};
53 defaultNewRecordIdl: IdlObject;
54 configLinkBasePath = '/staff/admin';
56 // Tell the admin page to disable and hide the automagic org unit filter
57 disableOrgFilter: boolean;
59 enableUndelete: boolean;
60 disableDelete: boolean;
61 deleteConfirmation: string;
64 private getParams$: Observable<ParamMap>;
65 private getRouteData$: Observable<any>;
66 private getParentUrl$: Observable<any>;
68 private schema: string;
69 private table: string;
70 private defaultNewRecord: Record<string, any>;
73 private route: ActivatedRoute,
74 private idl: IdlService
79 this.getParams$ = this.route.paramMap
81 this.schema = params.get('schema');
82 this.table = params.get('table');
85 this.getRouteData$ = this.route.data
86 .pipe(tap(routeData => {
87 const data = routeData[0];
90 // Schema and table can both be passed
91 // by either param or data
93 this.schema = data['schema'];
96 this.table = data['table'];
98 this.disableOrgFilter = data['disableOrgFilter'];
99 this.enableUndelete = data['enableUndelete'];
100 this.disableDelete = data['disableDelete'];
101 this.deleteConfirmation = data['deleteConfirmation'];
102 this.disableEdit = data['disableEdit'];
103 this.fieldOrder = data['fieldOrder'];
104 this.fieldOptions = data['fieldOptions'];
105 this.contextOrgSelectorPersistKey = data['contextOrgSelectorPersistKey'];
106 this.readonlyFields = data['readonlyFields'];
107 this.recordLabel = data['recordLabel'];
108 this.orgDefaultAllowed = data['orgDefaultAllowed'];
109 this.orgFieldsDefaultingToContextOrg = data['orgFieldsDefaultingToContextOrg'];
110 this.hideClearFilters = data['hideClearFilters'];
111 this.initialFilterValues = data['initialFilterValues'];
112 this.defaultNewRecord = data['defaultNewRecord'];
117 this.getParentUrl$ = this.route.parent.url
118 .pipe(tap(parentUrl => {
119 // Set the prefix to "server", "local", "workstation",
120 // extracted from the URL path.
121 // For admin pages that use none of these, avoid setting
122 // the prefix because that will cause it to double-up.
123 // e.g. eg.grid.acq.acq.cancel_reason
124 this.persistKeyPfx = this.route.snapshot.parent.url[0].path;
125 const selfPrefixers = ['acq', 'action_trigger', 'booking'];
126 if (selfPrefixers.indexOf(this.persistKeyPfx) > -1) {
127 // selfPrefixers, unlike 'server', 'local', and
128 // 'workstation', are the root of the path.
129 this.persistKeyPfx = '';
131 this.configLinkBasePath += '/' + this.persistKeyPfx;
135 this.getParentUrl$.subscribe();
136 this.getParams$.pipe(
137 switchMap(() => this.getRouteData$)
139 const fullTable = this.schema + '.' + this.table;
141 Object.keys(this.idl.classes).forEach(class_ => {
142 const classDef = this.idl.classes[class_];
143 if (classDef.table === fullTable) {
144 this.idlClass = class_;
145 this.classLabel = classDef.label;
149 if (!this.idlClass) {
150 throw new Error('Unable to find IDL class for table ' + fullTable);
153 if (this.defaultNewRecord) {
154 const record = this.idl.create(this.idlClass);
155 // Call IDL setter for each field that has a default value
156 Object.keys(this.defaultNewRecord).forEach(key => {
157 if (typeof (record[key]) === 'function') {
158 record[key].apply(record, [this.defaultNewRecord[key]]);
160 console.warn('Unknown field "' + key + '" in defaultNewRecord for table ' + fullTable);
163 this.defaultNewRecordIdl = record;