update-swiper.js 3.9 KB

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