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