(function( $ ){
$.fn.simpleopenweather = function(options){
var defaults = {
template : '<div class="simpleopenweather-place"> {{icon}} {{place}}: {{sky}} </div><div><span class="simpleopenweather-temperature">Temp: {{temperature.current}} ºC</span><span class="simpleopenweather-humidity"> Humidity: {{humidity}}%</span></div><div>Temp min/max: <span class="simpleopenweather-temperature-min">{{temperature.min}} ºC </span> /<span class="simpleopenweather-temperature-max">{{temperature.max}} ºC </span></div><div>Wind: <span class="simpleopenweather-wind-speed">{{wind.speed}} m/s</span> <span class="simpleopenweather-wind-direction">{{wind.direction}}º</span></div><div><span class="simpleopenweather-cloudiness">Cloudiness: {{cloudiness}}% </span> <span class="simpleopenweather-pressure">Pressure: {{pressure}} kPa</span></div>',
noweather : '<p>no weather report was found for that place!</p>',
error : '<p>something went wrong!</p>',
latitude : 0,
longitude : 0,
units : 'imperial',
lang : 'en',
iconset : 'http://openweathermap.org/img/w/',
iconfont : false,
weathericonsfont: false //Maintained at http://erikflowers.github.io/weather-icons
}
var settings = $.extend(defaults, options);
return this.each(function() {
var item = $(this);
var openweathermap_url = "http://api.openweathermap.org/data/2.5/weather";
var icons = {"01d": "B", "01n": "C", "02d": "H", "02n": "I", "03d": "N", "03n": "N", "04d": "Y", "04n": "Y", "09d": "Q", "09n": "Q", "10d": "R", "10n": "R", "11d": "0", "11n": "0", "13d": "W", "13n": "W", "50d": "J", "50n": "K"};
if(item.attr("data-simpleopenweather-city")){
openweathermap_url += "?q=" + item.attr("data-simpleopenweather-city");
} else if(item.attr("data-simpleopenweather-coord")){
latlon = item.attr("data-simpleopenweather-coord").split(',');
openweathermap_url += "?lat="+latlon[0]+"&lon="+latlon[1]+"&cnt=1";
} else if(settings.latitude != 0 && settings.longitude != 0){
openweathermap_url += "?lat="+settings.latitude+"&lon="+settings.longitude+"&cnt=1";
}
if(settings.lang != ''){
openweathermap_url += "&lang="+settings.lang;
}
if(settings.units == 'metric'){
openweathermap_url += "&units="+'metric';
} else{
openweathermap_url += "&units="+'imperial';
}
console.log(openweathermap_url);
$.ajax({
type : "GET",
dataType : "jsonp",
url : openweathermap_url,
success : function(weather){
if(!weather){
item.html(settings.noweather);
return;
}
if(settings.iconfont && settings.weathericonsfont==false)
{
var icon_name = icons[weather.weather[0].icon];
icon_name='<i class="meteocon">'+icon_name+'</i>';
}
else if(settings.weathericonsfont && settings.iconfont==false)
{
//get font icon from Weather Icons
var icon_name = getFontIcon(weather.weather[0].icon);
}
else
{
var icon_name = settings.iconset+weather.weather[0].icon+".png";
icon_name='<img src="'+icon_name+'"></img>';
}
var info = {
temp : {
'current' : weather.main.temp.toFixed(1),
'min' : weather.main.temp_min.toFixed(1),
'max' :weather.main.temp_max.toFixed(1),
},
humidity : weather.main.humidity,
pressure : weather.main.pressure,
wind : {
'speed': weather.wind.speed.toFixed(1),
'direction': weather.wind.deg.toFixed(1),
},
cloudiness : weather.clouds ? weather.clouds.all : "N/A",
sky : weather.weather[0].description,
icon : icon_name,
place : weather.name
};
item.html(settings.template.replace(/{{place}}/ig, info.place)
.replace(/{{temperature}}/ig, info.temp.current)
.replace(/{{temperature.current}}/ig, info.temp.current)
.replace(/{{temperature.min}}/ig, info.temp.min)
.replace(/{{temperature.max}}/ig, info.temp.max)
.replace(/{{humidity}}/ig, info.humidity)
.replace(/{{pressure}}/ig, info.pressure)
.replace(/{{wind.speed}}/ig, info.wind.speed)
.replace(/{{wind.direction}}/ig, info.wind.direction)
.replace(/{{cloudiness}}/ig, info.cloudiness)
.replace(/{{icon}}/ig, info.icon)
//.replace(/{{icon}}/ig, ((settings.iconfont) ? '<i class="meteocon">'+info.icon+'</i>' : '<img src="'+info.icon+'"></img>'))
.replace(/{{sky}}/ig, info.sky));
},
error : function(){
item.html(settings.error);
}
});
//get icon correspondency from
//Weather Icons
//Maintained at http://erikflowers.github.io/weather-icons
function getFontIcon(iconid){
var fontIcon;
switch (iconid){
case '01d':
fontIcon = '<i class="wi wi-day-sunny"></i>';
break;
case '01n':
fontIcon = '<i class="wi wi-night-clear"></i>';
break;
case '02d':
fontIcon = '<i class="wi wi-day-cloudy"></i>';
break;
case '02n':
fontIcon = '<i class="wi wi-night-cloudy"></i>';
break;
case '03d':
fontIcon = '<i class="wi wi-cloud"></i>';
break;
case '03n':
fontIcon = '<i class="wi wi-cloud"></i>';
break;
case '04d':
fontIcon = '<i class="wi wi-cloudy"></i>';
break;
case '04n':
fontIcon = '<i class="wi wi-cloudy"></i>';
break;
case '09d':
fontIcon = '<i class="wi wi-sprinkle"></i>';
break;
case '09n':
fontIcon = '<i class="wi wi-sprinkle"></i>';
break;
case '10d':
fontIcon = '<i class="wi wi-day-sprinkle"></i>';
break;
case '10n':
fontIcon = '<i class="wi wi-night-alt-sprinkle"></i>';
break;
case '11d':
fontIcon = '<i class="wi wi-day-lightning"></i>';
break;
case '11n':
fontIcon = '<i class="wi wi-night-alt-lightning"></i>';
break;
case '13d':
fontIcon = '<i class="wi wi-day-snow"></i>';
break;
case '13n':
fontIcon = '<i class="wi wi-night-snow"></i>';
break;
case '50d':
fontIcon = '<i class="wi wi-day-fog"></i>';
break;
case '50n':
fontIcon = '<i class="wi wi-fog"></i>';
break;
case 'r':
fontIcon = '<i class="wi wi-sprinkles"></i>';
break;
case 'sn50':
fontIcon = '<i class="wi wi-snowflake-cold"></i>';
break;
case 't50':
fontIcon = '<i class="wi wi-lightning"></i>';
break;
case 'w50':
fontIcon = '<i class="wi wi-strong-wind"></i>';
break;
default:
fontIcon = '<i class="wi wi-alien"></i>';
break;
}
return fontIcon;
}
});
}
})( jQuery );