]>
Commit | Line | Data |
---|---|---|
593f8afc DW |
1 | <template> |
2 | <div> | |
3 | <div class="card" :class="item.class"> | |
fb158d47 | 4 | <a :href="`https://openweathermap.org/city/` + locationId" :target="item.target" rel="noreferrer"> |
593f8afc DW |
5 | <div class="card-content"> |
6 | <div class="media"> | |
fd12de9e | 7 | <div v-if="image" class="media-left" :class="item.background"> |
593f8afc | 8 | <figure class="image is-48x48"> |
b79561bc DW |
9 | <img |
10 | :src="`http://openweathermap.org/img/wn/` + image + `@2x.png`" | |
11 | :alt="conditions" | |
12 | :title="conditions" | |
13 | /> | |
593f8afc DW |
14 | </figure> |
15 | </div> | |
16 | <div v-if="item.icon" class="media-left"> | |
17 | <figure class="image is-48x48"> | |
18 | <i style="font-size: 35px" :class="['fa-fw', item.icon]"></i> | |
19 | </figure> | |
20 | </div> | |
21 | <div class="media-content"> | |
22 | <p class="title is-4">{{ item.location }}</p> | |
23 | <p class="subtitle is-6"> | |
24 | <template v-if="item.subtitle"> | |
25 | {{ item.subtitle }} | |
26 | </template> | |
27 | <template v-else-if="api"> | |
fb158d47 DW |
28 | <template v-if="temp !== ''"> |
29 | {{ temp }} | |
30 | <span v-if="this.item.units === 'metric'">°C</span> | |
31 | <span v-else-if="this.item.units === 'imperial'">°F</span> | |
32 | <span v-else>K</span> | |
33 | </template> | |
34 | <template v-else> | |
35 | <span class="error">Data could not be retrieved</span> | |
36 | </template> | |
593f8afc DW |
37 | </template> |
38 | </p> | |
39 | </div> | |
40 | </div> | |
41 | <div class="tag" :class="item.tagstyle" v-if="item.tag"> | |
42 | <strong class="tag-text">#{{ item.tag }}</strong> | |
43 | </div> | |
44 | </div> | |
45 | </a> | |
46 | </div> | |
47 | </div> | |
48 | </template> | |
49 | ||
50 | <script> | |
51 | export default { | |
52 | name: "OpenWeather", | |
53 | props: { | |
54 | item: Object, | |
55 | }, | |
56 | data: () => ({ | |
fb158d47 | 57 | error: false, |
593f8afc | 58 | api: { |
fb158d47 | 59 | id: "", |
593f8afc | 60 | name: "", |
b79561bc DW |
61 | weather: [ |
62 | { | |
63 | main: "", | |
64 | description: "", | |
65 | icon: "", | |
66 | }, | |
67 | ], | |
593f8afc | 68 | main: { |
b79561bc DW |
69 | temp: "", |
70 | humidity: "", | |
71 | }, | |
72 | }, | |
593f8afc DW |
73 | }), |
74 | computed: { | |
fb158d47 | 75 | locationId: function () { |
593f8afc | 76 | if (this.api) { |
fb158d47 DW |
77 | return this.api.id; |
78 | } | |
79 | return ""; | |
80 | }, | |
81 | temp: function () { | |
82 | if (this.api && this.api.main.temp !== "") { | |
b79561bc DW |
83 | return parseInt(this.api.main.temp).toFixed(1); |
84 | } | |
85 | return ""; | |
86 | }, | |
87 | image: function () { | |
88 | if (this.api) { | |
89 | return this.api.weather[0].icon; | |
90 | } | |
91 | return ""; | |
92 | }, | |
93 | conditions: function () { | |
94 | if (this.api) { | |
95 | return this.api.weather[0].description; | |
593f8afc DW |
96 | } |
97 | return ""; | |
98 | }, | |
593f8afc DW |
99 | }, |
100 | created() { | |
101 | this.fetchStatus(); | |
102 | }, | |
103 | methods: { | |
104 | fetchStatus: async function () { | |
9e0ef05e | 105 | const url = `https://api.openweathermap.org/data/2.5/weather?q=${this.item.location}&appid=${this.item.apiKey}&units=${this.item.units}`; |
593f8afc DW |
106 | this.api = await fetch(url) |
107 | .then((response) => response.json()) | |
fb158d47 DW |
108 | .catch((e) => { |
109 | this.error = true; | |
110 | console.log(e) | |
111 | }); | |
593f8afc DW |
112 | }, |
113 | }, | |
114 | }; | |
115 | </script> | |
116 | ||
117 | <style scoped lang="scss"> | |
fb158d47 DW |
118 | |
119 | // Add a border around the weather image. | |
120 | // Otherwise the image is not always distinguishable. | |
fd12de9e DW |
121 | .media-left { |
122 | &.circle, | |
123 | &.square { | |
124 | background-color: #e4e4e4; | |
125 | } | |
126 | ||
127 | &.circle { | |
128 | border-radius: 90%; | |
129 | } | |
130 | ||
131 | img { | |
132 | max-height: 100%; | |
133 | } | |
134 | } | |
135 | ||
fb158d47 DW |
136 | .error { |
137 | color: #de0000; | |
138 | } | |
139 | ||
140 | // Change background color in dark mode. | |
fd12de9e DW |
141 | .is-dark { |
142 | .media-left { | |
143 | &.circle, | |
144 | &.square { | |
145 | background-color: #909090; | |
146 | } | |
147 | } | |
593f8afc | 148 | } |
b79561bc | 149 | </style> |