Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated support for sorting of drop downs #265

Merged
merged 5 commits into from
Feb 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/dialog-user/components/dialog-user/dialogField.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ describe('Dialog field test', () => {

describe('#convertValuesToArray', () => {
it('converts a string of default values to an array', () => {
dialogCtrl.dialogField.default_value = '["one", "two"]'
dialogCtrl.dialogField.default_value = '["one", "two"]';
dialogCtrl.convertValuesToArray();
expect(dialogCtrl.dialogField.default_value).toEqual(['one', 'two'])
expect(dialogCtrl.dialogField.default_value).toEqual(['one', 'two']);
});
});
});
Expand Down
15 changes: 14 additions & 1 deletion src/dialog-user/services/dialogData.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('DialogDataService test', () => {
[5, 'Test2'],
[2, 'Test5']
],
options: { sort_by: 'value', sort_order: 'descending' }
options: { sort_by: 'value', sort_order: 'descending', data_type: 'integer' }
};
const testSorted = dialogData.updateFieldSortOrder(testDropDown);
const expectedResult = [[5, 'Test2'], [2, 'Test5'], [0, 'Test']];
Expand All @@ -144,4 +144,17 @@ describe('DialogDataService test', () => {
const expectedSortedResult = [[5, 'C'], [0, 'B'], [2, 'A']];
expect(testSortedDescription).toEqual(expectedSortedResult);
});
it('should allow a numeric Description field to be sorted in a dropdown', () => {
const testDropDownDescription = {
values: [
['zero', '0'],
['five', '5'],
['two', '2']
],
options: { sort_by: 'description', sort_order: 'descending' }
};
const testSortedDescription = dialogData.updateFieldSortOrder(testDropDownDescription);
const expectedSortedResult = [['five', '5'], ['two', '2'], ['zero', '0']];
expect(testSortedDescription).toEqual(expectedSortedResult);
});
});
29 changes: 22 additions & 7 deletions src/dialog-user/services/dialogData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ export default class DialogDataService {
if (option[0] === String(field.default_value)) {
field.selected = option;
}
if (field.data_type === 'integer') {
dropDownValues.push([parseInt(option[0], 10), option[1]]);
} else {
dropDownValues.push(option);
}
const value = (field.data_type === 'integer' ? parseInt(option[0], 10) : option[0]);
const description = (isNaN(option[1]) ? option[1] : parseInt(option[1], 10));
dropDownValues.push([value, description]);
}
field.values = dropDownValues;
field.values = this.updateFieldSortOrder(field);
Expand All @@ -48,8 +46,25 @@ export default class DialogDataService {
*
**/
private updateFieldSortOrder(data) {
let values = _.sortBy(data.values, data.options.sort_by === 'value' ? 0 : 1);
return data.options.sort_order === 'ascending' ? values : values.reverse();
const SORT_DESCRIPTION = 1;
const SORT_VALUE = 0;
const FIRST_OPTION = 0;
const VALUE = 0;
const sortBy = (data.options.sort_by === 'value' ? SORT_VALUE : SORT_DESCRIPTION);
let tempValues = [...data.values];
let defaultDropdownField = [];
// The following if deals with a empty default option if it exists
if (data.data_type === 'integer' && _.isNaN(tempValues[FIRST_OPTION][VALUE]) ||
_.isNull(tempValues[FIRST_OPTION][VALUE])) {
defaultDropdownField = tempValues.shift();
}
let values = _.sortBy(tempValues, sortBy);
const sortedValues = data.options.sort_order === 'ascending' ? values : values.reverse();
if (defaultDropdownField.length) {
sortedValues.unshift(defaultDropdownField);
}

return sortedValues;
}

/**
Expand Down