-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget.js
99 lines (80 loc) · 2.74 KB
/
widget.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
"use strict";
/*global $,document,navigator*/
function round(x) {
return Math.round(x * 10000) / 10000.0;
}
function callWeatherService(pos) {
var req = $.ajax({
url: "/weather" + "?lat=" + round(pos.coords.latitude)
+ "&lon=" + round(pos.coords.longitude)
+ "&lang=" + navigator.language.substr(0, 2)
+ "&appid=3776c814478e05120788758173be5ecd",
dataType: "json",
timeout: 3000
});
req.success(function (data) {
var symb = $("#symbol"),
loctmp = $("#loctmp");
loctmp.text(data.name + ", " + Math.round(data.main.temp));
loctmp.append($("<span>°C</span>"));
symb.attr("alt", data.weather[0].main);
symb.attr("title", data.weather[0].main);
symb.attr("src", "img/weather-" + data.weather[0].icon + ".svg");
});
req.fail(function () {
var loctmp = $("#loctmp");
loctmp.text("!! Weather Service Failure !!");
});
}
function geolocate() {
var url = $.url(document.location.href),
pos;
if (url.param("lat") !== undefined && url.param("lon") !== undefined) {
pos = {
coords: {
latitude: url.param("lat"),
longitude: url.param("lon")
}
};
callWeatherService(pos);
} else if (navigator.geolocation !== undefined) {
navigator.geolocation.getCurrentPosition(
callWeatherService,
function (error) {
console.log("getCurrentPosition error = " + error.code + " " + error.message);
callWeatherService({
coords: {
latitude: 52.518611,
longitude: 13.408056
}
});
}, {
enableHighAccuracy: true,
timeout: 30000
});
}
}
$(document).ready(function () {
geolocate();
if (screen.width > screen.height) { /* landscape */
$("#symbol").css("height", "80%");
} else {
$("#symbol").css("width", "80%");
}
var $body = $("body"); // Cache this for performance
var setBodyScale = function () {
var scaleSource = $body.width(),
scaleFactor = 0.60,
maxScale = 800,
minScale = 60; // Tweak these values to taste
var fontSize = scaleSource * scaleFactor; // Multiply the width of the body by the scaling factor
if (fontSize > maxScale) fontSize = maxScale;
if (fontSize < minScale) fontSize = minScale; // Enforce the minimum and maximums
$body.css("font-size", fontSize + "%");
};
$(window).resize(function () {
setBodyScale();
});
setBodyScale();
$("#symbol").click(geolocate);
});