Skip to content

Commit

Permalink
add fix for resource ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
thostetler committed Jul 20, 2023
1 parent 9e5e14f commit 305fc6a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 22 deletions.
65 changes: 51 additions & 14 deletions src/js/mixins/link_generator_mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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
Expand All @@ -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,
});
}
});
Expand All @@ -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(':');
Expand Down Expand Up @@ -558,7 +595,7 @@ define(['underscore', 'js/mixins/openurl_generator'], function(
dataProducts = _.sortBy(dataProducts, 'count').reverse();

return {
fullTextSources: fullTextSources,
fullTextSources: sortByDefaultOrdering(fullTextSources),
dataProducts: dataProducts,
};
};
Expand Down
28 changes: 20 additions & 8 deletions src/js/widgets/resources/redux/middleware/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand Down Expand Up @@ -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 });
}

Expand Down
6 changes: 6 additions & 0 deletions test/mocha/js/mixins/link_generator_mixin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand All @@ -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: [],
Expand All @@ -213,6 +215,7 @@ define(['underscore', 'js/mixins/link_generator_mixin'], function(
name: 'Publisher PDF',
type: 'PDF',
description: 'Publisher PDF',
rawType: 'PUB_PDF',
},
],
dataProducts: [],
Expand All @@ -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',
Expand All @@ -246,6 +250,7 @@ define(['underscore', 'js/mixins/link_generator_mixin'], function(
name: 'Publisher PDF',
type: 'PDF',
description: 'Publisher PDF',
rawType: 'PUB_PDF',
},
],
dataProducts: [],
Expand All @@ -268,6 +273,7 @@ define(['underscore', 'js/mixins/link_generator_mixin'], function(
shortName: 'BAR',
name: 'BAR',
type: 'HTML',
rawType: 'BAR',
},
],
dataProducts: [],
Expand Down

0 comments on commit 305fc6a

Please sign in to comment.