context = window rest = window.JK.Rest() logger = context.JK.logger @SelectLocation = React.createClass({ mixins: [Reflux.listenTo(@LocationStore, "onLocationsChanged")] propTypes: { onItemChanged: React.PropTypes.func.isRequired } getInitialState: () -> {selectedCountry: null, countries: LocationStore.countries || {US: {name: 'United States', regions: []}}} onLocationsChanged: (countries) -> console.log("countires in ", countries) @setState({countries: countries}) onCountryChanged: (e) -> val = $(e.target).val() @changed(val, null, null) @setState({selectedCountry: val, selectedRegion: null, selectedCity: null}) if val? LocationActions.selectCountry(val) onRegionChanged: (e) -> val = $(e.target).val() @changed(this.currentCountry(), val, null) @setState({selectedRegion: val, selectedCity: null}) if val? && this.props.showCity LocationActions.selectRegion(this.currentCountry(), val) onCityChanged: (e) -> val = $(e.target).val() @changed(this.currentCountry(), this.currentRegion(), val) @setState({selectedCity: val}) changed: (country, region, city) -> if country == '' country = null if region == '' region = null if city == '' city = null @props.onItemChanged(country, region, city) currentCity: () -> this.state.selectedCity || this.props.selectedCity currentCountry: () -> this.state.selectedCountry || this.props.selectedCountry || 'US' currentRegion: () -> this.state.selectedRegion || this.props.selectedRegion defaultText: () -> if this.props.defaultText? this.props.defaultText else 'Any' render: () -> countries = [``] for countryId, countryInfo of @state.countries countries.push(``) country = @state.countries[this.currentCountry()] regions = [``] cities = [``] if country? && country.regions for region in country.regions regions.push(``) if this.currentRegion() == region.id && this.props.showCity for city in region.cities cities.push(``) if !this.props.hideCountry countryJsx = `

Country:

` disabled = regions.length == 1 if this.props.showCity cityJsx = `

City:

` `
{countryJsx}

State/Region:

{cityJsx}
` })