ohif-viewer/Packages/viewerbase/lib/helpers/sorting.js
Evren Ozkan af639b4de2 Allow to sort thumbnails by AcquisitionDateTime or any other stack property (sorted by InstanceNumber ascending by default)
- Retrieve AcquisitionDateTime via DICOMWeb (WIP for DIMSE)
- Add a global template helper to sort arrays by an array element property or deep array element propery, and sort type
2016-10-24 09:47:12 -04:00

59 lines
1.6 KiB
JavaScript

import { _ } from 'meteor/underscore';
import { Template } from 'meteor/templating';
/**
* Global Blaze UI helper to sort array elements
* by an array element's property (property) or deep object property (property.childProperty)
* Sorts ascending as default
*/
Template.registerHelper('sort', (array, sortBy, sortType) => {
if (!sortBy) {
return array;
}
console.log(array);
// To keep the order for the same values of the field which is used to sort:
// 1. Group the array by the field
// 2. Sort the grouped array
// 3. Ungroup the sorted array
const groupedArray = _.groupBy(array, (element) => {
if (sortBy) {
var groupingElement = getKeyValue(element, sortBy);
if (groupingElement) {
return groupingElement;
}
}
return element;
});
const sortedArray = _.sortBy(groupedArray, (element) => {
if (sortBy) {
var sortingElement = getKeyValue(element[0], sortBy);
if (sortingElement) {
return sortingElement;
}
}
return element;
});
if (sortType === 'desc') {
return _.flatten(sortedArray.reverse(), true);
}
return _.flatten(sortedArray, true);
});
function getKeyValue(object, keyPath) {
keyPath = keyPath.split('.');
for (var i = 0; i < keyPath.length; i++) {
if (object && _.has(object, keyPath[i])) {
object = object[keyPath[i]];
}
else {
return undefined;
}
}
return object;
}