-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
152 lines (126 loc) · 4.99 KB
/
main.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
refreshRate = 30 * 1000; // In Milliseconds
/**
Inserts <script> in the <head> to handle JSONP requests
**/
function jsonp(url, callback) {
var head = document.getElementsByTagName("head")[0];
var scripts = head.getElementsByTagName("script");
var scriptToRemove;
// Remove previous wrapper function
var wrapScriptPrefix = callback + "_";
for(i in scripts) {
if(scripts[i].type == "text/javascript" && scripts[i].innerHTML && beginsWith(scripts[i].innerHTML, wrapScriptPrefix)) {
scriptToRemove = scripts[i];
break;
}
}
if(scriptToRemove) {
head.removeChild(scriptToRemove);
scriptToRemove = null;
}
// Create wrapper function that points to the callback
var wrapScript = document.createElement("script");
wrapScript.type = "text/javascript";
callbackWrap = wrapScriptPrefix + Math.round(Math.random() * 10000); // Adds a _9999 suffix
wrapScript.innerHTML = callbackWrap + '=' + callback ;
head.appendChild(wrapScript);
// Remove previous script
for(i in scripts) {
if(scripts[i].type == "text/javascript" && scripts[i].src && beginsWith(scripts[i].src, url)) {
scriptToRemove = scripts[i];
break;
}
}
if(scriptToRemove) {
head.removeChild(scriptToRemove);
}
var scriptElement = document.createElement("script");
scriptElement.type = "text/javascript";
// i add to the url the call back function
var jsonpReq = url + "&jsoncallback=" + callbackWrap;
scriptElement.src = jsonpReq;
head.appendChild(scriptElement);
}
/**
Determines if string begins with subString
**/
function beginsWith(string, subString) {
return string.substr(0, subString.length) === subString;
}
/**
Main function that gets refreshed every refreshRate milliseconds and makes the call to the SFPark service
**/
function getPark() {
var url = "http://api.sfpark.org/sfpark/rest/availabilityservice?lat=37.792275&long=-122.397089&radius=2&uom=mile&response=json";
jsonp(url, 'sfParkResults');
setTimeout("getPark()", refreshRate);
}
/**
Creates a <tr> with a variable number of <td>s
**/
function createTableRow(element) {
var args = arguments;
var tableRow = document.createElement('tr');
for(i in args) {
var tableData = document.createElement('td');
tableData.innerHTML = args[i];
tableRow.appendChild(tableData);
}
return tableRow;
}
/**
Refreshes the UI with the data coming from the SFPark service
**/
function sfParkResults(parking) {
var lastUpdated = document.getElementById('lastUpdated');
var table = document.getElementById('parkResults');
var gMapsUrl = "http://maps.google.com/?q=";
var lud = new Date(parking.AVAILABILITY_UPDATED_TIMESTAMP);
var month = lud.getMonth() + 1;
lastUpdated.innerHTML = lud.getFullYear() + "-" + month + "-" + lud.getDate() + " " + lud.getHours() + ":" + lud.getMinutes() + ":" + lud.getSeconds();
// Get the table
var tbody = table.getElementsByTagName("tbody")[0];
// Remove the table contents
if(tbody.hasChildNodes()) {
while(tbody.childNodes.length >= 1) {
tbody.removeChild(tbody.firstChild);
}
}
var totOPER = 0, totOCC = 0, totAvailable = 0,numOcc;
for(i in parking.AVL) {
var obj = parking.AVL[i];
// Need to sanitize OCC since sometimes is greater than OPER (service bug?)
numOcc = Math.min(obj.OCC, obj.OPER);
var percentage = obj.OPER == 0 ? 0 : Math.round((numOcc * 100) / obj.OPER);
var numAvailable = obj.OPER - numOcc;
// Cumulative totals
totOPER += parseInt(obj.OPER);
totOCC += parseInt(numOcc);
totAvailable += parseInt(numAvailable);
// Reverse the coordinates to make them compatible with Google Maps
var addressToSearch = encodeURIComponent(obj.NAME + ", San Francisco, CA");
var nameHtml = "<a href='" + gMapsUrl + addressToSearch + "'>" + obj.NAME + "</a>";
var tableRow = createTableRow(nameHtml, obj.OPER, numOcc, numAvailable, percentage + "\%");
// Set styles for percentage > 75% and 100%
if(percentage == 100) {
tableRow.className += "full";
} else if (percentage >= 75) {
tableRow.className += "almost-full";
}
tbody.appendChild(tableRow);
}
// Footer
var tfoot = table.getElementsByTagName("tfoot")[0];
// Remove the table footer
if(tfoot.hasChildNodes()) {
while(tfoot.childNodes.length >= 1) {
tfoot.removeChild(tfoot.firstChild);
}
}
var occupiedPercentage = Math.round((totOCC * 100) / totOPER);
tfoot.appendChild(createTableRow('Total',totOPER,totOCC, totAvailable, occupiedPercentage));
}
// wait for all the page elements to load (including images), not very effective but I wanted to keep it simple
window.onload = function() {
getPark();
}