Quicknav is now even quicker!

This patch adds a search-string precheck that will route the
quicksearch result queries in one of two ways: If it's entirely
numeric, it assumes that you're trying to navigate by unique ID,
in which case it will try to present you options with that unique
id. If it is not entirely numeric, it will fall back to the LIKE
text search on the primary title.

Change-Id: I823c509196d21c587370351583abf87b950ee2a9
This commit is contained in:
Michael Krotscheck
2014-09-17 16:28:47 -07:00
parent 0bd2c31d00
commit 8baadc53c2

View File

@@ -88,30 +88,74 @@ angular.module('storyboard').controller('HeaderController',
/**
* Filter down the search string to actual resources that we can
* browse to directly (Explicitly not including users here).
* browse to directly (Explicitly not including users here). If the
* search string is entirely numeric, we'll instead do a
* straightforward GET :id.
*/
$scope.quickSearch = function (searchString) {
var deferred = $q.defer();
searchString = searchString || '';
$q.all({
projects: Project.criteriaResolver(searchString, 5),
stories: Story.criteriaResolver(searchString, 5)
}).then(function (results) {
var searches = [];
if (searchString.match(/^[0-9]+$/)) {
var getProjectDeferred = $q.defer();
var getStoryDeferred = $q.defer();
Project.get({id: searchString},
function (result) {
getProjectDeferred.resolve(Criteria.create(
'Project', result.id, result.name
));
}, function () {
getProjectDeferred.resolve(null);
});
Story.get({id: searchString},
function (result) {
getStoryDeferred.resolve(Criteria.create(
'Story', result.id, result.title
));
}, function () {
getStoryDeferred.resolve(null);
});
// If the search string is entirely numeric, do a GET.
searches.push(getProjectDeferred.promise);
searches.push(getStoryDeferred.promise);
} else {
searches.push(Project.criteriaResolver(searchString, 5));
searches.push(Story.criteriaResolver(searchString, 5));
}
$q.all(searches).then(function (searchResults) {
var criteria = [
Criteria.create('Text', searchString)
];
// Add the returned projects to the results list.
results.projects.forEach(function (item) {
/**
* Add a result to the returned criteria.
*/
var addResult = function (item) {
criteria.push(item);
});
// Add the returned stories to the results list.
results.stories.forEach(function (item) {
criteria.push(item);
});
};
for (var i = 0; i < searchResults.length; i++) {
var results = searchResults[i];
if (!results) {
continue;
}
if (!!results.forEach) {
// If it's iterable, do that. Otherwise just add it.
results.forEach(addResult);
} else {
addResult(results);
}
}
deferred.resolve(criteria);
});