dry enterprise angular

This commit is contained in:
Rafael Schouten
2014-09-25 13:03:18 +10:00
parent 3775cd29cb
commit 0e2774882f
18 changed files with 76 additions and 149 deletions

View File

@@ -1,13 +1,15 @@
Darkswarm.controller "HubsCtrl", ($scope, Hubs, Search, $document, $rootScope, HashNavigation, FilterSelectorsService, MapModal) ->
$scope.Hubs = Hubs
$scope.hubs = Hubs.visible
Darkswarm.controller "EnterprisesCtrl", ($scope, Enterprises, Search, $document, $rootScope, HashNavigation, FilterSelectorsService, EnterpriseModal) ->
$scope.Enterprises = Enterprises
$scope.totalActive = FilterSelectorsService.totalActive
$scope.clearAll = FilterSelectorsService.clearAll
$scope.filterText = FilterSelectorsService.filterText
$scope.FilterSelectorsService = FilterSelectorsService
$scope.query = Search.search()
$scope.openModal = EnterpriseModal.open
$scope.activeTaxons = []
$scope.show_profiles = false
$scope.filtersActive = false
$scope.show_profiles = false
$scope.openModal = MapModal.open
$scope.$watch "query", (query)->
Search.search query

View File

@@ -1,14 +0,0 @@
Darkswarm.controller "ProducersCtrl", ($scope, Producers, $filter, FilterSelectorsService, Search, MapModal) ->
$scope.Producers = Producers
$scope.totalActive = FilterSelectorsService.totalActive
$scope.clearAll = FilterSelectorsService.clearAll
$scope.filterText = FilterSelectorsService.filterText
$scope.FilterSelectorsService = FilterSelectorsService
$scope.filtersActive = false
$scope.activeTaxons = []
$scope.query = Search.search()
$scope.show_profiles = false
$scope.openModal = MapModal.open
$scope.$watch "query", (query)->
Search.search query

View File

@@ -1,4 +1,4 @@
Darkswarm.directive "hubModal", ($modal)->
Darkswarm.directive "enterpriseModal", ($modal)->
restrict: 'E'
replace: true
template: "<a>{{enterprise.name}}</a>"

View File

@@ -1,10 +0,0 @@
Darkswarm.directive "producerModal", ($modal)->
restrict: 'E'
replace: true
template: "<a ng-transclude></a>"
transclude: true
link: (scope, elem, attrs, ctrl)->
elem.on "click", (ev)=>
ev.stopPropagation()
scope.modalInstance = $modal.open(controller: ctrl, templateUrl: 'producer_modal.html', scope: scope)

View File

@@ -0,0 +1,7 @@
Darkswarm.factory "EnterpriseModal", ($modal, $rootScope)->
new class EnterpriseModal
open: (enterprise)->
scope = $rootScope.$new(true) # Spawn an isolate to contain the enterprise
scope.enterprise = enterprise
$modal.open(templateUrl: "enterprise_modal.html", scope: scope)

View File

@@ -1,13 +1,17 @@
Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer)->
Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer, visibleFilter)->
new class Enterprises
enterprises_by_id: {} # id/object pairs for lookup
enterprises_by_id: {} # id/object pairs for lookup
constructor: ->
@enterprises = enterprises
@enterprises = visibleFilter enterprises
for enterprise in enterprises
@enterprises_by_id[enterprise.id] = enterprise
@dereferenceEnterprises()
@dereferenceTaxons()
@producers = @enterprises.filter (enterprise)->
enterprise.is_primary_producer
@hubs = @enterprises.filter (enterprise)->
enterprise.is_distributor
dereferenceEnterprises: ->
if CurrentHub.hub?.id
CurrentHub.hub = @enterprises_by_id[CurrentHub.hub.id]
@@ -16,6 +20,7 @@ Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer)
Dereferencer.dereference enterprise.producers, @enterprises_by_id
dereferenceTaxons: ->
for enterprise in @enterprises
for enterprise in @enterprises
Dereferencer.dereference enterprise.taxons, Taxons.taxons_by_id
Dereferencer.dereference enterprise.supplied_taxons, Taxons.taxons_by_id

View File

@@ -1,9 +0,0 @@
Darkswarm.factory 'Hubs', ($filter, Enterprises, visibleFilter) ->
new class Hubs
constructor: ->
@hubs = @order Enterprises.enterprises.filter (hub)->
hub.is_distributor
@visible = visibleFilter @hubs
order: (hubs)->
$filter('orderBy')(hubs, ['-active', '+orders_close_at'])

View File

@@ -1,7 +1,7 @@
Darkswarm.factory "OfnMap", (Enterprises, MapModal, visibleFilter)->
Darkswarm.factory "OfnMap", (Enterprises, EnterpriseModal, visibleFilter)->
new class OfnMap
constructor: ->
@enterprises = (@extend(enterprise) for enterprise in visibleFilter(Enterprises.enterprises))
@enterprises = (@extend(enterprise) for enterprise in visibleFilter(Enterprises.enterprises))
# Adding methods to each enterprise
@@ -14,4 +14,4 @@ Darkswarm.factory "OfnMap", (Enterprises, MapModal, visibleFilter)->
icon: enterprise.icon
id: enterprise.id
reveal: =>
MapModal.open enterprise
EnterpriseModal.open enterprise

View File

@@ -1,12 +0,0 @@
Darkswarm.factory "MapModal", ($modal, $rootScope)->
new class MapModal
open: (enterprise)->
scope = $rootScope.$new(true) # Spawn an isolate to contain the enterprise
scope.enterprise = enterprise
if enterprise.is_distributor
scope.hub = enterprise
$modal.open(templateUrl: "hub_modal.html", scope: scope)
else
scope.producer = enterprise
$modal.open(templateUrl: "map_modal_producer.html", scope: scope)

View File

@@ -1,7 +0,0 @@
Darkswarm.factory 'Producers', (Enterprises, visibleFilter) ->
new class Producers
constructor: ->
@producers = Enterprises.enterprises.filter (enterprise)->
enterprise.is_primary_producer
@visible = visibleFilter @producers

View File

@@ -1,4 +0,0 @@
%ng-include{src: "'partials/enterprise_header.html'"}
%ng-include{src: "'partials/enterprise_details.html'"}
%ng-include{src: "'partials/hub_actions.html'"}
%ng-include{src: "'partials/close.html'"}

View File

@@ -1,3 +0,0 @@
%ng-include{src: "'partials/enterprise_header.html'"}
%ng-include{src: "'partials/enterprise_details.html'"}
%ng-include{src: "'partials/close.html'"}

View File

@@ -1,5 +1,5 @@
= inject_enterprises
#hubs.hubs{"ng-controller" => "HubsCtrl"}
#hubs.hubs{"ng-controller" => "EnterprisesCtrl"}
.row
.small-12.columns
%h1 Shop in your local area
@@ -17,7 +17,7 @@
.row{bindonce: true}
.small-12.columns
.active_table
%hub.active_table_node.row.animate-repeat{"ng-repeat" => "hub in filteredHubs = (hubs | searchEnterprises:query | taxons:activeTaxons | shipping:shippingTypes | showProfiles:show_profiles )",
%hub.active_table_node.row.animate-repeat{"ng-repeat" => "hub in hubs = (Enterprises.hubs | searchEnterprises:query | taxons:activeTaxons | shipping:shippingTypes | showProfiles:show_profiles | orderBy:['-active', '+orders_close_at'])",
"ng-class" => "{'closed' : !open(), 'open' : open(), 'inactive' : !hub.active, 'current' : current()}",
"scroll-after-load" => true,
"ng-controller" => "HubNodeCtrl",

View File

@@ -1,5 +1,5 @@
= inject_enterprises
.producers.pad-top{"ng-controller" => "ProducersCtrl"}
.producers.pad-top{"ng-controller" => "EnterprisesCtrl"}
.row
.small-12.columns.pad-top
%h1 Find local producers
@@ -24,7 +24,7 @@
.active_table
%producer.active_table_node.row.animate-repeat{id: "{{producer.path}}",
"scroll-after-load" => true,
"ng-repeat" => "producer in producers = (Producers.visible | showProfiles:show_profiles | searchEnterprises:query | taxons:activeTaxons)",
"ng-repeat" => "producer in producers = (Enterprises.producers | showProfiles:show_profiles | searchEnterprises:query | taxons:activeTaxons)",
"ng-controller" => "ProducerNodeCtrl",
"ng-class" => "{'closed' : !open(), 'open' : open(), 'inactive' : !producer.active}",
id: "{{producer.hash}}"}

View File

@@ -1,45 +0,0 @@
describe 'filtering Hubs', ->
filter = null
filterHubs = null
hubs = [{
name: "frogs"
other: "roger"
address:
zipcode: "cats"
city: "cambridge"
state: "kansas"
}, {
name: "donkeys"
other: "roger"
address:
zipcode: ""
city: "Wellington"
state: "uzbekistan"
}]
beforeEach ->
module 'Darkswarm'
inject ($filter) ->
filter = $filter
filterHubs = $filter('hubs')
it 'has a hub filter', ->
expect(filter('hubs')).not.toBeNull()
it "filters by name", ->
expect(filterHubs(hubs, 'donkeys').length).toEqual 1
it "is case insensitive", ->
expect(filterHubs(hubs, 'DONKEYS').length).toEqual 1
it "filters by state", ->
expect(filterHubs(hubs, 'kansas').length).toEqual 1
it "filters by zipcode", ->
expect(filterHubs(hubs, 'cats').length).toEqual 1
it "gives all hubs when no argument is specified", ->
expect(filterHubs(hubs, '').length).toEqual 2
it "does not filter by anything else", ->
expect(filterHubs(hubs, 'roger').length).toEqual 0

View File

@@ -1,28 +0,0 @@
describe 'filtering producers', ->
filter = null
filterProducers = null
producers = [{
name: "frogs"
other: "roger"
address:
zipcode: "cats"
city: "cambridge"
state: "kansas"
}, {
name: "donkeys"
other: "roger"
address:
zipcode: ""
city: "Wellington"
state: "uzbekistan"
}]
beforeEach ->
module 'Darkswarm'
inject ($filter) ->
filter = $filter
filterProducers = $filter('filterProducers')
it 'has a producer filter', ->
expect(filter('filterProducers')).not.toBeNull()

View File

@@ -0,0 +1,45 @@
describe 'filtering Enterprises', ->
filter = null
searchEnterprises = null
enterprises = [{
name: "frogs"
other: "roger"
address:
zipcode: "cats"
city: "cambridge"
state: "kansas"
}, {
name: "donkeys"
other: "roger"
address:
zipcode: ""
city: "Wellington"
state: "uzbekistan"
}]
beforeEach ->
module 'Darkswarm'
inject ($filter) ->
filter = $filter
searchEnterprises = $filter('enterprises')
it 'has a enterprise filter', ->
expect(filter('enterprises')).not.toBeNull()
it "filters by name", ->
expect(searchEnterprises(enterprises, 'donkeys').length).toEqual 1
it "is case insensitive", ->
expect(searchEnterprises(enterprises, 'DONKEYS').length).toEqual 1
it "filters by state", ->
expect(searchEnterprises(enterprises, 'kansas').length).toEqual 1
it "filters by zipcode", ->
expect(searchEnterprises(enterprises, 'cats').length).toEqual 1
it "gives all enterprises when no argument is specified", ->
expect(searchEnterprises(enterprises, '').length).toEqual 2
it "does not filter by anything else", ->
expect(searchEnterprises(enterprises, 'roger').length).toEqual 0