Files
openfoodnetwork/app/assets/javascripts/darkswarm/controllers/enterprises_controller.js.coffee
Maikel Linke bd19bf5f3c Refactoring filter selectors
The FilterSelectorsService is now able to create multiple independent
lists of selectors. These lists are explicitly given to the
filter-selector directive.

For example a group's page has one set for the group's producers and one
set for the group's shops.
2016-02-04 16:08:19 +11:00

68 lines
2.3 KiB
CoffeeScript

Darkswarm.controller "EnterprisesCtrl", ($scope, $rootScope, $timeout, Enterprises, Search, $document, HashNavigation, FilterSelectorsService, EnterpriseModal, enterpriseMatchesNameQueryFilter, distanceWithinKmFilter) ->
$scope.Enterprises = Enterprises
$scope.producers_to_filter = Enterprises.producers
$scope.filterSelectors = FilterSelectorsService.createSelectors()
$scope.query = Search.search()
$scope.openModal = EnterpriseModal.open
$scope.activeTaxons = []
$scope.show_profiles = false
$scope.filtersActive = false
$scope.distanceMatchesShown = false
$scope.$watch "query", (query)->
Enterprises.flagMatching query
Search.search query
$rootScope.$broadcast 'enterprisesChanged'
$scope.distanceMatchesShown = false
$timeout ->
Enterprises.calculateDistance query, $scope.firstNameMatch()
$rootScope.$broadcast 'enterprisesChanged'
$rootScope.$on "enterprisesChanged", ->
$scope.filterEnterprises()
$scope.updateVisibleMatches()
# When filter settings change, this could change which name match is at the top, or even
# result in no matches. This affects the reference point that the distance matches are
# calculated from, so we need to recalculate distances.
$scope.$watch '[activeTaxons, shippingTypes, show_profiles]', ->
$timeout ->
Enterprises.calculateDistance $scope.query, $scope.firstNameMatch()
$rootScope.$broadcast 'enterprisesChanged'
, true
$rootScope.$on "$locationChangeSuccess", (newRoute, oldRoute) ->
if HashNavigation.active "hubs"
$document.scrollTo $("#hubs"), 100, 200
$scope.filterEnterprises = ->
es = Enterprises.hubs
$scope.nameMatches = enterpriseMatchesNameQueryFilter(es, true)
$scope.distanceMatches = enterpriseMatchesNameQueryFilter(es, false)
$scope.distanceMatches = distanceWithinKmFilter($scope.distanceMatches, 50)
$scope.updateVisibleMatches = ->
$scope.visibleMatches = if $scope.nameMatches.length == 0 || $scope.distanceMatchesShown
$scope.nameMatches.concat $scope.distanceMatches
else
$scope.nameMatches
$scope.showDistanceMatches = ->
$scope.distanceMatchesShown = true
$scope.updateVisibleMatches()
$scope.firstNameMatch = ->
if $scope.nameMatchesFiltered?
$scope.nameMatchesFiltered[0]
else
undefined