Last active
August 29, 2015 14:04
-
-
Save guyjin/e559319f2a516612e705 to your computer and use it in GitHub Desktop.
js for classWeather Widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $(function() { | |
| $('.col-md-4').draggable(); | |
| $('.panel-heading').on('mousedown',function(){ | |
| $('.panel').css('cursor','move'); | |
| }); | |
| $('.panel').on('mouseup', function() { | |
| $(this).css('cursor','default'); | |
| }) | |
| // Trigger listener for city change | |
| $('#city').on('change', function() { | |
| if(checkID()){ | |
| var id = $('#city').val(); | |
| checkWeather(id); | |
| } else { | |
| hideStats(); | |
| } | |
| }); | |
| // Listener for Format Change | |
| $('.formats input:radio').on('change', function() { | |
| // console.log($('.formats input:radio[name=tempFormat]:checked').val()); | |
| var id = $('#city').val(); | |
| if(checkID()) { | |
| checkWeather(id); | |
| } else { | |
| hideStats(); | |
| } | |
| }) | |
| // function to run ajax call to open weather API | |
| function checkWeather(id) { | |
| $.ajax({ | |
| url: 'http://api.openweathermap.org/data/2.5/weather', | |
| type: 'GET', | |
| dataType: 'json', | |
| data: {id: id} | |
| }) | |
| .done(function (data) { | |
| updateName(data); | |
| updateTemp(data); | |
| updateIcon(data); | |
| }) | |
| .fail(function () { | |
| console.log("error"); | |
| }) | |
| .always(function () { | |
| console.log("complete"); | |
| }); | |
| // process data | |
| } | |
| // update city name | |
| function updateName(data) { | |
| var name = data.name; | |
| $('#cityName').text(name); | |
| } | |
| // update temp | |
| function updateTemp(data) { | |
| var newtemp = ''; | |
| var temp = data.main.temp; | |
| var format = $('.formats input:radio[name=tempFormat]:checked').val(); | |
| if(format == 'F') { | |
| newtemp = convertF(temp); | |
| } else { | |
| newtemp = convertC(temp); | |
| } | |
| $('#cityTemp').html(newtemp.toFixed(1) + "°"); | |
| } | |
| // update icon | |
| function updateIcon(data) { | |
| var iconCode = data.weather[0].icon; | |
| var imgsrc = "http://openweathermap.org/img/w/" + iconCode + ".png"; | |
| var desc = data.weather[0].description; | |
| var img = $('#icon img'); | |
| img.attr('src', imgsrc); | |
| img.attr('alt', desc); | |
| // img.addClass('hasSrc'); | |
| updateDesc(desc); | |
| } | |
| // update description | |
| function updateDesc(desc) { | |
| $('#description').text(desc); | |
| showStats(); | |
| } | |
| // convert to Farenheit 451 | |
| function convertF(temp) { | |
| var newtemp = (temp - 273.15) * 1.8000 + 32.00; | |
| return newtemp; | |
| } | |
| // convert to Celcius | |
| function convertC(temp) { | |
| var newtemp = (temp - 273.15); | |
| return newtemp; | |
| } | |
| // check for city id to be populated | |
| function checkID() { | |
| var id = $('#city').val(); | |
| if (id != '') { | |
| return true; | |
| } else { | |
| return false; | |
| } | |
| } | |
| // restore to defaults | |
| function resetForm() { | |
| // return to default state | |
| // $('.stats').removeClass('has') | |
| $('#cityTemp').html('-- °'); | |
| $('#cityName').text('City Weather'); | |
| var img = $('#icon img'); | |
| // img.removeClass('hasSrc'); | |
| img.attr('src',''); | |
| img.attr('alt','weather icon'); | |
| $('#description').empty(); | |
| } | |
| function showStats() { | |
| $('.stats span').animate({ | |
| opacity: 1 | |
| }, 1000); | |
| } | |
| function hideStats() { | |
| $('.stats span').animate({ | |
| opacity: 0 | |
| }, 1000, function(){ | |
| resetForm(); | |
| }) | |
| } | |
| }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment