diff --git a/src/js/mixins/link_generator_mixin.js b/src/js/mixins/link_generator_mixin.js index 862e5bdc2..fb2247104 100644 --- a/src/js/mixins/link_generator_mixin.js +++ b/src/js/mixins/link_generator_mixin.js @@ -5,14 +5,53 @@ define(['underscore', 'js/mixins/openurl_generator'], function( const GATEWAY_BASE_URL = '/link_gateway/'; const DEFAULT_ORDERING = [ - 'ADS PDF', - 'ADS Scanned Article', - 'My Institution', - 'Publisher Article', - 'Publisher PDF', - 'arXiv PDF', + 'ADS_PDF', + 'ADS_SCAN', + 'INSTITUTION', + 'PUB_HTML', + 'PUB_PDF', + 'EPRINT_PDF', + 'EPRINT_HTML', + 'AUTHOR_PDF', + 'AUTHOR_HTML', ]; + const sortByDefaultOrdering = (sources) => { + // initially sort the whole list by the DEFAULT_ORDERING array + const sortedSources = sources.sort((a, b) => { + const aIndex = DEFAULT_ORDERING.indexOf(a.rawType); + const bIndex = DEFAULT_ORDERING.indexOf(b.rawType); + + // If both elements are in the DEFAULT_ORDERING array, sort based on their indices + if (aIndex !== -1 && bIndex !== -1) { + return aIndex - bIndex; + } + + // If only 'a' is in DEFAULT_ORDERING, move it before 'b' + if (aIndex !== -1) { + return -1; + } + + // If only 'b' is in DEFAULT_ORDERING, move it before 'a' + if (bIndex !== -1) { + return 1; + } + + // If both elements are not in DEFAULT_ORDERING, maintain their relative order + return 0; + }); + + // then make sure that sources in DEFAULT_ORDERING are pushed to the top + return [ + ...sortedSources.filter((source) => + DEFAULT_ORDERING.includes(source.rawType) + ), + ...sortedSources.filter( + (source) => !DEFAULT_ORDERING.includes(source.rawType) + ), + ]; + }; + /** * @typedef {Object} LinkType * @property {string} name - Full name @@ -452,7 +491,7 @@ define(['underscore', 'js/mixins/openurl_generator'], function( */ const _processLinkData = function(data) { const createGatewayUrl = this._createGatewayUrl; - let fullTextSources = []; + const fullTextSources = []; let dataProducts = []; let countOpenUrls = 0; const property = data.property; @@ -477,6 +516,7 @@ define(['underscore', 'js/mixins/openurl_generator'], function( shortName: 'My Institution', name: 'My Institution', description: 'Find Article At My Institution', + rawType: 'INSTITUTION', }); countOpenUrls += 1; } @@ -489,6 +529,7 @@ define(['underscore', 'js/mixins/openurl_generator'], function( name: (linkInfo && linkInfo.name) || el, type: (linkInfo && linkInfo.type) || 'HTML', description: linkInfo && linkInfo.description, + rawType: el, }); // if entry cannot be split, then it will not be open access @@ -500,6 +541,7 @@ define(['underscore', 'js/mixins/openurl_generator'], function( name: (linkInfo && linkInfo.name) || el, type: (linkInfo && linkInfo.type) || 'HTML', description: linkInfo && linkInfo.description, + rawType: el, }); } }); @@ -520,17 +562,12 @@ define(['underscore', 'js/mixins/openurl_generator'], function( name: (info && info.name) || link.type, type: (info && info.type) || 'HTML', description: info && info.description, + rawType: 'EPRINT_PDF', }); } }); } - // reorder the full text sources based on our default ordering - fullTextSources = _.sortBy(fullTextSources, function(source) { - const rank = DEFAULT_ORDERING.indexOf(source.name); - return rank > -1 ? rank : 9999; - }); - // check the data property _.forEach(data.data, function(product) { const parts = product.split(':'); @@ -558,7 +595,7 @@ define(['underscore', 'js/mixins/openurl_generator'], function( dataProducts = _.sortBy(dataProducts, 'count').reverse(); return { - fullTextSources: fullTextSources, + fullTextSources: sortByDefaultOrdering(fullTextSources), dataProducts: dataProducts, }; }; diff --git a/src/js/widgets/resources/redux/middleware/api.js b/src/js/widgets/resources/redux/middleware/api.js index e3285a9f8..7d64d9b9e 100644 --- a/src/js/widgets/resources/redux/middleware/api.js +++ b/src/js/widgets/resources/redux/middleware/api.js @@ -82,15 +82,27 @@ define(['underscore', 'es6!../modules/api', 'es6!../modules/ui'], function( /** * Sorts a set of sources by type and then groups them by name * @param {Array} sources sources to reformat - * @param {object} object keyed by the source shortnames + * @returns {object} object keyed by the source shortnames */ - const reformatSources = (sources) => { - const typeOrder = ['PDF', 'HTML', 'SCAN']; + const groupSources = (sources) => { + const groups = {}; - return _(sources) - .sortBy((s) => typeOrder.indexOf(s.type)) - .groupBy('shortName') - .value(); + if (sources.length === 0) { + return groups; + } + + // group the sources by name, maintaining the incoming order + sources.forEach((source) => { + // if the source is not in the groups object, add it + if (!groups[source.shortName]) { + groups[source.shortName] = []; + } + + // add the source to the groups object + groups[source.shortName].push(source); + }); + + return groups; }; /** @@ -123,7 +135,7 @@ define(['underscore', 'es6!../modules/api', 'es6!../modules/ui'], function( } if (data.fullTextSources.length > 0) { - const fullTextSources = reformatSources(data.fullTextSources); + const fullTextSources = groupSources(data.fullTextSources); dispatch({ type: SET_FULL_TEXT_SOURCES, result: fullTextSources }); } diff --git a/test/mocha/js/mixins/link_generator_mixin.spec.js b/test/mocha/js/mixins/link_generator_mixin.spec.js index a67780978..ec5deaded 100644 --- a/test/mocha/js/mixins/link_generator_mixin.spec.js +++ b/test/mocha/js/mixins/link_generator_mixin.spec.js @@ -169,6 +169,7 @@ define(['underscore', 'js/mixins/link_generator_mixin'], function( name: 'Publisher Article', type: 'HTML', description: 'Electronic on-line publisher article (HTML)', + rawType: 'PUB_HTML', }, ], dataProducts: [], @@ -191,6 +192,7 @@ define(['underscore', 'js/mixins/link_generator_mixin'], function( name: 'Publisher Article', type: 'HTML', description: 'Electronic on-line publisher article (HTML)', + rawType: 'PUB_HTML', }, ], dataProducts: [], @@ -213,6 +215,7 @@ define(['underscore', 'js/mixins/link_generator_mixin'], function( name: 'Publisher PDF', type: 'PDF', description: 'Publisher PDF', + rawType: 'PUB_PDF', }, ], dataProducts: [], @@ -238,6 +241,7 @@ define(['underscore', 'js/mixins/link_generator_mixin'], function( shortName: 'My Institution', name: 'My Institution', description: 'Find Article At My Institution', + rawType: 'INSTITUTION', }, { url: '/link_gateway/foo/PUB_PDF', @@ -246,6 +250,7 @@ define(['underscore', 'js/mixins/link_generator_mixin'], function( name: 'Publisher PDF', type: 'PDF', description: 'Publisher PDF', + rawType: 'PUB_PDF', }, ], dataProducts: [], @@ -268,6 +273,7 @@ define(['underscore', 'js/mixins/link_generator_mixin'], function( shortName: 'BAR', name: 'BAR', type: 'HTML', + rawType: 'BAR', }, ], dataProducts: [],