From c98cf49ff25249196e55d6c6139d7e3c9c15fd18 Mon Sep 17 00:00:00 2001 From: Michael Abashian Date: Fri, 20 Jan 2017 15:28:37 -0500 Subject: [PATCH] Fix for smart search back/forward browser page navigation --- .../column-sort/column-sort.controller.js | 2 +- .../shared/paginate/paginate.controller.js | 2 +- .../smart-search/smart-search.controller.js | 49 +++++++++++++++++-- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/awx/ui/client/src/shared/column-sort/column-sort.controller.js b/awx/ui/client/src/shared/column-sort/column-sort.controller.js index 2dee59818c..eddf997f21 100644 --- a/awx/ui/client/src/shared/column-sort/column-sort.controller.js +++ b/awx/ui/client/src/shared/column-sort/column-sort.controller.js @@ -44,7 +44,7 @@ export default ['$scope', '$state', 'QuerySet', 'GetBasePath', queryset = _.merge($state.params[`${$scope.columnIterator}_search`], { order_by: order_by }); path = GetBasePath($scope.basePath) || $scope.basePath; - $state.go('.', { [$scope.columnIterator + '_search']: queryset }); + $state.go('.', { [$scope.columnIterator + '_search']: queryset }, {notify: false}); qs.search(path, queryset).then((res) =>{ $scope.dataset = res.data; $scope.collection = res.data.results; diff --git a/awx/ui/client/src/shared/paginate/paginate.controller.js b/awx/ui/client/src/shared/paginate/paginate.controller.js index bd9ff29032..044ad55859 100644 --- a/awx/ui/client/src/shared/paginate/paginate.controller.js +++ b/awx/ui/client/src/shared/paginate/paginate.controller.js @@ -21,7 +21,7 @@ export default ['$scope', '$stateParams', '$state', '$filter', 'GetBasePath', 'Q queryset = _.merge($stateParams[`${$scope.iterator}_search`], { page: page }); $state.go('.', { [$scope.iterator + '_search']: queryset - }); + }, {notify: false}); qs.search(path, queryset).then((res) => { $scope.dataset = res.data; $scope.collection = res.data.results; 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 b1baca4346..b460bd932a 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 @@ -6,7 +6,8 @@ export default ['$stateParams', '$scope', '$state', 'QuerySet', 'GetBasePath', ' defaults = _.find($state.$current.path, (step) => { return step.params.hasOwnProperty(`${$scope.iterator}_search`); }).params[`${$scope.iterator}_search`].config.value, - queryset = $stateParams[`${$scope.iterator}_search`]; + queryset = $stateParams[`${$scope.iterator}_search`], + stateChangeSuccessListener; // build $scope.tags from $stateParams.QuerySet, build fieldset key init(); @@ -20,6 +21,44 @@ export default ['$stateParams', '$scope', '$state', 'QuerySet', 'GetBasePath', ' $scope.options = data.options.data; $scope.$emit(`${$scope.list.iterator}_options`, data.options); }); + + function compareParams(a, b) { + for (let key in a) { + if (!(key in b) || a[key].toString() !== b[key].toString()) { + return false; + } + } + for (let key in b) { + if (!(key in a)) { + return false; + } + } + return true; + } + + if(stateChangeSuccessListener) { + stateChangeSuccessListener(); + } + + stateChangeSuccessListener = $scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) { + // State has changed - check to see if this is a param change + if(fromState.name === toState.name) { + if(!compareParams(fromParams[`${$scope.iterator}_search`], toParams[`${$scope.iterator}_search`])) { + // Params are not the same - we need to update the search. This should only happen when the user + // hits the forward/back navigation buttons in their browser. + queryset = toParams[`${$scope.iterator}_search`]; + qs.search(path, queryset).then((res) => { + $scope.dataset = res.data; + $scope.collection = res.data.results; + }); + + $scope.searchTerm = null; + $scope.searchTags = stripDefaultParams(queryset); + } + } + }); + + $scope.$on('$destroy', stateChangeSuccessListener); } // Removes state definition defaults and pagination terms @@ -70,7 +109,7 @@ export default ['$stateParams', '$scope', '$state', 'QuerySet', 'GetBasePath', ' let cleared = _.cloneDeep(defaults); delete cleared.page; queryset = cleared; - $state.go('.', {[$scope.iterator + '_search']: queryset}); + $state.go('.', {[$scope.iterator + '_search']: queryset}, {notify: false}); qs.search(path, queryset).then((res) => { $scope.dataset = res.data; $scope.collection = res.data.results; @@ -117,7 +156,7 @@ export default ['$stateParams', '$scope', '$state', 'QuerySet', 'GetBasePath', ' } }); $state.go('.', { - [$scope.iterator + '_search']: queryset }); + [$scope.iterator + '_search']: queryset }, {notify: false}); qs.search(path, queryset).then((res) => { $scope.dataset = res.data; $scope.collection = res.data.results; @@ -188,7 +227,7 @@ export default ['$stateParams', '$scope', '$state', 'QuerySet', 'GetBasePath', ' // This transition will not reload controllers/resolves/views // but will register new $stateParams[$scope.iterator + '_search'] terms $state.go('.', { - [$scope.iterator + '_search']: queryset }); + [$scope.iterator + '_search']: queryset }, {notify: false}); qs.search(path, queryset).then((res) => { $scope.dataset = res.data; $scope.collection = res.data.results; @@ -208,7 +247,7 @@ export default ['$stateParams', '$scope', '$state', 'QuerySet', 'GetBasePath', ' // This transition will not reload controllers/resolves/views // but will register new $stateParams[$scope.iterator + '_search'] terms $state.go('.', { - [$scope.iterator + '_search']: queryset }); + [$scope.iterator + '_search']: queryset }, {notify: false}); qs.search(path, queryset).then((res) => { $scope.dataset = res.data; $scope.collection = res.data.results;