const pkeyField = this.classes[idlClass].pkey || 'id';
return obj1[pkeyField]() === obj2[pkeyField]();
}
+
+ // Sort an array of fields from the IDL (like you might get from calling
+ // this.idlClasses[classname][fields])
+
+ sortIdlFields(fields: any[], desiredOrder: string[]): any[] {
+ let newList = [];
+
+ desiredOrder.forEach(name => {
+ const match = fields.filter(field => field.name === name)[0];
+ if (match) { newList.push(match); }
+ });
+
+ // Sort remaining fields by label
+ const remainder = fields.filter(f => !desiredOrder.includes(f.name));
+ remainder.sort((a, b) => {
+ if (a.label && b.label) {
+ return (a.label < b.label) ? -1 : 1;
+ } else if (a.label) {
+ return -1;
+ } else if (b.label) {
+ return 1;
+ }
+
+ // If no order specified and no labels to sort by,
+ // default to sorting by field name
+ return (a.label < b.label) ? -1 : 1;
+ });
+ newList = newList.concat(remainder);
+ return newList;
+ }
}
expect(service.pkeyMatches(org, user)).toBe(false);
});
+ it('should sort an array of IDL fields according to an array of field names', () => {
+ const fieldNames = ['name', 'owner', 'active', 'id'];
+ const idlFields = [
+ {'name': 'id', 'label': 'Object ID', 'dataType': 'id'},
+ {'name': 'name', 'label': 'The name of this object', 'datatype': 'text'},
+ {'name': 'active', 'datatype': 'bool'},
+ {'name': 'owner', 'type': 'link', 'key': 'id', 'class': 'aou', 'reltype': 'has_a', 'datatype': 'org_unit'}
+ ];
+ const expectedOrder = [
+ {'name': 'name', 'label': 'The name of this object', 'datatype': 'text'},
+ {'name': 'owner', 'type': 'link', 'key': 'id', 'class': 'aou', 'reltype': 'has_a', 'datatype': 'org_unit'},
+ {'name': 'active', 'datatype': 'bool'},
+ {'name': 'id', 'label': 'Object ID', 'dataType': 'id'},
+ ];
+ expect(service.sortIdlFields(idlFields, fieldNames)).toEqual(expectedOrder);
+ });
+
+ it('should sort IDL fields by label when it runs out of specified field names', () => {
+ const fieldNames = ['owner'];
+ const idlFields = [
+ {'name': 'id', 'label': 'Object ID', 'dataType': 'id'},
+ {'name': 'name', 'label': 'The name of this object', 'datatype': 'text'},
+ {'name': 'owner', 'type': 'link', 'key': 'id', 'class': 'aou', 'reltype': 'has_a', 'datatype': 'org_unit'}
+ ];
+ const expectedOrder = [
+ {'name': 'owner', 'type': 'link', 'key': 'id', 'class': 'aou', 'reltype': 'has_a', 'datatype': 'org_unit'},
+ {'name': 'id', 'label': 'Object ID', 'dataType': 'id'},
+ {'name': 'name', 'label': 'The name of this object', 'datatype': 'text'},
+ ];
+ expect(service.sortIdlFields(idlFields, fieldNames)).toEqual(expectedOrder);
+ });
+
+ it('should sort IDL fields by name when it runs out of other ways to sort', () => {
+ const fieldNames = ['owner'];
+ const idlFields = [
+ {'name': 'id', 'dataType': 'id'},
+ {'name': 'name', 'label': 'The name of this object', 'datatype': 'text'},
+ {'name': 'active', 'datatype': 'bool'},
+ {'name': 'owner', 'type': 'link', 'key': 'id', 'class': 'aou', 'reltype': 'has_a', 'datatype': 'org_unit'}
+ ];
+ const expectedOrder = [
+ {'name': 'owner', 'type': 'link', 'key': 'id', 'class': 'aou', 'reltype': 'has_a', 'datatype': 'org_unit'},
+ {'name': 'name', 'label': 'The name of this object', 'datatype': 'text'},
+ {'name': 'active', 'datatype': 'bool'},
+ {'name': 'id', 'dataType': 'id'},
+ ];
+ expect(service.sortIdlFields(idlFields, fieldNames)).toEqual(expectedOrder);
+ });
});
fields.map(field => this.constructOneField(field))
).then(() => {
-
- if (!this.fieldOrder) {
- this.fields = fields.sort((a, b) => a.label < b.label ? -1 : 1);
- return;
- }
-
- let newList = [];
- const ordered = this.fieldOrder.split(/,/);
-
- ordered.forEach(name => {
- const f1 = fields.filter(f2 => f2.name === name)[0];
- if (f1) { newList.push(f1); }
- });
-
- // Sort remaining fields by label
- const remainder = fields.filter(f => !ordered.includes(f.name));
- remainder.sort((a, b) => a.label < b.label ? -1 : 1);
- newList = newList.concat(remainder);
-
- this.fields = newList;
+ const order = this.fieldOrder ? this.fieldOrder.split(/,/) : [];
+ this.fields = this.idl.sortIdlFields(fields, order);
});
}
disableSelect: boolean;
dataSource: GridDataSource;
columnSet: GridColumnSet;
+ autoGeneratedColumnOrder: string;
rowSelector: GridRowSelector;
toolbarButtons: GridToolbarButton[];
toolbarCheckboxes: GridToolbarCheckbox[];
if (!this.columnSet.idlClass) { return; }
const pkeyField = this.idl.classes[this.columnSet.idlClass].pkey;
+ const specifiedColumnOrder = this.autoGeneratedColumnOrder ?
+ this.autoGeneratedColumnOrder.split(/,/) : [];
// generate columns for all non-virtual fields on the IDL class
- this.idl.classes[this.columnSet.idlClass].fields
- .filter(field => !field.virtual)
- .forEach(field => {
+ const fields = this.idl.classes[this.columnSet.idlClass].fields
+ .filter(field => !field.virtual);
+
+ const sortedFields = this.autoGeneratedColumnOrder ?
+ this.idl.sortIdlFields(fields, this.autoGeneratedColumnOrder.split(/,/)) :
+ fields;
+
+ sortedFields.forEach(field => {
if (!this.ignoredFields.filter(ignored => ignored === field.name).length) {
const col = new GridColumn();
col.name = field.name;