-
Notifications
You must be signed in to change notification settings - Fork 1
/
weather.vue
122 lines (113 loc) · 3.31 KB
/
weather.vue
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
<template>
<div class="main">
<div class="item is-unselectable" v-if="tempF !== ''">
<span :class="theme" class="is-size-4" v-if="!settings.metric.value">{{tempF}}</span>
<span :class="theme" class="is-size-4" v-else>{{tempC}}</span>
<span :class="theme" class="is-size-4">°</span>
<span :class="theme" class="is-size-6" v-if="!settings.metric.value">F</span>
<span :class="theme" class="is-size-6" v-else>C</span>
<span :class="theme" class="is-size-7">{{location.city_long}}, {{location.country_short}}</span>
</div>
</div>
</template>
<script>
import axios from 'axios';
import moment from 'moment';
import storage from '../../helpers/storage';
import store from '../../store';
import keys from '../../secret';
const api_url = 'https://api.openweathermap.org/data/2.5/weather?';
const api_key = keys.weather_api_key;
const widget_name = 'weather';
const manifest = {
name: widget_name, // Widget name
description: 'Simple weather widget',
settings: {
metric: {
name: 'Use Celcius',
value: true,
type: 'boolean',
}
},
layout: { // default layout
i: widget_name, // Must be the same name
x: 22,
y: 1,
w: 2,
h: 2,
},
};
export default {
name: manifest.name,
data: () => ({
location: storage.get('mdash-location') ,
tempF: '',
tempC: '',
}),
manifest: manifest,
computed: {
dark(){
return store.getters.settings.mdash.dark.value;
},
settings(){
return store.getters.settings[manifest.name];
},
theme(){
return {
'has-text-white': this.dark,
'has-text-black': !this.dark,
};
}
},
mounted() {
let weather = storage.get('weather');
if(weather !== null){
this.tempC = weather.tempC;
this.tempF = weather.tempF;
}
let loc = storage.get('mdash-location');
let url = api_url + 'lat='+loc.latitude+'&lon='+loc.longitude+'&units=metric&appid='+api_key;
let make_call = false;
if(weather !== null) {
let now = moment(new Date());
let last_checked = moment(new Date(storage.get('weather').last_checked));
make_call = now.diff(last_checked, 'hours') > 3;
}
if (weather === null || make_call) {
axios.get(url)
.then(res => {
console.log('mdash (Weather): Loaded weather data.');
this.tempC = Math.round(res.data.main.temp * 10) / 10;
this.tempF = this.c2f(res.data.main.temp);
storage.set('weather', {
last_checked: moment(new Date()),
tempC: this.tempC,
tempF: this.tempF,
});
});
}
},
watch:{
'settings.metric.value'(){
if(this.settings.metric.value){
}
}
},
methods:{
c2f(temp){
return Math.round(((temp * (9/5)) + 32) * 10) / 10;
}
}
};
</script>
<style scoped>
.main {
width: 100px;
height: 100px;
text-align: center;
font-family: 'Lato', sans-serif;
}
.item {
margin:auto;
}
</style>