|
1 |
| -import { extend, isObject, bindModuleMethods } from '../../shared/utils.js'; |
| 1 | +import { extend, isObject } from '../../shared/utils.js'; |
2 | 2 | import $ from '../../shared/dom.js';
|
3 | 3 |
|
4 |
| -const Thumbs = { |
5 |
| - init() { |
6 |
| - const swiper = this; |
7 |
| - const { thumbs: thumbsParams } = swiper.params; |
8 |
| - if (swiper.thumbs.initialized) return false; |
9 |
| - swiper.thumbs.initialized = true; |
10 |
| - const SwiperClass = swiper.constructor; |
11 |
| - if (thumbsParams.swiper instanceof SwiperClass) { |
12 |
| - swiper.thumbs.swiper = thumbsParams.swiper; |
13 |
| - extend(swiper.thumbs.swiper.originalParams, { |
14 |
| - watchSlidesProgress: true, |
15 |
| - slideToClickedSlide: false, |
16 |
| - }); |
17 |
| - extend(swiper.thumbs.swiper.params, { |
18 |
| - watchSlidesProgress: true, |
19 |
| - slideToClickedSlide: false, |
20 |
| - }); |
21 |
| - } else if (isObject(thumbsParams.swiper)) { |
22 |
| - swiper.thumbs.swiper = new SwiperClass( |
23 |
| - extend({}, thumbsParams.swiper, { |
24 |
| - watchSlidesVisibility: true, |
25 |
| - watchSlidesProgress: true, |
26 |
| - slideToClickedSlide: false, |
27 |
| - }), |
28 |
| - ); |
29 |
| - swiper.thumbs.swiperCreated = true; |
30 |
| - } |
31 |
| - swiper.thumbs.swiper.$el.addClass(swiper.params.thumbs.thumbsContainerClass); |
32 |
| - swiper.thumbs.swiper.on('tap', swiper.thumbs.onThumbClick); |
33 |
| - return true; |
34 |
| - }, |
35 |
| - onThumbClick() { |
36 |
| - const swiper = this; |
| 4 | +export default function Thumb({ swiper, extendParams, on }) { |
| 5 | + extendParams({ |
| 6 | + thumbs: { |
| 7 | + swiper: null, |
| 8 | + multipleActiveThumbs: true, |
| 9 | + autoScrollOffset: 0, |
| 10 | + slideThumbActiveClass: 'swiper-slide-thumb-active', |
| 11 | + thumbsContainerClass: 'swiper-container-thumbs', |
| 12 | + }, |
| 13 | + }); |
| 14 | + |
| 15 | + let initialized = false; |
| 16 | + let swiperCreated = false; |
| 17 | + |
| 18 | + swiper.thumbs = { |
| 19 | + swiper: null, |
| 20 | + }; |
| 21 | + |
| 22 | + function onThumbClick() { |
37 | 23 | const thumbsSwiper = swiper.thumbs.swiper;
|
38 | 24 | if (!thumbsSwiper) return;
|
39 | 25 | const clickedIndex = thumbsSwiper.clickedIndex;
|
@@ -71,9 +57,39 @@ const Thumbs = {
|
71 | 57 | else slideToIndex = prevIndex;
|
72 | 58 | }
|
73 | 59 | swiper.slideTo(slideToIndex);
|
74 |
| - }, |
75 |
| - update(initial) { |
76 |
| - const swiper = this; |
| 60 | + } |
| 61 | + |
| 62 | + function init() { |
| 63 | + const { thumbs: thumbsParams } = swiper.params; |
| 64 | + if (initialized) return false; |
| 65 | + initialized = true; |
| 66 | + const SwiperClass = swiper.constructor; |
| 67 | + if (thumbsParams.swiper instanceof SwiperClass) { |
| 68 | + swiper.thumbs.swiper = thumbsParams.swiper; |
| 69 | + extend(swiper.thumbs.swiper.originalParams, { |
| 70 | + watchSlidesProgress: true, |
| 71 | + slideToClickedSlide: false, |
| 72 | + }); |
| 73 | + extend(swiper.thumbs.swiper.params, { |
| 74 | + watchSlidesProgress: true, |
| 75 | + slideToClickedSlide: false, |
| 76 | + }); |
| 77 | + } else if (isObject(thumbsParams.swiper)) { |
| 78 | + swiper.thumbs.swiper = new SwiperClass( |
| 79 | + extend({}, thumbsParams.swiper, { |
| 80 | + watchSlidesVisibility: true, |
| 81 | + watchSlidesProgress: true, |
| 82 | + slideToClickedSlide: false, |
| 83 | + }), |
| 84 | + ); |
| 85 | + swiperCreated = true; |
| 86 | + } |
| 87 | + swiper.thumbs.swiper.$el.addClass(swiper.params.thumbs.thumbsContainerClass); |
| 88 | + swiper.thumbs.swiper.on('tap', onThumbClick); |
| 89 | + return true; |
| 90 | + } |
| 91 | + |
| 92 | + function update(initial) { |
77 | 93 | const thumbsSwiper = swiper.thumbs.swiper;
|
78 | 94 | if (!thumbsSwiper) return;
|
79 | 95 |
|
@@ -180,63 +196,33 @@ const Thumbs = {
|
180 | 196 | thumbsSwiper.slides.eq(swiper.realIndex + i).addClass(thumbActiveClass);
|
181 | 197 | }
|
182 | 198 | }
|
183 |
| - }, |
184 |
| -}; |
185 |
| -export default { |
186 |
| - name: 'thumbs', |
187 |
| - params: { |
188 |
| - thumbs: { |
189 |
| - swiper: null, |
190 |
| - multipleActiveThumbs: true, |
191 |
| - autoScrollOffset: 0, |
192 |
| - slideThumbActiveClass: 'swiper-slide-thumb-active', |
193 |
| - thumbsContainerClass: 'swiper-container-thumbs', |
194 |
| - }, |
195 |
| - }, |
196 |
| - create() { |
197 |
| - const swiper = this; |
198 |
| - bindModuleMethods(swiper, { |
199 |
| - thumbs: { |
200 |
| - swiper: null, |
201 |
| - initialized: false, |
202 |
| - ...Thumbs, |
203 |
| - }, |
204 |
| - }); |
205 |
| - }, |
206 |
| - on: { |
207 |
| - beforeInit(swiper) { |
208 |
| - const { thumbs } = swiper.params; |
209 |
| - if (!thumbs || !thumbs.swiper) return; |
210 |
| - swiper.thumbs.init(); |
211 |
| - swiper.thumbs.update(true); |
212 |
| - }, |
213 |
| - slideChange(swiper) { |
214 |
| - if (!swiper.thumbs.swiper) return; |
215 |
| - swiper.thumbs.update(); |
216 |
| - }, |
217 |
| - update(swiper) { |
218 |
| - if (!swiper.thumbs.swiper) return; |
219 |
| - swiper.thumbs.update(); |
220 |
| - }, |
221 |
| - resize(swiper) { |
222 |
| - if (!swiper.thumbs.swiper) return; |
223 |
| - swiper.thumbs.update(); |
224 |
| - }, |
225 |
| - observerUpdate(swiper) { |
226 |
| - if (!swiper.thumbs.swiper) return; |
227 |
| - swiper.thumbs.update(); |
228 |
| - }, |
229 |
| - setTransition(swiper, duration) { |
230 |
| - const thumbsSwiper = swiper.thumbs.swiper; |
231 |
| - if (!thumbsSwiper) return; |
232 |
| - thumbsSwiper.setTransition(duration); |
233 |
| - }, |
234 |
| - beforeDestroy(swiper) { |
235 |
| - const thumbsSwiper = swiper.thumbs.swiper; |
236 |
| - if (!thumbsSwiper) return; |
237 |
| - if (swiper.thumbs.swiperCreated && thumbsSwiper) { |
238 |
| - thumbsSwiper.destroy(); |
239 |
| - } |
240 |
| - }, |
241 |
| - }, |
242 |
| -}; |
| 199 | + } |
| 200 | + |
| 201 | + on('beforeInit', () => { |
| 202 | + const { thumbs } = swiper.params; |
| 203 | + if (!thumbs || !thumbs.swiper) return; |
| 204 | + init(); |
| 205 | + update(true); |
| 206 | + }); |
| 207 | + on('slideChange update resize observerUpdate', () => { |
| 208 | + if (!swiper.thumbs.swiper) return; |
| 209 | + update(); |
| 210 | + }); |
| 211 | + on('setTransition', (_s, duration) => { |
| 212 | + const thumbsSwiper = swiper.thumbs.swiper; |
| 213 | + if (!thumbsSwiper) return; |
| 214 | + thumbsSwiper.setTransition(duration); |
| 215 | + }); |
| 216 | + on('beforeDestroy', () => { |
| 217 | + const thumbsSwiper = swiper.thumbs.swiper; |
| 218 | + if (!thumbsSwiper) return; |
| 219 | + if (swiperCreated && thumbsSwiper) { |
| 220 | + thumbsSwiper.destroy(); |
| 221 | + } |
| 222 | + }); |
| 223 | + |
| 224 | + Object.assign(swiper.thumbs, { |
| 225 | + init, |
| 226 | + update, |
| 227 | + }); |
| 228 | +} |
0 commit comments