-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
156 lines (122 loc) · 3.55 KB
/
app.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
146
147
148
149
150
151
152
153
154
155
// variables
let imageContainer = document.getElementById("img_container");
let galleryContainer = document.getElementById("gallery_container");
let gallery = [];
let currentIndex = 0;
// display main image with regular size to improve loading speed (can change to raw to get better quality pictures)
// fetched alt_description from json
function showImage(index) {
const imageUrl = gallery[index].urls.regular;
const altText = gallery[index].alt_description;
imageContainer.innerHTML = `<img src="${imageUrl}" alt="${altText}">`;
}
// display top gallery and change picture when clicked
const createGallery = (gallery) => {
galleryContainer.innerHTML = '';
gallery.forEach((image, index) => {
let imgElement = document.createElement('img');
imgElement.src = image.urls.thumb;
imgElement.alt = image.alt_description;
imgElement.addEventListener('click', () => {
currentIndex = index;
showImage(currentIndex);
});
galleryContainer.appendChild(imgElement);
});
};
// hide_open button
const hide_open = document.getElementById("hide_open");
let galleryOpen = true;
hide_open.addEventListener("click", function () {
if (galleryOpen) {
galleryContainer.style.display = "none";
} else {
galleryContainer.style.display = "flex";
}
galleryOpen = !galleryOpen;
});
// fetch pictures from unsplash using API
async function search(queryParam) {
let response = await fetch(
`https://api.unsplash.com/search/photos?page=1&orientation=landscape&query=${queryParam}&client_id=ywoppbhOsnIKpTA0ekfrY_sjIkmImzjMj8TAj2o4jig`
);
let data = await response.json();
if (data.results.length > 0) {
gallery = data.results;
showImage(currentIndex);
createGallery(gallery);
}
}
// search form
const form = document.getElementById("form");
form.addEventListener("submit", function (event) {
event.preventDefault();
let query = event.target.input.value;
search(query);
form.reset()
});
// random image for loading page
// bunch of keywords so the first image is generated randomly from an array
document.addEventListener("DOMContentLoaded", function () {
const randomImg = randomImage();
search(randomImg);
});
function randomImage() {
const keywords = [
"cracow",
"poland",
"warsaw",
"norwich",
"new york",
"space",
"forest",
"landscape",
"mountains",
"city",
"lakes",
"architecture",
"animals",
"dolphin",
"paris",
"london",
"china",
];
const randomIndex = Math.floor(Math.random() * keywords.length);
return keywords[randomIndex];
}
// variables and functions for next / previous button
// works in circle - if it goes to the last image it starts from the begining
const buttonNext = document.getElementById("next");
const buttonPrevious = document.getElementById("previous");
function circleLeft() {
currentIndex--;
if (currentIndex < 0) {
currentIndex += gallery.length;
}
showImage(currentIndex);
}
function circleRight() {
currentIndex++;
if (currentIndex >= gallery.length) {
currentIndex -= gallery.length;
}
showImage(currentIndex);
}
// next and previous button, mouse click
buttonNext.addEventListener("click", function () {
circleRight();
});
buttonPrevious.addEventListener("click", function () {
circleLeft();
});
// next and previous button, for left and right arrow key
document.addEventListener("keydown", function (event) {
if (event.key === "ArrowRight") {
circleRight();
}
});
document.addEventListener("keydown", function (event) {
if (event.key === "ArrowLeft") {
circleLeft();
}
});