Sha256: 531ae2ac0552117be362885b6367e12a7521a98f2fea8c22af55c166be71e800

Contents?: true

Size: 1.52 KB

Versions: 49

Compression:

Stored size: 1.52 KB

Contents

var geodata;

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

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;
	}
};

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
piggybak-0.5.5 app/assets/javascripts/piggybak.states.js
piggybak-0.5.4 app/assets/javascripts/piggybak.states.js
piggybak-0.5.3 app/assets/javascripts/piggybak.states.js
piggybak-0.5.2 app/assets/javascripts/piggybak.states.js
piggybak-0.5.1 app/assets/javascripts/piggybak.states.js
piggybak-0.5.0 app/assets/javascripts/piggybak.states.js
piggybak-0.4.39 app/assets/javascripts/piggybak.states.js
piggybak-0.4.38 app/assets/javascripts/piggybak.states.js
piggybak-0.4.37 app/assets/javascripts/piggybak.states.js
piggybak-0.4.36 app/assets/javascripts/piggybak.states.js
piggybak-0.4.35 app/assets/javascripts/piggybak.states.js
piggybak-0.4.34 app/assets/javascripts/piggybak.states.js
piggybak-0.4.33 app/assets/javascripts/piggybak.states.js
piggybak-0.4.32 app/assets/javascripts/piggybak.states.js
piggybak-0.4.31 app/assets/javascripts/piggybak.states.js
piggybak-0.4.30 app/assets/javascripts/piggybak.states.js
piggybak-0.4.29 app/assets/javascripts/piggybak.states.js
piggybak-0.4.28 app/assets/javascripts/piggybak.states.js
piggybak-0.4.27 app/assets/javascripts/piggybak.states.js
piggybak-0.4.26 app/assets/javascripts/piggybak.states.js