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

Commit f224f16

Browse files
author
Łukasz Florczak
committed
Carousel preparations methods mixin
1 parent abc5aee commit f224f16

File tree

2 files changed

+98
-81
lines changed

2 files changed

+98
-81
lines changed

src/Agile.vue

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<script>
3838
import handlers from '@/mixins/handlers'
3939
import helpers from '@/mixins/helpers'
40+
import preparations from '@/mixins/preparations'
4041
import props from '@/mixins/props'
4142
import watchers from '@/mixins/watchers'
4243
@@ -180,87 +181,6 @@
180181
this.toggleFade()
181182
},
182183
183-
// Prepare settings object
184-
prepareSettings () {
185-
if (!this.initialSettings.responsive) {
186-
this.toggleFade()
187-
this.toggleAutoPlay()
188-
return false
189-
}
190-
191-
let newSettings = Object.assign({}, this.initialSettings)
192-
delete newSettings.responsive
193-
194-
this.initialSettings.responsive.forEach(option => {
195-
if (this.initialSettings.mobileFirst ? option.breakpoint < this.widthWindow : option.breakpoint > this.widthWindow) {
196-
for (let key in option.settings) {
197-
newSettings[key] = option.settings[key]
198-
}
199-
}
200-
})
201-
202-
this.settings = Object.assign({}, newSettings)
203-
},
204-
205-
// Prepare slides classes and styles
206-
prepareSlides () {
207-
this.slides = this.htmlCollectionToArray(this.$refs.slides.children)
208-
209-
// Probably timeout needed
210-
if (this.clonedSlides) {
211-
this.slidesClonedBefore = this.htmlCollectionToArray(this.$refs.slidesClonedBefore.children)
212-
this.slidesClonedAfter = this.htmlCollectionToArray(this.$refs.slidesClonedAfter.children)
213-
}
214-
215-
for (let slide of this.allSlides) {
216-
slide.classList.add('agile__slide')
217-
}
218-
},
219-
220-
// Prepare slides active/current classes
221-
prepareSlidesClasses () {
222-
// Remove active & current classes
223-
for (let i = 0; i < this.slidesCount; i++) {
224-
this.slides[i].classList.remove('agile__slide--active')
225-
this.slides[i].classList.remove('agile__slide--current')
226-
}
227-
228-
// Add active & current class for current slide
229-
this.slides[this.currentSlide].classList.add('agile__slide--active')
230-
231-
let start = (this.clonedSlides) ? this.slidesCount + this.currentSlide : this.currentSlide
232-
233-
if (this.centerMode) {
234-
start -= (Math.floor(this.settings.slidesToShow / 2) - +(this.settings.slidesToShow % 2 === 0))
235-
}
236-
237-
// To account for the combination of infinite = false and centerMode = true, ensure we don't overrun the bounds of the slide count.
238-
for (let i = Math.max(start, 0); i < Math.min(start + this.settings.slidesToShow, this.slidesCount); i++) {
239-
this.allSlides[i].classList.add('agile__slide--current')
240-
}
241-
},
242-
243-
// Prepare carousel styles
244-
prepareCarousel () {
245-
this.widthSlide = !this.settings.unagile ? this.widthContainer / this.settings.slidesToShow : 'auto'
246-
247-
// Actions on document resize
248-
for (let i = 0; i < this.allSlidesCount; i++) {
249-
this.allSlides[i].style.width = this.widthSlide + 'px'
250-
}
251-
252-
// Prepare track
253-
if (this.settings.unagile) {
254-
this.translateX = 0
255-
} else {
256-
if (this.currentSlide === null) {
257-
this.currentSlide = this.settings.initialSlide
258-
}
259-
260-
this.goTo(this.currentSlide, false, false)
261-
}
262-
},
263-
264184
toggleFade () {
265185
let enabled = (!this.settings.unagile && this.settings.fade)
266186

src/mixins/preparations.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* Carousel preparation methods
3+
*/
4+
const mixin = {
5+
methods: {
6+
/**
7+
* Prepare settings object
8+
*/
9+
prepareSettings () {
10+
if (!this.initialSettings.responsive) {
11+
this.toggleFade()
12+
this.toggleAutoPlay()
13+
return false
14+
}
15+
16+
let newSettings = Object.assign({}, this.initialSettings)
17+
delete newSettings.responsive
18+
19+
this.initialSettings.responsive.forEach(option => {
20+
if (this.initialSettings.mobileFirst ? option.breakpoint < this.widthWindow : option.breakpoint > this.widthWindow) {
21+
for (let key in option.settings) {
22+
newSettings[key] = option.settings[key]
23+
}
24+
}
25+
})
26+
27+
this.settings = Object.assign({}, newSettings)
28+
},
29+
30+
/**
31+
* Prepare slides classes and styles
32+
*/
33+
prepareSlides () {
34+
this.slides = this.htmlCollectionToArray(this.$refs.slides.children)
35+
36+
// Probably timeout needed
37+
if (this.clonedSlides) {
38+
this.slidesClonedBefore = this.htmlCollectionToArray(this.$refs.slidesClonedBefore.children)
39+
this.slidesClonedAfter = this.htmlCollectionToArray(this.$refs.slidesClonedAfter.children)
40+
}
41+
42+
for (let slide of this.allSlides) {
43+
slide.classList.add('agile__slide')
44+
}
45+
},
46+
47+
/**
48+
* Prepare slides active/current classes
49+
*/
50+
prepareSlidesClasses () {
51+
// Remove active & current classes
52+
for (let i = 0; i < this.slidesCount; i++) {
53+
this.slides[i].classList.remove('agile__slide--active')
54+
this.slides[i].classList.remove('agile__slide--current')
55+
}
56+
57+
// Add active & current class for current slide
58+
this.slides[this.currentSlide].classList.add('agile__slide--active')
59+
60+
let start = (this.clonedSlides) ? this.slidesCount + this.currentSlide : this.currentSlide
61+
62+
if (this.centerMode) {
63+
start -= (Math.floor(this.settings.slidesToShow / 2) - +(this.settings.slidesToShow % 2 === 0))
64+
}
65+
66+
// To account for the combination of infinite = false and centerMode = true, ensure we don't overrun the bounds of the slide count.
67+
for (let i = Math.max(start, 0); i < Math.min(start + this.settings.slidesToShow, this.slidesCount); i++) {
68+
this.allSlides[i].classList.add('agile__slide--current')
69+
}
70+
},
71+
72+
/**
73+
* Prepare carousel styles
74+
*/
75+
prepareCarousel () {
76+
this.widthSlide = !this.settings.unagile ? this.widthContainer / this.settings.slidesToShow : 'auto'
77+
78+
// Actions on document resize
79+
for (let i = 0; i < this.allSlidesCount; i++) {
80+
this.allSlides[i].style.width = this.widthSlide + 'px'
81+
}
82+
83+
// Prepare track
84+
if (this.settings.unagile) {
85+
this.translateX = 0
86+
} else {
87+
if (this.currentSlide === null) {
88+
this.currentSlide = this.settings.initialSlide
89+
}
90+
91+
this.goTo(this.currentSlide, false, false)
92+
}
93+
}
94+
}
95+
}
96+
97+
export default mixin

0 commit comments

Comments
 (0)