1 import {Component, Input, OnInit, TemplateRef, ViewChild} from '@angular/core';
2 import {IdlService, IdlObject} from '@eg/core/idl.service';
3 import {GridDataSource} from '@eg/share/grid/grid';
4 import {GridComponent} from '@eg/share/grid/grid.component';
5 import {TranslateComponent} from '@eg/staff/share/translate/translate.component';
6 import {ToastService} from '@eg/share/toast/toast.service';
7 import {Pager} from '@eg/share/util/pager';
8 import {PcrudService} from '@eg/core/pcrud.service';
9 import {OrgService} from '@eg/core/org.service';
10 import {PermService} from '@eg/core/perm.service';
11 import {AuthService} from '@eg/core/auth.service';
12 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
13 import {StringComponent} from '@eg/share/string/string.component';
16 * General purpose CRUD interface for IDL objects
18 * Object types using this component must be editable via PCRUD.
22 selector: 'eg-admin-page',
23 templateUrl: './admin-page.component.html'
26 export class AdminPageComponent implements OnInit {
28 @Input() idlClass: string;
30 // Default sort field, used when no grid sorting is applied.
31 @Input() sortField: string;
33 // Data source may be provided by the caller. This gives the caller
34 // complete control over the contents of the grid. If no data source
35 // is provided, a generic one is create which is sufficient for data
36 // that requires no special handling, filtering, etc.
37 @Input() dataSource: GridDataSource;
39 // Size of create/edito dialog. Uses large by default.
40 @Input() dialogSize: 'sm' | 'lg' = 'lg';
42 // If an org unit field is specified, an org unit filter
43 // is added to the top of the page.
44 @Input() orgField: string;
46 // Disable the auto-matic org unit field filter
47 @Input() disableOrgFilter: boolean;
49 // Include objects linking to org units which are ancestors
50 // of the selected org unit.
51 @Input() includeOrgAncestors: boolean;
53 // Ditto includeOrgAncestors, but descendants.
54 @Input() includeOrgDescendants: boolean;
56 // Optional grid persist key. This is the part of the key
58 @Input() persistKey: string;
60 // Optional path component to add to the generated grid persist key,
61 // formatted as (for example):
62 // 'eg.grid.admin.${persistKeyPfx}.config.billing_type'
63 @Input() persistKeyPfx: string;
65 // Optional comma-separated list of read-only fields
66 @Input() readonlyFields: string;
68 @ViewChild('grid') grid: GridComponent;
69 @ViewChild('editDialog') editDialog: FmRecordEditorComponent;
70 @ViewChild('successString') successString: StringComponent;
71 @ViewChild('createString') createString: StringComponent;
72 @ViewChild('createErrString') createErrString: StringComponent;
73 @ViewChild('updateFailedString') updateFailedString: StringComponent;
74 @ViewChild('translator') translator: TranslateComponent;
78 createNew: () => void;
79 deleteSelected: (rows: IdlObject[]) => void;
80 editSelected: (rows: IdlObject[]) => void;
82 // True if any columns on the object support translations
83 translateRowIdx: number;
84 translateFieldIdx: number;
85 translatableFields: string[];
86 translate: () => void;
88 contextOrg: IdlObject;
89 orgFieldLabel: string;
94 private idl: IdlService,
95 private org: OrgService,
96 private auth: AuthService,
97 private pcrud: PcrudService,
98 private perm: PermService,
99 private toast: ToastService
101 this.translatableFields = [];
106 if (this.disableOrgFilter) {
107 this.orgField = null;
111 if (!this.orgField) {
112 // If no org unit field is specified, try to find one.
113 // If an object type has multiple org unit fields, the
114 // caller should specify one or disable org unit filter.
115 this.idlClassDef.fields.forEach(field => {
116 if (field['class'] === 'aou') {
117 this.orgField = field.name;
123 this.orgFieldLabel = this.idlClassDef.field_map[this.orgField].label;
124 this.contextOrg = this.org.root();
129 this.idlClassDef = this.idl.classes[this.idlClass];
130 this.pkeyField = this.idlClassDef.pkey || 'id';
132 this.translatableFields =
133 this.idlClassDef.fields.filter(f => f.i18n).map(f => f.name);
135 if (!this.persistKey) {
138 (this.persistKeyPfx ? this.persistKeyPfx + '.' : '') +
139 this.idlClassDef.table;
142 // Limit the view org selector to orgs where the user has
143 // permacrud-encoded view permissions.
144 const pc = this.idlClassDef.permacrud;
145 if (pc && pc.retrieve) {
146 this.viewPerms = pc.retrieve.perms;
149 this.checkCreatePerms();
150 this.applyOrgValues();
152 // If the caller provides not data source, create a generic one.
153 if (!this.dataSource) {
154 this.initDataSource();
157 // TODO: pass the row activate handler via the grid markup
158 this.grid.onRowActivate.subscribe(
159 (idlThing: IdlObject) => this.showEditDialog(idlThing)
162 this.editSelected = (idlThings: IdlObject[]) => {
164 // Edit each IDL thing one at a time
165 const editOneThing = (thing: IdlObject) => {
166 if (!thing) { return; }
168 this.showEditDialog(thing).then(
169 () => editOneThing(idlThings.shift()));
172 editOneThing(idlThings.shift());
175 this.createNew = () => {
176 this.editDialog.mode = 'create';
177 // We reuse the same editor for all actions. Be sure
178 // create action does not try to modify an existing record.
179 this.editDialog.recId = null;
180 this.editDialog.record = null;
181 this.editDialog.open({size: this.dialogSize}).then(
183 this.createString.current()
184 .then(str => this.toast.success(str));
188 if (!rejection.dismissed) {
189 this.createErrString.current()
190 .then(str => this.toast.danger(str));
196 this.deleteSelected = (idlThings: IdlObject[]) => {
197 idlThings.forEach(idlThing => idlThing.isdeleted(true));
198 this.pcrud.autoApply(idlThings).subscribe(
199 val => console.debug('deleted: ' + val),
201 () => this.grid.reload()
205 // Open the field translation dialog.
206 // Link the next/previous actions to cycle through each translatable
207 // field on each row.
208 this.translate = () => {
209 this.translateRowIdx = 0;
210 this.translateFieldIdx = 0;
211 this.translator.fieldName = this.translatableFields[this.translateFieldIdx];
212 this.translator.idlObject = this.dataSource.data[this.translateRowIdx];
214 this.translator.nextString = () => {
216 if (this.translateFieldIdx < this.translatableFields.length - 1) {
217 this.translateFieldIdx++;
219 } else if (this.translateRowIdx < this.dataSource.data.length - 1) {
220 this.translateRowIdx++;
221 this.translateFieldIdx = 0;
224 this.translator.idlObject =
225 this.dataSource.data[this.translateRowIdx];
226 this.translator.fieldName =
227 this.translatableFields[this.translateFieldIdx];
230 this.translator.prevString = () => {
232 if (this.translateFieldIdx > 0) {
233 this.translateFieldIdx--;
235 } else if (this.translateRowIdx > 0) {
236 this.translateRowIdx--;
237 this.translateFieldIdx = 0;
240 this.translator.idlObject =
241 this.dataSource.data[this.translateRowIdx];
242 this.translator.fieldName =
243 this.translatableFields[this.translateFieldIdx];
246 this.translator.open({size: 'lg'});
251 this.canCreate = false;
252 const pc = this.idlClassDef.permacrud || {};
253 const perms = pc.create ? pc.create.perms : [];
254 if (perms.length === 0) { return; }
256 this.perm.hasWorkPermAt(perms, true).then(permMap => {
257 Object.keys(permMap).forEach(key => {
258 if (permMap[key].length > 0) {
259 this.canCreate = true;
265 orgOnChange(org: IdlObject) {
266 this.contextOrg = org;
271 this.dataSource = new GridDataSource();
273 this.dataSource.getRows = (pager: Pager, sort: any[]) => {
274 const orderBy: any = {};
277 // Sort specified from grid
278 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
280 } else if (this.sortField) {
281 // Default sort field
282 orderBy[this.idlClass] = this.sortField;
286 offset: pager.offset,
291 if (this.contextOrg) {
292 // Filter rows by those linking to the context org and
293 // optionally ancestor and descendant org units.
295 let orgs = [this.contextOrg.id()];
297 if (this.includeOrgAncestors) {
298 orgs = this.org.ancestors(this.contextOrg, true);
301 if (this.includeOrgDescendants) {
302 // can result in duplicate workstation org IDs... meh
304 this.org.descendants(this.contextOrg, true));
308 search[this.orgField] = orgs;
309 return this.pcrud.search(
310 this.idlClass, search, searchOps, {fleshSelectors: true});
313 // No org filter -- fetch all rows
314 return this.pcrud.retrieveAll(
315 this.idlClass, searchOps, {fleshSelectors: true});
319 disableAncestorSelector(): boolean {
320 return this.contextOrg &&
321 this.contextOrg.id() === this.org.root().id();
324 disableDescendantSelector(): boolean {
325 return this.contextOrg && this.contextOrg.children().length === 0;
328 showEditDialog(idlThing: IdlObject) {
329 this.editDialog.mode = 'update';
330 this.editDialog.recId = idlThing[this.pkeyField]();
331 return this.editDialog.open({size: this.dialogSize}).then(
333 this.successString.current()
334 .then(str => this.toast.success(str));
338 if (!rejection.dismissed) {
339 this.updateFailedString.current()
340 .then(str => this.toast.danger(str));