|
| 1 | +import {modalLink} from '../lis-modal-element'; |
| 2 | +import {LisGeneSearchResults} from '@legumeinfo/web-components'; |
| 3 | + |
| 4 | +/** The signature of a middleware function for the `LisGeneSearchElement` component. */ |
| 5 | +export type GeneSearchMiddleware = ( |
| 6 | + results: LisGeneSearchResults, |
| 7 | +) => LisGeneSearchResults; |
| 8 | + |
| 9 | +/** |
| 10 | + * Creates a middleware function that can be used with the `geneSearchFunctionFactory` function |
| 11 | + * to convert `identifiers` in `LisGeneSearchResults` into links that open a modal with the given |
| 12 | + * `modalId`. |
| 13 | + * @param {string} modalId - The HTML `id` of the target modal element. |
| 14 | + * @returns {GeneSearchMiddleware} The created middleware function. |
| 15 | + */ |
| 16 | +export function geneIdentifierModalLinkFactory( |
| 17 | + modalId: string, |
| 18 | +): GeneSearchMiddleware { |
| 19 | + return ({results: oldResults, ...pageInfo}) => { |
| 20 | + const results = oldResults.map(({identifier, ...geneInfo}) => { |
| 21 | + const data = {identifier, type: 'gene'}; |
| 22 | + return { |
| 23 | + ...geneInfo, |
| 24 | + identifier: modalLink(modalId, identifier, data), |
| 25 | + }; |
| 26 | + }); |
| 27 | + return {...pageInfo, results}; |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Creates a middleware function that can be used with the `geneSearchFunctionFactory` function |
| 33 | + * to convert `locations` in `LisGeneSearchResults` into links that open a modal with the given |
| 34 | + * `modalId`. |
| 35 | + * @param {string} modalId - The HTML `id` of the target modal element. |
| 36 | + * @returns {GeneSearchMiddleware} The created middleware function. |
| 37 | + */ |
| 38 | +export function locationModalLinkFactory( |
| 39 | + modalId: string, |
| 40 | +): GeneSearchMiddleware { |
| 41 | + return ({results: oldResults, ...pageInfo}) => { |
| 42 | + const results = oldResults.map(({locations: oldLocations, ...geneInfo}) => { |
| 43 | + const locations = oldLocations.map((location) => { |
| 44 | + // extract the data from the location string made by genesDataToSearchResults |
| 45 | + const re = /(?<identifier>.+):(?<start>\d+)-(?<end>\d+)/; |
| 46 | + const data = location.match(re)?.groups; |
| 47 | + // NOTE: block will not execute unless shim is out of sync with regexp |
| 48 | + if (data === undefined) { |
| 49 | + return location; |
| 50 | + } |
| 51 | + data.type = 'location'; |
| 52 | + return modalLink(modalId, location, data); |
| 53 | + }); |
| 54 | + return {...geneInfo, locations}; |
| 55 | + }); |
| 56 | + return {...pageInfo, results}; |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Creates a middleware function that can be used with the `geneSearchFunctionFactory` function |
| 62 | + * to convert `geneFamilyAssignments` in `LisGeneSearchResults` into links that open a modal with |
| 63 | + * the given `modalId`. |
| 64 | + * @param {string} modalId - The HTML `id` of the target modal element. |
| 65 | + * @returns {GeneSearchMiddleware} The created middleware function. |
| 66 | + */ |
| 67 | +export function geneFamilyAssignmentsModalLinkFactory( |
| 68 | + modalId: string, |
| 69 | +): GeneSearchMiddleware { |
| 70 | + return ({results: oldResults, ...pageInfo}) => { |
| 71 | + const results = oldResults.map( |
| 72 | + ({geneFamilyAssignments: oldGeneFamilyAssignments, ...geneInfo}) => { |
| 73 | + const geneFamilyAssignments = oldGeneFamilyAssignments.map( |
| 74 | + (identifier) => { |
| 75 | + const data = {identifier, type: 'geneFamily'}; |
| 76 | + return modalLink(modalId, identifier, data); |
| 77 | + }, |
| 78 | + ); |
| 79 | + return { |
| 80 | + ...geneInfo, |
| 81 | + geneFamilyAssignments, |
| 82 | + }; |
| 83 | + }, |
| 84 | + ); |
| 85 | + return {...pageInfo, results}; |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Creates a middleware function that can be used with the `geneSearchFunctionFactory` function |
| 91 | + * to convert `panGeneSets` in `LisGeneSearchResults` into links that open a modal with the given |
| 92 | + * `modalId`. |
| 93 | + * @param {string} modalId - The HTML `id` of the target modal element. |
| 94 | + * @returns {GeneSearchMiddleware} The created middleware function. |
| 95 | + */ |
| 96 | +export function panGeneSetsModalLinkFactory( |
| 97 | + modalId: string, |
| 98 | +): GeneSearchMiddleware { |
| 99 | + return ({results: oldResults, ...pageInfo}) => { |
| 100 | + const results = oldResults.map( |
| 101 | + ({panGeneSets: oldPanGeneSets, ...geneInfo}) => { |
| 102 | + const panGeneSets = oldPanGeneSets.map((identifier) => { |
| 103 | + const data = {identifier, type: 'panGeneSet'}; |
| 104 | + return modalLink(modalId, identifier, data); |
| 105 | + }); |
| 106 | + return { |
| 107 | + ...geneInfo, |
| 108 | + panGeneSets, |
| 109 | + }; |
| 110 | + }, |
| 111 | + ); |
| 112 | + return {...pageInfo, results}; |
| 113 | + }; |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Creates all middleware functions that can be used with the `geneSearchFunctionFactory` function |
| 118 | + * to add modal links to `LisGeneSearchResults`. |
| 119 | + * @param {string} modalId - The HTML `id` of the target modal element. |
| 120 | + * @returns {GeneSearchMiddleware[]} The created middleware functions. |
| 121 | + */ |
| 122 | +export function allModalLinksFactory(modalId: string): GeneSearchMiddleware[] { |
| 123 | + return [ |
| 124 | + geneIdentifierModalLinkFactory(modalId), |
| 125 | + locationModalLinkFactory(modalId), |
| 126 | + geneFamilyAssignmentsModalLinkFactory(modalId), |
| 127 | + panGeneSetsModalLinkFactory(modalId), |
| 128 | + ]; |
| 129 | +} |
| 130 | + |
| 131 | +/** The geneSearch portion of `LisGraphqlWebComponents.middleware`. */ |
| 132 | +export const geneSearchMiddleware = { |
| 133 | + geneIdentifierModalLinkFactory, |
| 134 | + geneFamilyAssignmentsModalLinkFactory, |
| 135 | + locationModalLinkFactory, |
| 136 | + panGeneSetsModalLinkFactory, |
| 137 | + allModalLinksFactory, |
| 138 | +}; |
0 commit comments