From f2d6f3d4f3fbe4d27d904cf6d9259fa7d0c8c0ec Mon Sep 17 00:00:00 2001 From: Michael Abashian Date: Wed, 21 Dec 2016 13:17:08 -0500 Subject: [PATCH 1/2] Handle failed searches gracefully and consistently fire search when enter is pressed --- awx/ui/client/src/shared/directives.js | 17 ++++++++++++++ .../smart-search/smart-search.controller.js | 22 ++++++++++++++++++- .../smart-search/smart-search.partial.html | 2 +- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/awx/ui/client/src/shared/directives.js b/awx/ui/client/src/shared/directives.js index 3c938d343e..4e8f8f38a8 100644 --- a/awx/ui/client/src/shared/directives.js +++ b/awx/ui/client/src/shared/directives.js @@ -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(); + } + }); + } + }; }]); diff --git a/awx/ui/client/src/shared/smart-search/smart-search.controller.js b/awx/ui/client/src/shared/smart-search/smart-search.controller.js index 3352cb5495..4b3e3685cd 100644 --- a/awx/ui/client/src/shared/smart-search/smart-search.controller.js +++ b/awx/ui/client/src/shared/smart-search/smart-search.controller.js @@ -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) => { diff --git a/awx/ui/client/src/shared/smart-search/smart-search.partial.html b/awx/ui/client/src/shared/smart-search/smart-search.partial.html index 3c0061fa33..073cb6f8d1 100644 --- a/awx/ui/client/src/shared/smart-search/smart-search.partial.html +++ b/awx/ui/client/src/shared/smart-search/smart-search.partial.html @@ -3,7 +3,7 @@
-
+
From 72367310e0276c3818c5d410893ee5489ea094e4 Mon Sep 17 00:00:00 2001 From: Michael Abashian Date: Wed, 21 Dec 2016 16:56:05 -0500 Subject: [PATCH 2/2] Handle case where the user just hits enter without inputing a search string --- .../smart-search/smart-search.controller.js | 70 ++++++++++--------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/awx/ui/client/src/shared/smart-search/smart-search.controller.js b/awx/ui/client/src/shared/smart-search/smart-search.controller.js index 4b3e3685cd..95f3be3adb 100644 --- a/awx/ui/client/src/shared/smart-search/smart-search.controller.js +++ b/awx/ui/client/src/shared/smart-search/smart-search.controller.js @@ -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) {