-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
171 lines (157 loc) · 4.28 KB
/
index.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Trending on GIPHY</title>
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<style>
#app {
margin: 5%;
display: flex;
flex-direction: column;
align-items: center;
font-family:
BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Fira Sans,
Droid Sans,Helvetica Neue,Helvetica,Arial,sans-serif;
}
#title {
margin-bottom: 50px;
font-size: 3rem;
}
#gifs {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
#footer {
margin-top: 100px;
display: flex;
justify-content: center;
}
#add-more-button {
color: #fff;
background-color: #0f77ea;
border-color: transparent;
padding: calc(.375em - 1px) .75em;
height: 2.25em;
font-size: 1rem;
border-radius: 4px;
}
.giphy-item {
background-color: #eee;
}
</style>
</head>
<body>
<div id="app">
<div id="title">Trending on GIPHY</div>
<div id="gifs">
<giphy-item
v-for="giphy in giphies.list"
:giphy="giphy"
:key="giphy.id"
/>
</div>
<div id="footer">
<button
id="add-more-button"
v-if="!giphies.isLimitReached()"
@click="moreGiphies"
:disabled='loading'
>
Load More!
</button>
<p v-else>
Maximum number loaded.
</p>
</div>
</div>
<script type="text/javascript">
const START_BATCH_SIZE = 10;
const LOAD_BATCH_SIZE = 5;
const MAX_VIEWABLE = 100;
class Giphy {
constructor(data) {
const DEFAULT_IMG_TYPE = 'fixed_height';
this._data = data;
this.id = data.id;
this.link = data.url;
this.image = data.images[DEFAULT_IMG_TYPE];
}
}
class GiphyCollection {
constructor(limit) {
this.limit = limit;
this.list = [];
this.existingIds = new Set();
this.currentOffset = 0;
}
/**
* Adds a array of giphies to collection if they are new and limit is not reached.
*
* @param {Object[]} giphies Array of Objects containing the raw data for each new giphy.
*/
add(giphies) {
giphies.forEach(giphyData => {
if (!this.isLimitReached() && !this.existingIds.has(giphyData.id)) {
this.list.push(new Giphy(giphyData));
this.existingIds.add(giphyData.id);
}
})
}
isLimitReached() {
return this.list.length >= this.limit;
}
fetch(batchSize) {
return axios
.get("https://api.giphy.com/v1/gifs/trending?api_key=YfKFvEqlqmymcVI88PMbrlzG8QwAoIhh" +
`&limit=${batchSize}&offset=${this.currentOffset}`)
.then(response => {
this.add(response.data.data);
this.currentOffset += batchSize;
return response;
})
}
}
Vue.component('giphy-item', {
props: ['giphy'],
template: `
<a :href="giphy.link" target="blank">
<img
class="giphy-item"
:src="giphy.image.url"
:width="giphy.image.width"
:height="giphy.image.height"
/>
</a>
`
})
var app = new Vue({
el: '#app',
data: {
loading: false,
giphies: new GiphyCollection(MAX_VIEWABLE),
},
methods: {
loadGiphies: function (batchSize) {
this.loading = true;
this.giphies.fetch(batchSize)
.then(response => {
this.loading = false;
})
.catch(error => {
this.loading = false;
})
},
moreGiphies: function () {
this.loadGiphies(LOAD_BATCH_SIZE);
}
},
mounted () {
this.loadGiphies(START_BATCH_SIZE);
}
})
</script>
</body>
</html>