Merge branch 'master' into fix_units

Conflicts:
	app/assets/javascripts/admin/products/units_controller.js.coffee
This commit is contained in:
Rafael Schouten
2014-07-14 15:10:26 +01:00
117 changed files with 1296 additions and 446 deletions

View File

@@ -1,3 +1,3 @@
angular.module("ofn.admin", ["ngResource", "ngAnimate", "ofn.dropdown"]).config ($httpProvider) ->
angular.module("ofn.admin", ["ngResource", "ngAnimate", "ofn.dropdown", "admin.products"]).config ($httpProvider) ->
$httpProvider.defaults.headers.common["X-CSRF-Token"] = $("meta[name=csrf-token]").attr("content")
$httpProvider.defaults.headers.common["Accept"] = "application/json, text/javascript, */*"

View File

@@ -17,7 +17,7 @@ angular.module("ofn.admin").directive "ofnDisplayAs", (optionValueNamer) ->
variant_unit_scale: variant_unit_scale
variant_unit: variant_unit
variant_unit_name: scope.product.variant_unit_name
scope.placeholder_text = new optionValueNamer(variant_object).name()
productUnitProperties = ->

View File

@@ -1,10 +1,10 @@
angular.module("admin.products")
.controller "unitsCtrl", ($scope) ->
.controller "unitsCtrl", ($scope, optionValueNamer) ->
$scope.product = { master: {} }
$scope.product.master.product = $scope.product
$scope.placeholder_text = ""
$scope.$watch ->
$scope.product.variant_unit_with_scale
, ->
$scope.$watchCollection '[product.variant_unit_with_scale, product.master.unit_value_with_description]', ->
if $scope.product.variant_unit_with_scale
match = $scope.product.variant_unit_with_scale.match(/^([^_]+)_([\d\.]+)$/)
if match
@@ -16,9 +16,6 @@ angular.module("admin.products")
else
$scope.product.variant_unit = $scope.product.variant_unit_scale = null
$scope.$watch ->
$scope.product.master.unit_value_with_description
, ->
if $scope.product.master.hasOwnProperty("unit_value_with_description")
match = $scope.product.master.unit_value_with_description.match(/^([\d\.]+(?= |$)|)( |)(.*)$/)
if match
@@ -27,6 +24,8 @@ angular.module("admin.products")
$scope.product.master.unit_value *= $scope.product.variant_unit_scale if $scope.product.master.unit_value && $scope.product.variant_unit_scale
$scope.product.master.unit_description = match[3]
$scope.placeholder_text = new optionValueNamer($scope.product.master).name()
$scope.variant_unit_options = [
["Weight (g)", "weight_1"],
["Weight (kg)", "weight_1000"],
@@ -42,5 +41,3 @@ angular.module("admin.products")
$scope.hasUnit = (product) ->
product.variant_unit_with_scale?

View File

@@ -1,4 +1,4 @@
angular.module("ofn.admin").factory "optionValueNamer", ($resource) ->
angular.module("admin.products").factory "optionValueNamer", ->
class OptionValueNamer
constructor: (@variant) ->

View File

@@ -11,6 +11,7 @@
#= require lodash.underscore.js
#= require angular-scroll.min.js
#= require angular-google-maps.min.js
#= require angular-timer.min.js
#= require ../shared/mm-foundation-tpls-0.2.2.min.js
#= require ../shared/bindonce.min.js
#= require ../shared/ng-infinite-scroll.min.js

View File

@@ -1,6 +1,14 @@
Darkswarm.controller "HubsCtrl", ($scope, Hubs, $document, $rootScope, HashNavigation) ->
Darkswarm.controller "HubsCtrl", ($scope, Hubs, Search, $document, $rootScope, HashNavigation, FilterSelectorsService) ->
$scope.Hubs = Hubs
$scope.hubs = Hubs.visible
$scope.totalActive = FilterSelectorsService.totalActive
$scope.clearAll = FilterSelectorsService.clearAll
$scope.filterText = FilterSelectorsService.filterText
$scope.FilterSelectorsService = FilterSelectorsService
$scope.query = Search.search()
$scope.$watch "query", (query)->
Search.search query
$rootScope.$on "$locationChangeSuccess", (newRoute, oldRoute) ->
if HashNavigation.active "hubs"

View File

@@ -1,2 +1,12 @@
Darkswarm.controller "ProducersCtrl", ($scope, Producers) ->
Darkswarm.controller "ProducersCtrl", ($scope, Producers, $filter, FilterSelectorsService, Search) ->
$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.$watch "query", (query)->
Search.search query

View File

@@ -1,11 +1,6 @@
Darkswarm.controller "ProductNodeCtrl", ($scope) ->
$scope.price = ->
if $scope.product.variants.length > 0
prices = (v.price for v in $scope.product.variants)
Math.min.apply(null, prices)
else
$scope.product.price
Darkswarm.controller "ProductNodeCtrl", ($scope, $modal) ->
$scope.enterprise = $scope.product.supplier # For the modal, so it's consistent
$scope.hasVariants = $scope.product.variants.length > 0
$scope.triggerProductModal = ->
$modal.open(templateUrl: "product_modal.html", scope: $scope)

View File

@@ -1,5 +1,9 @@
Darkswarm.controller "ProductsCtrl", ($scope, $rootScope, Product, OrderCycle) ->
Darkswarm.controller "ProductsCtrl", ($scope, $rootScope, Product, OrderCycle, FilterSelectorsService) ->
$scope.Product = Product
$scope.totalActive = FilterSelectorsService.totalActive
$scope.clearAll = FilterSelectorsService.clearAll
$scope.filterText = FilterSelectorsService.filterText
$scope.FilterSelectorsService = FilterSelectorsService
$scope.limit = 3
$scope.ordering = {order: "name"}
$scope.order_cycle = OrderCycle.order_cycle

View File

@@ -5,7 +5,9 @@ window.Darkswarm = angular.module("Darkswarm", ["ngResource",
'infinite-scroll',
'angular-flash.service',
'templates',
'timer',
'ngSanitize',
'ngAnimate',
'google-maps',
'duScroll',
]).config ($httpProvider, $tooltipProvider, $locationProvider, $anchorScrollProvider) ->

View File

@@ -0,0 +1,12 @@
Darkswarm.directive "activeSelector", ->
restrict: 'E'
transclude: true
replace: true
templateUrl: 'active_selector.html'
link: (scope, elem, attr)->
scope.selector.emit = scope.emit
elem.bind "click", ->
scope.$apply ->
scope.selector.active = !scope.selector.active
scope.emit()

View File

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

View File

@@ -0,0 +1,19 @@
Darkswarm.directive "shippingTypeSelector", (FilterSelectorsService)->
restrict: 'E'
replace: true
templateUrl: 'shipping_type_selector.html'
link: (scope, elem, attr)->
scope.shippingTypes =
pickup: false
delivery: false
scope.selectors =
delivery: FilterSelectorsService.new
icon: "ofn-i_039-delivery"
pickup: FilterSelectorsService.new
icon: "ofn-i_038-takeaway"
scope.emit = ->
scope.shippingTypes =
pickup: scope.selectors.pickup.active
delivery: scope.selectors.delivery.active

View File

@@ -0,0 +1,36 @@
Darkswarm.directive "taxonSelector", (FilterSelectorsService)->
restrict: 'E'
replace: true
scope:
objects: "&"
results: "="
templateUrl: "taxon_selector.html"
link: (scope, elem, attr)->
selectors_by_id = {}
selectors = ["foo"]
scope.emit = ->
scope.results = selectors.filter (selector)->
selector.active
.map (selector)->
selector.taxon.id
scope.selectors = ->
taxons = {}
selectors = []
for object in scope.objects()
for taxon in object.taxons
taxons[taxon.id] = taxon
if object.supplied_taxons
for taxon in 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] = FilterSelectorsService.new
taxon: taxon
selectors.push selector
selectors

View File

@@ -0,0 +1,14 @@
Darkswarm.filter 'active', ()->
(objects, options)->
objects ||= []
options ?= null
if options.open and !options.closed
objects.filter (obj)->
obj.active
else if options.closed and !options.open
objects.filter (obj)->
!obj.active
else
objects

View File

@@ -0,0 +1,6 @@
Darkswarm.filter "byProducer", ->
(objects, id) ->
objects ||= []
id ?= 0
objects.filter (obj)->
obj.producer.id == id

View File

@@ -0,0 +1,4 @@
Darkswarm.filter "capitalize", ->
(input, scope) ->
input = input.toLowerCase() if input?
input.substring(0, 1).toUpperCase() + input.substring(1)

View File

@@ -2,5 +2,5 @@ Darkswarm.filter 'filterProducers', (hubsFilter)->
(producers, text) ->
producers ||= []
text ?= ""
hubsFilter(producers, text)

View File

@@ -0,0 +1,13 @@
Darkswarm.filter 'shipping', ()->
(objects, options)->
objects ||= []
options ?= null
if options.pickup and !options.delivery
objects.filter (obj)->
obj.pickup
else if options.delivery and !options.pickup
objects.filter (obj)->
obj.delivery
else
objects

View File

@@ -0,0 +1,13 @@
Darkswarm.filter 'taxons', (Matcher)->
# Filter anything that responds to object.taxons, and/or object.primary_taxon
(objects, ids) ->
objects ||= []
ids ?= []
if ids.length == 0
objects
else
objects.filter (obj)->
taxons = obj.taxons
taxons.concat obj.supplied_taxons if obj.supplied_taxons
obj.primary_taxon?.id in ids || taxons.some (taxon)->
taxon.id in ids

View File

@@ -1,15 +1,21 @@
Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Dereferencer)->
Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer)->
new class Enterprises
enterprises_by_id: {} # id/object pairs for lookup
constructor: ->
@enterprises = enterprises
for enterprise in enterprises
@enterprises_by_id[enterprise.id] = enterprise
@dereference()
@dereferenceEnterprises()
@dereferenceTaxons()
dereference: ->
dereferenceEnterprises: ->
if CurrentHub.hub?.id
CurrentHub.hub = @enterprises_by_id[CurrentHub.hub.id]
for enterprise in @enterprises
Dereferencer.dereference enterprise.hubs, @enterprises_by_id
Dereferencer.dereference enterprise.producers, @enterprises_by_id
dereferenceTaxons: ->
for enterprise in @enterprises
Dereferencer.dereference enterprise.taxons, Taxons.taxons_by_id
Dereferencer.dereference enterprise.supplied_taxons, Taxons.taxons_by_id

View File

@@ -0,0 +1,28 @@
Darkswarm.factory "FilterSelectorsService", ->
# This stores all filters so we can access in-use counts etc
# Accessed via activeSelector Directive
new class FilterSelectorsService
selectors: []
new: (obj = {})->
obj.active = false
@selectors.push obj
obj
totalActive: =>
@selectors.filter (selector)->
selector.active
.length
filterText: (active)=>
total = @totalActive()
if total == 0
if active then "Hide filters" else "Filter by"
else if total == 1
"1 filter applied"
else
"#{@totalActive()} filters applied"
clearAll: =>
for selector in @selectors
selector.active = false
selector.emit()

View File

@@ -1,5 +1,5 @@
Darkswarm.factory 'Product', ($resource, Enterprises) ->
new class Product
Darkswarm.factory 'Product', ($resource, Enterprises, Dereferencer, Taxons) ->
new class Products
constructor: ->
@update()
@@ -10,7 +10,8 @@ Darkswarm.factory 'Product', ($resource, Enterprises) ->
update: =>
@loading = true
@products = $resource("/shop/products").query =>
@products = $resource("/shop/products").query (products)=>
@extend()
@dereference()
@loading = false
@
@@ -18,3 +19,12 @@ Darkswarm.factory 'Product', ($resource, Enterprises) ->
dereference: ->
for product in @products
product.supplier = Enterprises.enterprises_by_id[product.supplier.id]
Dereferencer.dereference product.taxons, Taxons.taxons_by_id
extend: ->
for product in @products
if product.variants.length > 0
prices = (v.price for v in product.variants)
product.price = Math.min.apply(null, prices)
product.hasVariants = product.variants.length > 0

View File

@@ -0,0 +1,9 @@
Darkswarm.factory "Search", ($location)->
new class Search
search: (query = false)->
if query
$location.search('query', query)
else if query == ""
$location.search('query', null)
else
$location.search()['query']

View File

@@ -0,0 +1,8 @@
Darkswarm.factory "Taxons", (taxons)->
new class Taxons
taxons: taxons
taxons_by_id: {}
constructor: ->
for taxon in @taxons
@taxons_by_id[taxon.id] = taxon

View File

@@ -0,0 +1,2 @@
%li{"ng-class" => "{active: selector.active}"}
%a{"ng-transclude" => true}

View File

@@ -0,0 +1,3 @@
%active-selector{"ng-repeat" => "(name, selector) in selectors"}
%i{"ng-class" => "selector.icon"}
{{ name | capitalize }}

View File

@@ -0,0 +1,3 @@
%active-selector{"ng-repeat" => "selector in selectors()"}
%render-svg{path: "{{selector.taxon.icon}}"}
%span {{ selector.taxon.name }}

View File

@@ -1 +0,0 @@
Frogs