/**
* Modules, services, and components used by all apps.
*/
-import {CommonModule} from '@angular/common';
+import {CommonModule, DatePipe, CurrencyPipe} from '@angular/common';
import {NgModule, ModuleWithProviders} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
+
import {EgEventService} from '@eg/core/event.service';
import {EgStoreService} from '@eg/core/store.service';
import {EgIdlService} from '@eg/core/idl.service';
import {EgPcrudService} from '@eg/core/pcrud.service';
import {EgOrgService} from '@eg/core/org.service';
import {EgAudioService} from '@eg/share/util/audio.service';
+import {EgFormatService} from '@eg/share/util/format.service';
@NgModule({
declarations: [
return {
ngModule: EgCommonModule,
providers: [
+ DatePipe,
+ CurrencyPipe,
EgEventService,
EgStoreService,
EgIdlService,
EgPermService,
EgPcrudService,
EgOrgService,
- EgAudioService
+ EgAudioService,
+ EgFormatService
]
};
}
import {EgOrgService} from '@eg/core/org.service';
import {EgPcrudService} from '@eg/core/pcrud.service';
import {EgStoreService} from '@eg/core/store.service';
+import {EgFormatService} from '@eg/share/util/format.service';
@Injectable()
private idl: EgIdlService,
private org: EgOrgService,
private store: EgStoreService,
- private pcrud: EgPcrudService
+ private pcrud: EgPcrudService,
+ private format: EgFormatService
) {
}
}
getRowColumnValue(row: any, col: EgGridColumn): string {
- if (row[col.name] === undefined || row[col.name] === null)
- return '';
-
- if (col.idlFieldDef)
- return this.getRowColumnIdlValue(row, col);
-
+ let val;
if (typeof row[col.name] == 'function') {
- let val = row[col.name]();
- if (val === undefined || val === null) return '';
- return val+'';
- }
-
- return row[col.name]+'';
- }
-
- getRowColumnIdlValue(row: any, col: EgGridColumn): string {
- let val = row[col.name]();
- if (val === undefined || val === null) return '';
-
- if (col.idlFieldDef.datatype == 'org_unit') {
- let o = this.org.get(val);
- if (o) return o.shortname();
+ val = row[col.name]();
+ } else {
+ val = row[col.name]
}
-
- return val+'';
+ return this.format.transform({value: val, datatype: col.datatype});
}
generateColumns(columnSet: EgGridColumnSet) {
col.name = field.name;
col.label = field.label || field.name;
col.idlFieldDef = field;
+ col.datatype = field.datatype;
if (field.name == this.idl.classes[columnSet.idlClass].pkey)
col.isIndex = true;
columnSet.add(col);
sort: number;
idlClass: string;
idlFieldDef: any;
+ datatype: string;
cellTemplate: TemplateRef<any>;
isIndex: boolean;
isDragTarget: boolean;
if (!col.flex) col.flex = 2;
if (!col.label) col.label = col.name;
if (!col.align) col.align = 'left';
+ if (!col.datatype) col.datatype = 'text';
col.visible = !col.hidden;
// Returns true if all of the requested indexes exist in the selector.
contains(index: string | string[]): boolean {
let indexes = [].concat(index);
- let selected = Object.keys(this.indexes);
for (let i = 0; i < indexes.length; i++) { // early exit
- if (!selected.includes(indexes[i]))
+ if (!this.indexes[indexes[i]])
return false;
}
return true;
indexes.forEach(i => delete this.indexes[i]);
}
+ // Returns the list of selected index values.
+ // in some contexts (template checkboxes) the value for an index is
+ // set to false to deselect instead of having it removed (via deselect()).
selected() {
- return Object.keys(this.indexes);
+ return Object.keys(this.indexes).filter(
+ ind => {return Boolean(this.indexes[ind])})
}
isEmpty(): boolean {
--- /dev/null
+import {Injectable} from '@angular/core';
+import {DatePipe, CurrencyPipe} from '@angular/common';
+import {EgIdlService, EgIdlObject} from '@eg/core/idl.service';
+import {EgOrgService} from '@eg/core/org.service';
+
+export interface EgFormatParams {
+ value: any;
+ idlClass?: string;
+ idlField?: string;
+ datatype?: string;
+ orgField?: string; // 'shortname' || 'name'
+}
+
+@Injectable()
+export class EgFormatService {
+
+ constructor(
+ private datePipe: DatePipe,
+ private currencyPipe: CurrencyPipe,
+ private idl: EgIdlService,
+ private org: EgOrgService
+ ) {}
+
+ /**
+ * Create a human-friendly display version of any field type.
+ */
+ transform(params: EgFormatParams): string {
+ let value = params.value;
+
+ if ( value === undefined
+ || value === null
+ || value === ''
+ || Number.isNaN(value)) return '';
+
+ let datatype = params.datatype;
+
+ if (!datatype) {
+ if (params.idlClass && params.idlField) {
+ datatype = this.idl.classes[params.idlClass]
+ .field_map[params.idlField].datatype;
+ } else {
+ // Assume it's a primitive value
+ return value + '';
+ }
+ }
+
+ switch(datatype) {
+
+ case 'org_unit':
+ let orgField = params.orgField || 'shortname';
+ let org = this.org.get(value);
+ return org ? org[orgField]() : '';
+
+ case 'timestamp':
+ let date = new Date(value);
+ // TODO: date format settings and options.
+ return this.datePipe.transform(date, 'yyyy-MM-dd');
+
+ case 'money':
+ return this.currencyPipe.transform(value);
+
+ default:
+ return value + '';
+ }
+ }
+}
+