update-swiper.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { isObject, extend } from './utils.js';
  2. function updateSwiper({
  3. swiper,
  4. slides,
  5. passedParams,
  6. changedParams,
  7. nextEl,
  8. prevEl,
  9. scrollbarEl,
  10. paginationEl
  11. }) {
  12. const updateParams = changedParams.filter(key => key !== 'children' && key !== 'direction' && key !== 'wrapperClass');
  13. const {
  14. params: currentParams,
  15. pagination,
  16. navigation,
  17. scrollbar,
  18. virtual,
  19. thumbs
  20. } = swiper;
  21. let needThumbsInit;
  22. let needControllerInit;
  23. let needPaginationInit;
  24. let needScrollbarInit;
  25. let needNavigationInit;
  26. let loopNeedDestroy;
  27. let loopNeedEnable;
  28. let loopNeedReloop;
  29. if (changedParams.includes('thumbs') && passedParams.thumbs && passedParams.thumbs.swiper && currentParams.thumbs && !currentParams.thumbs.swiper) {
  30. needThumbsInit = true;
  31. }
  32. if (changedParams.includes('controller') && passedParams.controller && passedParams.controller.control && currentParams.controller && !currentParams.controller.control) {
  33. needControllerInit = true;
  34. }
  35. if (changedParams.includes('pagination') && passedParams.pagination && (passedParams.pagination.el || paginationEl) && (currentParams.pagination || currentParams.pagination === false) && pagination && !pagination.el) {
  36. needPaginationInit = true;
  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. if (changedParams.includes('navigation') && passedParams.navigation && (passedParams.navigation.prevEl || prevEl) && (passedParams.navigation.nextEl || nextEl) && (currentParams.navigation || currentParams.navigation === false) && navigation && !navigation.prevEl && !navigation.nextEl) {
  42. needNavigationInit = true;
  43. }
  44. const destroyModule = mod => {
  45. if (!swiper[mod]) return;
  46. swiper[mod].destroy();
  47. if (mod === 'navigation') {
  48. if (swiper.isElement) {
  49. swiper[mod].prevEl.remove();
  50. swiper[mod].nextEl.remove();
  51. }
  52. currentParams[mod].prevEl = undefined;
  53. currentParams[mod].nextEl = undefined;
  54. swiper[mod].prevEl = undefined;
  55. swiper[mod].nextEl = undefined;
  56. } else {
  57. if (swiper.isElement) {
  58. swiper[mod].el.remove();
  59. }
  60. currentParams[mod].el = undefined;
  61. swiper[mod].el = undefined;
  62. }
  63. };
  64. if (changedParams.includes('loop') && swiper.isElement) {
  65. if (currentParams.loop && !passedParams.loop) {
  66. loopNeedDestroy = true;
  67. } else if (!currentParams.loop && passedParams.loop) {
  68. loopNeedEnable = true;
  69. } else {
  70. loopNeedReloop = true;
  71. }
  72. }
  73. updateParams.forEach(key => {
  74. if (isObject(currentParams[key]) && isObject(passedParams[key])) {
  75. extend(currentParams[key], passedParams[key]);
  76. if ((key === 'navigation' || key === 'pagination' || key === 'scrollbar') && 'enabled' in passedParams[key] && !passedParams[key].enabled) {
  77. destroyModule(key);
  78. }
  79. } else {
  80. const newValue = passedParams[key];
  81. if ((newValue === true || newValue === false) && (key === 'navigation' || key === 'pagination' || key === 'scrollbar')) {
  82. if (newValue === false) {
  83. destroyModule(key);
  84. }
  85. } else {
  86. currentParams[key] = passedParams[key];
  87. }
  88. }
  89. });
  90. if (updateParams.includes('controller') && !needControllerInit && swiper.controller && swiper.controller.control && currentParams.controller && currentParams.controller.control) {
  91. swiper.controller.control = currentParams.controller.control;
  92. }
  93. if (changedParams.includes('children') && slides && virtual && currentParams.virtual.enabled) {
  94. virtual.slides = slides;
  95. virtual.update(true);
  96. }
  97. if (changedParams.includes('children') && slides && currentParams.loop) {
  98. loopNeedReloop = true;
  99. }
  100. if (needThumbsInit) {
  101. const initialized = thumbs.init();
  102. if (initialized) thumbs.update(true);
  103. }
  104. if (needControllerInit) {
  105. swiper.controller.control = currentParams.controller.control;
  106. }
  107. if (needPaginationInit) {
  108. if (swiper.isElement && (!paginationEl || typeof paginationEl === 'string')) {
  109. paginationEl = document.createElement('div');
  110. paginationEl.classList.add('swiper-pagination');
  111. swiper.el.shadowEl.appendChild(paginationEl);
  112. }
  113. if (paginationEl) currentParams.pagination.el = paginationEl;
  114. pagination.init();
  115. pagination.render();
  116. pagination.update();
  117. }
  118. if (needScrollbarInit) {
  119. if (swiper.isElement && (!scrollbarEl || typeof scrollbarEl === 'string')) {
  120. scrollbarEl = document.createElement('div');
  121. scrollbarEl.classList.add('swiper-scrollbar');
  122. swiper.el.shadowEl.appendChild(scrollbarEl);
  123. }
  124. if (scrollbarEl) currentParams.scrollbar.el = scrollbarEl;
  125. scrollbar.init();
  126. scrollbar.updateSize();
  127. scrollbar.setTranslate();
  128. }
  129. if (needNavigationInit) {
  130. if (swiper.isElement) {
  131. if (!nextEl || typeof nextEl === 'string') {
  132. nextEl = document.createElement('div');
  133. nextEl.classList.add('swiper-button-next');
  134. swiper.el.shadowEl.appendChild(nextEl);
  135. }
  136. if (!prevEl || typeof prevEl === 'string') {
  137. prevEl = document.createElement('div');
  138. prevEl.classList.add('swiper-button-prev');
  139. swiper.el.shadowEl.appendChild(prevEl);
  140. }
  141. }
  142. if (nextEl) currentParams.navigation.nextEl = nextEl;
  143. if (prevEl) currentParams.navigation.prevEl = prevEl;
  144. navigation.init();
  145. navigation.update();
  146. }
  147. if (changedParams.includes('allowSlideNext')) {
  148. swiper.allowSlideNext = passedParams.allowSlideNext;
  149. }
  150. if (changedParams.includes('allowSlidePrev')) {
  151. swiper.allowSlidePrev = passedParams.allowSlidePrev;
  152. }
  153. if (changedParams.includes('direction')) {
  154. swiper.changeDirection(passedParams.direction, false);
  155. }
  156. if (loopNeedDestroy || loopNeedReloop) {
  157. swiper.loopDestroy();
  158. }
  159. if (loopNeedEnable || loopNeedReloop) {
  160. swiper.loopCreate();
  161. }
  162. swiper.update();
  163. }
  164. export { updateSwiper };