From 2fd25f6cc43735a18d6abf8613213f1281ab33bb Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 10 Apr 2015 17:42:54 +1000 Subject: [PATCH] Injecting all enterprises into group page The enterprises have related producers and hubs that need to be dereferenced. Therefore, we need a bigger set of enterprises to be loaded. Injecting all enterprises is simple and doesn't require changes if the set of needed enterprise data is growing in future. --- .../group_enterprises_controller.js.coffee | 3 +-- .../group_page_controller.js.coffee | 14 ++++++++++++++ .../darkswarm/services/map.js.coffee | 8 +++++--- app/views/groups/show.html.haml | 19 +++++++++++++------ 4 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 app/assets/javascripts/darkswarm/controllers/group_page_controller.js.coffee diff --git a/app/assets/javascripts/darkswarm/controllers/group_enterprises_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/group_enterprises_controller.js.coffee index ab8336116f..db1a9a1d8b 100644 --- a/app/assets/javascripts/darkswarm/controllers/group_enterprises_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/group_enterprises_controller.js.coffee @@ -1,5 +1,4 @@ -Darkswarm.controller "GroupEnterprisesCtrl", ($scope, Enterprises, Search, FilterSelectorsService) -> - $scope.Enterprises = Enterprises +Darkswarm.controller "GroupEnterprisesCtrl", ($scope, Search, FilterSelectorsService) -> $scope.totalActive = FilterSelectorsService.totalActive $scope.clearAll = FilterSelectorsService.clearAll $scope.filterText = FilterSelectorsService.filterText diff --git a/app/assets/javascripts/darkswarm/controllers/group_page_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/group_page_controller.js.coffee new file mode 100644 index 0000000000..b30d409b97 --- /dev/null +++ b/app/assets/javascripts/darkswarm/controllers/group_page_controller.js.coffee @@ -0,0 +1,14 @@ +Darkswarm.controller "GroupPageCtrl", ($scope, group_enterprises, Enterprises, MapConfiguration, OfnMap) -> + $scope.Enterprises = Enterprises + + group_enterprises_ids = group_enterprises.map (enterprise) => + enterprise.id + is_in_group = (enterprise) -> + group_enterprises_ids.indexOf(enterprise.id) != -1 + + $scope.group_producers = Enterprises.producers.filter is_in_group + $scope.group_hubs = Enterprises.hubs.filter is_in_group + + $scope.map = angular.copy MapConfiguration.options + $scope.mapMarkers = OfnMap.enterprise_markers group_enterprises + diff --git a/app/assets/javascripts/darkswarm/services/map.js.coffee b/app/assets/javascripts/darkswarm/services/map.js.coffee index 703c3c54bf..7ff9f553f2 100644 --- a/app/assets/javascripts/darkswarm/services/map.js.coffee +++ b/app/assets/javascripts/darkswarm/services/map.js.coffee @@ -1,11 +1,13 @@ -Darkswarm.factory "OfnMap", (Enterprises, EnterpriseModal, visibleFilter)-> +Darkswarm.factory "OfnMap", (Enterprises, EnterpriseModal, visibleFilter) -> new class OfnMap constructor: -> - @enterprises = (@extend(enterprise) for enterprise in visibleFilter(Enterprises.enterprises)) + @enterprises = @enterprise_markers(Enterprises.enterprises) + enterprise_markers: (enterprises) -> + @extend(enterprise) for enterprise in visibleFilter(enterprises) # Adding methods to each enterprise - extend: (enterprise)-> + extend: (enterprise) -> new class MapMarker # We're whitelisting attributes because GMaps tries to crawl # our data, and our data is recursive, so it breaks diff --git a/app/views/groups/show.html.haml b/app/views/groups/show.html.haml index 0459b2fc52..625d4f0d6d 100644 --- a/app/views/groups/show.html.haml +++ b/app/views/groups/show.html.haml @@ -1,4 +1,12 @@ -#group-page.row.pad-top +-# inject all enterprises as "enterprises" +-# it could be more efficient to inject only the enterprises that are related to the group += inject_enterprises + +-# inject enterprises in this group +-# further hubs and producers of these enterprises can't be resoleved within this small subset += inject_json_ams "group_enterprises", @group.enterprises, Api::EnterpriseSerializer, active_distributors: @active_distributors + +#group-page.row.pad-top{"ng-controller" => "GroupPageCtrl"} .small-12.columns.pad-top %header .row @@ -22,11 +30,10 @@ %tab{heading: 'Map', active: "active(\'\')", select: "select(\'\')"} - = inject_json_ams "enterprises", @group.enterprises, Api::EnterpriseSerializer, active_distributors: @active_distributors .map-container - %map{"ng-controller" => "MapCtrl", "ng-if" => "(active(\'\') && (mapShowed = true)) || mapShowed"} + %map{"ng-if" => "(active(\'\') && (mapShowed = true)) || mapShowed"} %google-map{options: "map.additional_options", center: "map.center", zoom: "map.zoom", styles: "map.styles", draggable: "true"} - %markers{models: "OfnMap.enterprises", fit: "true", + %markers{models: "mapMarkers", fit: "true", coords: "'self'", icon: "'icon'", click: "'reveal'"} %tab{heading: 'About us', @@ -50,7 +57,7 @@ .small-12.columns .active_table %producer.active_table_node.row.animate-repeat{id: "{{producer.path}}", - "ng-repeat" => "producer in filteredEnterprises = (Enterprises.producers | visible | searchEnterprises:query | taxons:activeTaxons)", + "ng-repeat" => "producer in filteredEnterprises = (group_producers | visible | searchEnterprises:query | taxons:activeTaxons)", "ng-controller" => "GroupEnterpriseNodeCtrl", "ng-class" => "{'closed' : !open(), 'open' : open(), 'inactive' : !producer.active}", id: "{{producer.hash}}"} @@ -77,7 +84,7 @@ .small-12.columns .active_table %hub.active_table_node.row.animate-repeat{id: "{{hub.hash}}", - "ng-repeat" => "hub in filteredEnterprises = (Enterprises.hubs | visible | searchEnterprises:query | taxons:activeTaxons | shipping:shippingTypes | showHubProfiles:show_profiles | orderBy:['-active', '+orders_close_at'])", + "ng-repeat" => "hub in filteredEnterprises = (group_hubs | visible | searchEnterprises:query | taxons:activeTaxons | shipping:shippingTypes | showHubProfiles:show_profiles | orderBy:['-active', '+orders_close_at'])", "ng-class" => "{'is_profile' : hub.category == 'hub_profile', 'closed' : !open(), 'open' : open(), 'inactive' : !hub.active, 'current' : current()}", "ng-controller" => "GroupEnterpriseNodeCtrl"} .small-12.columns