Skip to content
This repository was archived by the owner on Feb 24, 2023. It is now read-only.

Commit 988aa0b

Browse files
author
Łukasz Florczak
committed
Watchers mixin
1 parent 748ee3e commit 988aa0b

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

src/mixins/watchers.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Component watchers
3+
*/
4+
const mixin = {
5+
watch: {
6+
// Watch window width change
7+
widthWindow (newValue, oldValue) {
8+
if (oldValue) {
9+
this.prepareCarousel()
10+
this.toggleFade()
11+
}
12+
},
13+
14+
// Watch current slide change
15+
currentSlide () {
16+
this.prepareSlidesClasses()
17+
18+
// Set start time of slide
19+
this.autoplayStart = (this.settings.autoplay) ? +new Date() : null
20+
21+
this.$emit('afterChange', { currentSlide: this.currentSlide })
22+
},
23+
24+
// Recalculate settings
25+
currentBreakpoint () {
26+
this.prepareSettings()
27+
this.$emit('breakpoint', { breakpoint: this.currentBreakpoint })
28+
},
29+
30+
// Watch drag distance change
31+
dragDistance () {
32+
if (this.mouseDown) {
33+
if (this.dragDistance > this.swipeDistance && this.canGoToPrev) {
34+
this.goToPrev()
35+
this.handleMouseUp()
36+
}
37+
38+
if (this.dragDistance < -1 * this.swipeDistance && this.canGoToNext) {
39+
this.goToNext()
40+
this.handleMouseUp()
41+
}
42+
}
43+
},
44+
45+
'settings.fade' () {
46+
this.toggleFade()
47+
},
48+
49+
'settings.autoplay' () {
50+
this.toggleAutoPlay()
51+
},
52+
53+
pauseAutoPlay (nevValue) {
54+
if (nevValue) {
55+
// Store current slide remaining time and disable auto play mode
56+
this.remaining = this.settings.autoplaySpeed - (+new Date() - this.autoplayStart)
57+
this.disableAutoPlay()
58+
this.clearAutoPlayPause()
59+
} else {
60+
// Go to next after remaining time and rerun auto play mode
61+
this.autoplayTimeout = setTimeout(() => {
62+
this.clearAutoPlayPause()
63+
this.goToNext()
64+
this.toggleAutoPlay()
65+
}, this.remaining)
66+
}
67+
}
68+
}
69+
}
70+
71+
export default mixin

0 commit comments

Comments
 (0)