mirror of
https://github.com/nasa/openmct.git
synced 2025-03-25 13:28:38 +00:00
[Serach] Callbacks and renaming
Added validType() as a callback parameter to the search providers. It is defined in the search aggregator, and is used to put any restrictions on object type in the displayed search results. Renamed EverythingSearchProvider to GenericSearchProvider.
This commit is contained in:
parent
492dbcbc51
commit
c75d94289b
@ -47,7 +47,7 @@
|
||||
{
|
||||
"provides": "searchService",
|
||||
"type": "provider",
|
||||
"implementation": "providers/EverythingSearchProvider.js",
|
||||
"implementation": "providers/GenericSearchProvider.js",
|
||||
"depends": [ "objectService" ]
|
||||
},
|
||||
{
|
||||
|
@ -39,15 +39,12 @@ define(
|
||||
*/
|
||||
function SearchAggregator(providers) {
|
||||
|
||||
function getPromisedResults(resultsPromises, promiseIndex, finalResults) {
|
||||
if (promiseIndex >= resultsPromises.length) {
|
||||
return finalResults;
|
||||
} else {
|
||||
return resultsPromises[promiseIndex].then(function (results) {
|
||||
finalResults = finalResults.concat(results);
|
||||
return getPromisedResults(resultsPromises, promiseIndex + 1, finalResults);
|
||||
});
|
||||
}
|
||||
// Determines if a searchResult object is a valid type
|
||||
// to be displayed as a final result. Is passed to the
|
||||
// search providers as an argument.
|
||||
function validType(model) {
|
||||
// Nothing is currently disallowed
|
||||
return true;
|
||||
}
|
||||
|
||||
// Remove extra objects that have the same ID
|
||||
@ -85,6 +82,17 @@ define(
|
||||
return results;
|
||||
}
|
||||
|
||||
function getPromisedResults(resultsPromises, promiseIndex, finalResults) {
|
||||
if (promiseIndex >= resultsPromises.length) {
|
||||
return finalResults;
|
||||
} else {
|
||||
return resultsPromises[promiseIndex].then(function (results) {
|
||||
finalResults = finalResults.concat(results);
|
||||
return getPromisedResults(resultsPromises, promiseIndex + 1, finalResults);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Recieves results in the format of a serachResult object. It
|
||||
// has the members id, object, and score. It has a function
|
||||
// next() which returns the next highest score search result
|
||||
@ -98,7 +106,7 @@ define(
|
||||
|
||||
// Get result list promises
|
||||
for (var i = 0; i < providers.length; i += 1) {
|
||||
resultsPromises.push(providers[i].query(inputID));
|
||||
resultsPromises.push(providers[i].query(inputID, validType));
|
||||
}
|
||||
|
||||
// Wait for the promises to fufill
|
||||
|
@ -98,7 +98,7 @@ define(
|
||||
|
||||
// Processes results from the format that elasticsearch returns to
|
||||
// a list of objects in the format that mct-representation can use
|
||||
function processResults(rawResults) {
|
||||
function processResults(rawResults, validType) {
|
||||
var results = rawResults.data.hits.hits,
|
||||
resultsLength = results.length,
|
||||
ids = [],
|
||||
@ -131,9 +131,10 @@ define(
|
||||
for (j = 0; j < resultsLength; j += 1) {
|
||||
id = ids[j];
|
||||
|
||||
// Include any item except folders
|
||||
// Include items we can get models for
|
||||
if (objects[id].getModel) {
|
||||
if (objects[id].getModel().type !== "folder") {
|
||||
// Check to see if they are allowed to be included
|
||||
if (validType(objects[id].getModel())) {
|
||||
// Format the results as searchResult objects
|
||||
searchResults.push({
|
||||
id: id,
|
||||
@ -155,7 +156,6 @@ define(
|
||||
* Notes:
|
||||
* * The order of the results is from highest to lowest score,
|
||||
* as elsaticsearch determines them to be.
|
||||
* * Folders are not included in the results.
|
||||
* * Wildcards are supported.
|
||||
* * Fuzziness is used to produce more results that are still
|
||||
* relevant. (All results within a certain edit distance.)
|
||||
@ -164,10 +164,13 @@ define(
|
||||
*
|
||||
* @param inputID the name of the ID property of the html text
|
||||
* input where this funcion should find the search term
|
||||
* @param validType a function which takes a model for an object
|
||||
* and determines if it is of a valid type to include in the
|
||||
* final list of results
|
||||
* @param maxResults (optional) the maximum number of results
|
||||
* that this function should return
|
||||
*/
|
||||
function queryElasticsearch(inputID, maxResults) {
|
||||
function queryElasticsearch(inputID, validType, maxResults) {
|
||||
var searchTerm;
|
||||
|
||||
// Check to see if the user provided a maximum
|
||||
@ -190,7 +193,7 @@ define(
|
||||
"&size=" + maxResults
|
||||
}).then(function (rawResults) {
|
||||
// ...then process the data
|
||||
return processResults(rawResults);
|
||||
return processResults(rawResults, validType);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -22,7 +22,7 @@
|
||||
/*global define*/
|
||||
|
||||
/**
|
||||
* Module defining EverythingSearchProvider. Created by shale on 07/16/2015.
|
||||
* Module defining GenericSearchProvider. Created by shale on 07/16/2015.
|
||||
*/
|
||||
define(
|
||||
[],
|
||||
@ -42,7 +42,7 @@ define(
|
||||
* @param {string} SPACE the name of the persistence space from which
|
||||
* models should be retrieved.
|
||||
*/
|
||||
function EverythingSearchProvider(objectService) {
|
||||
function GenericSearchProvider(objectService) {
|
||||
|
||||
// Recursive helper function for getItems()
|
||||
function itemsHelper(children, i) {
|
||||
@ -98,7 +98,7 @@ define(
|
||||
}
|
||||
|
||||
// Determines if this item can be a valid result for this search term
|
||||
function validResult(item, term) {
|
||||
function match(item, term) {
|
||||
var itemModel = item.object.getModel(),
|
||||
itemName = itemModel.name.toLocaleLowerCase(),
|
||||
itemType = itemModel.type.toLocaleLowerCase();
|
||||
@ -106,16 +106,8 @@ define(
|
||||
return itemName.includes(term) || itemType.includes(term);
|
||||
}
|
||||
|
||||
// Determines if this item is a valid type for a search result
|
||||
function validType(item) {
|
||||
var itemModel = item.object.getModel();
|
||||
|
||||
// Only folders are disallowed
|
||||
return itemModel.type !== "folder";
|
||||
}
|
||||
|
||||
// Filter through a list of searchResults based on a search term
|
||||
function filterResults(results, term, resultsLength) {
|
||||
function filterResults(results, term, resultsLength, validType) {
|
||||
var searchResults = [],
|
||||
itemModel;
|
||||
|
||||
@ -123,7 +115,7 @@ define(
|
||||
// Prevent errors from getModel not being defined
|
||||
if (results[i].object.getModel) {
|
||||
// Include any items that match the term and are of valid type
|
||||
if (validResult(results[i], term) && validType(results[i])) {
|
||||
if (match(results[i], term) && validType(results[i].object.getModel())) {
|
||||
// Score the result
|
||||
score(results[i], term);
|
||||
// Add the result to the result list
|
||||
@ -144,15 +136,17 @@ define(
|
||||
* * A domain object qualifies as a match for a search term if
|
||||
* the object's name property contains the exact search term
|
||||
* as a substring.
|
||||
* * Folders are not included in the results.
|
||||
* * Wildcards are not supported.
|
||||
*
|
||||
* @param inputID the name of the ID property of the html text
|
||||
* input where this funcion should find the search term
|
||||
* @param validType a function which takes a model for an object
|
||||
* and determines if it is of a valid type to include in the
|
||||
* final list of results
|
||||
* @param maxResults (optional) the maximum number of results
|
||||
* that this function should return
|
||||
*/
|
||||
function queryManual(inputID, maxResults) {
|
||||
function queryManual(inputID, validType, maxResults) {
|
||||
var term,
|
||||
searchResults = [],
|
||||
resultsLength;
|
||||
@ -180,7 +174,7 @@ define(
|
||||
}
|
||||
|
||||
// Then filter through the items list
|
||||
searchResults = filterResults(searchResultItems, term, resultsLength);
|
||||
searchResults = filterResults(searchResultItems, term, resultsLength, validType);
|
||||
|
||||
//console.log('filtered searchResults (in Everything)', searchResults);
|
||||
return searchResults;
|
||||
@ -193,6 +187,6 @@ define(
|
||||
}
|
||||
|
||||
|
||||
return EverythingSearchProvider;
|
||||
return GenericSearchProvider;
|
||||
}
|
||||
);
|
Loading…
x
Reference in New Issue
Block a user