update-swiper.js 4.0 KB

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