Created
November 14, 2016 19:42
-
-
Save joeljackson/1b69504697f00c898c46d69c60a2d139 to your computer and use it in GitHub Desktop.
country_and_state_selector.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; | |
window.Honest || (window.Honest = {}); | |
Honest.CountryAndStateSelect = (function() { | |
CountryAndStateSelect.prototype.DEFAULT_COUNTRY = "United States"; | |
CountryAndStateSelect.prototype.DEFAULT_LABEL = "State"; | |
CountryAndStateSelect.prototype.STATES = { | |
"United States": [['Alabama', 'AL'], ['Alaska', 'AK'], ['Arizona', 'AZ'], ['Arkansas', 'AR'], ['California', 'CA'], ['Colorado', 'CO'], ['Connecticut', 'CT'], ['Delaware', 'DE'], ['District Of Columbia', 'DC'], ['Florida', 'FL'], ['Georgia', 'GA'], ['Hawaii', 'HI'], ['Idaho', 'ID'], ['Illinois', 'IL'], ['Indiana', 'IN'], ['Iowa', 'IA'], ['Kansas', 'KS'], ['Kentucky', 'KY'], ['Louisiana', 'LA'], ['Maine', 'ME'], ['Maryland', 'MD'], ['Massachusetts', 'MA'], ['Michigan', 'MI'], ['Minnesota', 'MN'], ['Mississippi', 'MS'], ['Missouri', 'MO'], ['Montana', 'MT'], ['Nebraska', 'NE'], ['Nevada', 'NV'], ['New Hampshire', 'NH'], ['New Jersey', 'NJ'], ['New Mexico', 'NM'], ['New York', 'NY'], ['North Carolina', 'NC'], ['North Dakota', 'ND'], ['Ohio', 'OH'], ['Oklahoma', 'OK'], ['Oregon', 'OR'], ['Pennsylvania', 'PA'], ['Rhode Island', 'RI'], ['South Carolina', 'SC'], ['South Dakota', 'SD'], ['Tennessee', 'TN'], ['Texas', 'TX'], ['Utah', 'UT'], ['Vermont', 'VT'], ['Virginia', 'VA'], ['Washington', 'WA'], ['West Virginia', 'WV'], ['Wisconsin', 'WI'], ['Wyoming', 'WY']], | |
"Canada": [['Alberta', 'AB'], ['British Columbia', 'BC'], ['Manitoba', 'MB'], ['New Brunswick', 'NB'], ['Newfoundland & Labrador', 'NL'], ['Northwest Territories', 'NT'], ['Nova Scotia', 'NS'], ['Nunavut', 'NU'], ['Ontario', 'ON'], ['Prince Edward Island', 'PE'], ['Quebec', 'QC'], ['Saskatchewan', 'SK'], ['Yukon', 'YT']] | |
}; | |
CountryAndStateSelect.prototype.LABELS = { | |
"Canada": "Province" | |
}; | |
function CountryAndStateSelect(countrySelector) { | |
this.showStatesForCountry = __bind(this.showStatesForCountry, this); | |
this.handleChange = __bind(this.handleChange, this); | |
var idForStateSelect; | |
this.countrySelector = countrySelector; | |
idForStateSelect = this.countrySelector.data()['stateSelectorId']; | |
this.stateSelector = $("#" + idForStateSelect); | |
this.stateLabel = $("[for=" + idForStateSelect + "]"); | |
} | |
CountryAndStateSelect.prototype.handleChange = function() { | |
return this.countrySelector.on('change', this.showStatesForCountry); | |
}; | |
CountryAndStateSelect.prototype.getSelectedCountry = function() { | |
return this.countrySelector.val() || this.DEFAULT_COUNTRY; | |
}; | |
CountryAndStateSelect.prototype.getStatesForCountry = function() { | |
return this.STATES[this.getSelectedCountry()]; | |
}; | |
CountryAndStateSelect.prototype.updateStateLabel = function() { | |
var labelText; | |
labelText = this.LABELS[this.getSelectedCountry()] || this.DEFAULT_LABEL; | |
return this.stateLabel.text(labelText); | |
}; | |
CountryAndStateSelect.prototype.showStatesForCountry = function() { | |
var _this = this; | |
this.stateSelector.empty().append($("<option></option>")); | |
_.each(this.getStatesForCountry(), function(conf) { | |
return _this.stateSelector.append($("<option></option>").attr('value', conf[1]).text(conf[0])); | |
}); | |
return this.updateStateLabel(); | |
}; | |
return CountryAndStateSelect; | |
})(); | |
$(function() { | |
return _.each($("[data-state-selector-id]"), function(element) { | |
return new Honest.CountryAndStateSelect($(element)).handleChange(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment