2015-07-16 10:35:26 -07:00
|
|
|
/*****************************************************************************
|
|
|
|
* Open MCT Web, Copyright (c) 2014-2015, United States Government
|
|
|
|
* as represented by the Administrator of the National Aeronautics and Space
|
|
|
|
* Administration. All rights reserved.
|
|
|
|
*
|
|
|
|
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
* Open MCT Web includes source code licensed under additional open source
|
|
|
|
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
|
|
|
* this source code distribution or the Licensing information page available
|
|
|
|
* at runtime from the About dialog for additional information.
|
|
|
|
*****************************************************************************/
|
|
|
|
/*global define*/
|
|
|
|
|
|
|
|
/**
|
2015-07-17 11:24:33 -07:00
|
|
|
* Module defining GenericSearchProvider. Created by shale on 07/16/2015.
|
2015-07-16 10:35:26 -07:00
|
|
|
*/
|
|
|
|
define(
|
|
|
|
[],
|
|
|
|
function () {
|
|
|
|
"use strict";
|
2015-07-16 11:31:11 -07:00
|
|
|
|
|
|
|
var DEFAULT_MAX_RESULTS = 100;
|
|
|
|
|
2015-07-16 10:35:26 -07:00
|
|
|
/**
|
|
|
|
* A model service which reads domain object models from an external
|
|
|
|
* persistence service.
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {PersistenceService} persistenceService the service in which
|
|
|
|
* domain object models are persisted.
|
|
|
|
* @param $q Angular's $q service, for working with promises
|
|
|
|
* @param {string} SPACE the name of the persistence space from which
|
|
|
|
* models should be retrieved.
|
|
|
|
*/
|
2015-07-17 11:24:33 -07:00
|
|
|
function GenericSearchProvider(objectService) {
|
2015-07-16 10:35:26 -07:00
|
|
|
|
|
|
|
// Recursive helper function for getItems()
|
|
|
|
function itemsHelper(children, i) {
|
|
|
|
if (i >= children.length) {
|
|
|
|
// Done!
|
|
|
|
return children;
|
|
|
|
} else if (children[i].hasCapability('composition')) {
|
|
|
|
// This child has children
|
|
|
|
return children[i].getCapability('composition').invoke().then(function (grandchildren) {
|
|
|
|
// Add grandchildren to the end of the list
|
|
|
|
// They will also be checked for composition
|
|
|
|
return itemsHelper(children.concat(grandchildren), i + 1);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// This child is a leaf
|
|
|
|
return itemsHelper(children, i + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts the filetree into a list
|
|
|
|
function getItems() {
|
|
|
|
// Aquire My Items (root folder)
|
|
|
|
return objectService.getObjects(['mine']).then(function (objects) {
|
|
|
|
// Get all of its descendents
|
2015-07-16 13:08:05 -07:00
|
|
|
return itemsHelper([objects.mine], 0).then(function (items) {
|
|
|
|
// Turn them into searchResult objects (object, id, and score)
|
|
|
|
var searchResultItems = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < items.length; i += 1) {
|
|
|
|
searchResultItems.push({
|
|
|
|
id: items[i].getId(),
|
|
|
|
object: items[i],
|
2015-07-16 14:36:02 -07:00
|
|
|
score: 0 // Assign actual score when filtering for term
|
2015-07-16 13:08:05 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//console.log('searchResultItems (in Everything)', searchResultItems);
|
|
|
|
return searchResultItems;
|
2015-07-16 10:35:26 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-17 13:49:09 -07:00
|
|
|
// Process the search input. Makes an array of search terms
|
|
|
|
// by splitting up the input at spaces.
|
|
|
|
function process(input) {
|
2015-07-17 14:06:14 -07:00
|
|
|
return input.toLocaleLowerCase().split(' ');
|
2015-07-16 14:36:02 -07:00
|
|
|
}
|
|
|
|
|
2015-07-17 13:49:09 -07:00
|
|
|
// Generate a score for an item based on its similarity to a search term.
|
|
|
|
// The score is equal to the number of terms that are a substring of the
|
|
|
|
// object name.
|
|
|
|
function score(item, terms, originalInput) {
|
|
|
|
var name = item.object.getModel().name.toLocaleLowerCase(),
|
2015-07-17 14:06:14 -07:00
|
|
|
weight = .65,
|
|
|
|
score = 0;
|
|
|
|
|
|
|
|
// Make the score really big if the item name and
|
|
|
|
// the original search input are the same
|
|
|
|
if (name === originalInput.toLocaleLowerCase()) {
|
|
|
|
score = 42;
|
|
|
|
}
|
2015-07-16 16:50:07 -07:00
|
|
|
|
2015-07-17 13:49:09 -07:00
|
|
|
for (var i = 0; i < terms.length; i++) {
|
|
|
|
// Increase the score if the term is in the item name
|
|
|
|
if (name.includes(terms[i])) {
|
|
|
|
score++;
|
2015-07-17 14:06:14 -07:00
|
|
|
|
|
|
|
// Add extra to the score if the search term exists
|
|
|
|
// as its own term within the items
|
|
|
|
if (name.split(' ').indexOf(terms[i]) !== -1) {
|
|
|
|
score += .5;
|
|
|
|
}
|
2015-07-17 13:49:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-17 14:06:14 -07:00
|
|
|
return score * weight;
|
2015-07-16 16:50:07 -07:00
|
|
|
}
|
|
|
|
|
2015-07-16 14:36:02 -07:00
|
|
|
// Filter through a list of searchResults based on a search term
|
2015-07-17 13:49:09 -07:00
|
|
|
function filterResults(results, originalInput, resultsLength, validType) {
|
|
|
|
var terms,
|
|
|
|
searchResults = [],
|
2015-07-16 16:50:07 -07:00
|
|
|
itemModel;
|
2015-07-16 14:36:02 -07:00
|
|
|
|
2015-07-17 13:49:09 -07:00
|
|
|
// Split the original search input into search terms
|
|
|
|
terms = process(originalInput);
|
|
|
|
|
2015-07-16 14:36:02 -07:00
|
|
|
for (var i = 0; i < resultsLength; i += 1) {
|
|
|
|
// Prevent errors from getModel not being defined
|
|
|
|
if (results[i].object.getModel) {
|
2015-07-17 13:49:09 -07:00
|
|
|
results[i].score = score(results[i], terms, originalInput);
|
|
|
|
// Include any items that match the terms and are of valid type
|
|
|
|
if (results[i].score > 0 && validType(results[i].object.getModel())) {
|
2015-07-16 14:36:02 -07:00
|
|
|
// Add the result to the result list
|
|
|
|
searchResults.push(results[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return searchResults;
|
|
|
|
}
|
|
|
|
|
2015-07-16 10:35:26 -07:00
|
|
|
/**
|
|
|
|
* Searches through the filetree for domain objects which match
|
|
|
|
* the search term. This function is to be used as a fallback
|
|
|
|
* in the case where other search services are not avaliable.
|
|
|
|
* Notes:
|
|
|
|
* * The order of the results is not guarenteed.
|
|
|
|
* * 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.
|
|
|
|
* * 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
|
2015-07-17 11:24:33 -07:00
|
|
|
* @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
|
2015-07-16 10:35:26 -07:00
|
|
|
* @param maxResults (optional) the maximum number of results
|
|
|
|
* that this function should return
|
|
|
|
*/
|
2015-07-17 13:49:09 -07:00
|
|
|
function queryGeneric(inputID, validType, maxResults) {
|
|
|
|
var input,
|
|
|
|
terms = [],
|
2015-07-16 10:35:26 -07:00
|
|
|
searchResults = [],
|
2015-07-16 14:36:02 -07:00
|
|
|
resultsLength;
|
2015-07-16 10:35:26 -07:00
|
|
|
|
|
|
|
// Check to see if the user provided a maximum
|
|
|
|
// number of results to display
|
|
|
|
if (!maxResults) {
|
|
|
|
// Else, we provide a default value.
|
|
|
|
maxResults = DEFAULT_MAX_RESULTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the user input
|
2015-07-17 13:49:09 -07:00
|
|
|
input = document.getElementById(inputID).value;
|
2015-07-16 10:35:26 -07:00
|
|
|
|
|
|
|
// Get items list
|
2015-07-16 13:08:05 -07:00
|
|
|
return getItems().then(function (searchResultItems) {
|
2015-07-16 10:35:26 -07:00
|
|
|
// Keep track of the number of results to display
|
2015-07-16 13:08:05 -07:00
|
|
|
if (searchResultItems.length < maxResults) {
|
|
|
|
resultsLength = searchResultItems.length;
|
2015-07-16 10:35:26 -07:00
|
|
|
} else {
|
|
|
|
resultsLength = maxResults;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then filter through the items list
|
2015-07-17 13:49:09 -07:00
|
|
|
searchResults = filterResults(searchResultItems, input, resultsLength, validType);
|
2015-07-16 10:35:26 -07:00
|
|
|
|
2015-07-16 13:08:05 -07:00
|
|
|
//console.log('filtered searchResults (in Everything)', searchResults);
|
2015-07-16 10:35:26 -07:00
|
|
|
return searchResults;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2015-07-17 13:49:09 -07:00
|
|
|
query: queryGeneric
|
2015-07-16 10:35:26 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-17 11:24:33 -07:00
|
|
|
return GenericSearchProvider;
|
2015-07-16 10:35:26 -07:00
|
|
|
}
|
|
|
|
);
|