Skip to content

Commit 5d91f25

Browse files
committed
update
1 parent f5205aa commit 5d91f25

File tree

2 files changed

+63
-13
lines changed

2 files changed

+63
-13
lines changed

OOP_Projects/gallery/starter/app.js

+53-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ function Gallery(element) {
2828
this.closeBtn = getElement('.close-btn');
2929

3030
// Bind function
31+
// pointing to Gallery
32+
this.closeModal = this.closeModal.bind(this);
33+
this.prevImage = this.prevImage.bind(this);
34+
this.nextImage = this.nextImage.bind(this);
35+
this.chooseImage = this.chooseImage.bind(this);
36+
3137
// ref u can use this one
3238
let self = this; //dia merujuk pada Gallery
3339
// ** Click
@@ -50,17 +56,23 @@ Gallery.prototype.openModal = function (selectedImage, list) {
5056
// *** invoke this.setMainImage
5157
this.setMainImage(selectedImage);
5258
// *** map modalImages
53-
// show display modal-image
59+
// menampilkan gambar yg diklik dibawah agar muncul sesuai ururat
60+
// jadi gambar yg diklik ada ikut juga dibawah
5461
this.modalImages.innerHTML = list
5562
.map(function (image) {
63+
// console.log(image);
5664
return `<img src="${
5765
image.src
58-
}" title="${image.title}" data-id="${image.dataset.id}" class="${selectedImage.dataset.id === image.dataset.id ? 'modal-img selected' : 'modal-img'}"/>`;
66+
}" title="${image.title}" data-id="${image.dataset.id}" class="${selectedImage.dataset.id === image.dataset.id ? 'modal-img selected' : 'modal-img'}" />`;
5967
})
6068
.join('');
61-
69+
// console.log(list); array img
6270
// *** invoke this.modal
6371
this.modal.classList.add('open');
72+
this.closeBtn.addEventListener('click', this.closeModal);
73+
this.prevBtn.addEventListener('click', this.prevImage);
74+
this.nextBtn.addEventListener('click', this.nextImage);
75+
this.modalImages.addEventListener('click', this.chooseImage);
6476
};
6577

6678
// for main-image, and image-name
@@ -70,6 +82,44 @@ Gallery.prototype.setMainImage = function (selectedImage) {
7082
this.imgName.textContent = selectedImage.title;
7183
};
7284

85+
Gallery.prototype.closeModal = function () {
86+
// close modal
87+
this.modal.classList.remove('open');
88+
this.closeBtn.removeEventListener('click', this.closeModal);
89+
this.prevBtn.removeEventListener('click', this.prevImage);
90+
this.nextBtn.removeEventListener('click', this.nextImage);
91+
};
92+
93+
// for nextImage mainImg yang dipilih
94+
Gallery.prototype.nextImage = function () {
95+
const selected = this.modalImages.querySelector('.selected');
96+
const next =
97+
selected.nextElementSibling || this.modalImages.firstElementChild;
98+
selected.classList.remove('selected');
99+
next.classList.add('selected');
100+
this.setMainImage(next);
101+
};
102+
103+
Gallery.prototype.prevImage = function () {
104+
const selected = this.modalImages.querySelector('.selected');
105+
const prev = selected.previousSibling || this.modalImages.lastChild;
106+
selected.classList.remove('selected');
107+
prev.classList.add('selected');
108+
this.setMainImage(prev);
109+
};
110+
111+
// for Choose Image
112+
Gallery.prototype.chooseImage = function (e) {
113+
const selected = this.modalImages.querySelector('.selected');
114+
// jika target sama dgn modal-img
115+
//
116+
if (e.target.classList.contains('modal-img')) {
117+
// console.log(e.target); check gambar dibawah maka hasilnya adalah sesuai dgn gambar diklik
118+
selected.classList.remove('selected');
119+
this.setMainImage(e.target);
120+
e.target.classList.add('selected');
121+
}
122+
};
73123
// create new instance object
74124
const nature = new Gallery(getElement('.nature'));
75125
const city = new Gallery(getElement('.city'));

OOP_Projects/gallery/starter/index.html

+10-10
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@
1919
src="./images/nature-1.jpeg"
2020
title="nature-1"
2121
class="img"
22-
data-set="1"
22+
data-id="1"
2323
alt="nature"
2424
/>
2525
<img
2626
src="./images/nature-2.jpeg"
2727
title="nature-2"
2828
class="img"
29-
data-set="2"
29+
data-id="2"
3030
alt="nature"
3131
/>
3232
<img
3333
src="./images/nature-3.jpeg"
3434
title="nature-3"
3535
class="img"
36-
data-set="3"
36+
data-id="3"
3737
alt="nature"
3838
/>
3939
</section>
@@ -43,35 +43,35 @@
4343
src="./images/city-1.jpeg"
4444
title="city-1"
4545
class="img"
46-
data-set="1"
46+
data-id="1"
4747
alt="city"
4848
/>
4949
<img
5050
src="./images/city-2.jpeg"
5151
title="city-2"
5252
class="img"
53-
data-set="2"
53+
data-id="2"
5454
alt="city"
5555
/>
5656
<img
5757
src="./images/city-3.jpeg"
5858
title="city-3"
5959
class="img"
60-
data-set="3"
60+
data-id="3"
6161
alt="city"
6262
/>
6363
<img
6464
src="./images/city-4.jpeg"
6565
title="city-4"
6666
class="img"
67-
data-set="4"
67+
data-id="4"
6868
alt="city"
6969
/>
7070
<img
7171
src="./images/city-5.jpeg"
7272
title="city-5"
7373
class="img"
74-
data-set="5"
74+
data-id="5"
7575
alt="city"
7676
/>
7777
</section>
@@ -98,14 +98,14 @@ <h3 class="image-name">image name</h3>
9898
src="./images/city-5.jpeg"
9999
title="city-5"
100100
class="modal-img"
101-
data-set="5"
101+
data-id="5"
102102
alt=""
103103
/>
104104
<img
105105
src="./images/city-4.jpeg"
106106
title="city-4"
107107
class="modal-img selected"
108-
data-set="4"
108+
data-id="4"
109109
alt=""
110110
/>
111111
</div>

0 commit comments

Comments
 (0)