-
Notifications
You must be signed in to change notification settings - Fork 29
/
jquery.ziptastic.js
106 lines (93 loc) · 3 KB
/
jquery.ziptastic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
(function( $ ) {
var requests = {};
var zipValid = {
pattern: /^[a-z0-9]+$/i
};
var protocol = '';
if (location.protocol == 'file:') {
protocol = 'https://';
} else {
protocol = location.protocol;
}
var referrer = document.location.origin + document.location.pathname;
$.ziptastic = function(country, zip, key, callback) {
country = country || 'US';
if (!key) {
alert("Please register for an API key at GetZiptastic.com.");
return;
}
country = country.toUpperCase();
if(!requests[country]) {
requests[country] = {};
}
if(!requests[country][zip]) {
requests[country][zip] = $.ajax({
url: protocol + '//zip.getziptastic.com/v3/' + country + '/' + zip,
headers: {'x-referrer': referrer, 'x-key': key},
contentType: "application/json",
type: 'GET',
dataType: 'json',
success: function(data) { requests[country][zip] = data[0]; },
error: function(e) { console.log('There was an error. ' + e.message ); }
});
// Bind to the finished request
requests[country][zip].done(function(data) {
if (typeof callback == 'function') {
var data_temp = data
var key = Object.keys(data_temp)[0];
requests[country][zip] = data_temp[key];
callback(data_temp[key].country, data_temp[key].state,
data_temp[key].state_short, data_temp[key].city, zip);
}
});
}
// Allow for binding to the deferred object
return requests[country][zip];
};
$.reverseZiptastic = function(latitude, longitude, key, callback) {
var request = $.ajax({
url: protocol + '//zip.getziptastic.com/v3/reverse/' + latitude + '/' + longitude + '/5000',
headers: {'x-referrer': referrer, 'x-key': key},
contentType: "application/json",
type: 'GET',
dataType: 'json',
error: function(e) { console.log('There was an error. ' + e.message ); }
});
request.done(function(data) {
if (typeof callback == 'function') {
callback(data[0].country, data[0].state,
data[0].state_short, data[0].city, zip);
}
});
}
$.fn.ziptastic = function( options ) {
return this.each(function() {
var ele = $(this);
if (options.reverseGeo == true) {
var geoSuccess = function(position) {
$.reverseZiptastic(position.coords.latitude, position.coords.longitude, options.key, function(country, state, state_short, city){
ele.trigger('zipChange', [country, state, state_short, city, zip]);
});
}
var geoError = function(e) {
error(e);
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(geoSuccess, geoError);
} else {
console.log('Geolocation is not supported by your browser.');
}
} else {
ele.on('keyup change', function() {
var zip = ele.val();
if(zipValid.pattern.test(zip)) {
$.ziptastic(options.country, zip, options.key, function(country, state, state_short, city) {
// Trigger the updated information
ele.trigger('zipChange', [country, state, state_short, city, zip]);
});
}
});
}
});
};
})( jQuery );