Handle failed searches gracefully and consistently fire search when enter is pressed

This commit is contained in:
Michael Abashian 2016-12-21 13:17:08 -05:00
parent 242467cb99
commit f2d6f3d4f3
3 changed files with 39 additions and 2 deletions

View File

@ -1151,4 +1151,21 @@ angular.module('AWDirectives', ['RestServices', 'Utilities', 'JobsHelper'])
});
}
};
}])
.directive('awEnterKey', [function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind("keydown keypress", function(event) {
var keyCode = event.which || event.keyCode;
if (keyCode === 13) {
scope.$apply(function() {
scope.$eval(attrs.awEnterKey);
});
event.preventDefault();
}
});
}
};
}]);

View File

@ -76,7 +76,8 @@ export default ['$stateParams', '$scope', '$state', 'QuerySet', 'GetBasePath', '
// add a search tag, merge new queryset, $state.go()
$scope.add = function(terms) {
let params = {};
let params = {},
origQueryset = _.clone(queryset);
_.forEach(terms.split(' '), (term) => {
// if only a value is provided, search using default keys
@ -114,6 +115,25 @@ export default ['$stateParams', '$scope', '$state', 'QuerySet', 'GetBasePath', '
// https://ui-router.github.io/docs/latest/interfaces/params.paramdeclaration.html#dynamic
// This transition will not reload controllers/resolves/views
// but will register new $stateParams[$scope.iterator + '_search'] terms
$state.go('.', {
[$scope.iterator + '_search']: queryset });
qs.search(path, queryset).then((res) => {
$scope.dataset = res.data;
$scope.collection = res.data.results;
})
.catch(function() {
$scope.revertSearch(origQueryset);
});
$scope.searchTerm = null;
$scope.searchTags = stripDefaultParams(queryset);
};
$scope.revertSearch = function(queryToBeRestored) {
queryset = queryToBeRestored;
// https://ui-router.github.io/docs/latest/interfaces/params.paramdeclaration.html#dynamic
// This transition will not reload controllers/resolves/views
// but will register new $stateParams[$scope.iterator + '_search'] terms
$state.go('.', {
[$scope.iterator + '_search']: queryset });
qs.search(path, queryset).then((res) => {

View File

@ -3,7 +3,7 @@
<div class="SmartSearch-bar">
<div class="SmartSearch-searchTermContainer">
<!-- string search input -->
<form class="SmartSearch-form" ng-submit="add(searchTerm)" novalidate>
<form name="smartSearch" class="SmartSearch-form" aw-enter-key="add(searchTerm)" novalidate>
<input class="SmartSearch-input" ng-model="searchTerm" placeholder="Search">
</form>
<div type="submit" class="SmartSearch-searchButton" ng-disabled="!searchTerm" ng-click="add(searchTerm)">