-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef9f8ef
commit 7907338
Showing
8 changed files
with
155 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { ISearchRequest } from "@esri/arcgis-rest-items"; | ||
|
||
/* | ||
* This util function take in a JSON object response from /sharing/rest/search | ||
* and return JSONAPI-format page parameters | ||
* | ||
* example AGO API response | ||
* { | ||
* nextStart: 31, | ||
* num: 10, | ||
* query: '(group:aa9b3013b77141cca147ac540139b353 OR group:3b8014ff38a9422ea99303979fa4d539)', | ||
* results: [ array of items ], | ||
* start: 21, | ||
* total: 147 | ||
* } | ||
* | ||
* should return => | ||
* { | ||
* number: 3, | ||
* size: 10 | ||
* } | ||
*/ | ||
|
||
export function convertAgoPages( | ||
response: any | ||
): { number: number; size: number } { | ||
return { | ||
number: Math.floor(response.start / response.num) + 1, | ||
size: response.num | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { ISearchResult } from "@esri/arcgis-rest-items"; | ||
import { ISearchParams } from "../common/params"; | ||
import { formatItem } from "./format-item"; | ||
import { convertAgoPages } from "./ago-response-to-jsonapi-page"; | ||
|
||
// This function is responsible for formatting results from the AGO API into the JSONAPI collection format | ||
export function formatItemCollection( | ||
searchResults: ISearchResult, | ||
facets: any = {}, | ||
params: ISearchParams | ||
) { | ||
// console.log('In hub.js => formatItemCollection'); | ||
const queryParams = queryParameters(searchResults, params); | ||
return { | ||
data: searchResults.results.map(result => { | ||
return formatItem(result, queryParams.q); | ||
}), | ||
meta: { | ||
query: searchResults.query, | ||
queryParameters: queryParams, | ||
stats: { | ||
aggs: facets, | ||
count: searchResults.results.length, | ||
totalCount: searchResults.total | ||
} | ||
} | ||
}; | ||
} | ||
|
||
function queryParameters(searchResults: ISearchResult, params: ISearchParams) { | ||
const queryParams: any = { | ||
page: convertAgoPages(searchResults) | ||
}; | ||
return Object.assign({}, params, queryParams); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// helper functions to make items look more like datasets | ||
// | ||
// item queries will look like this -> | ||
// { | ||
// data: [{ | ||
// id: item-id, | ||
// type: 'item', | ||
// attributes: { | ||
// // will have attribute 'parity' with datasets. Many of these values will be null. | ||
// }, | ||
// meta: { highlights: {} } | ||
// links: { | ||
// // computed links for the item result. These can be used throughout the app when linking to | ||
// // AGO, APIs, etc | ||
// } | ||
// }], | ||
// meta: { | ||
// aggs: { | ||
// // built by another query against AGO API and then formatted in the same format as the V3 API | ||
// }, | ||
// stats: { | ||
// count:, | ||
// totalCount:, | ||
// } | ||
// } | ||
// } | ||
import { hubTypeLookup } from "../common/hub-type-map"; | ||
import { calcHighlights } from "../common/highlights"; | ||
import { IItem } from "@esri/arcgis-rest-common-types"; | ||
|
||
export function formatItem(item: IItem, query: string) { | ||
const formattedItem: any = { | ||
id: item.id, | ||
type: "item", | ||
attributes: formatItemAttributes(item) | ||
}; | ||
if (query) { | ||
if (!item.meta) { | ||
// create highlights since AGO deos not return them | ||
formattedItem.meta = {}; | ||
formattedItem.meta.highlights = highlights(item, query); | ||
} | ||
} | ||
return formattedItem; | ||
} | ||
|
||
function formatItemAttributes(item: IItem) { | ||
const hubTypes = hubTypeLookup(item.type); | ||
return Object.assign({}, item, { | ||
/* computed or null attributes so items & datasets look the same */ | ||
name: item.title, | ||
searchDescription: item.description, | ||
hubType: hubTypes[0] || "Other" | ||
}); | ||
} | ||
|
||
function highlights(item: IItem, query: string) { | ||
// calculate highlights based on AGO restricted item, hence use description field but return as `searchDescription` | ||
// because the search-result/component expects searchDescription | ||
return { | ||
name: calcHighlights(item.title, query, "name"), | ||
searchDescription: calcHighlights(item.description, query, "description") | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
* Apache-2.0 */ | ||
|
||
export * from "./search"; | ||
export * from "./format-item-collection"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export function calcHighlights(input: string, query: string, property: string) { | ||
// 1. identify all the matches case insensitively | ||
// 2. Replace the original match(es) with mark tags | ||
// We want to match case insensitively but highlight case sensitively the original term | ||
// E.g. input string: `Capital bike share... blah blah capital.... CAPITAL` | ||
// We would like to highlight: `Capital`, `capital`, `CAPITAL` | ||
if (!input) return undefined; | ||
const matches = input.match(new RegExp(query, "ig")); // search globally and case insensitively | ||
if (!matches) return undefined; | ||
return matches.reduce((highlights, match) => { | ||
// match is what appears as is in the input string | ||
const replacement = `<mark class="hub-search-highlight ${property}-highlight">${match}</mark>`; | ||
// replace the case sensitive match with mark tags | ||
return highlights.replace(new RegExp(match, "g"), replacement); | ||
}, input); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
export { search as ago } from "./ago"; | ||
export { | ||
search as agoSearch, | ||
formatItemCollection as agoFormatItemCollection | ||
} from "./ago"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters