diff --git a/app/assets/javascripts/darkswarm/controllers/checkout/country_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/checkout/country_controller.js.coffee new file mode 100644 index 0000000000..347fa367ad --- /dev/null +++ b/app/assets/javascripts/darkswarm/controllers/checkout/country_controller.js.coffee @@ -0,0 +1,8 @@ +Darkswarm.controller "CountryCtrl", ($scope, availableCountries) -> + + $scope.countries = availableCountries + + $scope.countriesById = $scope.countries.reduce (obj, country) -> + obj[country.id] = country + obj + , {} diff --git a/app/helpers/checkout_helper.rb b/app/helpers/checkout_helper.rb index a874d996d9..bc885bd5dc 100644 --- a/app/helpers/checkout_helper.rb +++ b/app/helpers/checkout_helper.rb @@ -66,20 +66,6 @@ module CheckoutHelper Spree::Money.new order.total - order.total_tax, currency: order.currency end - def checkout_state_options(source_address) - if source_address == :billing - address = @order.billing_address - elsif source_address == :shipping - address = @order.shipping_address - end - - [[]] + address.country.states.map { |c| [c.name, c.id] } - end - - def checkout_country_options - available_countries.map { |c| [c.name, c.id] } - end - def validated_input(name, path, args = {}) attributes = { required: true, diff --git a/app/views/checkout/_billing.html.haml b/app/views/checkout/_billing.html.haml index bd590d63a5..07b0d0f251 100644 --- a/app/views/checkout/_billing.html.haml +++ b/app/views/checkout/_billing.html.haml @@ -19,25 +19,26 @@ %input{type: :checkbox, "ng-model" => "Checkout.default_bill_address"} = t :checkout_default_bill_address - = f.fields_for :bill_address, @order.bill_address do |ba| - .row - .small-12.columns - = validated_input t(:address), "order.bill_address.address1", "ofn-focus" => "accordion['billing']" - .row - .small-12.columns - = validated_input t(:address2), "order.bill_address.address2", required: false - .row - .small-6.columns - = validated_input t(:city), "order.bill_address.city" + %div{ "ng-controller" => "CountryCtrl" } + = f.fields_for :bill_address, @order.bill_address do |ba| + .row + .small-12.columns + = validated_input t(:address), "order.bill_address.address1", "ofn-focus" => "accordion['billing']" + .row + .small-12.columns + = validated_input t(:address2), "order.bill_address.address2", required: false + .row + .small-6.columns + = validated_input t(:city), "order.bill_address.city" - .small-6.columns - = validated_select t(:state), "order.bill_address.state_id", checkout_state_options(:billing) - .row - .small-6.columns - = validated_input t(:postcode), "order.bill_address.zipcode" + .small-6.columns + = validated_select t(:state), "order.bill_address.state_id", {}, {"ng-options" => "s.id as s.name for s in countriesById[order.bill_address.country_id].states"} + .row + .small-6.columns + = validated_input t(:postcode), "order.bill_address.zipcode" - .small-6.columns.right - = validated_select t(:country), "order.bill_address.country_id", checkout_country_options + .small-6.columns.right + = validated_select t(:country), "order.bill_address.country_id", {}, {"ng-init" => "order.bill_address.country_id = order.bill_address.country_id || #{Spree::Config[:default_country_id]}", "ng-options" => "c.id as c.name for c in countries"} .row .small-12.columns.text-right diff --git a/app/views/checkout/_shipping_ship_address.html.haml b/app/views/checkout/_shipping_ship_address.html.haml index 7b4db0d54c..1119886a47 100644 --- a/app/views/checkout/_shipping_ship_address.html.haml +++ b/app/views/checkout/_shipping_ship_address.html.haml @@ -1,28 +1,28 @@ .small-12.columns #ship_address{"ng-if" => "Checkout.requireShipAddress()"} %div.visible{"ng-if" => "!Checkout.ship_address_same_as_billing"} - .row - .small-6.columns - = validated_input t(:first_name), "order.ship_address.firstname", "ofn-focus" => "accordion['shipping']" - .small-6.columns - = validated_input t(:last_name), "order.ship_address.lastname" - .row - .small-12.columns - = validated_input t(:address), "order.ship_address.address1" - .row - .small-12.columns - = validated_input t(:address2), "order.ship_address.address2", required: false - .row - .small-6.columns - = validated_input t(:city), "order.ship_address.city" - .small-6.columns - = validated_select t(:state), "order.ship_address.state_id", checkout_state_options(:shipping) - .row - .small-6.columns - = validated_input t(:postcode), "order.ship_address.zipcode" - .small-6.columns.right - = validated_select t(:country), "order.ship_address.country_id", checkout_country_options - - .row - .small-6.columns - = validated_input t(:phone), "order.ship_address.phone" + %div{ "ng-controller" => "CountryCtrl" } + .row + .small-6.columns + = validated_input t(:first_name), "order.ship_address.firstname", "ofn-focus" => "accordion['shipping']" + .small-6.columns + = validated_input t(:last_name), "order.ship_address.lastname" + .row + .small-12.columns + = validated_input t(:address), "order.ship_address.address1" + .row + .small-12.columns + = validated_input t(:address2), "order.ship_address.address2", required: false + .row + .small-6.columns + = validated_input t(:city), "order.ship_address.city" + .small-6.columns + = validated_select t(:state), "order.ship_address.state_id", {}, {"ng-options" => "s.id as s.name for s in countriesById[order.ship_address.country_id].states"} + .row + .small-6.columns + = validated_input t(:postcode), "order.ship_address.zipcode" + .small-6.columns.right + = validated_select t(:country), "order.ship_address.country_id", {}, {"ng-init" => "order.ship_address.country_id = order.ship_address.country_id || #{Spree::Config[:default_country_id]}", "ng-options" => "c.id as c.name for c in countries"} + .row + .small-6.columns + = validated_input t(:phone), "order.ship_address.phone" diff --git a/app/views/checkout/edit.html.haml b/app/views/checkout/edit.html.haml index b3d30ed442..75c9eff08f 100644 --- a/app/views/checkout/edit.html.haml +++ b/app/views/checkout/edit.html.haml @@ -2,6 +2,7 @@ = t :checkout_title = inject_enterprise_and_relatives += inject_available_countries .darkswarm.footer-pad - content_for :order_cycle_form do