UNPKG

3.98 kBJavaScriptView Raw
1import { isObject, extend } from './utils.js';
2
3function updateSwiper({
4 swiper,
5 passedParams,
6 changedParams,
7 nextEl,
8 prevEl,
9 scrollbarEl,
10 paginationEl
11}) {
12 const updateParams = changedParams.filter(key => key !== 'children' && key !== 'direction');
13 const {
14 params: currentParams,
15 pagination,
16 navigation,
17 scrollbar,
18 thumbs
19 } = swiper;
20 let needThumbsInit;
21 let needControllerInit;
22 let needPaginationInit;
23 let needScrollbarInit;
24 let needNavigationInit;
25
26 if (changedParams.includes('thumbs') && passedParams.thumbs && passedParams.thumbs.swiper && currentParams.thumbs && !currentParams.thumbs.swiper) {
27 needThumbsInit = true;
28 }
29
30 if (changedParams.includes('controller') && passedParams.controller && passedParams.controller.control && currentParams.controller && !currentParams.controller.control) {
31 needControllerInit = true;
32 }
33
34 if (changedParams.includes('pagination') && passedParams.pagination && (passedParams.pagination.el || paginationEl) && (currentParams.pagination || currentParams.pagination === false) && pagination && !pagination.el) {
35 needPaginationInit = true;
36 }
37
38 if (changedParams.includes('scrollbar') && passedParams.scrollbar && (passedParams.scrollbar.el || scrollbarEl) && (currentParams.scrollbar || currentParams.scrollbar === false) && scrollbar && !scrollbar.el) {
39 needScrollbarInit = true;
40 }
41
42 if (changedParams.includes('navigation') && passedParams.navigation && (passedParams.navigation.prevEl || prevEl) && (passedParams.navigation.nextEl || nextEl) && (currentParams.navigation || currentParams.navigation === false) && navigation && !navigation.prevEl && !navigation.nextEl) {
43 needNavigationInit = true;
44 }
45
46 if (changedParams.includes('virtual')) {
47 if (passedParams.virtual && passedParams.virtual.slides && swiper.virtual) {
48 swiper.virtual.slides = passedParams.virtual.slides;
49 swiper.virtual.update();
50 }
51 }
52
53 const destroyModule = mod => {
54 if (!swiper[mod]) return;
55 swiper[mod].destroy();
56
57 if (mod === 'navigation') {
58 currentParams[mod].prevEl = undefined;
59 currentParams[mod].nextEl = undefined;
60 swiper[mod].prevEl = undefined;
61 swiper[mod].nextEl = undefined;
62 } else {
63 currentParams[mod].el = undefined;
64 swiper[mod].el = undefined;
65 }
66 };
67
68 updateParams.forEach(key => {
69 if (isObject(currentParams[key]) && isObject(passedParams[key])) {
70 extend(currentParams[key], passedParams[key]);
71 } else {
72 const newValue = passedParams[key];
73
74 if ((newValue === true || newValue === false) && (key === 'navigation' || key === 'pagination' || key === 'scrollbar')) {
75 if (newValue === false) {
76 destroyModule(key);
77 }
78 } else {
79 currentParams[key] = passedParams[key];
80 }
81 }
82 });
83
84 if (needThumbsInit) {
85 const initialized = thumbs.init();
86
87 if (initialized) {
88 thumbs.update(true);
89 }
90 }
91
92 if (needControllerInit) {
93 swiper.controller.control = currentParams.controller.control;
94 }
95
96 if (needPaginationInit) {
97 if (paginationEl) currentParams.pagination.el = paginationEl;
98 pagination.init();
99 pagination.render();
100 pagination.update();
101 }
102
103 if (needScrollbarInit) {
104 if (scrollbarEl) currentParams.scrollbar.el = scrollbarEl;
105 scrollbar.init();
106 scrollbar.updateSize();
107 scrollbar.setTranslate();
108 }
109
110 if (needNavigationInit) {
111 if (nextEl) currentParams.navigation.nextEl = nextEl;
112 if (prevEl) currentParams.navigation.prevEl = prevEl;
113 navigation.init();
114 navigation.update();
115 }
116
117 if (changedParams.includes('allowSlideNext')) {
118 swiper.allowSlideNext = passedParams.allowSlideNext;
119 }
120
121 if (changedParams.includes('allowSlidePrev')) {
122 swiper.allowSlidePrev = passedParams.allowSlidePrev;
123 }
124
125 if (changedParams.includes('direction')) {
126 swiper.changeDirection(passedParams.direction, false);
127 }
128
129 swiper.update();
130}
131
132export { updateSwiper };
\No newline at end of file