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('translator') translator: TranslateComponent;
73 createNew: () => void;
74 deleteSelected: (rows: IdlObject[]) => void;
76 // True if any columns on the object support translations
77 translateRowIdx: number;
78 translateFieldIdx: number;
79 translatableFields: string[];
80 translate: () => void;
82 contextOrg: IdlObject;
83 orgFieldLabel: string;
88 private idl: IdlService,
89 private org: OrgService,
90 private auth: AuthService,
91 private pcrud: PcrudService,
92 private perm: PermService,
93 private toast: ToastService
95 this.translatableFields = [];
100 if (this.disableOrgFilter) {
101 this.orgField = null;
105 if (!this.orgField) {
106 // If no org unit field is specified, try to find one.
107 // If an object type has multiple org unit fields, the
108 // caller should specify one or disable org unit filter.
109 this.idlClassDef.fields.forEach(field => {
110 if (field['class'] === 'aou') {
111 this.orgField = field.name;
117 this.orgFieldLabel = this.idlClassDef.field_map[this.orgField].label;
118 this.contextOrg = this.org.root();
123 this.idlClassDef = this.idl.classes[this.idlClass];
124 this.pkeyField = this.idlClassDef.pkey || 'id';
126 this.translatableFields =
127 this.idlClassDef.fields.filter(f => f.i18n).map(f => f.name);
129 if (!this.persistKey) {
132 (this.persistKeyPfx ? this.persistKeyPfx + '.' : '') +
133 this.idlClassDef.table;
136 // Limit the view org selector to orgs where the user has
137 // permacrud-encoded view permissions.
138 const pc = this.idlClassDef.permacrud;
139 if (pc && pc.retrieve) {
140 this.viewPerms = pc.retrieve.perms;
143 this.checkCreatePerms();
144 this.applyOrgValues();
146 // If the caller provides not data source, create a generic one.
147 if (!this.dataSource) {
148 this.initDataSource();
151 // TODO: pass the row activate handler via the grid markup
152 this.grid.onRowActivate.subscribe(
153 (idlThing: IdlObject) => {
154 this.editDialog.mode = 'update';
155 this.editDialog.recId = idlThing[this.pkeyField]();
156 this.editDialog.open({size: this.dialogSize}).then(
158 this.successString.current()
159 .then(str => this.toast.success(str));
167 this.createNew = () => {
168 this.editDialog.mode = 'create';
169 this.editDialog.open({size: this.dialogSize}).then(
171 this.createString.current()
172 .then(str => this.toast.success(str));
179 this.deleteSelected = (idlThings: IdlObject[]) => {
180 idlThings.forEach(idlThing => idlThing.isdeleted(true));
181 this.pcrud.autoApply(idlThings).subscribe(
182 val => console.debug('deleted: ' + val),
184 () => this.grid.reload()
188 // Open the field translation dialog.
189 // Link the next/previous actions to cycle through each translatable
190 // field on each row.
191 this.translate = () => {
192 this.translateRowIdx = 0;
193 this.translateFieldIdx = 0;
194 this.translator.fieldName = this.translatableFields[this.translateFieldIdx];
195 this.translator.idlObject = this.dataSource.data[this.translateRowIdx];
197 this.translator.nextString = () => {
199 if (this.translateFieldIdx < this.translatableFields.length - 1) {
200 this.translateFieldIdx++;
202 } else if (this.translateRowIdx < this.dataSource.data.length - 1) {
203 this.translateRowIdx++;
204 this.translateFieldIdx = 0;
207 this.translator.idlObject =
208 this.dataSource.data[this.translateRowIdx];
209 this.translator.fieldName =
210 this.translatableFields[this.translateFieldIdx];
213 this.translator.prevString = () => {
215 if (this.translateFieldIdx > 0) {
216 this.translateFieldIdx--;
218 } else if (this.translateRowIdx > 0) {
219 this.translateRowIdx--;
220 this.translateFieldIdx = 0;
223 this.translator.idlObject =
224 this.dataSource.data[this.translateRowIdx];
225 this.translator.fieldName =
226 this.translatableFields[this.translateFieldIdx];
229 this.translator.open({size: 'lg'});
234 this.canCreate = false;
235 const pc = this.idlClassDef.permacrud || {};
236 const perms = pc.create ? pc.create.perms : [];
237 if (perms.length === 0) { return; }
239 this.perm.hasWorkPermAt(perms, true).then(permMap => {
240 Object.keys(permMap).forEach(key => {
241 if (permMap[key].length > 0) {
242 this.canCreate = true;
248 orgOnChange(org: IdlObject) {
249 this.contextOrg = org;
254 this.dataSource = new GridDataSource();
256 this.dataSource.getRows = (pager: Pager, sort: any[]) => {
257 const orderBy: any = {};
260 // Sort specified from grid
261 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
263 } else if (this.sortField) {
264 // Default sort field
265 orderBy[this.idlClass] = this.sortField;
269 offset: pager.offset,
274 if (this.contextOrg) {
275 // Filter rows by those linking to the context org and
276 // optionally ancestor and descendant org units.
278 let orgs = [this.contextOrg.id()];
280 if (this.includeOrgAncestors) {
281 orgs = this.org.ancestors(this.contextOrg, true);
284 if (this.includeOrgDescendants) {
285 // can result in duplicate workstation org IDs... meh
287 this.org.descendants(this.contextOrg, true));
291 search[this.orgField] = orgs;
292 return this.pcrud.search(this.idlClass, search, searchOps);
295 // No org filter -- fetch all rows
296 return this.pcrud.retrieveAll(this.idlClass, searchOps);
300 disableAncestorSelector(): boolean {
301 return this.contextOrg &&
302 this.contextOrg.id() === this.org.root().id();
305 disableDescendantSelector(): boolean {
306 return this.contextOrg && this.contextOrg.children().length === 0;