|
1 | | -function getElement(selection) { |
2 | | - const element = document.querySelector(selection); |
| 1 | +function getElement(selectElement) { |
| 2 | + const element = document.querySelector(selectElement); |
| 3 | + // if true back to element value |
3 | 4 | if (element) { |
4 | 5 | return element; |
5 | 6 | } |
| 7 | + // if falses notice |
6 | 8 | throw new Error( |
7 | | - `Please check "${selection}" selector, no such element exists` |
| 9 | + `please check your element on html ${selectElement} cause not found` |
8 | 10 | ); |
9 | 11 | } |
| 12 | + |
| 13 | +function Gallery(element) { |
| 14 | + // for all picture tdk menggunakan ini |
| 15 | + //karena nodelist dan solusi ada spread operator |
| 16 | + // this.list = element.querySelectorAll('.img'); |
| 17 | + this.list = [...element.querySelectorAll('.img')]; |
| 18 | + // console.log(this.list); |
| 19 | + // ** Target |
| 20 | + this.container = element; |
| 21 | + this.modal = getElement('.modal'); //tampilan awal fixed |
| 22 | + this.mainImg = getElement('.main-img'); //content tuk gambar |
| 23 | + this.imgName = getElement('.image-name'); //tuk text dibawah content |
| 24 | + this.modalImages = getElement('.modal-images'); //gambar dibawah |
| 25 | + // ** close |
| 26 | + this.prevBtn = getElement('.prev-btn'); |
| 27 | + this.nextBtn = getElement('.next-btn'); |
| 28 | + this.closeBtn = getElement('.close-btn'); |
| 29 | + |
| 30 | + // Bind function |
| 31 | + // ref u can use this one |
| 32 | + let self = this; //dia merujuk pada Gallery |
| 33 | + // ** Click |
| 34 | + this.container.addEventListener( |
| 35 | + 'click', |
| 36 | + function (e) { |
| 37 | + // ** IF target same with img |
| 38 | + // tampilkan e.target , dan list (img) |
| 39 | + if (e.target.classList.contains('img')) { |
| 40 | + this.openModal(e.target, this.list); |
| 41 | + } |
| 42 | + // self.openModal(); |
| 43 | + }.bind(this) |
| 44 | + ); |
| 45 | +} |
| 46 | + |
| 47 | +// protype function for openModal() |
| 48 | +// param for selectedImage, and list (img) |
| 49 | +Gallery.prototype.openModal = function (selectedImage, list) { |
| 50 | + // *** invoke this.setMainImage |
| 51 | + this.setMainImage(selectedImage); |
| 52 | + // *** map modalImages |
| 53 | + // show display modal-image |
| 54 | + this.modalImages.innerHTML = list |
| 55 | + .map(function (image) { |
| 56 | + return `<img src="${ |
| 57 | + image.src |
| 58 | + }" title="${image.title}" data-id="${image.dataset.id}" class="${selectedImage.dataset.id === image.dataset.id ? 'modal-img selected' : 'modal-img'}"/>`; |
| 59 | + }) |
| 60 | + .join(''); |
| 61 | + |
| 62 | + // *** invoke this.modal |
| 63 | + this.modal.classList.add('open'); |
| 64 | +}; |
| 65 | + |
| 66 | +// for main-image, and image-name |
| 67 | +Gallery.prototype.setMainImage = function (selectedImage) { |
| 68 | + // set picture & title |
| 69 | + this.mainImg.src = selectedImage.src; |
| 70 | + this.imgName.textContent = selectedImage.title; |
| 71 | +}; |
| 72 | + |
| 73 | +// create new instance object |
| 74 | +const nature = new Gallery(getElement('.nature')); |
| 75 | +const city = new Gallery(getElement('.city')); |
0 commit comments