import {Component, Input, OnInit, TemplateRef, ViewChild} from '@angular/core';
+import {ActivatedRoute} from '@angular/router';
import {IdlService, IdlObject} from '@eg/core/idl.service';
import {GridDataSource} from '@eg/share/grid/grid';
import {GridComponent} from '@eg/share/grid/grid.component';
@ViewChild('editDialog') editDialog: FmRecordEditorComponent;
@ViewChild('successString') successString: StringComponent;
@ViewChild('createString') createString: StringComponent;
+ @ViewChild('createErrString') createErrString: StringComponent;
+ @ViewChild('updateFailedString') updateFailedString: StringComponent;
@ViewChild('translator') translator: TranslateComponent;
idlClassDef: any;
viewPerms: string;
canCreate: boolean;
+ // Filters may be passed via URL query param.
+ // They are used to augment the grid data search query.
+ gridFilters: {[key: string]: string | number};
+
constructor(
+ private route: ActivatedRoute,
private idl: IdlService,
private org: OrgService,
private auth: AuthService,
this.translatableFields = [];
}
- applyOrgValues() {
+ applyOrgValues(orgId?: number) {
if (this.disableOrgFilter) {
this.orgField = null;
if (this.orgField) {
this.orgFieldLabel = this.idlClassDef.field_map[this.orgField].label;
- this.contextOrg = this.org.root();
+ this.contextOrg = this.org.get(orgId) || this.org.root();
}
}
this.idlClassDef.table;
}
+ // gridFilters are a JSON encoded string
+ const filters = this.route.snapshot.queryParamMap.get('gridFilters');
+ if (filters) {
+ try {
+ this.gridFilters = JSON.parse(filters);
+ } catch (E) {
+ console.error('Invalid grid filters provided: ', filters)
+ }
+ }
+
// Limit the view org selector to orgs where the user has
// permacrud-encoded view permissions.
const pc = this.idlClassDef.permacrud;
this.viewPerms = pc.retrieve.perms;
}
+ const contextOrg = this.route.snapshot.queryParamMap.get('contextOrg');
this.checkCreatePerms();
- this.applyOrgValues();
+ this.applyOrgValues(Number(contextOrg));
// If the caller provides not data source, create a generic one.
if (!this.dataSource) {
// Edit each IDL thing one at a time
const editOneThing = (thing: IdlObject) => {
- if (!thing) return;
+ if (!thing) { return; }
this.showEditDialog(thing).then(
() => editOneThing(idlThings.shift()));
- }
+ };
- editOneThing(idlThings.shift()); };
+ editOneThing(idlThings.shift());
+ };
this.createNew = () => {
this.editDialog.mode = 'create';
+ // We reuse the same editor for all actions. Be sure
+ // create action does not try to modify an existing record.
+ this.editDialog.recId = null;
+ this.editDialog.record = null;
this.editDialog.open({size: this.dialogSize}).then(
ok => {
this.createString.current()
.then(str => this.toast.success(str));
this.grid.reload();
},
- err => {}
+ rejection => {
+ if (!rejection.dismissed) {
+ this.createErrString.current()
+ .then(str => this.toast.danger(str));
+ }
+ }
);
};
order_by: orderBy
};
+ if (!this.contextOrg && !this.gridFilters) {
+ // No org filter -- fetch all rows
+ return this.pcrud.retrieveAll(
+ this.idlClass, searchOps, {fleshSelectors: true});
+ }
+
+ const search: any = {};
+
if (this.contextOrg) {
// Filter rows by those linking to the context org and
// optionally ancestor and descendant org units.
this.org.descendants(this.contextOrg, true));
}
- const search = {};
search[this.orgField] = orgs;
- return this.pcrud.search(this.idlClass, search, searchOps);
}
- // No org filter -- fetch all rows
- return this.pcrud.retrieveAll(this.idlClass, searchOps);
+ if (this.gridFilters) {
+ // Lay the URL grid filters over our search object.
+ Object.keys(this.gridFilters).forEach(key => {
+ search[key] = this.gridFilters[key];
+ });
+ }
+
+ return this.pcrud.search(
+ this.idlClass, search, searchOps, {fleshSelectors: true});
};
}
.then(str => this.toast.success(str));
this.grid.reload();
},
- err => {}
+ rejection => {
+ if (!rejection.dismissed) {
+ this.updateFailedString.current()
+ .then(str => this.toast.danger(str));
+ }
+ }
);
}