-
Notifications
You must be signed in to change notification settings - Fork 1
/
weatherWidget.min.js
1 lines (1 loc) · 2.78 KB
/
weatherWidget.min.js
1
!function(e){"use strict";jQuery.support.cors=!0,e.fn.weatherWidget=function(t){t=e.extend({zipCode:""},t);var r={apiURL:"http://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20location%3D%22{{zipCode}}%22&format=json",loadingTemplate:'<div class="weather-widget-loading">Loading...</div>',errorTemplate:'<div class="weather-widget-error"><strong>Error:</strong><br/>{{errorMessage}}</div>',htmlTemplate:'<div class="weather-widget-title">{{title}}</div> <div class="weather-widget-current-conditions"> <div class="weather-widget-current-temp">{{currentTemp}}°</div> <div class="weather-widget-current-image-container"> <div class="weather-widget-current-image"><img src="{{imageURL}}" /></div> <div class="weather-widget-current-image-text">{{imageText}}</div> </div> </div> <div class="weather-widget-forecast"> {{forecastItems}} </div>',forecastItemTemplate:'<div class="weather-widget-forecast-item"> <div class="weather-widget-forecast-day">{{forecastDay}}</div> <div class="weather-widget-forecast-temp">{{forecastHigh}}° / {{forecastLow}}°</div> </div>'},a=function(t,a){var s=e(a);s.addClass("weather-widget"),s.html(r.loadingTemplate),i(s)},i=function(a){e.ajax({type:"GET",url:r.apiURL.replace("{{zipCode}}",t.zipCode),dataType:"json",success:function(e){if(e.query&&e.query.count>0&&e.query.results&&e.query.results.channel){var t=e.query.results.channel.item;t&&t.forecast&&5==t.forecast.length?c(a,t):t&&t.title?c(a,!1,t.title):c(a,!1,"Weather information was not found.")}else c(a,!1,"Weather information was not found.")},error:function(){c(a,!1)}})},s=function(e){if(null==e||"string"!=typeof e)return"";var t=e.indexOf("Conditions for");if(-1==t)return"";t+=15;var r=e.indexOf(" at ");return-1==r||t>=r?"":e.substring(t,r)},o=function(e){var t=e.match(/src\s*=\s*"(.+?)"/i);return t&&t.length>1?t[1]:""},c=function(e,t,a){var i="";if(t===!1)a=a?a:"Weather data could not be loaded.",i=r.errorTemplate.replace("{{errorMessage}}",a);else{for(var c=s(t.title),n=o(t.description),d="",l=0;l<t.forecast.length;l++){var f=t.forecast[l];d+=r.forecastItemTemplate.replace("{{forecastDay}}",f.day).replace("{{forecastHigh}}",f.high).replace("{{forecastLow}}",f.low)}i=r.htmlTemplate.replace("{{title}}",c).replace("{{currentTemp}}",t.condition.temp).replace("{{imageURL}}",n).replace("{{imageText}}",t.condition.text).replace("{{forecastItems}}",d)}e.html(i)};return this.each(a)}}(jQuery);