|
| 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')); |
0 commit comments