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 @ViewChild('grid') grid: GridComponent;
66 @ViewChild('editDialog') editDialog: FmRecordEditorComponent;
67 @ViewChild('successString') successString: StringComponent;
68 @ViewChild('createString') createString: StringComponent;
69 @ViewChild('createErrString') createErrString: StringComponent;
70 @ViewChild('updateFailedString') updateFailedString: StringComponent;
71 @ViewChild('translator') translator: TranslateComponent;
75 createNew: () => void;
76 deleteSelected: (rows: IdlObject[]) => void;
77 editSelected: (rows: IdlObject[]) => void;
79 // True if any columns on the object support translations
80 translateRowIdx: number;
81 translateFieldIdx: number;
82 translatableFields: string[];
83 translate: () => void;
85 contextOrg: IdlObject;
86 orgFieldLabel: string;
91 private idl: IdlService,
92 private org: OrgService,
93 private auth: AuthService,
94 private pcrud: PcrudService,
95 private perm: PermService,
96 private toast: ToastService
98 this.translatableFields = [];
103 if (this.disableOrgFilter) {
104 this.orgField = null;
108 if (!this.orgField) {
109 // If no org unit field is specified, try to find one.
110 // If an object type has multiple org unit fields, the
111 // caller should specify one or disable org unit filter.
112 this.idlClassDef.fields.forEach(field => {
113 if (field['class'] === 'aou') {
114 this.orgField = field.name;
120 this.orgFieldLabel = this.idlClassDef.field_map[this.orgField].label;
121 this.contextOrg = this.org.root();
126 this.idlClassDef = this.idl.classes[this.idlClass];
127 this.pkeyField = this.idlClassDef.pkey || 'id';
129 this.translatableFields =
130 this.idlClassDef.fields.filter(f => f.i18n).map(f => f.name);
132 if (!this.persistKey) {
135 (this.persistKeyPfx ? this.persistKeyPfx + '.' : '') +
136 this.idlClassDef.table;
139 // Limit the view org selector to orgs where the user has
140 // permacrud-encoded view permissions.
141 const pc = this.idlClassDef.permacrud;
142 if (pc && pc.retrieve) {
143 this.viewPerms = pc.retrieve.perms;
146 this.checkCreatePerms();
147 this.applyOrgValues();
149 // If the caller provides not data source, create a generic one.
150 if (!this.dataSource) {
151 this.initDataSource();
154 // TODO: pass the row activate handler via the grid markup
155 this.grid.onRowActivate.subscribe(
156 (idlThing: IdlObject) => this.showEditDialog(idlThing)
159 this.editSelected = (idlThings: IdlObject[]) => {
161 // Edit each IDL thing one at a time
162 const editOneThing = (thing: IdlObject) => {
165 this.showEditDialog(thing).then(
166 () => editOneThing(idlThings.shift()));
169 editOneThing(idlThings.shift()); };
171 this.createNew = () => {
172 this.editDialog.mode = 'create';
173 this.editDialog.open({size: this.dialogSize}).then(
175 this.createString.current()
176 .then(str => this.toast.success(str));
180 this.createErrString.current()
181 .then(str => this.toast.danger(str));
186 this.deleteSelected = (idlThings: IdlObject[]) => {
187 idlThings.forEach(idlThing => idlThing.isdeleted(true));
188 this.pcrud.autoApply(idlThings).subscribe(
189 val => console.debug('deleted: ' + val),
191 () => this.grid.reload()
195 // Open the field translation dialog.
196 // Link the next/previous actions to cycle through each translatable
197 // field on each row.
198 this.translate = () => {
199 this.translateRowIdx = 0;
200 this.translateFieldIdx = 0;
201 this.translator.fieldName = this.translatableFields[this.translateFieldIdx];
202 this.translator.idlObject = this.dataSource.data[this.translateRowIdx];
204 this.translator.nextString = () => {
206 if (this.translateFieldIdx < this.translatableFields.length - 1) {
207 this.translateFieldIdx++;
209 } else if (this.translateRowIdx < this.dataSource.data.length - 1) {
210 this.translateRowIdx++;
211 this.translateFieldIdx = 0;
214 this.translator.idlObject =
215 this.dataSource.data[this.translateRowIdx];
216 this.translator.fieldName =
217 this.translatableFields[this.translateFieldIdx];
220 this.translator.prevString = () => {
222 if (this.translateFieldIdx > 0) {
223 this.translateFieldIdx--;
225 } else if (this.translateRowIdx > 0) {
226 this.translateRowIdx--;
227 this.translateFieldIdx = 0;
230 this.translator.idlObject =
231 this.dataSource.data[this.translateRowIdx];
232 this.translator.fieldName =
233 this.translatableFields[this.translateFieldIdx];
236 this.translator.open({size: 'lg'});
241 this.canCreate = false;
242 const pc = this.idlClassDef.permacrud || {};
243 const perms = pc.create ? pc.create.perms : [];
244 if (perms.length === 0) { return; }
246 this.perm.hasWorkPermAt(perms, true).then(permMap => {
247 Object.keys(permMap).forEach(key => {
248 if (permMap[key].length > 0) {
249 this.canCreate = true;
255 orgOnChange(org: IdlObject) {
256 this.contextOrg = org;
261 this.dataSource = new GridDataSource();
263 this.dataSource.getRows = (pager: Pager, sort: any[]) => {
264 const orderBy: any = {};
267 // Sort specified from grid
268 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
270 } else if (this.sortField) {
271 // Default sort field
272 orderBy[this.idlClass] = this.sortField;
276 offset: pager.offset,
281 if (this.contextOrg) {
282 // Filter rows by those linking to the context org and
283 // optionally ancestor and descendant org units.
285 let orgs = [this.contextOrg.id()];
287 if (this.includeOrgAncestors) {
288 orgs = this.org.ancestors(this.contextOrg, true);
291 if (this.includeOrgDescendants) {
292 // can result in duplicate workstation org IDs... meh
294 this.org.descendants(this.contextOrg, true));
298 search[this.orgField] = orgs;
299 return this.pcrud.search(this.idlClass, search, searchOps);
302 // No org filter -- fetch all rows
303 return this.pcrud.retrieveAll(this.idlClass, searchOps);
307 disableAncestorSelector(): boolean {
308 return this.contextOrg &&
309 this.contextOrg.id() === this.org.root().id();
312 disableDescendantSelector(): boolean {
313 return this.contextOrg && this.contextOrg.children().length === 0;
316 showEditDialog(idlThing: IdlObject) {
317 this.editDialog.mode = 'update';
318 this.editDialog.recId = idlThing[this.pkeyField]();
319 return this.editDialog.open({size: this.dialogSize}).then(
321 this.successString.current()
322 .then(str => this.toast.success(str));
326 this.updateFailedString.current()
327 .then(str => this.toast.danger(str));