This repository has been archived by the owner on Mar 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
polymer-ui-weather.html
140 lines (131 loc) · 4.82 KB
/
polymer-ui-weather.html
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
<!--
Copyright 2013 The Polymer Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../polymer-jsonp/polymer-jsonp.html"/>
<polymer-element name="polymer-ui-weather" attributes="autoupdate">
<template>
<link rel="stylesheet" href="polymer-ui-weather.css">
<div id="widget-title">{{current.display_location.full}}</div>
<template bind="{{current}}" id="current">
<div id="current-condition">{{temp_f}}°</div>
<div id="current-icon" class="{{icon}}">{{weather}}</div>
<div id="current-wind" class="{{wind_dir}}">{{wind_dir}} {{wind_mph}} mph</div>
</template>
<div id="latest">
<template repeat="{{forecast.forecastday}}">
<div class="entry">
<div class="day">{{date.weekday_short}}</div>
<div class="icon {{icon}}">{{conditions}}</div>
<div class="condition high">{{high.fahrenheit}}</div>
<div class="condition low">{{low.fahrenheit}}</div>
</div>
</template>
</div>
<polymer-jsonp id="jsonp_current" response="{{currentConditionsResponse}}"></polymer-jsonp>
<polymer-jsonp id="jsonp_forecast" handleAs="json" response="{{forecastResponse}}"></polymer-jsonp>
</template>
<script>
Polymer('polymer-ui-weather', {
position: null,
currentConditionsResponse: null,
forecastResponse: null,
autoupdate: false,
updateInterval: 60000,
apikey: '6d9e44e91f890cae',
position: {
coords: {
latitude: 37.773285,
longitude: -122.417725
}
},
icons: ['clear', 'cloudy', 'rainy', 'sunny', 'mostlysunny', 'partlycloudy'],
ready: function() {
this.loadFromStorage();
},
loadFromStorage: function() {
var data = localStorage.getItem('wu-weather-data');
var cached = data && JSON.parse(data);
// Discard stored data if it's too old.
if (!cached ||
(new Date() - cached.timestamp) > (60 * 60 * 1000)) {
this.positionChanged();
this.updatePosition();
return;
}
this.current = cached.current;
this.forecast = cached.forecast;
},
saveToStorage: function() {
// Only store when we have all the data.
if (this.current && this.forecast) {
this.forecast.forecastday = this.forecast.forecastday.splice(0, 5);
if (!~this.icons.indexOf(this.current.icon)) {
this.current.icon = 'cloudy';
}
for (x=0; x<this.forecast.forecastday.length; x++) {
if (!~this.icons.indexOf(this.forecast.forecastday[x].icon)) {
this.forecast.forecastday[x].icon = 'cloudy';
}
}
var data = {
current: this.current,
forecast: this.forecast,
timestamp: new Date().getTime()
};
localStorage.setItem('wu-weather-data', JSON.stringify(data));
}
},
autoupdateChanged: function() {
if (this.autoupdate) {
if (!this.intervalId) {
this.intervalId = setInterval(this.updatePosition.bind(this),
this.updateInterval);
}
} else {
clearInterval(this.intervalId);
this.intervalId = null;
}
},
positionChanged: function() {
if (!this.$.jsonp_current.isInFlight()) {
this.$.jsonp_current.url = 'https://api.wunderground.com/api/' +
this.apikey + '/conditions/q/' +
this.position.coords.latitude + ',' +
this.position.coords.longitude + '.json?callback=';
this.$.jsonp_current.go();
}
if (!this.$.jsonp_forecast.isInFlight()) {
this.$.jsonp_forecast.url = 'https://api.wunderground.com/api/' +
this.apikey + '/forecast10day/q/' +
this.position.coords.latitude + ',' +
this.position.coords.longitude + '.json?callback=';
this.$.jsonp_forecast.go();
}
},
currentConditionsResponseChanged: function() {
this.current = this.currentConditionsResponse.current_observation;
this.saveToStorage();
},
forecastResponseChanged: function() {
this.forecast = this.forecastResponse.forecast.simpleforecast;
this.saveToStorage();
},
updatePosition: function() {
// Get the current location.
navigator.geolocation.getCurrentPosition(
this.getCurrentPositionSuccess.bind(this),
this.getCurrentPositionError.bind(this), {timeout: 10000});
localStorage.clear();
},
getCurrentPositionSuccess: function(position) {
this.position = position;
},
getCurrentPositionError: function(error) {
console.log('PositionError', error);
}
});
</script>
</polymer-element>