Skip to content

Commit 61d15d6

Browse files
committed
almost a professionnal developper
1 parent 935c7aa commit 61d15d6

File tree

8 files changed

+2951
-11279
lines changed

8 files changed

+2951
-11279
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
function Gallery(gallery) {
2+
if (!gallery) {
3+
throw new Error('No Gallery Found !');
4+
}
5+
this.gallery = gallery;
6+
7+
// select the elements we need
8+
this.images = Array.from(gallery.querySelectorAll('img'));
9+
this.modal = document.querySelector('.modal');
10+
this.prevButton = this.modal.querySelector('.prev');
11+
this.nextButton = this.modal.querySelector('.next');
12+
13+
// binding our methods to the instance when they need a "this" referring to themself
14+
this.handleKeyDown = this.handleKeyDown.bind(this);
15+
this.showNextImage = this.showNextImage.bind(this);
16+
this.showPrevImage = this.showPrevImage.bind(this);
17+
this.handleClickOutside = this.handleClickOutside.bind(this);
18+
19+
// Event listeners
20+
this.images.forEach((image) =>
21+
image.addEventListener('click', (event) =>
22+
this.showImage(event.currentTarget)
23+
)
24+
);
25+
26+
// Accessibility : open image on enter key
27+
this.images.forEach((image) =>
28+
image.addEventListener('keyup', (event) => {
29+
if (event.key === 'Enter') {
30+
this.showImage(event.currentTarget);
31+
}
32+
})
33+
);
34+
35+
this.modal.addEventListener('click', this.handleClickOutside);
36+
}
37+
38+
Gallery.prototype.openModal = function () {
39+
if (this.modal.matches('.open')) return;
40+
this.modal.classList.add('open');
41+
42+
// Event listeners bound to a modal opening
43+
window.addEventListener('keydown', this.handleKeyDown, { useCapture: true });
44+
this.nextButton.addEventListener('click', this.showNextImage);
45+
this.prevButton.addEventListener('click', this.showPrevImage);
46+
};
47+
48+
Gallery.prototype.closeModal = function () {
49+
this.modal.classList.remove('open');
50+
window.removeEventListener('keydown', this.handleKeyDown, {
51+
useCapture: true,
52+
});
53+
this.nextButton.removeEventListener('click', this.showNextImage);
54+
this.prevButton.removeEventListener('click', this.showPrevImage);
55+
};
56+
57+
Gallery.prototype.handleClickOutside = function (event) {
58+
if (event.target === event.currentTarget) {
59+
this.closeModal();
60+
}
61+
};
62+
63+
Gallery.prototype.handleKeyDown = function (event) {
64+
if (event.key === 'Escape' && this.modal.matches('.open')) {
65+
event.preventDefault();
66+
this.closeModal();
67+
return;
68+
}
69+
if (event.key === 'ArrowRight') return this.showNextImage();
70+
if (event.key === 'ArrowLeft') return this.showPrevImage();
71+
};
72+
73+
Gallery.prototype.showImage = function (element) {
74+
if (!element) {
75+
console.info('no image to show');
76+
return;
77+
}
78+
this.modal.querySelector('img').src = element.src;
79+
this.modal.querySelector('h2').textContent = element.title;
80+
this.modal.querySelector('figure p').textContent =
81+
element.dataset.description;
82+
this.currentImage = element;
83+
this.openModal();
84+
};
85+
86+
Gallery.prototype.showNextImage = function () {
87+
console.log(this);
88+
this.showImage(
89+
this.currentImage.nextElementSibling || this.gallery.firstElementChild
90+
);
91+
};
92+
93+
Gallery.prototype.showPrevImage = function () {
94+
this.showImage(
95+
this.currentImage.previousElementSibling || this.gallery.lastElementChild
96+
);
97+
};
98+
99+
const gallery1 = new Gallery(document.querySelector('.gallery1'));
100+
const gallery2 = new Gallery(document.querySelector('.gallery2'));

exercises/58 - Gallery/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ <h2>Test Title</h2>
107107
</div>
108108
</div>
109109

110-
<script src="./gallery.js"></script>
110+
<script src="./gallery-prototype.js"></script>
111111
</body>
112112

113113
</html>

exercises/59 - Slider/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
</div>
3939
</div>
4040

41-
<div class="dog-slider">
41+
<div class="cat-slider">
4242
<div class="slides">
4343
<div class="slide">0001</div>
4444
<div class="slide">0002</div>
@@ -52,7 +52,7 @@
5252
</div>
5353

5454
</div>
55-
<script src="./src/index.js"></script>
55+
<script src="./src/index-prototype.js"></script>
5656
</body>
5757

5858
</html>

0 commit comments

Comments
 (0)