-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (117 loc) · 4.46 KB
/
index.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
const {ipcRenderer, shell} = require('electron')
var geocoder = new google.maps.Geocoder();
var img = document.createElement('img');
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
}
//Get the latitude and the longitude;
function successFunction(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
codeLatLng(lat, lng)
}
function errorFunction(){
alert("Geocoder failed");
}
function initialize() {
geocoder = new google.maps.Geocoder();
}
function codeLatLng(lat, lng) {
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
//find country name
for (var i=0; i<results[0].address_components.length; i++) {
for (var b=0;b<results[0].address_components[i].types.length;b++) {
if (results[0].address_components[i].types[b] == "locality") {
//this is the object you are looking for
city = results[0].address_components[i];
document.querySelector('.city').textContent = city.long_name;
break;
}
if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
//this is the object you are looking for
province = results[0].address_components[i];
document.querySelector('.province').textContent = province.long_name;
break;
}
}
}
} else {
console.log("No results found");
}
} else {
console.log("Geocoder failed due to: " + status);
}
});
}
document.addEventListener('click', (event) => {
if (event.target.href) {
// Open links in external browser
shell.openExternal(event.target.href)
event.preventDefault()
} else if (event.target.classList.contains('js-refresh-action')) {
updateWeather()
} else if (event.target.classList.contains('js-quit-action')) {
window.close()
}
})
const getGeoLocation = () => {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(resolve, reject)
})
}
const getWeather = (position) => {
// FIXME replace with your own API key
// Register for one at https://darksky.net/dev/
const apiKey = '3542a79dafa9d79ee9f25958fdfea0ab'
const location = `${position.coords.latitude},${position.coords.longitude}`
const url = `https://api.forecast.io/forecast/${apiKey}/${location}?units=si`
return window.fetch(url).then((response) => {
return response.json()
})
}
const updateView = (weather) => {
const currently = weather.currently
console.log(weather.currently);
img.src = 'assets/img/' + currently.icon + '.svg';
document.getElementById('weatherIcon').appendChild(img)
document.querySelector('.summary').textContent = currently.summary
document.querySelector('.temperature').textContent = `${Math.round(currently.temperature)}`
document.querySelector('.apparent').textContent = `${Math.round(currently.apparentTemperature)}`
document.querySelector('.wind').textContent = `${Math.round(currently.windSpeed)} mph`
document.querySelector('.wind-direction').textContent = getWindDirection(currently.windBearing)
document.querySelector('.humidity').textContent = `${Math.round(currently.humidity * 100)}%`
document.querySelector('.precipitation-chance').textContent = `${Math.round(currently.precipProbability * 100)}%`
}
const getWindDirection = (direction) => {
if (direction < 45) return 'NNE'
if (direction === 45) return 'NE'
if (direction < 90) return 'ENE'
if (direction === 90) return 'E'
if (direction < 135) return 'ESE'
if (direction === 135) return 'SE'
if (direction < 180) return 'SSE'
if (direction === 180) return 'S'
if (direction < 225) return 'SSW'
if (direction === 225) return 'SW'
if (direction < 270) return 'WSW'
if (direction === 270) return 'W'
if (direction < 315) return 'WNW'
if (direction === 315) return 'NW'
if (direction < 360) return 'NNW'
return 'N'
}
const updateWeather = () => {
getGeoLocation().then(getWeather).then((weather) => {
// Use local time
weather.currently.time = Date.now()
initialize();
ipcRenderer.send('weather-updated', weather)
updateView(weather)
previousWeather = weather
})
}
// Update initial weather when loaded
document.addEventListener('DOMContentLoaded', updateWeather)