From 9f750e419dfdc030c3cdfe6271e966fca7d94196 Mon Sep 17 00:00:00 2001 From: Marvin Frederickson Date: Mon, 5 Jun 2017 16:33:59 -0800 Subject: [PATCH 1/2] refactor for consistency, use new events --- .../javascripts/concerto_weather/weather.js | 184 +++++++++--------- 1 file changed, 96 insertions(+), 88 deletions(-) diff --git a/app/assets/javascripts/concerto_weather/weather.js b/app/assets/javascripts/concerto_weather/weather.js index df63841..e377ed5 100644 --- a/app/assets/javascripts/concerto_weather/weather.js +++ b/app/assets/javascripts/concerto_weather/weather.js @@ -1,99 +1,107 @@ -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) - } - }); -} +var ConcertoWeather = { + _initialized: false, -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 = " \ - "; - tableBody = "
NameDistrict/County/RegionProvince/StateCountry
"; - 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(); + weatherRowClickHandler: function (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"); + }, - if (info_el.length != 0 && cityQuery.length > 0) { - // clear out any prior selected city - $("#weather_config_lat").val(""); - $("#weather_config_lng").val(""); + reverseGeocode: function (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 + }; - // 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}, + url: "//nominatim.openstreetmap.org/reverse", + data: params, 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.

"); + // 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', ConcertoWeather.weatherRowClickHandler) } }); - } -} + }, + + buildResultsTable: function (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 = " \ + "; + tableBody = "
NameDistrict/County/RegionProvince/StateCountry
"; + 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++) { + ConcertoWeather.reverseGeocode(places[i]); + } + } + }, + + searchForCityInfo: function () { + var info_el = $(".city-info"); + var cityQuery = $("input#weather_config_city_query").val(); -function initCityIdSearch() { - var query_el = $("input#weather_config_city_query") - query_el.on("focusout", searchForCityInfo); + 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 + ConcertoWeather.buildResultsTable(data); + }, + error: function(data) { + $(info_el).empty().html("

No results found.

"); + } + }); + } + }, + + initHandlers: function () { + if (!ConcertoWeather._initialized) { + var query_el = $("input#weather_config_city_query") + query_el.on("focusout", ConcertoWeather.searchForCityInfo); + ConcertoWeather._initialized = true; + } + } } -$(document).ready(initCityIdSearch); +$(document).ready(ConcertoWeather.initHandlers); +$(document).on('turbolinks:load', ConcertoWeather.initHandlers); From a3e4d01383ef79798c6f4ce2f2b5109ee8b87551 Mon Sep 17 00:00:00 2001 From: Marvin Frederickson Date: Fri, 3 Aug 2018 17:01:02 -0800 Subject: [PATCH 2/2] bump major version - requires turbolinks 5 --- lib/concerto_weather/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/concerto_weather/version.rb b/lib/concerto_weather/version.rb index 2175e5e..b054285 100644 --- a/lib/concerto_weather/version.rb +++ b/lib/concerto_weather/version.rb @@ -1,3 +1,3 @@ module ConcertoWeather - VERSION = '0.6'.freeze + VERSION = '1.0'.freeze end