-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
168 lines (144 loc) · 4.82 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
156
157
158
159
160
161
162
163
164
165
166
167
168
// Author: Md. Hasibur Rashid
// Task Description: Bug fixing
// Live: https://hasibcsepstubd.github.io/JS-Debug/index.html
// Code: https://github.com/hasibcsepstubd/JS-Debug
const imagesArea = document.querySelector('.images');
const gallery = document.querySelector('.gallery');
const galleryHeader = document.querySelector('.gallery-header');
const searchBtn = document.getElementById('search-btn');
const sliderBtn = document.getElementById('create-slider');
const sliderContainer = document.getElementById('sliders');
const backButton = document.getElementById('back-btn');
const spinner = document.getElementById('search-spinner');
// selected image
let sliders = [];
// If this key doesn't work
// Find the name in the url and go to their website
// to create your own api key
const KEY = '15674931-a9d714b6e9d654524df198e00&q';
// show images
const showImages = (images) => {
backButton.style.display = 'none';
gallery.innerHTML = '';
// show gallery title
galleryHeader.style.display = 'flex';
images.forEach(image => {
let div = document.createElement('div');
div.className = 'col-lg-3 col-md-4 col-xs-6 img-item mb-2';
div.innerHTML = ` <img class="img-fluid img-thumbnail" onclick=selectItem(event,"${image.webformatURL}") src="${image.webformatURL}" alt="${image.tags}">`;
gallery.appendChild(div);
})
showSpinner();
imagesArea.style.display = 'block';
}
const getImages = (query) => {
fetch(`https://pixabay.com/api/?key=${KEY}=${query}&image_type=photo&pretty=true`)
.then(response => response.json())
.then(data => showImages(data.hits)) // fixing the bug-1
.catch(err => console.log(err));
}
let slideIndex = 0;
const selectItem = (event, img) => {
let element = event.target;
element.classList.add('added');
let item = sliders.indexOf(img);
if (item === -1) {
sliders.push(img);
} else {
// fixing bug-5, introduce toggle features
// alert('Hey, Already added !')
delete sliders[item];
element.classList.remove('added');
}
}
var timer
const createSlider = () => {
// check slider image length
if (sliders.length < 2) {
alert('Select at least 2 image.')
return;
}
// crate slider previous next area
sliderContainer.innerHTML = '';
const prevNext = document.createElement('div');
prevNext.className = "prev-next d-flex w-100 justify-content-between align-items-center";
prevNext.innerHTML = `
<span class="prev" onclick="changeItem(-1)"><i class="fas fa-chevron-left"></i></span>
<span class="next" onclick="changeItem(1)"><i class="fas fa-chevron-right"></i></span>
`;
sliderContainer.appendChild(prevNext)
document.querySelector('.main').style.display = 'block';
// hide image aria and show back button ==========================
backButton.style.display = 'block';
imagesArea.style.display = 'none';
// fixing bug-3 ===================================================
let duration = document.getElementById('duration').value;
if (duration > 1000) {
duration = duration;
}
else {
duration = 1000;
}
// ===================================================================
sliders.forEach(slide => {
let item = document.createElement('div')
item.className = "slider-item";
item.innerHTML = `<img class="w-100"
src="${slide}"
alt="">`;
sliderContainer.appendChild(item)
})
changeSlide(0)
timer = setInterval(function () {
slideIndex++;
changeSlide(slideIndex);
}, duration);
}
// change slider index
const changeItem = index => {
changeSlide(slideIndex += index);
}
// change slide item
const changeSlide = (index) => {
const items = document.querySelectorAll('.slider-item');
if (index < 0) {
slideIndex = items.length - 1
index = slideIndex;
};
if (index >= items.length) {
index = 0;
slideIndex = 0;
}
items.forEach(item => {
item.style.display = "none"
})
items[index].style.display = "block"
}
searchBtn.addEventListener('click', function () {
showSpinner();
document.querySelector('.main').style.display = 'none';
// clearInterval(timer);
const search = document.getElementById('search');
getImages(search.value)
sliders.length = 0;
})
sliderBtn.addEventListener('click', function () {
createSlider()
})
// Searching event listener for Enter button ==========================
const input = document.getElementById("search");
input.addEventListener("keyup", function (event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("search-btn").click();
}
});
// New Feature-1: Back button event listener ==========================
backButton.addEventListener('click', function () {
imagesArea.style.display = 'block';
document.querySelector('.main').style.display = 'none';
})
// New Feature-2: Loading animation ==================================
const showSpinner = () => {
spinner.classList.toggle('d-none');
}