Adding relations links to table
Change-Id: I5dd2d206a6da7fec163342f54f1143a7783f7758
diff --git a/src/app/core/services/helpers/config.helpers.ts b/src/app/core/services/helpers/config.helpers.ts
index 1df2662..b2d966a 100644
--- a/src/app/core/services/helpers/config.helpers.ts
+++ b/src/app/core/services/helpers/config.helpers.ts
@@ -4,11 +4,17 @@
import {IModeldef} from '../../../datasources/rest/modeldefs.rest';
import {IXosFormConfig, IXosFormInput} from '../../form/form';
import {IXosAuthService} from '../../../datasources/rest/auth.rest';
+import {IModelStoreService} from '../../../datasources/stores/model.store';
export interface IXosModelDefsField {
name: string;
type: string;
validators?: any;
+ hint?: string;
+ relation?: {
+ model: string;
+ type: string;
+ };
}
export interface IXosConfigHelpersService {
@@ -20,10 +26,10 @@
pluralize(string: string, quantity?: number, count?: boolean): string;
toLabel(string: string, pluralize?: boolean): string;
toLabels(string: string[], pluralize?: boolean): string[];
- urlFromCoreModel(name: string): string;
}
export class ConfigHelpers {
+ static $inject = ['toastr', 'AuthService', 'ModelStore'];
excluded_fields = [
'created',
@@ -46,7 +52,8 @@
constructor(
private toastr: ng.toastr.IToastrService,
- private AuthService: IXosAuthService
+ private AuthService: IXosAuthService,
+ private ModelStore: IModelStoreService
) {
pluralize.addIrregularRule('xos', 'xosses');
pluralize.addPluralRule(/slice$/i, 'slices');
@@ -125,6 +132,17 @@
col.link = item => `#/core${baseUrl.replace(':id?', item.id)}`;
}
+ // if the field identify a relation, create a link
+ if (f.relation && f.relation.type === 'many_to_one') {
+ // TODO read the related model name and replace the value, use the xosTable format method?
+ col.type = 'custom';
+ col.formatter = item => {
+ this.populateRelated(item, item[f.name], f);
+ return item[f.name];
+ };
+ col.link = item => `#${this.urlFromCoreModel(f.relation.model)}/${item[f.name]}`;
+ }
+
if (f.name === 'backend_status') {
col.type = 'icon';
col.formatter = (item) => {
@@ -214,4 +232,20 @@
private capitalizeFirst(string: string): string {
return string.slice(0, 1).toUpperCase() + string.slice(1);
}
+
+ private populateRelated(item: any, fk: string, field: IXosModelDefsField): any {
+ // if the relation is not defined return
+ if (!fk || angular.isUndefined(fk) || fk === null) {
+ return;
+ }
+ this.ModelStore.query(field.relation.model)
+ .subscribe(res => {
+ if (angular.isDefined(res) && angular.isDefined(fk)) {
+ let ri = _.find(res, {id: fk});
+ if (angular.isDefined(ri)) {
+ item[`${field.name}-formatted`] = angular.isDefined(ri.name) ? ri.name : ri.humanReadableName;
+ }
+ }
+ });
+ }
}