-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
129 lines (110 loc) · 3.57 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
$(document).ready(function() {
// If ID parameter used, display detailed results.
if(url_query('id')) {
getInspectionDetails(url_query('id'));
}
// If search parameter used, display search results.
else if(url_query('search')) {
searchList(url_query('search'));
}
// If latest parameter is used, display most recent results.
else if(url_query('latest')) {
mostRecent(url_query('limit'));
}
// If worst parameter is used, display worst results.
else if(url_query('worst')) {
worstOffenders(url_query('limit'));
}
// Handlers for search field.
$("#search").click(function() {
var searchText = $("#name").val();
if(searchText == "") {
$("#name").addClass("error");
}
else {
window.location.replace(url_base(window.location.href) + '?search=' + searchText);
}
});
// User presses enter.
$("#name").keypress(function(event) {
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13'){
$("#search").trigger('click');
}
});
// If the user focuses on the search field, remove any indication of bad entry.
$("#name").focus(function(){
$(this).removeClass("error");
});
});
// Base URL for API calls.
var base_url = 'https://health.data.ny.gov/resource/tbxv-5tbd.json';
var query_base = '?county=ONONDAGA&$select=operation_name,%20nys_health_operation_id,%20facility_address,%20city,%20date';
// Get list of worst offenders.
function worstOffenders(limit) {
var url = base_url + query_base + '&$where=total_critical_violations>0&$order=total_critical_violations%20DESC&$limit=' + limit;
getPlaceList(url, "worst-list", "Worst Offenders");
}
// Get list of latest inspections.
function mostRecent(limit) {
var url = base_url + query_base + '&$order=date%20DESC&$limit=' + limit;
getPlaceList(url, "recent-list", "Most Recent");
}
// Get list of inspections by name.
function searchList(name) {
var url = base_url + query_base + '&$where=contains(operation_name,%20%27' + name + '%27)';
getPlaceList(url, "search-list", "Results");
}
// Method to get list of inspections.
function getPlaceList (url, id, title) {
requestJSON(url, function(json) {
results.title = title;
results.places = json;
placesList = Handlebars.templates.list({ Places : results });
$("#results").append('<div id="' + id + '"></div>');
$('#' + id).append(placesList);
});
}
// Method to get details of specific inspection.
function getInspectionDetails(id) {
var url = base_url + '?nys_health_operation_id=' + id;
requestJSON(url, function(json) {
$("#results").append("<h4>Details</h4>");
details = Handlebars.templates.detail({ Details : json[0] });
$("#results").append(details);
});
}
// Method to make API call.
function requestJSON(url, callback) {
$.ajax({
url: url,
beforeSend: function() {
clearContents();
$('.alert-warning').removeClass('hide');
},
complete: function(xhr) {
$('.alert-warning').addClass('hide');
callback.call(null, xhr.responseJSON);
}
});
}
// Utility method to clear div contents
function clearContents() {
$("#results div").empty();
}
// Parse URL Queries
function url_query(query) {
query = query.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var expr = "[\\?&]" + query + "=([^&#]*)";
var regex = new RegExp( expr );
var results = regex.exec( window.location.href );
if ( results !== null ) {
return results[1];
} else {
return false;
}
}
// Get base URL without querystring parameters.
function url_base(url) {
return window.location.href.split('?')[0];
}