Skip to content
This repository was archived by the owner on Sep 11, 2018. It is now read-only.

Commit 8d1b2b6

Browse files
author
Claudio Ludovico Panetta
committed
Improve the way theater changes photos
This patch brings a new and improved way to navigate between photos. Previously each time you reach the end or the beginning of the gallery the Theater just vanish automatically and this wasn't intented at all! What I want instead is that the gallery never close unless the user clicks on the "x" button or press escape or whatever. In order to accomplish this I implemented two new mutations and remove the "change index logic" from the theater itself which should not be aware of the index change anyway. I also fix the undefined error on "changeSelectedPhoto" finally!
1 parent 9fcd176 commit 8d1b2b6

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

src/components/Theater.vue

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,11 @@
6262
6363
methods: {
6464
previousPhoto() {
65-
let photo = this.$store.getters.photos[this.photoIndex - 1];
66-
this.$store.commit("changeSelectedPhoto", photo);
65+
this.$store.commit("previousPhotoOfIndex", this.photoIndex - 1);
6766
},
6867
6968
nextPhoto() {
70-
let photo = this.$store.getters.photos[this.photoIndex + 1];
71-
this.$store.commit("changeSelectedPhoto", photo);
69+
this.$store.commit("nextPhotoOfIndex", this.photoIndex + 1);
7270
},
7371
7472
closeTheater() {
@@ -79,7 +77,7 @@
7977
}
8078
</script>
8179

82-
<style>
80+
<style scoped>
8381
.Theater {
8482
position: fixed;
8583
top: 0;

src/store/mutations.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,50 @@
11
export const mutations = {
2+
3+
/**
4+
* Write the photos inside the Vuex store
5+
* so we can use them
6+
*
7+
* @param state
8+
* @param {Array} photos
9+
*/
210
writePhotos(state, photos) {
311
state.photos = photos;
412
},
513

14+
/**
15+
* Change the current photo on the theater if
16+
* exists otherwise we set the current photo to
17+
* an empty object and the theater will close automatically
18+
*
19+
* @param state
20+
* @param {Object} photo
21+
*/
622
changeSelectedPhoto(state, photo) {
7-
state.selectedPhoto = ((photo !== undefined || photo !== null) && Object.keys(photo).length > 0) ? photo : {};
23+
state.selectedPhoto = ((photo !== undefined && photo !== null) && Object.keys(photo).length > 0) ? photo : {};
24+
},
25+
26+
/**
27+
* If the index is equal to -1 we need to get the last photo of the gallery
28+
* so we can create that chain effect that we want instead of closing
29+
* the theater which is not intended at the moment.
30+
*
31+
* @param state
32+
* @param {number} index
33+
*/
34+
previousPhotoOfIndex(state, index) {
35+
index = (index + 1 === 0) ? state.photos.length - 1 : index;
36+
state.selectedPhoto = state.photos[index];
37+
},
38+
39+
/**
40+
* If we reach the latest element of the array we want to go back to the first one
41+
* creating the second part of the chain for the photo gallery.
42+
*
43+
* @param state
44+
* @param {number} index
45+
*/
46+
nextPhotoOfIndex(state, index) {
47+
index = (index - state.photos.length === 0) ? 0 : index;
48+
state.selectedPhoto = state.photos[index];
849
}
950
};

0 commit comments

Comments
 (0)