From 13c6ce678627b2e9e129da36ce72f736a9054078 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Fri, 30 May 2014 15:52:20 +1000 Subject: [PATCH] Starting a basic implementation of the Cart/CurrentOrder so we know whether it's empty --- .../darkswarm/controllers/cart_controller.js.coffee | 2 ++ .../darkswarm/directives/empties_cart.js.coffee | 7 +++---- .../darkswarm/services/current_order.js.coffee | 7 +++++++ app/views/json/_current_order.rabl | 13 +++++++++++++ app/views/layouts/darkswarm.html.haml | 1 + app/views/shared/menu/_cart.html.haml | 5 +++++ app/views/shared/menu/_large_menu.html.haml | 6 +----- app/views/shared/menu/_mobile_menu.html.haml | 6 +----- 8 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 app/assets/javascripts/darkswarm/controllers/cart_controller.js.coffee create mode 100644 app/assets/javascripts/darkswarm/services/current_order.js.coffee create mode 100644 app/views/json/_current_order.rabl create mode 100644 app/views/shared/menu/_cart.html.haml diff --git a/app/assets/javascripts/darkswarm/controllers/cart_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/cart_controller.js.coffee new file mode 100644 index 0000000000..fa60b0589c --- /dev/null +++ b/app/assets/javascripts/darkswarm/controllers/cart_controller.js.coffee @@ -0,0 +1,2 @@ +Darkswarm.controller "CartCtrl", ($scope, CurrentOrder) -> + $scope.CurrentOrder = CurrentOrder diff --git a/app/assets/javascripts/darkswarm/directives/empties_cart.js.coffee b/app/assets/javascripts/darkswarm/directives/empties_cart.js.coffee index 58595f9e24..b2e93b5c9b 100644 --- a/app/assets/javascripts/darkswarm/directives/empties_cart.js.coffee +++ b/app/assets/javascripts/darkswarm/directives/empties_cart.js.coffee @@ -1,10 +1,11 @@ -Darkswarm.directive "ofnEmptiesCart", (CurrentHub, Navigation) -> +Darkswarm.directive "ofnEmptiesCart", (CurrentHub, CurrentOrder, Navigation) -> restrict: "A" scope: hub: '=ofnEmptiesCart' template: "{{action}} {{hub.name}}" link: (scope, elm, attr)-> - if CurrentHub.id and CurrentHub.id isnt scope.hub.id + # A hub is selected, we're changing to a different hub, and the cart isn't empty + if CurrentHub.id and CurrentHub.id isnt scope.hub.id and not CurrentOrder.empty() scope.action = attr.change elm.bind 'click', (ev)-> ev.preventDefault() @@ -12,5 +13,3 @@ Darkswarm.directive "ofnEmptiesCart", (CurrentHub, Navigation) -> Navigation.go scope.hub.path else scope.action = attr.shop - - diff --git a/app/assets/javascripts/darkswarm/services/current_order.js.coffee b/app/assets/javascripts/darkswarm/services/current_order.js.coffee new file mode 100644 index 0000000000..6e329a38f9 --- /dev/null +++ b/app/assets/javascripts/darkswarm/services/current_order.js.coffee @@ -0,0 +1,7 @@ +Darkswarm.factory 'CurrentOrder', (currentOrder) -> + new class CurrentOrder + constructor: -> + @[k] = v for k, v of currentOrder + + empty: => + @line_items.length == 0 diff --git a/app/views/json/_current_order.rabl b/app/views/json/_current_order.rabl new file mode 100644 index 0000000000..3ea31c2137 --- /dev/null +++ b/app/views/json/_current_order.rabl @@ -0,0 +1,13 @@ +object current_order +attributes :id, :item_total + +if current_order + child line_items: :line_items do + attributes :id, :variant_id, :quantity, :price + end + + node :cart_count do + cart_count + end +end + diff --git a/app/views/layouts/darkswarm.html.haml b/app/views/layouts/darkswarm.html.haml index edeffafeed..a7598e24a5 100644 --- a/app/views/layouts/darkswarm.html.haml +++ b/app/views/layouts/darkswarm.html.haml @@ -16,6 +16,7 @@ %body.off-canvas{"ng-app" => "Darkswarm"} = inject_json "currentHub", "current_hub" + = inject_json "currentOrder", "current_order" = inject_json "user", "current_user" .off-canvas-wrap{offcanvas: true} diff --git a/app/views/shared/menu/_cart.html.haml b/app/views/shared/menu/_cart.html.haml new file mode 100644 index 0000000000..d2854affb5 --- /dev/null +++ b/app/views/shared/menu/_cart.html.haml @@ -0,0 +1,5 @@ +%a.icon{href: cart_url, "ng-controller" => "CartCtrl"} + %i.fi-shopping-cart.nav-branded + %span + {{ CurrentOrder.cart_count }} + items diff --git a/app/views/shared/menu/_large_menu.html.haml b/app/views/shared/menu/_large_menu.html.haml index 7a72b80f60..44c1e03903 100644 --- a/app/views/shared/menu/_large_menu.html.haml +++ b/app/views/shared/menu/_large_menu.html.haml @@ -43,8 +43,4 @@ %span.nav-primary.nav-branded {{ CurrentHub.name }} %li.divider %li.cart - %a.icon{href: cart_url} - %i.fi-shopping-cart.nav-branded - %span - = cart_count - items + = render partial: "shared/menu/cart" diff --git a/app/views/shared/menu/_mobile_menu.html.haml b/app/views/shared/menu/_mobile_menu.html.haml index 5e9f22c597..b01671e6c4 100644 --- a/app/views/shared/menu/_mobile_menu.html.haml +++ b/app/views/shared/menu/_mobile_menu.html.haml @@ -3,11 +3,7 @@ %a.left-off-canvas-toggle.menu-icon %span %section.right - %a.nav-branded.icon{href: cart_url} - %i.fi-shopping-cart - %span - = cart_count - items + = render partial: "shared/menu/cart" %a{href: main_app.shop_path} {{ CurrentHub.name }}