From fb8d7907f918bfed1dd1e85ea519245addd799d3 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Wed, 9 Jul 2014 14:07:38 +1000 Subject: [PATCH] Rearchitecting the thing --- .../producers_controller.js.coffee | 14 ++------ .../directives/active_selector.js.coffee | 1 + .../directives/taxon_selector.js.coffee | 32 +++++++++++++++++-- .../selectors/taxon_selector.js.coffee | 27 ---------------- .../templates/taxon_selector.html.haml | 2 +- .../stylesheets/darkswarm/animations.sass | 4 +-- app/views/producers/_filters.html.haml | 3 +- app/views/producers/index.html.haml | 2 +- 8 files changed, 40 insertions(+), 45 deletions(-) delete mode 100644 app/assets/javascripts/darkswarm/services/selectors/taxon_selector.js.coffee diff --git a/app/assets/javascripts/darkswarm/controllers/producers_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/producers_controller.js.coffee index 7d31c06000..5da0fc7912 100644 --- a/app/assets/javascripts/darkswarm/controllers/producers_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/producers_controller.js.coffee @@ -1,12 +1,4 @@ -Darkswarm.controller "ProducersCtrl", ($scope, Producers, TaxonSelector, $filter) -> +Darkswarm.controller "ProducersCtrl", ($scope, Producers, $filter) -> $scope.Producers = Producers - $scope.TaxonSelector = TaxonSelector - $scope.filtersActive = false - $scope.oldFiltered = [] - - $scope.filteredProducers = -> - filtered = $filter("filterProducers")(Producers.visible, $scope.query) - if $scope.oldFiltered != filtered - $scope.oldFiltered = filtered - TaxonSelector.collectTaxons filtered - filtered + $scope.filtersActive = true + $scope.activeTaxons = [] diff --git a/app/assets/javascripts/darkswarm/directives/active_selector.js.coffee b/app/assets/javascripts/darkswarm/directives/active_selector.js.coffee index fbbca3a88b..803c83eee3 100644 --- a/app/assets/javascripts/darkswarm/directives/active_selector.js.coffee +++ b/app/assets/javascripts/darkswarm/directives/active_selector.js.coffee @@ -7,3 +7,4 @@ Darkswarm.directive "activeSelector", -> elem.bind "click", -> scope.$apply -> scope.selector.active = !scope.selector.active + scope.emit() diff --git a/app/assets/javascripts/darkswarm/directives/taxon_selector.js.coffee b/app/assets/javascripts/darkswarm/directives/taxon_selector.js.coffee index 89d2a4d4ec..888d17d6ec 100644 --- a/app/assets/javascripts/darkswarm/directives/taxon_selector.js.coffee +++ b/app/assets/javascripts/darkswarm/directives/taxon_selector.js.coffee @@ -1,5 +1,33 @@ -Darkswarm.directive "taxonSelector", (TaxonSelector) -> +Darkswarm.directive "taxonSelector", -> restrict: 'E' + scope: + objects: "&" + results: "=" templateUrl: "taxon_selector.html" + link: (scope, elem, attr)-> - scope.TaxonSelector = TaxonSelector + selectors_by_id = {} + selectors = ["foo"] + + scope.emit = -> + scope.results = selectors.filter (selector)-> + selector.active + .map (selector)-> + selector.taxon.id + + scope.selectors = -> + console.log "selectoring" + taxons = {} + selectors = [] + for object in scope.objects() + for taxon in (object.taxons.concat object?.supplied_taxons) + taxons[taxon.id] = taxon + for id, taxon of taxons + if selector = selectors_by_id[id] + selectors.push selector + else + selector = selectors_by_id[id] = + active: false + taxon: taxon + selectors.push selector + selectors diff --git a/app/assets/javascripts/darkswarm/services/selectors/taxon_selector.js.coffee b/app/assets/javascripts/darkswarm/services/selectors/taxon_selector.js.coffee deleted file mode 100644 index f9d7bfebff..0000000000 --- a/app/assets/javascripts/darkswarm/services/selectors/taxon_selector.js.coffee +++ /dev/null @@ -1,27 +0,0 @@ -Darkswarm.factory 'TaxonSelector', -> - new class TaxonSelector - selectors: [] - selectors_by_id: {} - # Collect all taxons on the supplied enterprises - collectTaxons: (enterprises)-> - taxons = {} - @selectors = [] - selectors = [] - for enterprise in enterprises - for taxon in (enterprise.taxons.concat enterprise.supplied_taxons) - taxons[taxon.id] = taxon - for id, taxon of taxons - if @selectors_by_id[id] - selectors.push @selectors_by_id[id] - else - @selectors_by_id[id] = - active: false - taxon: taxon - selectors.push @selectors_by_id[id] - @selectors = selectors - - active: -> - @selectors.filter (selector)-> - selector.active - .map (selector)-> - selector.taxon.id diff --git a/app/assets/javascripts/templates/taxon_selector.html.haml b/app/assets/javascripts/templates/taxon_selector.html.haml index 6e376a4ae2..e26b36c64a 100644 --- a/app/assets/javascripts/templates/taxon_selector.html.haml +++ b/app/assets/javascripts/templates/taxon_selector.html.haml @@ -1,4 +1,4 @@ %ul.small-block-grid-4 - %active-selector{"ng-repeat" => "selector in TaxonSelector.selectors"} + %active-selector{"ng-repeat" => "selector in selectors()"} %render-svg{path: "{{selector.taxon.icon}}"} %span {{ selector.taxon.name }} diff --git a/app/assets/stylesheets/darkswarm/animations.sass b/app/assets/stylesheets/darkswarm/animations.sass index 96bd5b19ae..3fcbb41202 100644 --- a/app/assets/stylesheets/darkswarm/animations.sass +++ b/app/assets/stylesheets/darkswarm/animations.sass @@ -91,8 +91,8 @@ .animate-repeat.ng-move, .animate-repeat.ng-enter, .animate-repeat.ng-leave - -webkit-transition: all linear 0.5s - transition: all linear 0.5s + -webkit-transition: all linear 0.2s + transition: all linear 0.2s .animate-repeat.ng-leave.ng-leave-active, .animate-repeat.ng-move, diff --git a/app/views/producers/_filters.html.haml b/app/views/producers/_filters.html.haml index a67ec630e0..3cdb4e3e75 100644 --- a/app/views/producers/_filters.html.haml +++ b/app/views/producers/_filters.html.haml @@ -20,7 +20,8 @@ Something .row.filter-box .small-12.large-6.columns - %taxon-selector{enterprises: "Producers.visible"} + %taxon-selector{objects: "Producers.visible | filterProducers:query", + results: "activeTaxons"} .small-12.large-6.columns %shipping-type-selector diff --git a/app/views/producers/index.html.haml b/app/views/producers/index.html.haml index 876f5a986b..15b02f955f 100644 --- a/app/views/producers/index.html.haml +++ b/app/views/producers/index.html.haml @@ -27,7 +27,7 @@ .active_table %producer.active_table_node.row.animate-repeat{id: "{{producer.path}}", "scroll-after-load" => true, - "ng-repeat" => "producer in veryFilteredProducers = (filteredProducers() | taxons:TaxonSelector.active() | shipping:shippingTypes)", + "ng-repeat" => "producer in producers = (Producers.visible | filterProducers:query | taxons:activeTaxons | shipping:shippingTypes)", "ng-controller" => "ProducerNodeCtrl", "ng-class" => "{'closed' : !open(), 'open' : open(), 'inactive' : !producer.active}", id: "{{producer.hash}}"}