1/** 2 * Name: Location Weather 3 * Dependencies: OpenWeatherMap API for Location Weather plugin. 4 * Author: Rubel Miah 5 * Author URL: http://rubelmiah.com/ 6 */ 7 8; (function ($) { 9 10 $.fn.locationWeather = function (options) { 11 12 // return if no element was bound 13 // so chained events can continue 14 if (!this.length) { 15 return this; 16 } 17 18 // define default parameters 19 const defaults = { 20 descriptionTarget: null, 21 maxTemperatureTarget: null, 22 minTemperatureTarget: null, 23 windSpeedTarget: null, 24 humidityTarget: null, 25 sunriseTarget: null, 26 sunsetTarget: null, 27 placeTarget: null, 28 iconTarget: null, 29 customIcons: null, 30 units: 'c', 31 city: null, 32 lat: null, 33 lng: null, 34 key: null, 35 weatherDate: null, 36 lang: 'en', 37 success: function () { }, 38 error: function (message) { } 39 } 40 41 // define plugin 42 const plugin = this; 43 44 // define element 45 const el = $(this); 46 47 // define settings 48 plugin.settings = {} 49 50 // merge defaults and options 51 plugin.settings = $.extend({}, defaults, options); 52 53 // define settings namespace 54 const s = plugin.settings; 55 56 // date converter 57 function dateConverter(t) { 58 var a = new Date(t * 1000); 59 var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; 60 var year = a.getFullYear(); 61 var month = months[a.getMonth()]; 62 var date = a.getDate(); 63 return date + ' ' + month + ', ' + year; 64 } 65 66 // format time function 67 const formatTime = function (unixTimestamp) { 68 69 // define milliseconds using unix time stamp 70 const milliseconds = unixTimestamp * 1000; 71 72 // create a new date using milliseconds 73 const date = new Date(milliseconds); 74 75 // define hours 76 let hours = date.getHours(); 77 78 // if hours are greater than 12 79 if (hours > 12) { 80 81 // calculate remaining hours in the day 82 let hoursRemaining = 24 - hours; 83 84 // redefine hours as the reamining hours subtracted from a 12 hour day 85 hours = 12 - hoursRemaining; 86 } 87 88 // define minutes 89 let minutes = date.getMinutes(); 90 91 // convert minutes to a string 92 minutes = minutes.toString(); 93 94 // if minutes has less than 2 characters 95 if (minutes.length < 2) { 96 97 // add a 0 to minutes 98 minutes = 0 + minutes; 99 } 100 101 // construct time using hours and minutes 102 let time = hours + ':' + minutes; 103 104 return time; 105 } 106 107 // define basic api endpoint 108 let apiURL = 'https://api.openweathermap.org/data/2.5/weather?lang=' + s.lang; 109 110 let weatherObj; 111 112 let temperature; 113 let minTemperature; 114 let maxTemperature; 115 116 // if city isn't null 117 if (s.city != null) { 118 119 // define API url using city (and remove any spaces in city) 120 apiURL += '&q=' + s.city; 121 122 } else if (s.lat != null && s.lng != null) { 123 124 // define API url using lat and lng 125 apiURL += '&lat=' + s.lat + '&lon=' + s.lng; 126 } 127 128 // if api key was supplied 129 if (s.key != null) { 130 131 // append api key paramater 132 apiURL += '&appid=' + s.key; 133 } 134 135 $.ajax({ 136 type: 'GET', 137 url: apiURL, 138 dataType: 'jsonp', 139 success: function (data) { 140 141 if (data) { 142 143 // if units are 'f' 144 if (s.units == 'f') { 145 146 // define temperature as fahrenheit 147 temperature = Math.round(((data.main.temp - 273.15) * 1.8) + 32) + '°F'; 148 149 // define min temperature as fahrenheit 150 minTemperature = Math.round(((data.main.temp_min - 273.15) * 1.8) + 32) + '°F'; 151 152 // define max temperature as fahrenheit 153 maxTemperature = Math.round(((data.main.temp_max - 273.15) * 1.8) + 32) + '°F'; 154 155 } else { 156 157 // define temperature as celsius 158 temperature = Math.round(data.main.temp - 273.15) + '°C'; 159 160 // define min temperature as celsius 161 minTemperature = Math.round(data.main.temp_min - 273.15) + '°C'; 162 163 // define max temperature as celsius 164 maxTemperature = Math.round(data.main.temp_max - 273.15) + '°C'; 165 } 166 167 weatherObj = { 168 city: `${data.name}, ${data.sys.country}`, 169 temperature: { 170 current: temperature, 171 min: minTemperature, 172 max: maxTemperature, 173 units: s.units.toUpperCase() 174 }, 175 description: data.weather[0].description, 176 windspeed: `${Math.round(data.wind.speed)} Mps`, 177 humidity: `${data.main.humidity}%`,
178 sunrise: `${formatTime(data.sys.sunrise)} AM`, 179 sunset: `${formatTime(data.sys.sunset)} PM` 180 }; 181 182 // set temperature 183 el.html(temperature); 184 185 // if minTemperatureTarget isn't null 186 if (s.minTemperatureTarget != null) { 187 188 // set minimum temperature 189 $(s.minTemperatureTarget).text(minTemperature); 190 } 191 192 // if maxTemperatureTarget isn't null 193 if (s.maxTemperatureTarget != null) { 194 195 // set maximum temperature 196 $(s.maxTemperatureTarget).text(maxTemperature); 197 } 198 199 // set weather description 200 $(s.descriptionTarget).text(weatherObj.description); 201 202 // set weather date 203 $(s.weatherDate).text(dateConverter(data.dt)); 204 205 // if iconTarget and default weather icon aren't null 206 if (s.iconTarget != null && data.weather[0].icon != null) { 207 208 let iconURL; 209 210 // if customIcons isn't null 211 if (s.customIcons != null) { 212 213 // define the default icon name 214 const defaultIconFileName = data.weather[0].icon; 215 216 let timeOfDay; 217 let iconName; 218 219 // if default icon name contains the letter 'd' 220 if (defaultIconFileName.indexOf('d') != -1) { 221 222 // define time of day as day 223 timeOfDay = 'day'; 224 225 } else { 226 227 // define time of day as night 228 timeOfDay = 'night'; 229 } 230 231 // if icon is clear sky 232 if (defaultIconFileName == '01d' || defaultIconFileName == '01n') { 233 iconName = 'clear'; 234 } 235 236 // if icon is clouds 237 if (defaultIconFileName == '02d' || defaultIconFileName == '02n' || defaultIconFileName == '03d' || defaultIconFileName == '03n' || defaultIconFileName == '04d' || defaultIconFileName == '04n') { 238 iconName = 'clouds'; 239 } 240 241 // if icon is shower rain 242 if (defaultIconFileName == '09d' || defaultIconFileName == '09n') { 243 iconName = 'shower-rain'; 244 } 245 246 // if icon is rain 247 if (defaultIconFileName == '10d' || defaultIconFileName == '10n') { 248 iconName = 'rain'; 249 } 250 251 // if icon is thunderstorm 252 if (defaultIconFileName == '11d' || defaultIconFileName == '11n') { 253 iconName = 'storm'; 254 } 255 256 // if icon is snow 257 if (defaultIconFileName == '13d' || defaultIconFileName == '13n') { 258 259 iconName = 'snow'; 260 } 261 262 // if icon is mist 263 if (defaultIconFileName == '50d' || defaultIconFileName == '50n') { 264 iconName = 'mist'; 265 } 266 267 // define custom icon URL 268 iconURL = `${s.customIcons}${timeOfDay}/${iconName}.png`; 269 270 } else { 271 272 // define icon URL using default icon 273 iconURL = `https://openweathermap.org/img/w/${data.weather[0].icon}.png`; 274 } 275 276 // set iconTarget src attribute as iconURL 277 $(s.iconTarget).attr('src', iconURL); 278 } 279 280 // if placeTarget isn't null 281 if (s.placeTarget != null) { 282 283 // set place 284 $(s.placeTarget).text(weatherObj.city); 285 } 286 287 // if windSpeedTarget isn't null 288 if (s.windSpeedTarget != null) { 289 290 // set wind speed 291 $(s.windSpeedTarget).text(weatherObj.windspeed); 292 } 293 294 // if humidityTarget isn't null 295 if (s.humidityTarget != null) { 296 297 // set humidity 298 $(s.humidityTarget).text(weatherObj.humidity); 299 } 300 301 // if sunriseTarget isn't null 302 if (s.sunriseTarget != null) { 303 304 // set sunrise 305 $(s.sunriseTarget).text(weatherObj.sunrise); 306 } 307 308 // if sunriseTarget isn't null 309 if (s.sunsetTarget != null) { 310 311 // set sunset 312 $(s.sunsetTarget).text(weatherObj.sunset); 313 } 314 315 // run success callback 316 s.success.call(this, weatherObj); 317 } 318 }, 319 error: function (jqXHR, textStatus, errorThrown) { 320 // run error callback 321 s.error.call(this, { 322 error: textStatus 323 }); 324 } 325 }); 326 } 327})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.