From 63e4430ea4fd576196a1984c5be8dad0aba44283 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Fri, 15 May 2020 13:36:10 +0200 Subject: [PATCH] Update cart page links to be disabled when changes are required and form is unsaved See: https://stackoverflow.com/questions/23425254/enable-disable-anchor-tags-using-angularjs --- .../directives/disable_dynamically.js.coffee | 12 ++++++++++++ app/views/spree/orders/form/_cart_links.html.haml | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 app/assets/javascripts/darkswarm/directives/disable_dynamically.js.coffee diff --git a/app/assets/javascripts/darkswarm/directives/disable_dynamically.js.coffee b/app/assets/javascripts/darkswarm/directives/disable_dynamically.js.coffee new file mode 100644 index 0000000000..7cff41f3f4 --- /dev/null +++ b/app/assets/javascripts/darkswarm/directives/disable_dynamically.js.coffee @@ -0,0 +1,12 @@ +# Allows disabling of link buttons via disabled attribute. +# This is normally ignored, ie the link appears disabled but is still clickable. + +Darkswarm.directive "disableDynamically", -> + restrict: 'A' + + link: (scope, element, attrs) -> + element.on 'click', (e) -> + if attrs.disabled + e.preventDefault() + return + diff --git a/app/views/spree/orders/form/_cart_links.html.haml b/app/views/spree/orders/form/_cart_links.html.haml index 8da012cd78..d63d9395ac 100644 --- a/app/views/spree/orders/form/_cart_links.html.haml +++ b/app/views/spree/orders/form/_cart_links.html.haml @@ -1,5 +1,5 @@ .row.links{'data-hook' => "cart_buttons"} - %a.button.large.secondary{href: current_shop_products_path} + %a.button.large.secondary{href: current_shop_products_path, "ng-disabled" => "#{@insufficient_stock_lines.any?}", "disable-dynamically" => true} = t :orders_edit_continue - %a#checkout-link.button.large.primary.right{href: main_app.checkout_path} + %a#checkout-link.button.large.primary.right{href: main_app.checkout_path, "ng-disabled" => "#{@insufficient_stock_lines.any?}", "disable-dynamically" => true} = t :orders_edit_checkout