function weatherRowClickHandler(event) {
// Handle click events on city results
// Lat/lng saved with weather content for future API calls.
$("#weather_config_lat").val($(this).attr("data-lat"));
$("#weather_config_lng").val($(this).attr("data-lng"));
// Reset all selected rows
$(".city-info").find("tr").each(function() {
$(this).attr("data-selected", "0");
$(this).removeClass("alert-info");
});
// Set a new selected row
$(this).attr("data-selected", "1");
$(this).addClass("alert-info");
}
function reverseGeocode(place) {
// Reverse gecode returned coordinates from OpenWeatherMap API
// OpenWeatherMap API returns limited location information so this is important
// if users want to distinguish similarly named places
var params = {
format: "json",
lat: place.coord.lat,
lon: place.coord.lon
};
$.ajax({
url: "//nominatim.openstreetmap.org/reverse",
data: params,
dataType: "json",
success: function(data) {
// Add a row to our results table
var lat = place.coord.lat;
var lng = place.coord.lon;
var row = "
\
" + place.name + " | \
" + (data.address.county || '') + " | \
" + (data.address.state || '') + " | \
" + (data.address.country_code.toUpperCase() || '') + " | ";
$('#cityResults tr:last').after(row);
// Handle click events for search results
$('#cityResults tr:last').on('click', weatherRowClickHandler)
}
});
}
function buildResultsTable(data) {
var places = data.list;
var info_el = $(".city-info");
// Build a table to display city query results
// User can select a city that best matches their intended location
table = " \
Name | District/County/Region | Province/State | Country | ";
tableBody = "
";
tableBody += "
You must select your city from the list above. Can't find your city? Try entering your zip code or your city along with its state (ex: Madison, WI)."
// Insert our empty results table
$(info_el).empty().html(table + tableBody);
// Find the address info for each weather search result
// Then insert the place data into our results table
if (typeof places != 'undefined') {
for (var i = 0; i < places.length; i++) {
reverseGeocode(places[i]);
}
}
}
function searchForCityInfo() {
var info_el = $(".city-info");
var cityQuery = $("input#weather_config_city_query").val();
if (info_el.length != 0 && cityQuery.length > 0) {
// clear out any prior selected city
$("#weather_config_lat").val("");
$("#weather_config_lng").val("");
// Add a 'searching' placeholder while city query results are loading
$(info_el).empty().html(" searching...");
// Query the OpenWeatherAPI to find a list of matching cities based on input
$.ajax({
url: "/concerto_weather/city_search.js",
data: {"q": cityQuery},
dataType: "json",
timeout: 6000,
success: function(data) {
// Build a table of results returned from city query
buildResultsTable(data);
},
error: function(data) {
$(info_el).empty().html("No results found.
");
}
});
}
}
function initCityIdSearch() {
var query_el = $("input#weather_config_city_query")
query_el.on("focusout", searchForCityInfo);
}
$(document).ready(initCityIdSearch);