-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
269 lines (254 loc) · 9.99 KB
/
app.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
if(typeof console == "undefined" || !console || typeof console.log != "function"){
console = {
log: function(){}
};
}
var treeaddress, treetype, latin, bday, treemarker, newtree;
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng( 42.323206, -71.074847 ),
zoom: 11,
mapTypeId: google.maps.MapTypeId.ROADMAP,
streetViewControl: false
});
var infowindow = new google.maps.InfoWindow();
var geocoder = new google.maps.Geocoder();
var ft = new google.maps.FusionTablesLayer({map:map,suppressInfoWindows:true,query:{select:"'Geocodable address'",from:"18amWr6Z69048wr2b5B-W1Cmc2Thiu9Tj5-xvy8k"},styles:[{markerOptions:{iconName:"small_green"}},{markerOptions:{iconName:"small_blue"},where:"Adopted=1"}]});
var infoWindow = new google.maps.InfoWindow();
google.maps.event.addListener(ft, "click", function(e){
if(map.getZoom() < 16){
map.setCenter(e.latLng);
map.setZoom(16);
}
var headline = e.row["Common Name"].value;
if(headline == "No Data"){
// for user submitted trees
headline = "Street Tree";
}
var content = "<h3>" + headline + "</h3>";
content += "<i>" + latinify(e.row["Latin Name"].value) + "</i><br/>";
content += "<div>Planted " + e.row["Birthdate"].value + " at ";
content += "<span class='address'>" + e.row["Address"].value.toLowerCase() + ".</span><br/>" + e.row["Neighborhood"].value + "<br/>This tree is ";
var age = getAge( e.row );
if(age == 1){
content += "1 year old.";
}
else{
content += age + " years old.";
}
content += "</div>";
if(e.row["Adopted"].value == 1){
var adoptphrase = "Adopted";
if(e.row["SignName"].value && e.row["SignName"].value.length){
adoptphrase += " by " + e.row["SignName"].value;
}
content += "<div class='adopted greenovate-cyan'>" + adoptphrase + "</div>";
}
infoWindow.setContent(content);
infoWindow.setPosition(e.latLng);
infoWindow.open(map);
// enable adopt button
document.getElementById("adoptme").className = "btn btn-success greenovate-green";
document.getElementById("adoptme").disabled = "";
document.getElementById("adoptme").onclick = function(){
treeaddress = e.row["Address"].value;
treetype = e.row["Common Name"].value;
latin = latinify(e.row["Latin Name"].value);
bday = e.row["Birthdate"].value;
adoptWindow();
};
});
var searchbar = document.createElement("div");
searchbar.id = "searchbar";
var address = document.createElement("input");
address.id = "address";
address.placeholder = "Address";
address.className = "x-large";
address.type = "text";
address.onkeydown = function(e){
if(e.keyCode == 13){
searchAddress();
}
};
searchbar.appendChild(address);
var sbtn = document.createElement("button");
sbtn.className = "btn btn-primary";
sbtn.innerHTML = "Search";
sbtn.onclick = searchAddress;
searchbar.appendChild(sbtn);
document.getElementById("map").appendChild(searchbar);
var searchAddress = function(){
var lookup = document.getElementById("address").value;
if(lookup.toLowerCase().indexOf("boston") == -1){
lookup += ", Boston, MA";
}
geocoder.geocode( { 'address': lookup }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
map.setZoom(16);
}
});
};
// for trees without set birthdays, their birthday is the first day of their planting season
// after 2030, use first day of season in planted-year as birthday
// for trees planted after 2030, use first day of season in 2030 as birthday
var seasons = {
spring: [ "3/20/2008","3/20/2009","3/20/2010","3/20/2011","3/20/2012","3/20/2013","3/20/2014","3/20/2015","3/20/2016","3/20/2017","3/20/2018","3/20/2019","3/19/2020","3/20/2021","3/20/2022","3/20/2023","3/19/2024","3/20/2025","3/20/2026","3/20/2027","3/19/2028","3/20/2029","3/20/2030" ],
fall: [ "9/22/2008","9/22/2009","9/22/2010","9/23/2011","9/22/2012","9/22/2013","9/22/2014","9/23/2015","9/22/2016","9/22/2017","9/22/2018","9/23/2019","9/22/2020","9/22/2021","9/22/2022","9/23/2023","9/22/2024","9/22/2025","9/22/2026","9/23/2027","9/22/2028","9/22/2029","9/22/2030" ]
};
function getAge(tree){
var birthdate = tree["Birthdate"].value.toLowerCase();
var age;
if(birthdate.indexOf("fall") > -1 || birthdate.indexOf("spring") > -1){
var currentYear = (new Date()).getFullYear();
var birthYear = 1 * birthdate.replace("spring","").replace("fall","").replace(" ","").replace(" ","").replace(" ","");
var lastYear = (new Date(seasons.fall[ seasons.fall.length-1 ])).getFullYear();
if(birthYear <= lastYear){
age = currentYear - birthYear;
if(currentYear <= lastYear){
// can compare to this season
if(birthdate.indexOf("spring") > -1){
for(var s=0;s<seasons.spring.length;s++){
if((new Date(seasons.spring[s])).getFullYear() == currentYear){
if( new Date(seasons.spring[s]) > new Date() ){
// birthday coming up this year
age--;
}
break;
}
}
}
else{
for(var s=0;s<seasons.fall.length;s++){
if((new Date(seasons.fall[s])).getFullYear() == currentYear){
if( new Date(seasons.fall[s]) > new Date() ){
// birthday coming up this year
age--;
}
break;
}
}
}
}
else{
// compare to last year in system
var currentdate = new Date();
if(birthdate.indexOf("spring") > -1){
var lastbirthdate = new Date(seasons.spring[ seasons.spring.length-1 ]);
if( lastbirthdate.getMonth() > currentdate.getMonth() || ( lastbirthdate.getMonth() == currentdate.getMonth() && lastbirthdate.getDate() >= currentdate.getDate() ) ){
// birthday coming up this year
age--;
}
}
else{
var lastbirthdate = new Date(seasons.fall[ seasons.fall.length-1 ]);
if( lastbirthdate.getMonth() > currentdate.getMonth() || ( lastbirthdate.getMonth() == currentdate.getMonth() && lastbirthdate.getDate() >= currentdate.getDate() ) ){
// birthday coming up this year
age--;
}
}
}
}
else{
// planted after last year in system
if(birthdate.indexOf("spring") > -1){
age = (new Date()) * 1 - (new Date(seasons.spring[ seasons.spring.length-1 ])) * 1;
}
else{
age = (new Date()) * 1 - (new Date(seasons.fall[ seasons.fall.length-1 ])) * 1;
}
age = Math.floor(age / (365 * 24 * 60 * 60 * 1000));
}
}
else{
// specific birthdate
age = (new Date()) * 1 - (new Date(tree["Birthdate"].value)) * 1;
age = Math.floor(age / (365 * 24 * 60 * 60 * 1000));
}
return age;
}
function latinify(name){
name = name[0].toUpperCase() + name.substring(1).toLowerCase();
return name;
}
function adoptWindow(){
TINY.box.show({html: document.getElementById("adoptformtemp").innerHTML ,animate:true,close:true,boxid:'adoptform',top:5});
setTimeout(function(){
if(newtree){
document.getElementsByClassName("newtree")[1].value = "new";
}
for(var i=0;i<document.getElementsByClassName("treeaddress").length;i++){
if(typeof document.getElementsByClassName("treeaddress")[i].value != "undefined"){
document.getElementsByClassName("treeaddress")[i].value = treeaddress;
}
else{
document.getElementsByClassName("treeaddress")[i].textContent = treeaddress;
}
if(typeof document.getElementsByClassName("treetype")[i] != "undefined"){
if(typeof document.getElementsByClassName("treetype")[i].value != "undefined"){
document.getElementsByClassName("treetype")[i].value = treetype;
}
else{
document.getElementsByClassName("treetype")[i].innerHTML = treetype;
}
}
if(typeof document.getElementsByClassName("latin")[i] != "undefined"){
if(typeof document.getElementsByClassName("latin")[i].value != "undefined"){
document.getElementsByClassName("latin")[i].value = latin;
}
else{
document.getElementsByClassName("latin")[i].innerHTML = latin;
}
}
if(typeof document.getElementsByClassName("bday")[i] != "undefined"){
if(typeof document.getElementsByClassName("bday")[i].value != "undefined"){
document.getElementsByClassName("bday")[i].value = bday;
}
else{
document.getElementsByClassName("bday")[i].innerHTML = bday;
}
}
}
}, 500);
}
function cancelWindow(){
TINY.box.hide();
newtree = false;
}
function addTreeMarker(){
if(typeof treemarker != "undefined" && treemarker){
treemarker.setMap(null);
}
treemarker = new google.maps.Marker({
map: map,
draggable: true,
position: map.getCenter()
});
google.maps.event.addListener(treemarker, "dragend", function(e){
infowindow.close();
});
google.maps.event.addListener(treemarker, "dragend", function(e){
infowindow.setContent('<h4>Tree Type</h4><input id="inputtype" type="text" value=""/><br/><button class="btn btn-primary pull-right" onclick="openAdopt()">Adopt</button>');
infowindow.open(map, treemarker);
});
google.maps.event.addListener(treemarker, "click", function(e){
infowindow.setContent('<h4>Tree Type</h4><input id="inputtype" type="text" value=""/><br/><button class="btn btn-primary pull-right" onclick="openAdopt()">Adopt</button>');
infowindow.open(map, treemarker);
});
}
function openAdopt(){
treeaddress = treemarker.getPosition().lat().toFixed(6) + "," + treemarker.getPosition().lng().toFixed(6);
treetype = document.getElementById("inputtype").value;
latin = treetype;
var today = new Date();
bday = (today.getMonth()+1) + "/" + today.getDate() + "/" + today.getFullYear();
newtree = true;
adoptWindow();
}
function makeAdopt(){
if(document.getElementsByClassName("commit")[1].checked){
document.getElementsByTagName("form")[1].submit();
}
else{
alert("Please approve your commitment and the city's disclaimer before adopting.");
}
}