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