1 import {Component, Input, OnInit, TemplateRef, ViewChild} from '@angular/core';
2 import {ActivatedRoute} from '@angular/router';
3 import {IdlService, IdlObject} from '@eg/core/idl.service';
4 import {GridDataSource} from '@eg/share/grid/grid';
5 import {GridComponent} from '@eg/share/grid/grid.component';
6 import {TranslateComponent} from '@eg/share/translate/translate.component';
7 import {ToastService} from '@eg/share/toast/toast.service';
8 import {Pager} from '@eg/share/util/pager';
9 import {PcrudService} from '@eg/core/pcrud.service';
10 import {OrgService} from '@eg/core/org.service';
11 import {PermService} from '@eg/core/perm.service';
12 import {AuthService} from '@eg/core/auth.service';
13 import {FmRecordEditorComponent, FmFieldOptions
14 } from '@eg/share/fm-editor/fm-editor.component';
15 import {StringComponent} from '@eg/share/string/string.component';
16 import {OrgFamily} from '@eg/share/org-family-select/org-family-select.component';
19 * General purpose CRUD interface for IDL objects
21 * Object types using this component must be editable via PCRUD.
25 selector: 'eg-admin-page',
26 templateUrl: './admin-page.component.html'
29 export class AdminPageComponent implements OnInit {
31 @Input() idlClass: string;
33 // Default sort field, used when no grid sorting is applied.
34 @Input() sortField: string;
36 // Data source may be provided by the caller. This gives the caller
37 // complete control over the contents of the grid. If no data source
38 // is provided, a generic one is create which is sufficient for data
39 // that requires no special handling, filtering, etc.
40 @Input() dataSource: GridDataSource;
42 // Size of create/edito dialog. Uses large by default.
43 @Input() dialogSize: 'sm' | 'lg' = 'lg';
45 // comma-separated list of fields to hide.
46 // This does not imply all other fields should be visible, only that
47 // the selected fields will be hidden.
48 @Input() hideFields: string;
50 // If an org unit field is specified, an org unit filter
51 // is added to the top of the page.
52 @Input() orgField: string;
54 // Disable the auto-matic org unit field filter
55 @Input() disableOrgFilter: boolean;
57 // Include objects linking to org units which are ancestors
58 // of the selected org unit.
59 @Input() includeOrgAncestors: boolean;
61 // Ditto includeOrgAncestors, but descendants.
62 @Input() includeOrgDescendants: boolean;
64 // Optional grid persist key. This is the part of the key
66 @Input() persistKey: string;
68 // Optional path component to add to the generated grid persist key,
69 // formatted as (for example):
70 // 'eg.grid.admin.${persistKeyPfx}.config.billing_type'
71 @Input() persistKeyPfx: string;
73 // Optional comma-separated list of read-only fields
74 @Input() readonlyFields: string;
76 // Optional template containing help/about text which will
77 // be added to the page, above the grid.
78 @Input() helpTemplate: TemplateRef<any>;
80 // Override field options for create/edit dialog
81 @Input() fieldOptions: {[field: string]: FmFieldOptions};
83 // Override default values for fm-editor
84 @Input() defaultNewRecord: IdlObject;
87 @ViewChild('grid', { static: true }) grid: GridComponent;
88 @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
89 @ViewChild('successString', { static: true }) successString: StringComponent;
90 @ViewChild('createString', { static: true }) createString: StringComponent;
91 @ViewChild('createErrString', { static: true }) createErrString: StringComponent;
92 @ViewChild('updateFailedString', { static: true }) updateFailedString: StringComponent;
93 @ViewChild('deleteFailedString', { static: true }) deleteFailedString: StringComponent;
94 @ViewChild('deleteSuccessString', { static: true }) deleteSuccessString: StringComponent;
95 @ViewChild('translator', { static: true }) translator: TranslateComponent;
100 // True if any columns on the object support translations
101 translateRowIdx: number;
102 translateFieldIdx: number;
103 translatableFields: string[];
105 contextOrg: IdlObject;
106 searchOrgs: OrgFamily;
107 orgFieldLabel: string;
111 // Filters may be passed via URL query param.
112 // They are used to augment the grid data search query.
113 gridFilters: {[key: string]: string | number};
116 private route: ActivatedRoute,
117 public idl: IdlService,
118 private org: OrgService,
119 public auth: AuthService,
120 public pcrud: PcrudService,
121 private perm: PermService,
122 public toast: ToastService
124 this.translatableFields = [];
127 applyOrgValues(orgId?: number) {
129 if (this.disableOrgFilter) {
130 this.orgField = null;
134 if (!this.orgField) {
135 // If no org unit field is specified, try to find one.
136 // If an object type has multiple org unit fields, the
137 // caller should specify one or disable org unit filter.
138 this.idlClassDef.fields.forEach(field => {
139 if (field['class'] === 'aou') {
140 this.orgField = field.name;
146 this.orgFieldLabel = this.idlClassDef.field_map[this.orgField].label;
147 this.contextOrg = this.org.get(orgId) || this.org.root();
148 this.searchOrgs = {primaryOrgId: this.contextOrg.id()};
153 this.idlClassDef = this.idl.classes[this.idlClass];
154 this.pkeyField = this.idlClassDef.pkey || 'id';
156 this.translatableFields =
157 this.idlClassDef.fields.filter(f => f.i18n).map(f => f.name);
159 if (!this.persistKey) {
162 (this.persistKeyPfx ? this.persistKeyPfx + '.' : '') +
163 this.idlClassDef.table;
166 // gridFilters are a JSON encoded string
167 const filters = this.route.snapshot.queryParamMap.get('gridFilters');
170 this.gridFilters = JSON.parse(filters);
172 console.error('Invalid grid filters provided: ', filters);
176 // Limit the view org selector to orgs where the user has
177 // permacrud-encoded view permissions.
178 const pc = this.idlClassDef.permacrud;
179 if (pc && pc.retrieve) {
180 this.viewPerms = pc.retrieve.perms;
183 const contextOrg = this.route.snapshot.queryParamMap.get('contextOrg');
184 this.checkCreatePerms();
185 this.applyOrgValues(Number(contextOrg));
187 // If the caller provides not data source, create a generic one.
188 if (!this.dataSource) {
189 this.initDataSource();
192 // TODO: pass the row activate handler via the grid markup
193 this.grid.onRowActivate.subscribe(
194 (idlThing: IdlObject) => this.showEditDialog(idlThing)
199 this.canCreate = false;
200 const pc = this.idlClassDef.permacrud || {};
201 const perms = pc.create ? pc.create.perms : [];
202 if (perms.length === 0) { return; }
204 this.perm.hasWorkPermAt(perms, true).then(permMap => {
205 Object.keys(permMap).forEach(key => {
206 if (permMap[key].length > 0) {
207 this.canCreate = true;
214 this.dataSource = new GridDataSource();
216 this.dataSource.getRows = (pager: Pager, sort: any[]) => {
217 const orderBy: any = {};
220 // Sort specified from grid
221 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
223 } else if (this.sortField) {
224 // Default sort field
225 orderBy[this.idlClass] = this.sortField;
229 offset: pager.offset,
234 if (!this.contextOrg && !this.gridFilters) {
235 // No org filter -- fetch all rows
236 return this.pcrud.retrieveAll(
237 this.idlClass, searchOps, {fleshSelectors: true});
240 const search: any = {};
242 search[this.orgField] = this.searchOrgs.orgIds || [this.contextOrg.id()];
244 if (this.gridFilters) {
245 // Lay the URL grid filters over our search object.
246 Object.keys(this.gridFilters).forEach(key => {
247 search[key] = this.gridFilters[key];
251 return this.pcrud.search(
252 this.idlClass, search, searchOps, {fleshSelectors: true});
256 showEditDialog(idlThing: IdlObject): Promise<any> {
257 this.editDialog.mode = 'update';
258 this.editDialog.recordId = idlThing[this.pkeyField]();
259 return new Promise((resolve, reject) => {
260 this.editDialog.open({size: this.dialogSize}).subscribe(
262 this.successString.current()
263 .then(str => this.toast.success(str));
268 this.updateFailedString.current()
269 .then(str => this.toast.danger(str));
276 editSelected(idlThings: IdlObject[]) {
278 // Edit each IDL thing one at a time
279 const editOneThing = (thing: IdlObject) => {
280 if (!thing) { return; }
282 this.showEditDialog(thing).then(
283 () => editOneThing(idlThings.shift()));
286 editOneThing(idlThings.shift());
289 deleteSelected(idlThings: IdlObject[]) {
290 idlThings.forEach(idlThing => idlThing.isdeleted(true));
291 this.pcrud.autoApply(idlThings).subscribe(
293 console.debug('deleted: ' + val);
294 this.deleteSuccessString.current()
295 .then(str => this.toast.success(str));
298 this.deleteFailedString.current()
299 .then(str => this.toast.danger(str));
301 () => this.grid.reload()
306 this.editDialog.mode = 'create';
307 // We reuse the same editor for all actions. Be sure
308 // create action does not try to modify an existing record.
309 this.editDialog.recordId = null;
310 this.editDialog.record = null;
311 this.editDialog.open({size: this.dialogSize}).subscribe(
313 this.createString.current()
314 .then(str => this.toast.success(str));
318 if (!rejection.dismissed) {
319 this.createErrString.current()
320 .then(str => this.toast.danger(str));
325 // Open the field translation dialog.
326 // Link the next/previous actions to cycle through each translatable
327 // field on each row.
329 this.translateRowIdx = 0;
330 this.translateFieldIdx = 0;
331 this.translator.fieldName = this.translatableFields[this.translateFieldIdx];
332 this.translator.idlObject = this.dataSource.data[this.translateRowIdx];
334 this.translator.nextString = () => {
336 if (this.translateFieldIdx < this.translatableFields.length - 1) {
337 this.translateFieldIdx++;
339 } else if (this.translateRowIdx < this.dataSource.data.length - 1) {
340 this.translateRowIdx++;
341 this.translateFieldIdx = 0;
344 this.translator.idlObject =
345 this.dataSource.data[this.translateRowIdx];
346 this.translator.fieldName =
347 this.translatableFields[this.translateFieldIdx];
350 this.translator.prevString = () => {
352 if (this.translateFieldIdx > 0) {
353 this.translateFieldIdx--;
355 } else if (this.translateRowIdx > 0) {
356 this.translateRowIdx--;
357 this.translateFieldIdx = 0;
360 this.translator.idlObject =
361 this.dataSource.data[this.translateRowIdx];
362 this.translator.fieldName =
363 this.translatableFields[this.translateFieldIdx];
366 this.translator.open({size: 'lg'});