diff --git a/app/assets/javascripts/darkswarm/directives/change_order_cycle.js.coffee b/app/assets/javascripts/darkswarm/directives/change_order_cycle.js.coffee new file mode 100644 index 0000000000..a98b5508e8 --- /dev/null +++ b/app/assets/javascripts/darkswarm/directives/change_order_cycle.js.coffee @@ -0,0 +1,19 @@ +Darkswarm.directive "ofnChangeOrderCycle", (OrderCycle, Cart, storage) -> + # Compares chosen order cycle with pre-set OrderCycle. Will trigger + # a confirmation if they are different, and Cart isn't empty + restrict: "A" + scope: true + link: (scope, elm, attr)-> + order_cycle_id = -> + parseInt elm.val() + + cart_needs_emptying = -> + OrderCycle.order_cycle?.order_cycle_id && OrderCycle.order_cycle.order_cycle_id != order_cycle_id() && !Cart.empty() + + elm.bind 'change', (ev)-> + if cart_needs_emptying() + if confirm "Are you sure? This will change your selected order cycle and remove any items in your shopping cart." + Cart.clear() + scope.changeOrderCycle() + else + scope.changeOrderCycle() diff --git a/app/views/enterprises/shop.html.haml b/app/views/enterprises/shop.html.haml index 3af68f4a50..7ad617a4a1 100644 --- a/app/views/enterprises/shop.html.haml +++ b/app/views/enterprises/shop.html.haml @@ -16,7 +16,7 @@ / Will this label should be a variable to reflect 'Ready for pickup / delivery' as appropriate %select.avenir#order_cycle_id{"ng-model" => "order_cycle.order_cycle_id", - "ng-change" => "changeOrderCycle()", + "ofn-change-order-cycle" => true, "ng-options" => "oc.id as oc.time for oc in #{@order_cycles.map {|oc| {time: pickup_time(oc), id: oc.id}}.to_json}", "popover-placement" => "left", "popover" => "Choose when you want your order:", "popover-trigger" => "openTrigger"}