Use ofn-select2 directive for country and state selectors on enterprise forms

This commit is contained in:
Rob Harrington
2018-06-14 12:35:03 +10:00
committed by Maikel Linke
parent d1019fcc5e
commit dabef16606
6 changed files with 17 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
# Used in enterprise new and edit forms to reset the state when the country is changed
angular.module("admin.enterprises").controller 'countryCtrl', ($scope, availableCountries) ->
$scope.countries = availableCountries
$scope.countriesById = $scope.countries.reduce (obj, country) ->
@@ -7,19 +7,12 @@ angular.module("admin.enterprises").controller 'countryCtrl', ($scope, available
obj
, {}
$scope.countryOnChange = (stateSelectElemId) ->
$scope.$watch 'Enterprise.address.country_id', (newID, oldID) ->
$scope.clearState() unless $scope.addressStateMatchesCountry()
$scope.refreshStateSelector(stateSelectElemId)
$scope.clearState = ->
$scope.enterprise_address_attributes_state = {}
$scope.Enterprise.address.state_id = null
$scope.addressStateMatchesCountry = ->
$scope.countriesById[$scope.enterprise_address_attributes_country.id].states.some (state) -> state.id == $scope.enterprise_address_attributes_state?.id
$scope.refreshStateSelector = (stateSelectElemId) ->
# workaround select2 (using jQuery and setTimeout) to force a refresh of the selected value
setTimeout ->
selectedState = jQuery('#' + stateSelectElemId)
jQuery('#' + stateSelectElemId).select2("val", selectedState)
, 500
$scope.countriesById[$scope.Enterprise.address.country_id].states.some (state) ->
state.id == $scope.Enterprise.address.state_id

View File

@@ -0,0 +1,5 @@
angular.module("admin.enterprises").controller 'NewEnterpriseController', ($scope, defaultCountryID) ->
$scope.Enterprise =
address:
country_id: defaultCountryID
state_id: null