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

Commit 7c65a0a

Browse files
author
Claudio Ludovico Panetta
committed
Carousel major update for v0.3
This major update includes a bunch of new features as well as a new redesigned system for storing the current state of the carousel! Introducing: Vuex. With vuex we are now ready to scale this carousel without cluttering the main components. In fact vuex can be used to store all the current photos and the selected one, is that cool? Our components have less code and they don't have to bother anymore with a complicated share event handler. We have also done some minor optimization to the infrastructure removing useless code and refactoring the current one to be more modular. Patch notes: - Add a new folder that contains the store system built with Vuex - Remove the old shared events handler - Improve the quality of the components with a complete refactor - Add components name - Add Type on components properties - Remove useless spacing inside the files
1 parent 668b1ac commit 7c65a0a

File tree

10 files changed

+201
-106
lines changed

10 files changed

+201
-106
lines changed

dist/carousel.js

Lines changed: 46 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vuejs-carousel",
33
"description": "Complete photo carousel build with VueJS and web standards in mind",
4-
"version": "0.2.2",
4+
"version": "0.3",
55
"author": "Claudio Ludovico Panetta <ludovico1990@hotmail.it>",
66
"private": false,
77
"license": "MIT",

src/carousel.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
require("./shared/EventDispatcher.js");
2-
31
import Vue from "vue";
42
import VueResource from "vue-resource";
3+
import {store} from "./store/store";
4+
55
import Photos from "./components/Photos.vue";
66
import Theater from "./components/Theater.vue";
77

@@ -11,11 +11,13 @@ Vue.config.debug = true;
1111
const carousel = new Vue({
1212
el: "#your-application-id",
1313

14-
components: { Photos, Theater },
14+
store,
15+
16+
components: {Photos, Theater},
1517

1618
mounted() {
1719
// Debug purpose
18-
if(Vue.config.debug) {
20+
if (Vue.config.debug) {
1921
console.log("Debug is on");
2022
}
2123
}

src/components/Photos.vue

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,29 @@
1010

1111
<script>
1212
export default {
13+
name: "Photos",
1314
14-
props: ["source"],
15+
props: {
16+
source: { Type: String }
17+
},
1518
16-
data() {
17-
return {
18-
photos: {},
19+
computed: {
20+
photos() {
21+
return this.$store.getters.photos
1922
}
2023
},
2124
2225
mounted() {
23-
24-
window.EventDispatcher.listen("previousPhotoHasBeenRequested", (index) => {
25-
index--;
26-
if (index > 0) {
27-
this.selectThisPhoto(this.photos[index], index);
28-
} else {
29-
this.selectThisPhoto({});
30-
}
31-
});
32-
33-
window.EventDispatcher.listen("nextPhotoHasBeenRequested", (index) => {
34-
index++;
35-
if (index <= this.photos.length - 1) {
36-
this.selectThisPhoto(this.photos[index], index);
37-
} else {
38-
this.selectThisPhoto({});
39-
}
40-
});
41-
42-
this.$http.get(this.source).then((fetchedPhotos) => {
43-
this.photos = fetchedPhotos.data;
26+
this.$http.get(this.source).then((response) => {
27+
this.$store.commit("writePhotos", response.data);
4428
}, (response) => {
4529
console.error(response);
4630
});
4731
},
4832
4933
methods: {
50-
51-
selectThisPhoto(photo, index = null) {
52-
53-
if (index !== null) {
54-
photo.index = index;
55-
}
56-
57-
window.EventDispatcher.fire("photoHasBeenSelected", photo);
34+
selectThisPhoto(photo, index) {
35+
this.$store.commit("changeSelectedPhoto", {photo, index});
5836
},
5937
},
6038
}
@@ -81,4 +59,8 @@
8159
display: block;
8260
max-width: 100%;
8361
}
84-
</style>
62+
63+
64+
65+
66+
</style>

src/components/Theater.vue

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</div>
2323
<div class="Theater__information">
2424
<div class="Theater__header">
25-
<h4>{{ selectedPhoto.of }}</h4>
25+
<h4></h4>
2626
<div class="Theater__command Theater__command--header">
2727
<a href="#close-theater" @click.prevent="closeTheater">
2828
<i class="fa fa-fw fa-lg fa-times" aria-hidden="true"></i>
@@ -35,56 +35,58 @@
3535
</div>
3636
</div>
3737
</template>
38+
3839
<script>
3940
export default {
40-
41-
data() {
42-
return {
43-
selectedPhoto: {},
44-
}
45-
},
46-
47-
mounted() {
48-
window.EventDispatcher.listen("photoHasBeenSelected", (photo) => {
49-
this.changeSelectedPhoto(photo);
50-
});
51-
},
41+
name: "Theater",
5242
5343
methods: {
44+
previousPhoto(index) {
5445
55-
changeSelectedPhoto(photo) {
56-
this.selectedPhoto = photo;
57-
},
46+
index--;
47+
let payload = {};
48+
if (index > 0) {
49+
payload.photo = this.$store.getters.photos[index]
50+
payload.index = index;
51+
}
5852
59-
previousPhoto(index) {
60-
window.EventDispatcher.fire("previousPhotoHasBeenRequested", index);
53+
this.$store.commit("changeSelectedPhoto", payload);
6154
},
6255
6356
nextPhoto(index) {
64-
window.EventDispatcher.fire("nextPhotoHasBeenRequested", index);
57+
index++;
58+
let payload = {};
59+
if (index <= this.$store.getters.photos.length - 1) {
60+
payload.photo = this.$store.getters.photos[index]
61+
payload.index = index;
62+
}
63+
this.$store.commit("changeSelectedPhoto", payload);
6564
},
6665
6766
closeTheater() {
68-
this.changeSelectedPhoto({});
67+
this.$store.commit("changeSelectedPhoto", {});
6968
},
7069
},
7170
72-
computed: {
71+
computed: {
72+
selectedPhoto() {
73+
return this.$store.getters.selectedPhoto;
74+
},
7375
7476
isPhotoSelected() {
75-
return Object.keys(this.selectedPhoto).length !== 0 && this.selectedPhoto.constructor === Object;
77+
return this.$store.getters.isPhotoSelected;
7678
},
7779
7880
isTheaterClose() {
7981
return {
8082
"Theater--closed": !this.isPhotoSelected,
8183
}
8284
},
83-
}
85+
},
8486
}
8587
</script>
86-
<style>
8788

89+
<style>
8890
.Theater {
8991
position: fixed;
9092
top: 0;
@@ -170,4 +172,16 @@
170172
.Theater__command--header > a {
171173
color: black;
172174
}
175+
176+
177+
178+
179+
180+
181+
182+
183+
184+
185+
186+
173187
</style>

src/shared/EventDispatcher.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/store/getters.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
export const getters = {
2+
3+
/**
4+
* Return the current array of
5+
* photos from the state property
6+
*
7+
* It doesn't matter if the array is
8+
* empty.
9+
*
10+
* @param state
11+
* @returns {Array}
12+
*/
13+
photos: state => {
14+
return state.photos;
15+
},
16+
17+
/**
18+
* Return the current selected photo as
19+
* an object.
20+
*
21+
* @param state
22+
* @returns {state.selectedPhoto|{}}
23+
*/
24+
selectedPhoto: state => {
25+
return state.selectedPhoto;
26+
},
27+
28+
/**
29+
* Returns a boolean that determine if there is
30+
* a selected photo or an empty object for the
31+
* "selectedPhoto" state
32+
*
33+
* @param state
34+
* @param getters
35+
* @returns {boolean}
36+
*/
37+
isPhotoSelected: (state, getters) => {
38+
return Object.keys(getters.selectedPhoto).length > 0;
39+
}
40+
};

src/store/mutations.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export const mutations = {
2+
writePhotos(state, photos) {
3+
state.photos = photos;
4+
},
5+
6+
changeSelectedPhoto(state, payload) {
7+
if (Object.keys(payload).length <= 0) {
8+
state.selectedPhoto = {};
9+
} else {
10+
if ("index" in payload) {
11+
payload.photo.index = payload.index;
12+
}
13+
14+
state.selectedPhoto = payload.photo;
15+
}
16+
},
17+
};

src/store/states.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export const state = {
2+
3+
/**
4+
* Each time a photo is selected from
5+
* the gallery this property became a
6+
* Photo Object
7+
*/
8+
selectedPhoto: {},
9+
10+
/**
11+
* List of photos available inside
12+
* the gallery.
13+
*/
14+
photos: [],
15+
};

src/store/store.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Vue from "vue";
2+
import Vuex from "vuex";
3+
Vue.use(Vuex);
4+
5+
import {state} from "./states";
6+
import {getters} from "./getters";
7+
import {mutations} from "./mutations";
8+
9+
/**
10+
* Carousel main store
11+
*
12+
* We are using the Vuex store system because
13+
* it easier to keep track of what's happening
14+
* between the Photo Gallery and the Theater
15+
*
16+
* @type {Store}
17+
*/
18+
export const store = new Vuex.Store({
19+
state,
20+
getters,
21+
mutations,
22+
});

0 commit comments

Comments
 (0)