From 3ada2d3ca0b18292e669d1092d3665d833fabe6d Mon Sep 17 00:00:00 2001 From: Rajat Vig Date: Fri, 18 Sep 2015 11:47:47 -0700 Subject: [PATCH] magic-overrides should clone from currentSearch The magic search should clone currentSearch to remember current search criteria. Co-Authored-By: Kristine Brown Closes-Bug: #1497408 Change-Id: I0d09da9d70af4d7b6d1a2c7feaa7ce836d23749f --- .../magic-search/magic-overrides.directive.js | 36 ++++-- ...c.js => magic-overrides.directive.spec.js} | 108 ++++++++++++------ 2 files changed, 98 insertions(+), 46 deletions(-) rename horizon/static/framework/widgets/magic-search/{magic-overrides.spec.js => magic-overrides.directive.spec.js} (68%) diff --git a/horizon/static/framework/widgets/magic-search/magic-overrides.directive.js b/horizon/static/framework/widgets/magic-search/magic-overrides.directive.js index 74815a2475..c6364be262 100644 --- a/horizon/static/framework/widgets/magic-search/magic-overrides.directive.js +++ b/horizon/static/framework/widgets/magic-search/magic-overrides.directive.js @@ -75,6 +75,7 @@ return directive; function MagicOverridesController($element, $scope, $timeout, $window) { + /** * showMenu and hideMenu depend on Foundation's dropdown. They need * to be modified to work with another dropdown implementation. @@ -103,27 +104,39 @@ $scope.initSearch(); }); }); + $scope.$on('$destroy', function () { facetsChangedWatcher(); }); + function getFacets(currentFacets) { + if (angular.isUndefined(currentFacets)) { + var initialFacets = $window.location.search; + if (initialFacets.indexOf('?') === 0) { + initialFacets = initialFacets.slice(1); + } + return initialFacets.split('&'); + } else { + return currentFacets.map(function(facet) { + return facet.name; + }); + } + } + /** * Override magic_search.js 'initFacets' to fix browser refresh issue * and to emit('checkFacets') to flag facets as 'isServer' */ - $scope.initFacets = function () { - // set facets selected and remove them from 'facetsObj' - var initialFacets = $window.location.search; - if (initialFacets.indexOf('?') === 0) { - initialFacets = initialFacets.slice(1); - } - initialFacets = initialFacets.split('&'); - if (initialFacets.length > 1 || initialFacets[0].length > 0) { + $scope.initFacets = function(currentFacets) { + var facets = getFacets(currentFacets); + + if (facets.length > 1 || (facets[0] && facets[0].length > 0)) { $timeout(function () { $scope.strings.prompt = ''; }); } - angular.forEach(initialFacets, function (facet) { + + angular.forEach(facets, function(facet) { var facetParts = facet.split('='); angular.forEach($scope.facetsObj, function (value) { if (value.name == facetParts[0]) { @@ -188,12 +201,15 @@ } // re-init to restore facets cleanly $scope.facetsObj = $scope.copyFacets($scope.facetsSave); + var currentSearch = angular.copy($scope.currentSearch); $scope.currentSearch = []; - $scope.initFacets(); + $scope.initFacets(currentSearch); // broadcast to check facets for server-side $scope.$emit('checkFacets', $scope.currentSearch); }; + + $scope.emitQuery(); } } })(); diff --git a/horizon/static/framework/widgets/magic-search/magic-overrides.spec.js b/horizon/static/framework/widgets/magic-search/magic-overrides.directive.spec.js similarity index 68% rename from horizon/static/framework/widgets/magic-search/magic-overrides.spec.js rename to horizon/static/framework/widgets/magic-search/magic-overrides.directive.spec.js index dfd7618474..8e9e9e0703 100644 --- a/horizon/static/framework/widgets/magic-search/magic-overrides.spec.js +++ b/horizon/static/framework/widgets/magic-search/magic-overrides.directive.spec.js @@ -149,9 +149,22 @@ expect($magicScope.deleteFacetEntirely).toHaveBeenCalledWith([ 'name', 'myname' ]); }); - it('currentSearch should have two items when URL has two search terms', function () { - $window.location.search = '?name=myname&status=active'; - $magicScope.initFacets(); + it('currentSearch should have one item when given one search term', function () { + var currentFacets = [{name: 'name=myname'}]; + $magicScope.initFacets(currentFacets); + $timeout.flush(); + + expect($magicScope.currentSearch.length).toBe(1); + expect($magicScope.currentSearch[0].label).toEqual([ 'Name', 'myname' ]); + expect($magicScope.currentSearch[0].name).toBe('name=myname'); + + // 'name' facet should be deleted (singleton) + expect($magicScope.deleteFacetEntirely).toHaveBeenCalledWith([ 'name', 'myname' ]); + }); + + it('currentSearch should have two items when given two search terms', function () { + var currentFacets = [{name: 'name=myname'}, {name: 'status=active'}]; + $magicScope.initFacets(currentFacets); $timeout.flush(); // only 'active' option should be removed from 'status' facet (not singleton) @@ -160,8 +173,8 @@ }); it('flavor facet should be removed if search term includes flavor', function () { - $window.location.search = '?flavor=m1.tiny'; - $magicScope.initFacets(); + var currentFacets = [{name: 'flavor=m1.tiny'}]; + $magicScope.initFacets(currentFacets); $timeout.flush(); // entire 'flavor' facet should be removed even if some options left (singleton) @@ -170,24 +183,27 @@ it('currentSearch should have one item when search is textSearch', function () { $magicScope.textSearch = 'test'; - $magicScope.initFacets(); + $magicScope.initFacets([]); + $timeout.flush(); expect($magicScope.currentSearch[0].label).toEqual([ 'Text', 'test' ]); expect($magicScope.currentSearch[0].name).toBe('text=test'); }); - it('filteredObj should have two remaining items when URL has one search term', function () { - $window.location.search = '?name=myname&status=active'; - $magicScope.initFacets(); + it('currentSearch should have textSearch and currentSearch', function () { + $magicScope.textSearch = 'test'; + $magicScope.initFacets([{name: 'flavor=m1.tiny'}]); + $timeout.flush(); - // filteredObj should have only unused facets and options - // 'name' facet is singleton and should have been removed - // 'status' facet is not single and should remain with one option left - expect($magicScope.filteredObj.length).toBe(2); + expect($magicScope.currentSearch.length).toBe(2); + expect($magicScope.currentSearch[0].label).toEqual([ 'Flavor', 'm1.tiny' ]); + expect($magicScope.currentSearch[0].name).toBe('flavor=m1.tiny'); + expect($magicScope.currentSearch[1].label).toEqual([ 'Text', 'test' ]); + expect($magicScope.currentSearch[1].name).toBe('text=test'); }); it('should call checkFacets when initFacets called', function () { - $magicScope.initFacets(); + $magicScope.initFacets([]); expect($magicScope.$emit).toHaveBeenCalledWith('checkFacets', []); }); @@ -195,7 +211,7 @@ describe('removeFacet', function () { beforeEach(function () { - spyOn($magicScope, 'initFacets'); + spyOn($magicScope, 'initFacets').and.callThrough(); }); it('should call emitQuery, initFacets and emit checkFacets on removeFacet', function () { @@ -208,7 +224,7 @@ expect($magicScope.currentSearch).toEqual([]); expect($magicScope.emitQuery).toHaveBeenCalledWith('name=myname'); - expect($magicScope.initFacets).toHaveBeenCalled(); + expect($magicScope.initFacets).toHaveBeenCalledWith([]); expect($magicScope.$emit).toHaveBeenCalledWith('checkFacets', []); expect($magicScope.strings.prompt).toBe('Prompt'); }); @@ -223,27 +239,47 @@ expect($magicScope.strings.prompt).toBe(''); }); - /*eslint-disable max-len */ - it('should call resetState, initFacets and emit checkFacets on removeFacet when facet selected', - /*eslint-enable max-len */ - function () { - var initialSearch = { - name: 'name=myname', - label: [ 'Name', 'myname' ] - }; - $magicScope.currentSearch.push(initialSearch); - $magicScope.facetSelected = { - 'name': 'status', - 'label': [ 'Status', 'active' ] - }; - $magicScope.removeFacet(0); + it('should emit checkFacets on removeFacet if facetSelected', function () { + var initialSearch = { + name: 'name=myname', + label: [ 'Name', 'myname' ] + }; + $magicScope.currentSearch.push(initialSearch); + $magicScope.facetSelected = { + 'name': 'status', + 'label': [ 'Status', 'active' ] + }; + $magicScope.removeFacet(0); + + expect($magicScope.currentSearch).toEqual([]); + expect($magicScope.resetState).toHaveBeenCalled(); + expect($magicScope.initFacets).toHaveBeenCalledWith([]); + expect($magicScope.$emit).toHaveBeenCalledWith('checkFacets', []); + }); + + it('should emit checkFacets and remember state on removeFacet if facetSelected', function () { + var search1 = { + name: 'name=myname', + label: [ 'Name', 'myname' ] + }; + var search2 = { + name: 'flavor=m1.tiny', + label: [ 'Flavor', 'm1.tiny' ] + }; + $magicScope.currentSearch.push(search1); + $magicScope.currentSearch.push(search2); + $magicScope.facetSelected = { + 'name': 'status', + 'label': [ 'Status', 'active' ] + }; + $magicScope.removeFacet(0); + + expect($magicScope.currentSearch).toEqual([search2]); + expect($magicScope.resetState).toHaveBeenCalled(); + expect($magicScope.initFacets).toHaveBeenCalledWith([search2]); + expect($magicScope.$emit).toHaveBeenCalledWith('checkFacets', [search2]); + }); - expect($magicScope.currentSearch).toEqual([]); - expect($magicScope.resetState).toHaveBeenCalled(); - expect($magicScope.initFacets).toHaveBeenCalled(); - expect($magicScope.$emit).toHaveBeenCalledWith('checkFacets', []); - } - ); }); });