update-swiper.js 3.9 KB

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