Skip to content

Commit d2e45db

Browse files
committed
create starter js file for video 58
1 parent 14f7d9f commit d2e45db

File tree

1 file changed

+0
-91
lines changed

1 file changed

+0
-91
lines changed

exercises/58 - Gallery/gallery.js

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +0,0 @@
1-
function Gallery(gallery) {
2-
if (!gallery) {
3-
throw new Error('No Gallery Found!');
4-
}
5-
// select the elements we need
6-
const images = Array.from(gallery.querySelectorAll('img'));
7-
const modal = document.querySelector('.modal');
8-
const prevButton = modal.querySelector('.prev');
9-
const nextButton = modal.querySelector('.next');
10-
let currentImage;
11-
12-
function openModal() {
13-
console.info('Opening Modal...');
14-
// First check if the modal is already open
15-
if (modal.matches('.open')) {
16-
console.info('Madal already open');
17-
return; // stop the function from running
18-
}
19-
modal.classList.add('open');
20-
21-
// Event listeners to be bound when we open the modal:
22-
window.addEventListener('keyup', handleKeyUp);
23-
nextButton.addEventListener('click', showNextImage);
24-
prevButton.addEventListener('click', showPrevImage);
25-
}
26-
27-
function closeModal() {
28-
modal.classList.remove('open');
29-
// TODO: add event listeners for clicks and keyboard..
30-
window.removeEventListener('keyup', handleKeyUp);
31-
nextButton.removeEventListener('click', showNextImage);
32-
prevButton.removeEventListener('click', showPrevImage);
33-
}
34-
35-
function handleClickOutside(e) {
36-
if (e.target === e.currentTarget) {
37-
closeModal();
38-
}
39-
}
40-
41-
function handleKeyUp(event) {
42-
if (event.key === 'Escape') return closeModal();
43-
if (event.key === 'ArrowRight') return showNextImage();
44-
if (event.key === 'ArrowLeft') return showPrevImage();
45-
}
46-
47-
function showNextImage() {
48-
showImage(currentImage.nextElementSibling || gallery.firstElementChild);
49-
}
50-
function showPrevImage() {
51-
showImage(currentImage.previousElementSibling || gallery.lastElementChild);
52-
}
53-
54-
function showImage(el) {
55-
if (!el) {
56-
console.info('no image to show');
57-
return;
58-
}
59-
// update the modal with this info
60-
console.log(el);
61-
modal.querySelector('img').src = el.src;
62-
modal.querySelector('h2').textContent = el.title;
63-
modal.querySelector('figure p').textContent = el.dataset.description;
64-
currentImage = el;
65-
openModal();
66-
}
67-
68-
// These are our Event Listeners!
69-
images.forEach(image =>
70-
image.addEventListener('click', e => showImage(e.currentTarget))
71-
);
72-
73-
// loop over each image
74-
images.forEach(image => {
75-
// attach an event listener for each image
76-
image.addEventListener('keyup', e => {
77-
// when that is keyup'd, check if it was enter
78-
if (e.key === 'Enter') {
79-
// if it was, show that image
80-
showImage(e.currentTarget);
81-
}
82-
});
83-
});
84-
85-
modal.addEventListener('click', handleClickOutside);
86-
}
87-
88-
// Use it on the page
89-
90-
const gallery1 = Gallery(document.querySelector('.gallery1'));
91-
const gallery2 = Gallery(document.querySelector('.gallery2'));

0 commit comments

Comments
 (0)