update-swiper.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { isObject, extend } from './utils.js';
  2. function updateSwiper(_ref) {
  3. let {
  4. swiper,
  5. slides,
  6. passedParams,
  7. changedParams,
  8. nextEl,
  9. prevEl,
  10. scrollbarEl,
  11. paginationEl
  12. } = _ref;
  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. if (changedParams.includes('thumbs') && passedParams.thumbs && passedParams.thumbs.swiper && currentParams.thumbs && !currentParams.thumbs.swiper) {
  28. needThumbsInit = true;
  29. }
  30. if (changedParams.includes('controller') && passedParams.controller && passedParams.controller.control && currentParams.controller && !currentParams.controller.control) {
  31. needControllerInit = true;
  32. }
  33. if (changedParams.includes('pagination') && passedParams.pagination && (passedParams.pagination.el || paginationEl) && (currentParams.pagination || currentParams.pagination === false) && pagination && !pagination.el) {
  34. needPaginationInit = true;
  35. }
  36. if (changedParams.includes('scrollbar') && passedParams.scrollbar && (passedParams.scrollbar.el || scrollbarEl) && (currentParams.scrollbar || currentParams.scrollbar === false) && scrollbar && !scrollbar.el) {
  37. needScrollbarInit = true;
  38. }
  39. if (changedParams.includes('navigation') && passedParams.navigation && (passedParams.navigation.prevEl || prevEl) && (passedParams.navigation.nextEl || nextEl) && (currentParams.navigation || currentParams.navigation === false) && navigation && !navigation.prevEl && !navigation.nextEl) {
  40. needNavigationInit = true;
  41. }
  42. const destroyModule = mod => {
  43. if (!swiper[mod]) return;
  44. swiper[mod].destroy();
  45. if (mod === 'navigation') {
  46. currentParams[mod].prevEl = undefined;
  47. currentParams[mod].nextEl = undefined;
  48. swiper[mod].prevEl = undefined;
  49. swiper[mod].nextEl = undefined;
  50. } else {
  51. currentParams[mod].el = undefined;
  52. swiper[mod].el = undefined;
  53. }
  54. };
  55. updateParams.forEach(key => {
  56. if (isObject(currentParams[key]) && isObject(passedParams[key])) {
  57. extend(currentParams[key], passedParams[key]);
  58. } else {
  59. const newValue = passedParams[key];
  60. if ((newValue === true || newValue === false) && (key === 'navigation' || key === 'pagination' || key === 'scrollbar')) {
  61. if (newValue === false) {
  62. destroyModule(key);
  63. }
  64. } else {
  65. currentParams[key] = passedParams[key];
  66. }
  67. }
  68. });
  69. if (updateParams.includes('controller') && !needControllerInit && swiper.controller && swiper.controller.control && currentParams.controller && currentParams.controller.control) {
  70. swiper.controller.control = currentParams.controller.control;
  71. }
  72. if (changedParams.includes('children') && virtual && currentParams.virtual.enabled) {
  73. virtual.slides = slides;
  74. virtual.update(true);
  75. } else if (changedParams.includes('children') && swiper.lazy && swiper.params.lazy.enabled) {
  76. swiper.lazy.load();
  77. }
  78. if (needThumbsInit) {
  79. const initialized = thumbs.init();
  80. if (initialized) thumbs.update(true);
  81. }
  82. if (needControllerInit) {
  83. swiper.controller.control = currentParams.controller.control;
  84. }
  85. if (needPaginationInit) {
  86. if (paginationEl) currentParams.pagination.el = paginationEl;
  87. pagination.init();
  88. pagination.render();
  89. pagination.update();
  90. }
  91. if (needScrollbarInit) {
  92. if (scrollbarEl) currentParams.scrollbar.el = scrollbarEl;
  93. scrollbar.init();
  94. scrollbar.updateSize();
  95. scrollbar.setTranslate();
  96. }
  97. if (needNavigationInit) {
  98. if (nextEl) currentParams.navigation.nextEl = nextEl;
  99. if (prevEl) currentParams.navigation.prevEl = prevEl;
  100. navigation.init();
  101. navigation.update();
  102. }
  103. if (changedParams.includes('allowSlideNext')) {
  104. swiper.allowSlideNext = passedParams.allowSlideNext;
  105. }
  106. if (changedParams.includes('allowSlidePrev')) {
  107. swiper.allowSlidePrev = passedParams.allowSlidePrev;
  108. }
  109. if (changedParams.includes('direction')) {
  110. swiper.changeDirection(passedParams.direction, false);
  111. }
  112. swiper.update();
  113. }
  114. export { updateSwiper };