-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
223 lines (196 loc) · 9.36 KB
/
script.js
File metadata and controls
223 lines (196 loc) · 9.36 KB
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
module.exports = {
// ################################# BEGIN SETTINGS #################################
// To get your own API key, go to https://developer.forecast.io/ #
api_key: '<API_KEY>', // Put your Forcast.io api key inside the quotes here #
refresh_rate: 120000, // Time in milliseconds between refreshes #
lat: 'auto', // Options are auto, or a valid latitude (auto doesn't always work) #
lon: 'auto', // Options are auto, or a valid longitude (auto doesn't always work) #
units: 'auto', // Options are us, si, ca, uk, auto #
onclick_link: 'http://www.wunderground.com', // Link to open when widget is clocked #
// ################################## END SETTINGS ##################################
command: '',
refreshFrequency: false,
// Runs once at the load of the script
afterRender: function () {
// A workaround to use scss instead of stylus
var widget = $('#forecast-widget-index-js');
for (var i = 0; i < document.styleSheets.length; i++) {
if (document.styleSheets[i].ownerNode.nextElementSibling != null && document.styleSheets[i].ownerNode.nextElementSibling.id === 'forecast-widget-index-js') {
for (var j in document.styleSheets[i].rules[0].style) {
if (typeof document.styleSheets[i].rules[0].style[j] === 'string' && document.styleSheets[i].rules[0].style[j] !== '') {
widget.css(j, document.styleSheets[i].rules[0].style[j]);
}
}
break;
}
}
// Keeping a constant context through varying scopes
var uber = this;
// Show an error if no API key is set
if (uber.api_key === 'API_KEY') {
widget.html(uber.api_instructions);
widget.css('text-shadow', '1px 1px 15px rgb(0, 0, 0)');
widget.css('font-weight', 900);
widget.css('font-size', '20px');
return;
}
// Executes shell command (curl)
var ready = function () {
uber.run(uber.command, function () {});
uber.refresh();
setInterval(function () {
uber.run(uber.command, function () {});
uber.refresh();
}, uber.refresh_rate);
};
// Gets the location and makes the shell command to send to forecast.io
if (uber.lat == 'auto' && uber.lon == 'auto') {
geolocation.getCurrentPosition(function (e) {
uber.lat = e.position.coords.latitude;
uber.lon = e.position.coords.longitude;
uber.command = uber.makeCommand(uber.api_key, uber.lat + ',' + uber.lon, uber.units);
ready();
});
} else {
uber.command = uber.makeCommand(uber.api_key, uber.lat + ',' + uber.lon, uber.units);
ready();
}
// Opens a specified link when the widget is clicked
$('#forecast-widget-index-js').click(function () {
uber.run('open ' + uber.onclick_link, function () {});
});
},
// Makes the shell command to execute (curl)
makeCommand: function (api_key, location, units) {
var exclude = 'minutely,alerts,flags';
return 'curl -sS "https://api.forecast.io/forecast/' + api_key + '/' + location + '?units=' + units + '&exclude=' + exclude + '"';
},
// Runs every <refresh_rate> milliseconds
update: function (output) {
// Make sure that we have valid JSON (first run is empty, no api key is Forbidden)
if (output && output !== '' && output !== 'Forbidden\n') {
output = JSON.parse(output);
// Temperature direction (rising or falling)
var next_hour_temp = output.hourly.data[1].temperature; // Next hour's temp
var current_temp = output.currently.temperature; // Current temp
if (next_hour_temp > current_temp) {
$('#temp-direction').text('and rising');
} else {
$('#temp-direction').text('and falling');
}
// Temperature and summary
$('.fe-temp').text(Math.round(current_temp));
$('.fe-summary').text(output.currently.summary);
// Wind speed and bearing
var wind_speed = Math.round(output.currently.windSpeed);
var wind_speed_units = this.unit_labels[this.units || 'us'].speed;
var wind_bearing = this.bearing(output.currently.windBearing);
$('.fe-wind').text('Wind: ' + wind_speed + ' ' + wind_speed_units + ' (' + wind_bearing + ')');
// Icon
this.changeIcon($('#fe-current-icon'), output.currently.icon);
// TODO: Check if there is a output.weekly.high / low
// Find the max and min temperatures for the week
var temp_min_week = 1000;
var temp_max_week = -1000;
for (var day in output.daily.data) {
if (output.daily.data[day].temperatureMax > temp_max_week) {
temp_max_week = output.daily.data[day].temperatureMax;
}
if (output.daily.data[day].temperatureMin < temp_min_week) {
temp_min_week = output.daily.data[day].temperatureMin;
}
}
for (day in output.daily.data) {
// Change current day's name
if (day === '0') {
$('#day' + day).find('.day-text').text('Tod');
} else {
$('#day' + day).find('.day-text').text(this.dayMapping[new Date(output.daily.data[day].time * 1000).getDay()]);
}
// Set day's weather icon
this.changeIcon($('#day' + day).find('.weather-icon'), output.daily.data[day].icon);
// Temperature bars
var day_high = Math.round(output.daily.data[day].temperatureMax) + '°';
var day_low = Math.round(output.daily.data[day].temperatureMin) + '°';
var day_high_rel = this.map(output.daily.data[day].temperatureMax, temp_min_week, temp_max_week, 0, 1);
var day_low_rel = this.map(output.daily.data[day].temperatureMin, temp_min_week, temp_max_week, 0, 1);
var height = 100;
$('#day' + day).find('.bar').attr('data-content-high', day_high);
$('#day' + day).find('.bar').attr('data-content-low', day_low);
$('#day' + day).find('.bar').css('top', height - (day_high_rel * height));
$('#day' + day).find('.bar').css('bottom', day_low_rel * height);
}
// Makes the widget visible after it's loaded
$('.fe-forecast').removeClass('loading');
} else {
// Show an error message if API key is invalid
if (output === 'Forbidden\n') {
var widget = $('#forecast-widget-index-js');
widget.html(this.api_instructions);
widget.css('text-shadow', '1px 1px 15px rgb(0, 0, 0)');
widget.css('font-weight', 900);
widget.css('font-size', '20px');
}
}
},
// Fades icons out and in (and handles loading them)
changeIcon: function (element, icon) {
if (element.css('-webkit-mask-image') !== 'url(' + window.location.origin + '/forecast.widget/icons/' + icon + '.png)') {
if (element.css('-webkit-mask-image') === 'url(' + window.location.origin + '/)') {
element.addClass('loading');
element.addClass('prepare-loading');
element.css('-webkit-mask-image', 'url(forecast.widget/icons/' + icon + '.png)');
element.removeClass('loading');
setTimeout(function () {
element.removeClass('prepare-loading');
}, 500);
} else {
element.addClass('prepare-loading');
element.addClass('loading');
setTimeout(function () {
element.css('-webkit-mask-image', 'url(forecast.widget/icons/' + icon + '.png)');
element.removeClass('loading');
setTimeout(function () {
element.removeClass('prepare-loading');
}, 500);
}, 500);
}
}
},
// Maps a value in range A to a value in range B
map: function (x, in_min, in_max, out_min, out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
},
// Returns a readable (N, S, E, W, etc) bearing based on degrees
bearing: function (bearing) {
var direction_index = Math.round(bearing / 45);
return ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW', 'N'][direction_index];
},
unit_labels: {
auto: {
speed: 'mph'
},
us: {
speed: 'mph'
},
si: {
speed: 'm/s'
},
ca: {
speed: 'km/h'
},
uk: {
speed: 'mph'
}
},
dayMapping: {
0: 'Sun',
1: 'Mon',
2: 'Tue',
3: 'Wed',
4: 'Thu',
5: 'Fri',
6: 'Sat'
},
api_instructions: '<b>Installation Instructions</b><br>Replace "API_KEY" in index.js with an API key obtained from https://developer.forecast.io/'
};