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