Handle case where the user just hits enter without inputing a search string

This commit is contained in:
Michael Abashian 2016-12-21 16:56:05 -05:00
parent f2d6f3d4f3
commit 72367310e0

View File

@ -79,15 +79,6 @@ export default ['$stateParams', '$scope', '$state', 'QuerySet', 'GetBasePath', '
let params = {},
origQueryset = _.clone(queryset);
_.forEach(terms.split(' '), (term) => {
// if only a value is provided, search using default keys
if (term.split(':').length === 1) {
params = _.merge(params, setDefaults(term));
} else {
params = _.merge(params, qs.encodeParam(term));
}
});
function setDefaults(term) {
// "name" and "description" are sane defaults for MOST models, but not ALL!
// defaults may be configured in ListDefinition.defaultSearchParams
@ -101,32 +92,43 @@ export default ['$stateParams', '$scope', '$state', 'QuerySet', 'GetBasePath', '
}
}
params.page = '1';
queryset = _.merge(queryset, params, (objectValue, sourceValue, key, object) => {
if (object[key] && object[key] !== sourceValue){
return [object[key], sourceValue];
}
else {
// // https://lodash.com/docs/3.10.1#merge
// If customizer fn returns undefined merging is handled by default _.merge algorithm
return undefined;
}
});
// 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);
});
if(terms && terms !== '') {
_.forEach(terms.split(' '), (term) => {
// if only a value is provided, search using default keys
if (term.split(':').length === 1) {
params = _.merge(params, setDefaults(term));
} else {
params = _.merge(params, qs.encodeParam(term));
}
});
$scope.searchTerm = null;
$scope.searchTags = stripDefaultParams(queryset);
params.page = '1';
queryset = _.merge(queryset, params, (objectValue, sourceValue, key, object) => {
if (object[key] && object[key] !== sourceValue){
return [object[key], sourceValue];
}
else {
// // https://lodash.com/docs/3.10.1#merge
// If customizer fn returns undefined merging is handled by default _.merge algorithm
return undefined;
}
});
// 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) {