-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyellow.js
361 lines (312 loc) · 10.3 KB
/
yellow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
const apiKey = "1d433a5d633eb68797e98a83e4fa4b6d";
"use strict";
var Weather = function () {};
Weather.prototype.setDate = function () {
var d = new Date();
var dateString = d.toDateString();
var timeString = d.toTimeString();
$("#date").text(dateString + " " + timeString);
};
Weather.prototype.getLocation = function () {
//get location from ip address
var res = {
"city": "Buenos Aires",
"country": "Argentina",
"countryCode": "AR",
"query": "200.61.38.44",
"regionName": "Buenos Aires F.D.",
"status": "success"
};
if (res.status === "success") {
document.getElementById("location").value = res.city + ", " + res.countryCode;
this.location = res.city + ", " + res.countryCode;
this.currentWeather();
this.forecast();
}
};
Weather.prototype.setLocation = function () {
//set location from input text
$("#f_locator").on("submit", function (e) {
e.preventDefault();
this.location = $("#location").val();
this.currentWeather();
this.forecast();
this.loadAnimation();
}.bind(this));
};
Weather.prototype.getWeatherIcon = function (wId, sunrise, sunset) {
//get weather icon passing returned ID of openweather API. Optional sunrise and sunset time, to determine if is day or night type icon. Return an object with icon name and icon animation properties.
if (wId) {
var icon = {};
icon.name = "na";
icon.animation = "wi-scale";
function between(min, max, group, animation) {
if (wId >= min && wId < max) {
icon.name = group ? group : "na";
icon.animation = animation ? animation : "wi-scale";
}
}
between(200, 300, "thunderstorm", "wi-fade");
between(300, 400, "showers", "wi-moveY");
between(500, 600, "rain", "wi-moveY");
between(600, 700, "snow", "wi-moveY");
between(700, 800, "na", "wi-fade");
between(801, 900, "cloudy", "wi-moveX");
between(900, 1000, "na");
var cond = {
200: "storm-showers",
201: "storm-showers",
202: "thunderstorm",
500: "rain-mix",
501: "rain-mix",
502: "rain",
511: "sleet",
520: "rain-mix",
521: "rain-mix",
600: "snow",
611: "sleet",
701: "fog",
741: "fog",
905: "windy",
906: "hail"
};
var neutralCond = {
711: "smoke",
731: "sandstorm",
761: "dust",
762: "volcano",
781: "tornado",
900: "tornado",
902: "hurricane",
903: "snowflake-cold",
904: "hot",
958: "gale-warning",
959: "gale-warning",
960: "storm-warning",
961: "storm-warning",
962: "hurricane"
};
var dayCond = {
721: "haze",
800: "sunny"
};
var nightCond = {
800: "clear",
701: "fog",
741: "fog"
};
icon.name = cond[wId] ? cond[wId] : icon.name;
icon.name = neutralCond[wId] ? neutralCond[wId] : icon.name;
icon.name = dayCond[wId] ? dayCond[wId] : icon.name;
var time = "day";
if (sunrise && sunset) {
var now = Date.now() / 1000;
var srDate = new Date(sunrise * 1000);
if (srDate.getDate() == new Date().getDate()) {
if (now <= sunrise && now >= sunset) {
time = nightCond[wId] ? "night" : "night-alt";
icon.name = nightCond[wId] ? nightCond[wId] : icon.name;
}
} else {
time = nightCond[wId] ? "night" : "night-alt";
icon.name = nightCond[wId] ? nightCond[wId] : icon.name;
}
}
if (icon.name != "na" && !neutralCond[wId]) {
icon.name = "wi-" + time + "-" + icon.name;
} else {
icon.name = "wi-" + icon.name;
}
icon.animation = icon.name == "wi-day-sunny" ? "wi-rotate" : icon.animation;
return icon;
}
}; //end getWeatherIcon
Weather.prototype.displayWeatherIcon = function (selector, icon) {
//display weather icon. first arg is where to display. Second arg is what returned from getWeatherIcon method.
if (selector && typeof icon == "object") {
if (icon.name != "na") {
$(selector).addClass(icon.name);
animate(selector, icon.animation, 2000, 0, "linear", "infinite");
}
}
};
Weather.prototype.currentWeather = function () {
//get current weather from openweather API, format, and display it.
if (this.location) {
function setMain(res) {
if (res.main) {
$("#temperature").text(Math.round(res.main.temp) + "°");
$("#description").text(res.weather[0].description);
if (res.main.humidity) {
$("#humidity").text(res.main.humidity);
} else {
$("#humidity").text("0");
}
}
}
$.getJSON("https://api.openweathermap.org/data/2.5/weather", {
q: this.location,
units: "metric",
appid: "1d433a5d633eb68797e98a83e4fa4b6d"
}, function (json) {
var wId = json.weather[0].id;
if (wId) {
var icon = this.getWeatherIcon(wId, json.sys.sunrise, json.sys.sunset);
this.displayWeatherIcon("#wicon-main", icon);
}
setMain(json);
}.bind(this));
}
}; //end currentWeather
Weather.prototype.forecast = function () {
//get forecast (4 days) weather from openweather API, format, and display it.
function setForecast(res) {
this.daily = [];
var list = res.list;
for (var i = 0, len = list.length; i < len; i++) {
this.daily[i] = this.daily[i] ? this.daily[i] : {};
this.daily[i].maxTemp = Math.round(list[i].temp.max);
this.daily[i].minTemp = Math.round(list[i].temp.min);
this.daily[i].day = new Date(list[i].dt * 1000).getDay();
var iconId = list[i].weather[0].id;
this.daily[i].icon = this.getWeatherIcon(iconId);
}
}
function displayForecast() {
var days = ["Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat"],
_this = this;
$(".days-box").children(".col-xs-3").each(function (index) {
$(this).children('.day').text(days[_this.daily[index].day]);
$(this).find('.d-min-temp').text(_this.daily[index].minTemp + "°");
$(this).find('.d-max-temp').text(_this.daily[index].maxTemp + "°");
$(this).find('.wi').addClass(_this.daily[index].icon.name);
});
}
$.getJSON("https://api.openweathermap.org/data/2.5/forecast/daily", {
q: this.location,
cnt: 4,
units: "metric",
appid: "bc1301b0b23fe6ef52032a7e5bb70820"
}, function (json) {
setForecast.call(this, json);
displayForecast.call(this);
}.bind(this));
};
Weather.prototype.setUnit = function () {
//Switch between Celsius and Farhenheit
var prevUnit = "C";
$("#unit-switch").on("click", function () {
var newUnit = prevUnit == "C" ? "F" : "C";
$("span:contains('°')").each(function (index, el) {
var temp_current = parseFloat($(el).text());
if (newUnit == "F") {
var temp_new = Math.round(temp_current * 1.8 + 32);
} else if (newUnit == "C") {
var temp_new = Math.round((temp_current - 32) / 1.8);
}
$(el).text(temp_new + "°");
animate(el, "fadeIn", 400, 0, "ease-out");
});
prevUnit = newUnit;
});
};
Weather.prototype.loadAnimation = function () {
$(".loading").css("display", "block");
var countAjax = 0;
$(document).ajaxComplete(function () {
countAjax++;
if (countAjax == 2) {
$(".loading").fadeOut();
loadTooltips();
animate(".days-box", "scale", 400, 500, "ease-out");
var delayAnim = 1300;
$(".days-box").children(".col-xs-3").each(function () {
animate(this, "fadeIn", 350, delayAnim, "ease-out");
delayAnim += 350;
});
}
});
}; //Animation using CSS @keyframes
function animate(selector, keyFrameName, duration, delay = 0, timing = "ease", iteration = 1) {
//jQuery selector; CSS keyframes name; duration in ms; delay in ms;
$(selector).css({
"visibility": "hidden"
});
setTimeout(function () {
$(selector).css({
"visibility": "visible"
});
$(selector).css({
"animation": keyFrameName + " " + duration + "ms " + timing + " " + iteration
});
}, delay);
if (iteration != "infinite") {
setTimeout(function () {
$(selector).css({
"animation": ""
});
}, (delay + duration) * iteration);
}
}
;
/* https://codepen.io/sinapsis7 */
function loadTooltips() {
//search for elements that have data-tooltips attributes on the web page, and display it.
$("[data-tooltip]").each(function () {
var tag = $(this)[0].tagName.toLowerCase();
var tooltip = $(this).attr("data-tooltip");
var tooltipParentH = $(this).outerHeight();
var parentPosition = $(this).position();
$(tooltip).insertAfter(this);
$(tooltip).css({
"max-width": document.body.clientWidth - parentPosition.left - 5 + "px",
"transition": "opacity 0.3s"
});
function showTooltip() {
$(tooltip).css({
"visibility": "visible",
"opacity": 1,
"top": parentPosition.top + tooltipParentH + 10 + "px",
"left": parentPosition.left + "px"
});
}
function hideTooltip() {
$(tooltip).on("mouseenter", stopTimerHide);
function stopTimerHide() {
clearTimeout(timerHide);
$(tooltip).on("mouseleave", hideTooltip);
}
var timerHide = setTimeout(function () {
$(tooltip).css({
"visibility": "hidden",
"opacity": 0
});
}, 100);
}
var _this = $(this);
if (tag == "input") {
$(this).on("focus", function () {
showTooltip();
_this.off("mouseleave", hideTooltip);
});
$(this).on("blur", function () {
hideTooltip();
_this.on("mouseleave", hideTooltip);
});
$(this).on("mouseenter", showTooltip);
$(this).on("mouseleave", hideTooltip);
} else {
$(this).on("mouseenter", showTooltip);
$(this).on("mouseleave", hideTooltip);
}
});
} //Run
var weather = new Weather();
$(document).ready(function () {
$("#unit-switch").prop('checked', true);
weather.loadAnimation();
weather.setDate();
weather.getLocation();
weather.setLocation();
weather.setUnit();
});