-
Notifications
You must be signed in to change notification settings - Fork 0
/
parking_functions.js
86 lines (74 loc) · 2.61 KB
/
parking_functions.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
/*
* KSU Parking Garage Status Page
* http://www.collegianmedia.com/
*
* parking_functions.js
*
*/
/*
* Function for alerts
*/
function parking_alert(the_alert) {
jQuery('.alert').html(the_alert);
}
/*
* Format the text representation of the data.
*/
function formattedData(spotsAvailable) {
if (spotsAvailable < 1) {
return "No spots available"
}
return ("Approximately " + spotsAvailable + " spots available");
}
/*
* Function to set the status bars
*/
function setPercentage(parkingType, spotsAvailable, totalSpots) {
// Figure out the percentage of available spots
thePercentage = (spotsAvailable / parseFloat(totalSpots)) * 100;
// Set the width of the bar based on the percentage
$('.' + parkingType + '-percentage').css('width', thePercentage + '%').removeClass('loading').removeClass('active').text("");
// Set the text on that bar
$('.status-' + parkingType + '-text').text(formattedData(spotsAvailable));
// Clear any colors from the bar
clearColors(parkingType);
// Set the color of the bar
if (thePercentage > 40) {
$('.' + parkingType + '-percentage').addClass('progress-bar-success');
$('.progress-' + parkingType).addClass('background-success');
} else if (thePercentage < 5 ) {
$('.' + parkingType + '-percentage').addClass('progress-bar-danger');
$('.progress-' + parkingType).addClass('background-danger');
} else {
$('.' + parkingType + '-percentage').addClass('progress-bar-warning');
$('.progress-' + parkingType).addClass('background-warning');
}
}
/*
* Function to remove all color classes
*/
function clearColors(parkingType) {
$('.' + parkingType + '-percentage').removeClass('progress-bar-success').removeClass('progress-bar-warning').removeClass('progress-bar-danger');
$('.progress-' + parkingType).removeClass('background-success').removeClass('background-danger').removeClass('background-warning');
}
/*
* Function to check the status using the API
*/
function check_status() {
$.getJSON("http://garage.ksucloud.net/resources?key=6c88a6bb-4da0-4ffa-8b2c-1b11b8572ccf", function(parkingData) {
$.each(parkingData.resources, function(index, item) {
var setSelector;
if (item.name == "Public") {
setSelector = 'public';
} else if (item.name == "Students") {
setSelector = 'student';
} else if (item.name == "Faculty and Staff") {
setSelector = 'faculty';
} else {
parking_alert("Something seems to be wrong. Check back later.");
console.log("Item in JSON response not as expected");
}
setPercentage(setSelector, item.available, item.total);
});
});
}