-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
153 lines (118 loc) · 4.23 KB
/
script.js
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
let images = []; //to store all images recieved from the API
const imageContainer = document.getElementById('imageContainer');
//creating the image element to be plcaed
function createImageElement(imageUrl, imageTitle) {
const imageDiv = document.createElement('div');
imageDiv.classList.add('image');
const imageElement = document.createElement('img');
imageElement.src = imageUrl;
imageElement.alt = imageTitle;
const titleDiv = document.createElement('div');
titleDiv.classList.add('title');
titleDiv.innerHTML += imageTitle;
// Add event listener to open image in a new tab
imageElement.addEventListener('click', () => {
window.open(imageUrl, '_blank');
});
imageDiv.appendChild(imageElement);
imageDiv.appendChild(titleDiv);
imageContainer.appendChild(imageDiv);
}
//fetch the pictures from the subreddit
function sendReq(req) {
fetch(req)
.then(response => response.json())
.then(data => {
images = data.data.children;
//create image element for each image
images.forEach(image => {
const imageUrl = image.data.url;
const imageTitle = image.data.title;
//make sure it is image before creating the image element and is not hosted on external platform like imgur
if ((imageUrl.endsWith(".jpg") || imageUrl.endsWith(".jpeg") || imageUrl.endsWith(".png")) && !imageUrl.includes("imgur.com")) {
createImageElement(imageUrl, imageTitle);
};
});
})
.catch(error => {
console.log('An error occurred:', error);
});
}
//implement search
const searchInput = document.getElementById('searchInput');
searchInput.addEventListener('input', () => {
const searchQuery = searchInput.value.trim().toLowerCase();
//if search query is empty
if (searchQuery === '') {
images.forEach(image => {
const imageUrl = image.data.url;
if ((imageUrl.endsWith(".jpg") || imageUrl.endsWith(".jpeg") || imageUrl.endsWith(".png")) && !imageUrl.includes("imgur.com")) {
createImageElement(image.data.url, image.data.title);
}
});
} else {
const filteredImages = images.filter(image => {
const title = image.data.title.toLowerCase();
return title.includes(searchQuery);
});
imageContainer.innerHTML = '';
filteredImages.forEach(image => {
const imageUrl = image.data.url;
if ((imageUrl.endsWith(".jpg") || imageUrl.endsWith(".jpeg") || imageUrl.endsWith(".png")) && !imageUrl.includes("imgur.com")) {
createImageElement(image.data.url, image.data.title);
}
});
}
});
//implement size change
const sizeInput = document.getElementById('size');
const container = document.getElementById('imageContainer');
sizeInput.addEventListener('change', () => {
const size = sizeInput.value;
if (size === 'small') {
container.classList.add('S');
container.classList.remove('M');
container.classList.remove('L');
} else if (size === 'medium') {
container.classList.add('M');
container.classList.remove('S');
container.classList.remove('L');
} else if (size === 'large') {
container.classList.add('L');
container.classList.remove('M');
container.classList.remove('S');
}
});
//implement sorting of images by new,top etc.
//default fetch
document.addEventListener('DOMContentLoaded', () => {
sendReq('https://www.reddit.com/r/pics/hot.json?limit=500') //default sort option is hot
});
function clearImageContainer() {
imageContainer.innerHTML = '';
}
const sortInput = document.getElementById('sort');
sortInput.addEventListener('change', () => {
const sortBy = sortInput.value;
clearImageContainer(); //clear the previous fetch
if (sortBy === 'hot') {
const url = 'https://www.reddit.com/r/pics/hot.json?limit=500';
const req = new Request(url);
sendReq(req);
}
else if (sortBy === 'new') {
const url = 'https://www.reddit.com/r/pics/new.json?limit=500';
const req = new Request(url);
sendReq(req);
}
else if (sortBy == 'top') {
const url = 'https://www.reddit.com/r/pics/top.json?limit=500';
const req = new Request(url);
sendReq(req);
}
else if (sortBy == 'rising') {
const url = 'https://www.reddit.com/r/pics/rising.json?limit=500';
const req = new Request(url);
sendReq(req);
}
})