-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathXkcdComic.vue
98 lines (94 loc) · 2.23 KB
/
XkcdComic.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
<template>
<div class="xkcd-wrapper" v-tooltip="toolTip(alt)">
<h3 class="xkcd-title">{{ title }}</h3>
<a :href="`https://xkcd.com/${comicNum}/`">
<img :src="image" :alt="alt" class="xkcd-comic"/>
</a>
</div>
</template>
<script>
import WidgetMixin from '@/mixins/WidgetMixin';
import { widgetApiEndpoints } from '@/utils/defaults';
export default {
mixins: [WidgetMixin],
components: {},
data() {
return {
image: null,
title: '',
alt: '',
comicNum: '',
};
},
computed: {
/* Let user select which comic to display: random, latest or a specific number */
comicNumber() {
const usersChoice = this.options.comic;
if (!usersChoice) {
return 'latest';
} else if (usersChoice === 'random') {
return Math.abs(Math.floor(Math.random() * (1 - 2553)));
}
return usersChoice;
},
endpoint() {
return `${widgetApiEndpoints.xkcdComic}?comic=${this.comicNumber}`;
},
},
methods: {
/* Make GET request to CoinGecko API endpoint */
fetchData() {
fetch(this.endpoint)
.then(response => {
if (!response.ok) {
this.error('Network response was not ok');
}
return response.json();
})
.then(data => {
this.processData(data);
})
.catch(dataFetchError => {
this.error('Unable to fetch data', dataFetchError);
})
.finally(() => {
this.finishLoading();
});
},
/* Assign data variables to the returned data */
processData(data) {
this.image = data.img;
this.title = data.safe_title;
this.alt = data.alt;
this.comicNum = data.num;
},
toolTip(alt) {
const content = alt;
return {
content, html: false, trigger: 'hover focus', delay: 250, classes: 'xkcd-alt-tt',
};
},
},
};
</script>
<style scoped lang="scss">
.xkcd-wrapper {
.xkcd-title {
font-size: 1.2rem;
margin: 0.25rem auto;
color: var(--widget-text-color);
}
.xkcd-comic {
display: flex;
width: 100%;
max-width: 380px;
margin: 0.25rem auto;
border-radius: var(--curve-factor);
}
}
</style>
<style lang="scss">
.xkcd-alt-tt {
min-width: 20rem;
}
</style>