Decouple ofn-empties-cart directive from hub-choice navigation

This commit is contained in:
Rohan Mitchell
2015-07-29 16:14:46 +10:00
parent 09b4b3e659
commit fc409c97bf

View File

@@ -1,13 +1,19 @@
Darkswarm.directive "ofnEmptiesCart", (CurrentHub, Cart, Navigation, storage) ->
Darkswarm.directive "ofnEmptiesCart", (CurrentHub, Cart, storage) ->
# Compares scope.hub with CurrentHub. Will trigger an confirmation if they are different,
# and Cart isn't empty
restrict: "A"
scope:
hub: "=ofnEmptiesCart"
link: (scope, elm, attr)->
if CurrentHub.hub?.id and CurrentHub.hub.id isnt scope.hub.id and !Cart.empty()
link: (scope, elm, attr)->
cart_will_need_emptying = ->
CurrentHub.hub?.id and CurrentHub.hub.id isnt scope.hub.id and !Cart.empty()
clear_cart = ->
storage.clearAll() # One day this will have to be moar GRANULAR
if cart_will_need_emptying()
elm.bind 'click', (ev)->
ev.preventDefault()
if confirm "Are you sure? This will change your selected Hub and remove any items in your shopping cart."
storage.clearAll() # One day this will have to be moar GRANULAR
Navigation.go scope.hub.path
if confirm "Are you sure? This will change your selected hub and remove any items in your shopping cart."
clear_cart()
else
ev.preventDefault()