update-swiper.mjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* underscore in name -> watch for changes */
  2. const paramsList = ['eventsPrefix', 'injectStyles', 'injectStylesUrls', 'modules', 'init', '_direction', 'oneWayMovement', 'swiperElementNodeName', 'touchEventsTarget', 'initialSlide', '_speed', 'cssMode', 'updateOnWindowResize', 'resizeObserver', 'nested', 'focusableElements', '_enabled', '_width', '_height', 'preventInteractionOnTransition', 'userAgent', 'url', '_edgeSwipeDetection', '_edgeSwipeThreshold', '_freeMode', '_autoHeight', 'setWrapperSize', 'virtualTranslate', '_effect', 'breakpoints', 'breakpointsBase', '_spaceBetween', '_slidesPerView', 'maxBackfaceHiddenSlides', '_grid', '_slidesPerGroup', '_slidesPerGroupSkip', '_slidesPerGroupAuto', '_centeredSlides', '_centeredSlidesBounds', '_slidesOffsetBefore', '_slidesOffsetAfter', 'normalizeSlideIndex', '_centerInsufficientSlides', '_watchOverflow', 'roundLengths', 'touchRatio', 'touchAngle', 'simulateTouch', '_shortSwipes', '_longSwipes', 'longSwipesRatio', 'longSwipesMs', '_followFinger', 'allowTouchMove', '_threshold', 'touchMoveStopPropagation', 'touchStartPreventDefault', 'touchStartForcePreventDefault', 'touchReleaseOnEdges', 'uniqueNavElements', '_resistance', '_resistanceRatio', '_watchSlidesProgress', '_grabCursor', 'preventClicks', 'preventClicksPropagation', '_slideToClickedSlide', '_loop', 'loopAdditionalSlides', 'loopAddBlankSlides', 'loopPreventsSliding', '_rewind', '_allowSlidePrev', '_allowSlideNext', '_swipeHandler', '_noSwiping', 'noSwipingClass', 'noSwipingSelector', 'passiveListeners', 'containerModifierClass', 'slideClass', 'slideActiveClass', 'slideVisibleClass', 'slideFullyVisibleClass', 'slideNextClass', 'slidePrevClass', 'slideBlankClass', 'wrapperClass', 'lazyPreloaderClass', 'lazyPreloadPrevNext', 'runCallbacksOnInit', 'observer', 'observeParents', 'observeSlideChildren',
  3. // modules
  4. 'a11y', '_autoplay', '_controller', 'coverflowEffect', 'cubeEffect', 'fadeEffect', 'flipEffect', 'creativeEffect', 'cardsEffect', 'hashNavigation', 'history', 'keyboard', 'mousewheel', '_navigation', '_pagination', 'parallax', '_scrollbar', '_thumbs', 'virtual', 'zoom', 'control'];
  5. function isObject(o) {
  6. return typeof o === 'object' && o !== null && o.constructor && Object.prototype.toString.call(o).slice(8, -1) === 'Object' && !o.__swiper__;
  7. }
  8. function extend(target, src) {
  9. const noExtend = ['__proto__', 'constructor', 'prototype'];
  10. Object.keys(src).filter(key => noExtend.indexOf(key) < 0).forEach(key => {
  11. if (typeof target[key] === 'undefined') target[key] = src[key];else if (isObject(src[key]) && isObject(target[key]) && Object.keys(src[key]).length > 0) {
  12. if (src[key].__swiper__) target[key] = src[key];else extend(target[key], src[key]);
  13. } else {
  14. target[key] = src[key];
  15. }
  16. });
  17. }
  18. function needsNavigation(params) {
  19. if (params === void 0) {
  20. params = {};
  21. }
  22. return params.navigation && typeof params.navigation.nextEl === 'undefined' && typeof params.navigation.prevEl === 'undefined';
  23. }
  24. function needsPagination(params) {
  25. if (params === void 0) {
  26. params = {};
  27. }
  28. return params.pagination && typeof params.pagination.el === 'undefined';
  29. }
  30. function needsScrollbar(params) {
  31. if (params === void 0) {
  32. params = {};
  33. }
  34. return params.scrollbar && typeof params.scrollbar.el === 'undefined';
  35. }
  36. function uniqueClasses(classNames) {
  37. if (classNames === void 0) {
  38. classNames = '';
  39. }
  40. const classes = classNames.split(' ').map(c => c.trim()).filter(c => !!c);
  41. const unique = [];
  42. classes.forEach(c => {
  43. if (unique.indexOf(c) < 0) unique.push(c);
  44. });
  45. return unique.join(' ');
  46. }
  47. function attrToProp(attrName) {
  48. if (attrName === void 0) {
  49. attrName = '';
  50. }
  51. return attrName.replace(/-[a-z]/g, l => l.toUpperCase().replace('-', ''));
  52. }
  53. function wrapperClass(className) {
  54. if (className === void 0) {
  55. className = '';
  56. }
  57. if (!className) return 'swiper-wrapper';
  58. if (!className.includes('swiper-wrapper')) return `swiper-wrapper ${className}`;
  59. return className;
  60. }
  61. function updateSwiper(_ref) {
  62. let {
  63. swiper,
  64. slides,
  65. passedParams,
  66. changedParams,
  67. nextEl,
  68. prevEl,
  69. scrollbarEl,
  70. paginationEl
  71. } = _ref;
  72. const updateParams = changedParams.filter(key => key !== 'children' && key !== 'direction' && key !== 'wrapperClass');
  73. const {
  74. params: currentParams,
  75. pagination,
  76. navigation,
  77. scrollbar,
  78. virtual,
  79. thumbs
  80. } = swiper;
  81. let needThumbsInit;
  82. let needControllerInit;
  83. let needPaginationInit;
  84. let needScrollbarInit;
  85. let needNavigationInit;
  86. let loopNeedDestroy;
  87. let loopNeedEnable;
  88. let loopNeedReloop;
  89. if (changedParams.includes('thumbs') && passedParams.thumbs && passedParams.thumbs.swiper && !passedParams.thumbs.swiper.destroyed && currentParams.thumbs && (!currentParams.thumbs.swiper || currentParams.thumbs.swiper.destroyed)) {
  90. needThumbsInit = true;
  91. }
  92. if (changedParams.includes('controller') && passedParams.controller && passedParams.controller.control && currentParams.controller && !currentParams.controller.control) {
  93. needControllerInit = true;
  94. }
  95. if (changedParams.includes('pagination') && passedParams.pagination && (passedParams.pagination.el || paginationEl) && (currentParams.pagination || currentParams.pagination === false) && pagination && !pagination.el) {
  96. needPaginationInit = true;
  97. }
  98. if (changedParams.includes('scrollbar') && passedParams.scrollbar && (passedParams.scrollbar.el || scrollbarEl) && (currentParams.scrollbar || currentParams.scrollbar === false) && scrollbar && !scrollbar.el) {
  99. needScrollbarInit = true;
  100. }
  101. if (changedParams.includes('navigation') && passedParams.navigation && (passedParams.navigation.prevEl || prevEl) && (passedParams.navigation.nextEl || nextEl) && (currentParams.navigation || currentParams.navigation === false) && navigation && !navigation.prevEl && !navigation.nextEl) {
  102. needNavigationInit = true;
  103. }
  104. const destroyModule = mod => {
  105. if (!swiper[mod]) return;
  106. swiper[mod].destroy();
  107. if (mod === 'navigation') {
  108. if (swiper.isElement) {
  109. swiper[mod].prevEl.remove();
  110. swiper[mod].nextEl.remove();
  111. }
  112. currentParams[mod].prevEl = undefined;
  113. currentParams[mod].nextEl = undefined;
  114. swiper[mod].prevEl = undefined;
  115. swiper[mod].nextEl = undefined;
  116. } else {
  117. if (swiper.isElement) {
  118. swiper[mod].el.remove();
  119. }
  120. currentParams[mod].el = undefined;
  121. swiper[mod].el = undefined;
  122. }
  123. };
  124. if (changedParams.includes('loop') && swiper.isElement) {
  125. if (currentParams.loop && !passedParams.loop) {
  126. loopNeedDestroy = true;
  127. } else if (!currentParams.loop && passedParams.loop) {
  128. loopNeedEnable = true;
  129. } else {
  130. loopNeedReloop = true;
  131. }
  132. }
  133. updateParams.forEach(key => {
  134. if (isObject(currentParams[key]) && isObject(passedParams[key])) {
  135. Object.assign(currentParams[key], passedParams[key]);
  136. if ((key === 'navigation' || key === 'pagination' || key === 'scrollbar') && 'enabled' in passedParams[key] && !passedParams[key].enabled) {
  137. destroyModule(key);
  138. }
  139. } else {
  140. const newValue = passedParams[key];
  141. if ((newValue === true || newValue === false) && (key === 'navigation' || key === 'pagination' || key === 'scrollbar')) {
  142. if (newValue === false) {
  143. destroyModule(key);
  144. }
  145. } else {
  146. currentParams[key] = passedParams[key];
  147. }
  148. }
  149. });
  150. if (updateParams.includes('controller') && !needControllerInit && swiper.controller && swiper.controller.control && currentParams.controller && currentParams.controller.control) {
  151. swiper.controller.control = currentParams.controller.control;
  152. }
  153. if (changedParams.includes('children') && slides && virtual && currentParams.virtual.enabled) {
  154. virtual.slides = slides;
  155. virtual.update(true);
  156. } else if (changedParams.includes('virtual') && virtual && currentParams.virtual.enabled) {
  157. if (slides) virtual.slides = slides;
  158. virtual.update(true);
  159. }
  160. if (changedParams.includes('children') && slides && currentParams.loop) {
  161. loopNeedReloop = true;
  162. }
  163. if (needThumbsInit) {
  164. const initialized = thumbs.init();
  165. if (initialized) thumbs.update(true);
  166. }
  167. if (needControllerInit) {
  168. swiper.controller.control = currentParams.controller.control;
  169. }
  170. if (needPaginationInit) {
  171. if (swiper.isElement && (!paginationEl || typeof paginationEl === 'string')) {
  172. paginationEl = document.createElement('div');
  173. paginationEl.classList.add('swiper-pagination');
  174. paginationEl.part.add('pagination');
  175. swiper.el.appendChild(paginationEl);
  176. }
  177. if (paginationEl) currentParams.pagination.el = paginationEl;
  178. pagination.init();
  179. pagination.render();
  180. pagination.update();
  181. }
  182. if (needScrollbarInit) {
  183. if (swiper.isElement && (!scrollbarEl || typeof scrollbarEl === 'string')) {
  184. scrollbarEl = document.createElement('div');
  185. scrollbarEl.classList.add('swiper-scrollbar');
  186. scrollbarEl.part.add('scrollbar');
  187. swiper.el.appendChild(scrollbarEl);
  188. }
  189. if (scrollbarEl) currentParams.scrollbar.el = scrollbarEl;
  190. scrollbar.init();
  191. scrollbar.updateSize();
  192. scrollbar.setTranslate();
  193. }
  194. if (needNavigationInit) {
  195. if (swiper.isElement) {
  196. if (!nextEl || typeof nextEl === 'string') {
  197. nextEl = document.createElement('div');
  198. nextEl.classList.add('swiper-button-next');
  199. nextEl.innerHTML = swiper.hostEl.constructor.nextButtonSvg;
  200. nextEl.part.add('button-next');
  201. swiper.el.appendChild(nextEl);
  202. }
  203. if (!prevEl || typeof prevEl === 'string') {
  204. prevEl = document.createElement('div');
  205. prevEl.classList.add('swiper-button-prev');
  206. prevEl.innerHTML = swiper.hostEl.constructor.prevButtonSvg;
  207. prevEl.part.add('button-prev');
  208. swiper.el.appendChild(prevEl);
  209. }
  210. }
  211. if (nextEl) currentParams.navigation.nextEl = nextEl;
  212. if (prevEl) currentParams.navigation.prevEl = prevEl;
  213. navigation.init();
  214. navigation.update();
  215. }
  216. if (changedParams.includes('allowSlideNext')) {
  217. swiper.allowSlideNext = passedParams.allowSlideNext;
  218. }
  219. if (changedParams.includes('allowSlidePrev')) {
  220. swiper.allowSlidePrev = passedParams.allowSlidePrev;
  221. }
  222. if (changedParams.includes('direction')) {
  223. swiper.changeDirection(passedParams.direction, false);
  224. }
  225. if (loopNeedDestroy || loopNeedReloop) {
  226. swiper.loopDestroy();
  227. }
  228. if (loopNeedEnable || loopNeedReloop) {
  229. swiper.loopCreate();
  230. }
  231. swiper.update();
  232. }
  233. export { needsPagination as a, needsScrollbar as b, attrToProp as c, uniqueClasses as d, extend as e, isObject as i, needsNavigation as n, paramsList as p, updateSwiper as u, wrapperClass as w };