From 0ab75fe2ea59009182626df2ed35e5b1694fd8a1 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 9 Jul 2015 11:59:50 +1000 Subject: [PATCH] Take the first name match from the post-filter results to avoid matching off a result the user can't see --- .../controllers/enterprises_controller.js.coffee | 8 ++++++-- .../darkswarm/services/enterprises.js.coffee | 15 ++++----------- app/views/home/_hubs.html.haml | 14 +++++++------- app/views/home/_hubs_table.html.haml | 4 ++-- .../darkswarm/services/enterprise_spec.js.coffee | 15 ++------------- 5 files changed, 21 insertions(+), 35 deletions(-) diff --git a/app/assets/javascripts/darkswarm/controllers/enterprises_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/enterprises_controller.js.coffee index 23a1e6ef9c..19f42b5430 100644 --- a/app/assets/javascripts/darkswarm/controllers/enterprises_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/enterprises_controller.js.coffee @@ -1,4 +1,4 @@ -Darkswarm.controller "EnterprisesCtrl", ($scope, $rootScope, Enterprises, Search, $document, HashNavigation, FilterSelectorsService, EnterpriseModal, enterpriseMatchesNameQueryFilter, distanceWithinKmFilter) -> +Darkswarm.controller "EnterprisesCtrl", ($scope, $rootScope, $timeout, Enterprises, Search, $document, HashNavigation, FilterSelectorsService, EnterpriseModal, enterpriseMatchesNameQueryFilter, distanceWithinKmFilter) -> $scope.Enterprises = Enterprises $scope.totalActive = FilterSelectorsService.totalActive $scope.clearAll = FilterSelectorsService.clearAll @@ -13,11 +13,15 @@ Darkswarm.controller "EnterprisesCtrl", ($scope, $rootScope, Enterprises, Search $scope.$watch "query", (query)-> - Enterprises.evaluateQuery query + Enterprises.flagMatching query Search.search query $rootScope.$broadcast 'enterprisesChanged' $scope.distanceMatchesShown = false + $timeout -> + Enterprises.calculateDistance query, $scope.nameMatchesFiltered[0] + $rootScope.$broadcast 'enterprisesChanged' + $rootScope.$on "enterprisesChanged", -> $scope.filterEnterprises() diff --git a/app/assets/javascripts/darkswarm/services/enterprises.js.coffee b/app/assets/javascripts/darkswarm/services/enterprises.js.coffee index d58298b376..2dddfbc7ef 100644 --- a/app/assets/javascripts/darkswarm/services/enterprises.js.coffee +++ b/app/assets/javascripts/darkswarm/services/enterprises.js.coffee @@ -28,10 +28,6 @@ Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer, Dereferencer.dereference enterprise.taxons, Taxons.taxons_by_id Dereferencer.dereference enterprise.supplied_taxons, Taxons.taxons_by_id - evaluateQuery: (query) -> - @flagMatching query - @calculateDistance query - flagMatching: (query) -> for enterprise in @enterprises enterprise.matches_name_query = if query? && query.length > 0 @@ -39,15 +35,12 @@ Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer, else false - firstMatching: -> - (enterprise for enterprise in @enterprises when enterprise.matches_name_query)[0] - - calculateDistance: (query) -> + calculateDistance: (query, firstMatching) -> if query?.length > 0 - if @firstMatching()? - @setDistanceFrom @firstMatching() + if firstMatching? + @setDistanceFrom firstMatching else - @calculateDistanceGeo(query) + @calculateDistanceGeo query else @resetDistance() diff --git a/app/views/home/_hubs.html.haml b/app/views/home/_hubs.html.haml index 3a6313f5ea..40074ce0bb 100644 --- a/app/views/home/_hubs.html.haml +++ b/app/views/home/_hubs.html.haml @@ -10,17 +10,17 @@ .row .small-12.columns - .name-matches{"ng-show" => "nameMatches.length > 0"} + .name-matches{"ng-show" => "nameMatchesFiltered.length > 0"} %h2 Did you mean? = render "home/hubs_table", enterprises: "nameMatches" - .distance-matches{"ng-if" => "nameMatches.length == 0 || distanceMatchesShown"} - %h2{"ng-show" => "nameMatches.length > 0 || query.length > 0"} + .distance-matches{"ng-if" => "nameMatchesFiltered.length == 0 || distanceMatchesShown"} + %h2{"ng-show" => "nameMatchesFiltered.length > 0 || query.length > 0"} Closest to - %span{"ng-show" => "nameMatches.length > 0"} {{ nameMatches[0].name }}... - %span{"ng-hide" => "nameMatches.length > 0"} {{ query }}... + %span{"ng-show" => "nameMatchesFiltered.length > 0"} {{ nameMatchesFiltered[0].name }}... + %span{"ng-hide" => "nameMatchesFiltered.length > 0"} {{ query }}... = render "home/hubs_table", enterprises: "distanceMatches" - .show-distance-matches{"ng-show" => "nameMatches.length > 0 && !distanceMatchesShown"} - %a{href: "", "ng-click" => "showDistanceMatches()"} Show me shops near {{ nameMatches[0].name }} + .show-distance-matches{"ng-show" => "nameMatchesFiltered.length > 0 && !distanceMatchesShown"} + %a{href: "", "ng-click" => "showDistanceMatches()"} Show me shops near {{ nameMatchesFiltered[0].name }} diff --git a/app/views/home/_hubs_table.html.haml b/app/views/home/_hubs_table.html.haml index 5dd17b35d3..8842079f56 100644 --- a/app/views/home/_hubs_table.html.haml +++ b/app/views/home/_hubs_table.html.haml @@ -1,5 +1,5 @@ .active_table - %hub.active_table_node.row{"ng-repeat" => "hub in filteredEnterprises = (#{enterprises} | visible | taxons:activeTaxons | shipping:shippingTypes | showHubProfiles:show_profiles | orderBy:['-active', '+distance', '+orders_close_at'])", + %hub.active_table_node.row{"ng-repeat" => "hub in #{enterprises}Filtered = (#{enterprises} | visible | taxons:activeTaxons | shipping:shippingTypes | showHubProfiles:show_profiles | orderBy:['-active', '+distance', '+orders_close_at'])", "ng-class" => "{'is_profile' : hub.category == 'hub_profile', 'closed' : !open(), 'open' : open(), 'inactive' : !hub.active, 'current' : current()}", "ng-controller" => "HubNodeCtrl", id: "{{hub.hash}}"} @@ -7,4 +7,4 @@ = render 'home/skinny' = render 'home/fat' - = render 'shared/components/enterprise_no_results', enterprises: enterprises + = render 'shared/components/enterprise_no_results', enterprises: "#{enterprises}Filtered" diff --git a/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee index f9b866518c..67774aecbd 100644 --- a/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/enterprise_spec.js.coffee @@ -101,27 +101,16 @@ describe "Enterprises service", -> Enterprises.flagMatching '' expect(e.matches_name_query).toBe false for e in enterprises - describe "finding the first enterprise matched by a query", -> - it "returns the enterprise when one exists", -> - enterprises[0].matches_name_query = false - enterprises[1].matches_name_query = true - expect(Enterprises.firstMatching()).toEqual enterprises[1] - - it "returns undefined otherwise", -> - e.matches_name_query = false for e in enterprises - expect(Enterprises.firstMatching()).toBeUndefined() - describe "calculating the distance of enterprises from a location", -> describe "when a query is provided", -> it "sets the distance from the enterprise when a name match is available", -> - spyOn(Enterprises, "firstMatching").andReturn('match') spyOn(Enterprises, "setDistanceFrom") - Enterprises.calculateDistance "asdf" + Enterprises.calculateDistance "asdf", 'match' expect(Enterprises.setDistanceFrom).toHaveBeenCalledWith('match') it "calculates the distance from the geocoded query otherwise", -> spyOn(Enterprises, "calculateDistanceGeo") - Enterprises.calculateDistance "asdf" + Enterprises.calculateDistance "asdf", undefined expect(Enterprises.calculateDistanceGeo).toHaveBeenCalledWith("asdf") it "resets the distance when query is null", ->