UNPKG

labo-components

Version:
122 lines (110 loc) 5.09 kB
import IDUtil from '../util/IDUtil'; import SearchResults from '../model/SearchResults'; import LocalStorageHandler from '../util/LocalStorageHandler'; import FlexRouter from '../util/FlexRouter'; export default class SearchAPI { //TODO combined search instead of a single query to multiple indices for each layer //TODO field specific search in search term syntax //TODO query should become a list of queries for each selected index //TODO turn the returned result into a proper SearchResult object static search = (query, collectionConfig, callback, storeAfterExecution = true) => { if(query.offset + query.size <= 10000) { let url = _config.SEARCH_API_BASE + '/layered_search/' + query.collectionId; const xhr = new XMLHttpRequest(); const searchId = query.searchId || IDUtil.guid(); xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE) { if(xhr.status === 200) { const data = JSON.parse(xhr.responseText); if(data['error']){ callback({searchId : searchId, error : data['error'], query : query}); } else{ if(data && data.params) { const searchResults = SearchResults.construct(searchId, false, data, collectionConfig) if(storeAfterExecution) { LocalStorageHandler.storeJSONInLocalStorage( 'stored-search-results', searchResults.toLocalStorageObject() ); FlexRouter.setBrowserHistory({queryId : 'cache'}, 'single-search-history'); } callback(searchResults); } else { callback({searchId : searchId, error : 'Server returned no results', query : query}); } } } else { callback({searchId : searchId, error : 'Server returned an error response', query : query}); } } } xhr.open("POST", url); xhr.timeout = 50000; xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xhr.send(JSON.stringify(query)); } else { console.debug('Currently the search engine cannot look beyond this point, please narrow your search terms'); callback(SearchResults.construct(null, true, {params : query}, collectionConfig)) } }; //transforms regular object into key/value pairs required for the grlc API (e.g {"key": "gtaa", "value": gtaaId}) static __toGRLCQueryParams = params => { if(!params) return null; return Object.keys(params).map(key => { return {"key": key, "value": params[key]} }); }; //at this point there is no way of telling what kind of query is returned, so only an agnostic format function //can be provided (meaning anything related to entity types should be done afterwards). static grlc = (endpoint, queryName, queryParams, formatFunc, callback) => { const url = _config.SEARCH_API_BASE + '/grlc/' + queryName; const query = { "endpoint": endpoint, "parameters": SearchAPI.__toGRLCQueryParams(queryParams) } const xhr = new XMLHttpRequest(); xhr.onreadystatechange = () => { if (xhr.readyState === XMLHttpRequest.DONE) { if(xhr.status === 200) { const data = JSON.parse(xhr.responseText); const formattedData = formatFunc(data, queryParams); callback(formattedData); } else { callback({error : 'Server returned an error response: ' + xhr.status}); } } }; xhr.open("POST", url); xhr.timeout = 50000; xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xhr.send(JSON.stringify(query)); }; //autocomplete api for Media Suite indices. For external autocomplete (e.g. Unesco) //see ExternalAPI static autocomplete = (vocabularyId, term, method, fields, lang, callback) => { const url = _config.SEARCH_API_BASE + '/autocomplete/' + vocabularyId; const params = { "term": term, "method": method, "fields": fields, "lang": lang } const xhr = new XMLHttpRequest(); xhr.onreadystatechange = () => { if (xhr.readyState === XMLHttpRequest.DONE) { if(xhr.status === 200) { const data = JSON.parse(xhr.responseText); callback(data); } else { callback({error : 'Server returned an error response: ' + xhr.status}); } } }; xhr.open("POST", url); xhr.timeout = 50000; xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xhr.send(JSON.stringify(params)); return xhr; }; }