/** * Plugin: jquery.zWeatherFeed * * Version: 1.3.0 * (c) Copyright 2011-2014, Zazar Ltd * * Description: jQuery plugin for display of Yahoo! Weather feeds * * History: * 1.3.0 - Added refresh timer * 1.2.1 - Handle invalid locations * 1.2.0 - Added forecast data option * 1.1.0 - Added user callback function * New option to use WOEID identifiers * New day/night CSS class for feed items * Updated full forecast link to feed link location * 1.0.3 - Changed full forecast link to Weather Channel due to invalid Yahoo! link Add 'linktarget' option for forecast link * 1.0.2 - Correction to options / link * 1.0.1 - Added hourly caching to YQL to avoid rate limits * Uses Weather Channel location ID and not Yahoo WOEID * Displays day or night background images **/ (function($){ $.fn.weatherfeed = function(locations, options, fn) { // Set plugin defaults var defaults = { unit: 'c', image: true, country: false, highlow: true, wind: false, humidity: false, visibility: false, sunrise: false, sunset: false, forecast: false, link: true, showerror: true, linktarget: '_self', woeid: false, refresh: 0 }; var options = $.extend(defaults, options); var row = 'odd'; // Functions return this.each(function(i, e) { var $e = $(e); // Add feed class to user div if (!$e.hasClass('weatherFeed')) $e.addClass('weatherFeed'); // Check and append locations if (!$.isArray(locations)) return false; var count = locations.length; if (count > 10) count = 10; var locationid = ''; for (var i=0; i 0) { // Set timer interval for scrolling var interval = setInterval(function(){ sendRequest(query, api, options); }, options.refresh * 60000); } // Function to gather new weather data function sendRequest(query, api, options) { // Reset odd and even classes row = 'odd'; // Clear user div $e.html(''); $.ajax({ type: 'GET', url: api, dataType: 'json', success: function(data) { if (data.query) { if (data.query.results.channel.length > 0 ) { console.log(data.query.results); console.log(api); // Multiple locations var result = data.query.results.channel.length; for (var i=0; iWeather information unavailable

'); } }, error: function(data) { if (options.showerror) $e.html('

Weather request failed

'); } }); }; // Function to each feed item var _process = function(e, feed, options) { var $e = $(e); // Check for invalid location if (feed.description != 'Yahoo! Weather Error') { // Format feed items // var wd = feed.wind.direction; // if (wd>=348.75&&wd<=360){wd="N"};if(wd>=0&&wd<11.25){wd="N"};if(wd>=11.25&&wd<33.75){wd="NNE"};if(wd>=33.75&&wd<56.25){wd="NE"};if(wd>=56.25&&wd<78.75){wd="ENE"};if(wd>=78.75&&wd<101.25){wd="E"};if(wd>=101.25&&wd<123.75){wd="ESE"};if(wd>=123.75&&wd<146.25){wd="SE"};if(wd>=146.25&&wd<168.75){wd="SSE"};if(wd>=168.75&&wd<191.25){wd="S"};if(wd>=191.25 && wd<213.75){wd="SSW"};if(wd>=213.75&&wd<236.25){wd="SW"};if(wd>=236.25&&wd<258.75){wd="WSW"};if(wd>=258.75 && wd<281.25){wd="W"};if(wd>=281.25&&wd<303.75){wd="WNW"};if(wd>=303.75&&wd<326.25){wd="NW"};if(wd>=326.25&&wd<348.75){wd="NNW"}; var wf = feed.item.forecast[0]; // Determine day or night image wpd = feed.item.pubDate; n = wpd.indexOf(":"); tpb = _getTimeAsDate(wpd.substr(n-2,8)); tsr = _getTimeAsDate(feed.astronomy.sunrise); tss = _getTimeAsDate(feed.astronomy.sunset); // Get night or day if (tpb>tsr && tpb'; if (options.country) html += '
'+ feed.location.country +'
'; html += '
'+ feed.item.condition.temp +'°
'; html += '
'+ feed.item.condition.text +'
'; // Add optional data if (options.highlow) html += '
High: '+ wf.high +'° Low: '+ wf.low +'°
'; if (options.wind) html += '
Wind: '+ wd +' '+ feed.wind.speed + feed.units.speed +'
'; if (options.humidity) html += '
Humidity: '+ feed.atmosphere.humidity +'
'; if (options.visibility) html += '
Visibility: '+ feed.atmosphere.visibility +'
'; if (options.sunrise) html += '
Sunrise: '+ feed.astronomy.sunrise +'
'; if (options.sunset) html += '
Sunset: '+ feed.astronomy.sunset +'
'; // Add item forecast data if (options.forecast) { html += '
'; var wfi = feed.item.forecast; for (var i=0; i'; html += '
'+ wfi[i].day +'
'; html += '
'+ wfi[i].date +'
'; html += '
'+ wfi[i].text +'
'; html += '
High: '+ wfi[i].high +' Low: '+ wfi[i].low +'
'; html += '
' } html += '' } if (options.link) html += ''; } else { var html = '
'; html += '
City not found
'; } html += '
'; // Alternate row classes if (row == 'odd') { row = 'even'; } else { row = 'odd'; } // Apply new weather content $e.append(html); }; // Get time string as date var _getTimeAsDate = function(t) { d = new Date(); r = new Date(d.toDateString() +' '+ t); return r; }; }); }; })(jQuery);