From b5166088a3cca603e0a130f8cee810c9ed008e01 Mon Sep 17 00:00:00 2001 From: "Emanuel F. Oliveira" Date: Wed, 18 Jan 2017 17:11:00 -0200 Subject: [PATCH] Extending capabilities of "findBy" method from TypeSafeCollection to search an ordered dataset as needed by hanging-protocols package. --- .../ruleEntryDialog/ruleEntryDialog.js | 16 ++----- .../client/lib/classes/TypeSafeCollection.js | 46 ++++++++++++++----- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/Packages/ohif-hanging-protocols/client/components/ruleEntryDialog/ruleEntryDialog.js b/Packages/ohif-hanging-protocols/client/components/ruleEntryDialog/ruleEntryDialog.js index 4dabc5e87..49f2b334e 100644 --- a/Packages/ohif-hanging-protocols/client/components/ruleEntryDialog/ruleEntryDialog.js +++ b/Packages/ohif-hanging-protocols/client/components/ruleEntryDialog/ruleEntryDialog.js @@ -136,20 +136,12 @@ function getActiveViewportImageId() { function getAbstractPriorValue(imageId) { // @TypeSafeStudies - // @TODO: Implement MIN and MAX methods in TypeSafeCollection - // const currentStudy = ViewerStudies.findOne({}, { - // sort: { - // studyDate: -1 - // }, - // limit: 1 - // }); - const allStudies = OHIF.viewer.Studies.all({ + // Retrieves the first study of the collection using the given sort order. + // Since we're only interrested in the first record, "null" will be used + // as search criteria (thus no actual search will be made). + const currentStudy = OHIF.viewer.Studies.findBy(null, { sort: [ ['studyDate', 'desc'] ] }); - if (allStudies.length < 1) { - return; - } - const currentStudy = allStudies[0]; const priorStudy = cornerstoneTools.metaData.get('study', imageId); if (!priorStudy) { diff --git a/Packages/ohif-viewerbase/client/lib/classes/TypeSafeCollection.js b/Packages/ohif-viewerbase/client/lib/classes/TypeSafeCollection.js index 637ad8df8..2c5b5d202 100644 --- a/Packages/ohif-viewerbase/client/lib/classes/TypeSafeCollection.js +++ b/Packages/ohif-viewerbase/client/lib/classes/TypeSafeCollection.js @@ -163,16 +163,32 @@ export class TypeSafeCollection { /** * Find the first element that strictly matches the specified property map. - * Attention!!! The reactive source will not be notified if no valid property map is supplied... * @param {Object} propertyMap A property map that will be macthed against all collection elements. + * @param {Object} options A set of options. Currently only "options.sort" option is supported. + * @param {Object.SortingSpecifier} options.sort An optional sorting specifier. If a sorting specifier is supplied + * but is not valid, an exception will be thrown. * @returns {Any} The matched element or undefined if not match was found. */ - findBy(propertyMap) { + findBy(propertyMap, options) { let found; - if (_isObject(propertyMap)) { + if (_isObject(options)) { + // if the "options" argument is provided and is a valid object, + // it must be applied to the dataset before search... + const all = this.all(options); + if (all.length > 0) { + if (_isObject(propertyMap)) { + found = all.find(item => _compareToPropertyMapStrict(propertyMap, item)); + } else { + found = all[0]; // simply extract the first element... + } + } + } else if (_isObject(propertyMap)) { found = this._elements().find(item => _compareToPropertyMapStrict(propertyMap, item.payload)); + if (found) { + found = found.payload; + } } - return found && found.payload; + return found; } /** @@ -201,7 +217,7 @@ export class TypeSafeCollection { * @param {Object} propertyMap A property map that will be macthed against all collection elements. * @param {Object} options A set of options. Currently only "options.sort" option is supported. * @param {Object.SortingSpecifier} options.sort An optional sorting specifier. If a sorting specifier is supplied - * but it's not valid, an exception will be thrown. + * but is not valid, an exception will be thrown. * @returns {Array} An array with all elements that match the given criteria and sorted in the specified sorting order. */ findAllBy(propertyMap, options) { @@ -241,7 +257,7 @@ export class TypeSafeCollection { * Returns a list with all elements of the collection optionally sorted by a sorting specifier criteria. * @param {Object} options A set of options. Currently only "options.sort" option is supported. * @param {Object.SortingSpecifier} options.sort An optional sorting specifier. If a sorting specifier is supplied - * but it's not valid, an exception will be thrown. + * but is not valid, an exception will be thrown. * @returns {Array} An array with all elements stored in the collection. */ all(options) { @@ -284,6 +300,11 @@ function _isFunction(subject) { return typeof subject === 'function'; } +/** + * Shortcut for Object's prototype "hasOwnProperty" method. + */ +const _hasOwnProperty = Object.prototype.hasOwnProperty; + /** * Retrieve an object's property value by name. Composite property names (e.g., 'address.country.name') are accepted. * @param {Object} targetObject The object we want read the property from... @@ -319,12 +340,13 @@ function _compareToPropertyMapStrict(propertyMap, targetObject) { let result = false; // "for in" loops do not thown exceptions for invalid data types... for (let propertyName in propertyMap) { - // Attention!!! The 'hasOwnProperty' test has been intentionally skipped... - if (propertyMap[propertyName] !== _getPropertyValue(targetObject, propertyName)) { - result = false; - break; - } else if (result !== true) { - result = true; + if (_hasOwnProperty.call(propertyMap, propertyName)) { + if (propertyMap[propertyName] !== _getPropertyValue(targetObject, propertyName)) { + result = false; + break; + } else if (result !== true) { + result = true; + } } } return result;