Sha256: 5669a3c82edb49615fb40c3a55db2878a2389d272c04653457c222b093fb6339

Contents?: true

Size: 1.52 KB

Versions: 34

Compression:

Stored size: 1.52 KB

Contents

var geodata;

var piggybak_states = {
	initialize_listeners: function() {
		$('#piggybak_order_shipping_address_attributes_country_id').change(function() {
			piggybak_states.update_state_option('shipping');
		});
		$('#piggybak_order_billing_address_attributes_country_id').change(function() {
			piggybak_states.update_state_option('billing');
		});
		return;
	},
	populate_geodata: function() {
		$.ajax({
			url: geodata_lookup,
			cached: false,
			dataType: "JSON",
			success: function(data) {
				geodata = data;
				piggybak_states.update_state_option('shipping');
				piggybak_states.update_state_option('billing');
			}
		});
	},
	update_state_option: function(type, block) {
		var country_field = $('#piggybak_order_' + type + '_address_attributes_country_id');
		var country_id = country_field.val();
		var new_field;

		if(geodata.countries["country_" + country_id].length > 0) {
			new_field = $('<select>');
			$.each(geodata.countries["country_" + country_id], function(i, j) {
				new_field.append($('<option>').val(j.id).html(j.name));
			});	
		} else {
			new_field = $('<input>');
		}
		var old_field = $('#piggybak_order_' + type + '_address_attributes_state_id');
		new_field.attr('name', old_field.attr('name')).attr('id', old_field.attr('id'));
		if(old_field.prop('tagName') == new_field.prop('tagName')) {
			new_field.val(old_field.val());
		}
		old_field.replaceWith(new_field);

		if(block) {
			block();
		}
		return;
	}
};

$(function() {
	piggybak_states.populate_geodata();
	piggybak_states.initialize_listeners();
});

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
piggybak-0.6.34 app/assets/javascripts/piggybak/piggybak.states.js
piggybak-0.6.33 app/assets/javascripts/piggybak/piggybak.states.js
piggybak-0.6.32 app/assets/javascripts/piggybak/piggybak.states.js
piggybak-0.6.31 app/assets/javascripts/piggybak/piggybak.states.js
piggybak-0.6.30 app/assets/javascripts/piggybak/piggybak.states.js
piggybak-0.6.29 app/assets/javascripts/piggybak/piggybak.states.js
piggybak-0.6.28 app/assets/javascripts/piggybak/piggybak.states.js
piggybak-0.6.27 app/assets/javascripts/piggybak/piggybak.states.js
piggybak-0.6.26 app/assets/javascripts/piggybak/piggybak.states.js
piggybak-0.6.25 app/assets/javascripts/piggybak/piggybak.states.js
piggybak-0.6.24 app/assets/javascripts/piggybak/piggybak.states.js
piggybak-0.6.23 app/assets/javascripts/piggybak/piggybak.states.js
piggybak-0.6.22 app/assets/javascripts/piggybak/piggybak.states.js
piggybak-0.6.21 app/assets/javascripts/piggybak/piggybak.states.js
piggybak-0.6.20 app/assets/javascripts/piggybak/piggybak.states.js
piggybak-0.6.19 app/assets/javascripts/piggybak/piggybak.states.js
piggybak-0.6.18 app/assets/javascripts/piggybak/piggybak.states.js
piggybak-0.6.17 app/assets/javascripts/piggybak/piggybak.states.js
piggybak-0.6.16 app/assets/javascripts/piggybak/piggybak.states.js
piggybak-0.6.15 app/assets/javascripts/piggybak/piggybak.states.js