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

Commit 32a3219

Browse files
author
Claudio Ludovico Panetta
committed
Optimization update for v0.4
This update brings a lot of optimizations needed in order to create a scalable carousel for the future and thus we need to bump the version up because it's a major change. First thing first we tried to set a standard for the code itself with good identation across all files. We decouple the Photos component by splitting the component in two entities: - Photos is a general container and it should not be responsible of the Photo itself and his behaviours - Photo is a single photo component which is responsible of controlling the single photo loaded from the source. We also optimize the theater by removing the index as parameter because we can fetch it from the vuex itself, we don't need to pass index as a separate parameter anymore. We have sone some small changes on the Vuex store that introduce some new getters and an optimized `changeSelectedPhoto` mutation.
1 parent 94fe4a9 commit 32a3219

File tree

9 files changed

+143
-108
lines changed

9 files changed

+143
-108
lines changed

dist/carousel.js

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

index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@
3636

3737
<div class="hero-body">
3838
<div class="container has-text-centered">
39-
<h1 class="title">VueJs Carousel <small><img src="https://img.shields.io/npm/v/vuejs-carousel.svg" alt="VueJs Version"></small></h1>
39+
<h1 class="title">
40+
<span>VueJs Carousel</span>
41+
<small><img src="https://img.shields.io/npm/v/vuejs-carousel.svg" alt="VueJs Version"></small>
42+
<small><img src="https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg" alt="Part of Awesome VueJs"></small>
43+
</h1>
4044
<h2 class="subtitle">Easy to use VueJS component for Photo galleries, built with VueJS and web standards in mind</h2>
4145
</div>
4246
</div>

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.3.1",
4+
"version": "0.4",
55
"author": "Claudio Ludovico Panetta <ludovico1990@hotmail.it>",
66
"private": false,
77
"license": "MIT",

src/carousel.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ const carousel = new Vue({
1313

1414
store,
1515

16-
components: {Photos, Theater},
16+
components: {
17+
Photos,
18+
Theater
19+
},
1720

1821
mounted() {
1922
// Debug purpose

src/components/Photo.vue

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<template>
2+
<li class="Photo">
3+
<figure class="Photo__container">
4+
<img class="Photo__source" :src="thumbnail" @click="selectThisPhoto(photo)"/>
5+
</figure>
6+
</li>
7+
</template>
8+
9+
<script>
10+
export default {
11+
name: "photo",
12+
13+
props: {
14+
photo: {
15+
type: Object,
16+
required: true
17+
},
18+
},
19+
20+
computed: {
21+
thumbnail() {
22+
return this.photo.thumbnailUrl;
23+
}
24+
},
25+
26+
methods: {
27+
selectThisPhoto(photo) {
28+
this.$store.commit("changeSelectedPhoto", photo);
29+
},
30+
},
31+
}
32+
</script>

src/components/Photos.vue

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
<template id="photos">
22
<ul class="Photos">
3-
<li class="Photo" v-for="(photo, index) in photos">
4-
<figure class="Photo__container">
5-
<img class="Photo__source" :src="photo.thumbnailUrl" @click="selectThisPhoto(photo, index)"/>
6-
</figure>
7-
</li>
3+
<photo v-for="photo in photos" :photo="photo"></photo>
84
</ul>
95
</template>
106

117
<script>
8+
import Photo from "./Photo.vue";
9+
1210
export default {
13-
name: "Photos",
11+
name: "photos",
1412
15-
props: {
16-
source: { Type: String }
13+
components: {
14+
Photo
1715
},
1816
1917
computed: {
@@ -22,19 +20,19 @@
2220
}
2321
},
2422
23+
props: {
24+
source: {
25+
Type: String
26+
}
27+
},
28+
2529
mounted() {
2630
this.$http.get(this.source).then((response) => {
2731
this.$store.commit("writePhotos", response.data);
2832
}, (response) => {
2933
console.error(response);
3034
});
3135
},
32-
33-
methods: {
34-
selectThisPhoto(photo, index) {
35-
this.$store.commit("changeSelectedPhoto", {photo, index});
36-
},
37-
},
3836
}
3937
</script>
4038

@@ -59,8 +57,4 @@
5957
display: block;
6058
max-width: 100%;
6159
}
62-
63-
64-
65-
6660
</style>

src/components/Theater.vue

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
</figure>
99
<div class="Theater__commands">
1010
<div class="Theater__command">
11-
<a href="#previous" @click.prevent="previousPhoto(selectedPhoto.index)">
11+
<a href="#previous" @click.prevent="previousPhoto()">
1212
<i class="fa fa-fw fa-lg fa-4x fa-angle-double-left" aria-hidden="true"></i>
1313
</a>
1414
</div>
1515
<div class="Theater__command">
16-
<a href="#next" @click.prevent="nextPhoto(selectedPhoto.index)">
16+
<a href="#next" @click.prevent="nextPhoto()">
1717
<i class="fa fa-fw fa-lg fa-4x fa-angle-double-right" aria-hidden="true"></i>
1818
</a>
1919
</div>
@@ -22,7 +22,7 @@
2222
</div>
2323
<div class="Theater__information">
2424
<div class="Theater__header">
25-
<h4></h4>
25+
<h4 v-text="selectedPhoto.title"></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>
@@ -38,37 +38,13 @@
3838

3939
<script>
4040
export default {
41-
name: "Theater",
41+
name: "theater",
4242
43-
methods: {
44-
previousPhoto(index) {
45-
46-
index--;
47-
let payload = {};
48-
if (index > 0) {
49-
payload.photo = this.$store.getters.photos[index]
50-
payload.index = index;
51-
}
52-
53-
this.$store.commit("changeSelectedPhoto", payload);
54-
},
55-
56-
nextPhoto(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);
64-
},
65-
66-
closeTheater() {
67-
this.$store.commit("changeSelectedPhoto", {});
43+
computed: {
44+
photoIndex() {
45+
return this.$store.getters.photoIndex;
6846
},
69-
},
7047
71-
computed: {
7248
selectedPhoto() {
7349
return this.$store.getters.selectedPhoto;
7450
},
@@ -83,6 +59,23 @@
8359
}
8460
},
8561
},
62+
63+
methods: {
64+
previousPhoto() {
65+
let photo = this.$store.getters.photos[this.photoIndex - 1];
66+
this.$store.commit("changeSelectedPhoto", photo);
67+
},
68+
69+
nextPhoto() {
70+
let photo = this.$store.getters.photos[this.photoIndex + 1];
71+
this.$store.commit("changeSelectedPhoto", photo);
72+
},
73+
74+
closeTheater() {
75+
this.$store.commit("changeSelectedPhoto", {});
76+
},
77+
},
78+
8679
}
8780
</script>
8881

@@ -172,16 +165,4 @@
172165
.Theater__command--header > a {
173166
color: black;
174167
}
175-
176-
177-
178-
179-
180-
181-
182-
183-
184-
185-
186-
187168
</style>

src/store/getters.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,18 @@ export const getters = {
3131
* "selectedPhoto" state
3232
*
3333
* @param state
34-
* @param getters
3534
* @returns {boolean}
3635
*/
37-
isPhotoSelected: (state, getters) => {
38-
return Object.keys(getters.selectedPhoto).length > 0;
36+
isPhotoSelected: (state) => {
37+
return Object.keys(state.selectedPhoto).length > 0;
38+
},
39+
40+
/**
41+
*
42+
* @param state
43+
* @returns {number}
44+
*/
45+
photoIndex: state => {
46+
return state.photos.indexOf(state.selectedPhoto);
3947
}
4048
};

src/store/mutations.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@ export const mutations = {
33
state.photos = photos;
44
},
55

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-
},
6+
changeSelectedPhoto(state, photo) {
7+
state.selectedPhoto = (photo != null && Object.keys(photo).length > 0) ? photo : {};
8+
}
179
};

0 commit comments

Comments
 (0)