-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowGenerator.js
119 lines (94 loc) · 3.47 KB
/
showGenerator.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
107
108
109
110
111
112
113
114
115
116
117
118
119
var showGenerator = (function() {
var CONFIGS = {
API_KEY : "AIzaSyApTvGE2HLUR3KOOYiPphzZUsmAaqcB0Nw",
base_url : "http://maps.googleapis.com/maps/api/staticmap?",
show_details_form : "form[name=newShowDetails]"
};
// Function declarations
var init, generateNewShow, generateStaticMapURL, encodeAddress, newShow;
// Variable declarations
var new_show = {};
init = function() {
buildBookingTypes();
$(CONFIGS.show_details_form).on('submit', function (e) {
e.preventDefault();
newShow(this);
});
};
generateNewShow = function (data) {
console.log("GEOCODING");
console.log("*************");
console.log(data);
if (data.status != "ZERO_RESULTS") {
var $form = $(CONFIGS.show_details_form);
var $results = $('.results');
var date = new Date();
// Get a unique time, and convert from base 10 to base 32 to save some space
new_show.id = date.getTime().toString(32);
new_show.title = $form.find('[name=title]').val();
new_show.miniMap = generateStaticMapURL(data.results.pop());
new_show.venue = $form.find('[name=venue]').val();
new_show.bookingTypes = [];
_inputsBT = $form.find('[name=bookingTypes] input');
for (input in _inputsBT) {
if (_inputsBT[input].checked) {
new_show.bookingTypes.push(_inputsBT[input].value);
}
}
console.log(new_show.bookingTypes);
$results.find('h1').html(new_show.address);
$results.find('p').html(new_show.miniMap);
$results.find('img').attr('src',new_show.miniMap);
$('code').html(JSON.stringify(new_show));
} else {
console.log("No Results");
}
};
buildBookingTypes = function () {
var _BT, _inputList;
_BT = utils.getBookingTypes();
console.log(_BT);
_inputList = "";
for (option in _BT) {
_inputList += '<label for="'+option+'">'+_BT[option]+'</label><input type="checkbox" name="'+option+'" id="'+option+'" value="'+option+'"/>'
}
console.log(_inputList);
$("#noBookingTypes").replaceWith(_inputList);
};
generateStaticMapURL = function (data) {
console.log(data);
var lat = data.geometry.location.lat;
var lng = data.geometry.location.lng;
var url = CONFIGS.base_url + "center=" + encodeAddress(new_show.address) + "&zoom=15&size=150x150&maptype=roadmap&markers=color:red%7C" + lat + "," + lng + "&key=" + CONFIGS.API_KEY;
console.log("MAP DATA");
console.log("*************");
console.log(url);
return url;
};
encodeAddress = function (address) {
return address.replace(/ /g, "+").replace(/#/g, "");
};
newShow = function(form) {
var new_street = form.children.namedItem("address").value;
var new_city = form.children.namedItem("city").value;
var new_state = form.children.namedItem("state").value;
var new_zip = form.children.namedItem("zip").value;
new_show.address = new_street +
((new_city !== "")? " " + new_city : "") +
((new_state !== "")? ", " + new_state : "") +
((new_zip !== "")? " " + new_zip : "");
// Get geocoding info from Google
$.ajax({
url: "https://maps.googleapis.com/maps/api/geocode/json?address=" + encodeURI(encodeAddress(new_show.address)) + "&key=" + CONFIGS.API_KEY,
dataType: "json"
}).success(function(data) {
generateNewShow(data);
}).error(function() {
console.log("Geocoder failed to load");
});
};
init();
// Return Public Functions
return {
}
})();