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('translator') translator: TranslateComponent;
76 createNew: () => void;
77 deleteSelected: (rows: IdlObject[]) => void;
78 editSelected: (rows: IdlObject[]) => void;
80 // True if any columns on the object support translations
81 translateRowIdx: number;
82 translateFieldIdx: number;
83 translatableFields: string[];
84 translate: () => void;
86 contextOrg: IdlObject;
87 orgFieldLabel: string;
92 private idl: IdlService,
93 private org: OrgService,
94 private auth: AuthService,
95 private pcrud: PcrudService,
96 private perm: PermService,
97 private toast: ToastService
99 this.translatableFields = [];
104 if (this.disableOrgFilter) {
105 this.orgField = null;
109 if (!this.orgField) {
110 // If no org unit field is specified, try to find one.
111 // If an object type has multiple org unit fields, the
112 // caller should specify one or disable org unit filter.
113 this.idlClassDef.fields.forEach(field => {
114 if (field['class'] === 'aou') {
115 this.orgField = field.name;
121 this.orgFieldLabel = this.idlClassDef.field_map[this.orgField].label;
122 this.contextOrg = this.org.root();
127 this.idlClassDef = this.idl.classes[this.idlClass];
128 this.pkeyField = this.idlClassDef.pkey || 'id';
130 this.translatableFields =
131 this.idlClassDef.fields.filter(f => f.i18n).map(f => f.name);
133 if (!this.persistKey) {
136 (this.persistKeyPfx ? this.persistKeyPfx + '.' : '') +
137 this.idlClassDef.table;
140 // Limit the view org selector to orgs where the user has
141 // permacrud-encoded view permissions.
142 const pc = this.idlClassDef.permacrud;
143 if (pc && pc.retrieve) {
144 this.viewPerms = pc.retrieve.perms;
147 this.checkCreatePerms();
148 this.applyOrgValues();
150 // If the caller provides not data source, create a generic one.
151 if (!this.dataSource) {
152 this.initDataSource();
155 // TODO: pass the row activate handler via the grid markup
156 this.grid.onRowActivate.subscribe(
157 (idlThing: IdlObject) => this.showEditDialog(idlThing)
160 this.editSelected = (idlThings: IdlObject[]) => {
162 // Edit each IDL thing one at a time
163 const editOneThing = (thing: IdlObject) => {
166 this.showEditDialog(thing).then(
167 () => editOneThing(idlThings.shift()));
170 editOneThing(idlThings.shift()); };
172 this.createNew = () => {
173 this.editDialog.mode = 'create';
174 this.editDialog.open({size: this.dialogSize}).then(
176 this.createString.current()
177 .then(str => this.toast.success(str));
184 this.deleteSelected = (idlThings: IdlObject[]) => {
185 idlThings.forEach(idlThing => idlThing.isdeleted(true));
186 this.pcrud.autoApply(idlThings).subscribe(
187 val => console.debug('deleted: ' + val),
189 () => this.grid.reload()
193 // Open the field translation dialog.
194 // Link the next/previous actions to cycle through each translatable
195 // field on each row.
196 this.translate = () => {
197 this.translateRowIdx = 0;
198 this.translateFieldIdx = 0;
199 this.translator.fieldName = this.translatableFields[this.translateFieldIdx];
200 this.translator.idlObject = this.dataSource.data[this.translateRowIdx];
202 this.translator.nextString = () => {
204 if (this.translateFieldIdx < this.translatableFields.length - 1) {
205 this.translateFieldIdx++;
207 } else if (this.translateRowIdx < this.dataSource.data.length - 1) {
208 this.translateRowIdx++;
209 this.translateFieldIdx = 0;
212 this.translator.idlObject =
213 this.dataSource.data[this.translateRowIdx];
214 this.translator.fieldName =
215 this.translatableFields[this.translateFieldIdx];
218 this.translator.prevString = () => {
220 if (this.translateFieldIdx > 0) {
221 this.translateFieldIdx--;
223 } else if (this.translateRowIdx > 0) {
224 this.translateRowIdx--;
225 this.translateFieldIdx = 0;
228 this.translator.idlObject =
229 this.dataSource.data[this.translateRowIdx];
230 this.translator.fieldName =
231 this.translatableFields[this.translateFieldIdx];
234 this.translator.open({size: 'lg'});
239 this.canCreate = false;
240 const pc = this.idlClassDef.permacrud || {};
241 const perms = pc.create ? pc.create.perms : [];
242 if (perms.length === 0) { return; }
244 this.perm.hasWorkPermAt(perms, true).then(permMap => {
245 Object.keys(permMap).forEach(key => {
246 if (permMap[key].length > 0) {
247 this.canCreate = true;
253 orgOnChange(org: IdlObject) {
254 this.contextOrg = org;
259 this.dataSource = new GridDataSource();
261 this.dataSource.getRows = (pager: Pager, sort: any[]) => {
262 const orderBy: any = {};
265 // Sort specified from grid
266 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
268 } else if (this.sortField) {
269 // Default sort field
270 orderBy[this.idlClass] = this.sortField;
274 offset: pager.offset,
279 if (this.contextOrg) {
280 // Filter rows by those linking to the context org and
281 // optionally ancestor and descendant org units.
283 let orgs = [this.contextOrg.id()];
285 if (this.includeOrgAncestors) {
286 orgs = this.org.ancestors(this.contextOrg, true);
289 if (this.includeOrgDescendants) {
290 // can result in duplicate workstation org IDs... meh
292 this.org.descendants(this.contextOrg, true));
296 search[this.orgField] = orgs;
297 return this.pcrud.search(this.idlClass, search, searchOps);
300 // No org filter -- fetch all rows
301 return this.pcrud.retrieveAll(this.idlClass, searchOps);
305 disableAncestorSelector(): boolean {
306 return this.contextOrg &&
307 this.contextOrg.id() === this.org.root().id();
310 disableDescendantSelector(): boolean {
311 return this.contextOrg && this.contextOrg.children().length === 0;
314 showEditDialog(idlThing: IdlObject) {
315 this.editDialog.mode = 'update';
316 this.editDialog.recId = idlThing[this.pkeyField]();
317 return this.editDialog.open({size: this.dialogSize}).then(
319 this.successString.current()
320 .then(str => this.toast.success(str));