update-swiper.mjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import { s as setInnerHTML } from './utils.mjs';
  2. /* underscore in name -> watch for changes */
  3. 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',
  4. // modules
  5. 'a11y', '_autoplay', '_controller', 'coverflowEffect', 'cubeEffect', 'fadeEffect', 'flipEffect', 'creativeEffect', 'cardsEffect', 'hashNavigation', 'history', 'keyboard', 'mousewheel', '_navigation', '_pagination', 'parallax', '_scrollbar', '_thumbs', 'virtual', 'zoom', 'control'];
  6. function isObject(o) {
  7. return typeof o === 'object' && o !== null && o.constructor && Object.prototype.toString.call(o).slice(8, -1) === 'Object' && !o.__swiper__;
  8. }
  9. function extend(target, src) {
  10. const noExtend = ['__proto__', 'constructor', 'prototype'];
  11. Object.keys(src).filter(key => noExtend.indexOf(key) < 0).forEach(key => {
  12. if (typeof target[key] === 'undefined') target[key] = src[key];else if (isObject(src[key]) && isObject(target[key]) && Object.keys(src[key]).length > 0) {
  13. if (src[key].__swiper__) target[key] = src[key];else extend(target[key], src[key]);
  14. } else {
  15. target[key] = src[key];
  16. }
  17. });
  18. }
  19. function needsNavigation(params) {
  20. if (params === void 0) {
  21. params = {};
  22. }
  23. return params.navigation && typeof params.navigation.nextEl === 'undefined' && typeof params.navigation.prevEl === 'undefined';
  24. }
  25. function needsPagination(params) {
  26. if (params === void 0) {
  27. params = {};
  28. }
  29. return params.pagination && typeof params.pagination.el === 'undefined';
  30. }
  31. function needsScrollbar(params) {
  32. if (params === void 0) {
  33. params = {};
  34. }
  35. return params.scrollbar && typeof params.scrollbar.el === 'undefined';
  36. }
  37. function uniqueClasses(classNames) {
  38. if (classNames === void 0) {
  39. classNames = '';
  40. }
  41. const classes = classNames.split(' ').map(c => c.trim()).filter(c => !!c);
  42. const unique = [];
  43. classes.forEach(c => {
  44. if (unique.indexOf(c) < 0) unique.push(c);
  45. });
  46. return unique.join(' ');
  47. }
  48. function attrToProp(attrName) {
  49. if (attrName === void 0) {
  50. attrName = '';
  51. }
  52. return attrName.replace(/-[a-z]/g, l => l.toUpperCase().replace('-', ''));
  53. }
  54. function wrapperClass(className) {
  55. if (className === void 0) {
  56. className = '';
  57. }
  58. if (!className) return 'swiper-wrapper';
  59. if (!className.includes('swiper-wrapper')) return `swiper-wrapper ${className}`;
  60. return className;
  61. }
  62. function updateSwiper(_ref) {
  63. let {
  64. swiper,
  65. slides,
  66. passedParams,
  67. changedParams,
  68. nextEl,
  69. prevEl,
  70. scrollbarEl,
  71. paginationEl
  72. } = _ref;
  73. const updateParams = changedParams.filter(key => key !== 'children' && key !== 'direction' && key !== 'wrapperClass');
  74. const {
  75. params: currentParams,
  76. pagination,
  77. navigation,
  78. scrollbar,
  79. virtual,
  80. thumbs
  81. } = swiper;
  82. let needThumbsInit;
  83. let needControllerInit;
  84. let needPaginationInit;
  85. let needScrollbarInit;
  86. let needNavigationInit;
  87. let loopNeedDestroy;
  88. let loopNeedEnable;
  89. let loopNeedReloop;
  90. if (changedParams.includes('thumbs') && passedParams.thumbs && passedParams.thumbs.swiper && !passedParams.thumbs.swiper.destroyed && currentParams.thumbs && (!currentParams.thumbs.swiper || currentParams.thumbs.swiper.destroyed)) {
  91. needThumbsInit = true;
  92. }
  93. if (changedParams.includes('controller') && passedParams.controller && passedParams.controller.control && currentParams.controller && !currentParams.controller.control) {
  94. needControllerInit = true;
  95. }
  96. if (changedParams.includes('pagination') && passedParams.pagination && (passedParams.pagination.el || paginationEl) && (currentParams.pagination || currentParams.pagination === false) && pagination && !pagination.el) {
  97. needPaginationInit = true;
  98. }
  99. if (changedParams.includes('scrollbar') && passedParams.scrollbar && (passedParams.scrollbar.el || scrollbarEl) && (currentParams.scrollbar || currentParams.scrollbar === false) && scrollbar && !scrollbar.el) {
  100. needScrollbarInit = true;
  101. }
  102. if (changedParams.includes('navigation') && passedParams.navigation && (passedParams.navigation.prevEl || prevEl) && (passedParams.navigation.nextEl || nextEl) && (currentParams.navigation || currentParams.navigation === false) && navigation && !navigation.prevEl && !navigation.nextEl) {
  103. needNavigationInit = true;
  104. }
  105. const destroyModule = mod => {
  106. if (!swiper[mod]) return;
  107. swiper[mod].destroy();
  108. if (mod === 'navigation') {
  109. if (swiper.isElement) {
  110. swiper[mod].prevEl.remove();
  111. swiper[mod].nextEl.remove();
  112. }
  113. currentParams[mod].prevEl = undefined;
  114. currentParams[mod].nextEl = undefined;
  115. swiper[mod].prevEl = undefined;
  116. swiper[mod].nextEl = undefined;
  117. } else {
  118. if (swiper.isElement) {
  119. swiper[mod].el.remove();
  120. }
  121. currentParams[mod].el = undefined;
  122. swiper[mod].el = undefined;
  123. }
  124. };
  125. if (changedParams.includes('loop') && swiper.isElement) {
  126. if (currentParams.loop && !passedParams.loop) {
  127. loopNeedDestroy = true;
  128. } else if (!currentParams.loop && passedParams.loop) {
  129. loopNeedEnable = true;
  130. } else {
  131. loopNeedReloop = true;
  132. }
  133. }
  134. updateParams.forEach(key => {
  135. if (isObject(currentParams[key]) && isObject(passedParams[key])) {
  136. Object.assign(currentParams[key], passedParams[key]);
  137. if ((key === 'navigation' || key === 'pagination' || key === 'scrollbar') && 'enabled' in passedParams[key] && !passedParams[key].enabled) {
  138. destroyModule(key);
  139. }
  140. } else {
  141. const newValue = passedParams[key];
  142. if ((newValue === true || newValue === false) && (key === 'navigation' || key === 'pagination' || key === 'scrollbar')) {
  143. if (newValue === false) {
  144. destroyModule(key);
  145. }
  146. } else {
  147. currentParams[key] = passedParams[key];
  148. }
  149. }
  150. });
  151. if (updateParams.includes('controller') && !needControllerInit && swiper.controller && swiper.controller.control && currentParams.controller && currentParams.controller.control) {
  152. swiper.controller.control = currentParams.controller.control;
  153. }
  154. if (changedParams.includes('children') && slides && virtual && currentParams.virtual.enabled) {
  155. virtual.slides = slides;
  156. virtual.update(true);
  157. } else if (changedParams.includes('virtual') && virtual && currentParams.virtual.enabled) {
  158. if (slides) virtual.slides = slides;
  159. virtual.update(true);
  160. }
  161. if (changedParams.includes('children') && slides && currentParams.loop) {
  162. loopNeedReloop = true;
  163. }
  164. if (needThumbsInit) {
  165. const initialized = thumbs.init();
  166. if (initialized) thumbs.update(true);
  167. }
  168. if (needControllerInit) {
  169. swiper.controller.control = currentParams.controller.control;
  170. }
  171. if (needPaginationInit) {
  172. if (swiper.isElement && (!paginationEl || typeof paginationEl === 'string')) {
  173. paginationEl = document.createElement('div');
  174. paginationEl.classList.add('swiper-pagination');
  175. paginationEl.part.add('pagination');
  176. swiper.el.appendChild(paginationEl);
  177. }
  178. if (paginationEl) currentParams.pagination.el = paginationEl;
  179. pagination.init();
  180. pagination.render();
  181. pagination.update();
  182. }
  183. if (needScrollbarInit) {
  184. if (swiper.isElement && (!scrollbarEl || typeof scrollbarEl === 'string')) {
  185. scrollbarEl = document.createElement('div');
  186. scrollbarEl.classList.add('swiper-scrollbar');
  187. scrollbarEl.part.add('scrollbar');
  188. swiper.el.appendChild(scrollbarEl);
  189. }
  190. if (scrollbarEl) currentParams.scrollbar.el = scrollbarEl;
  191. scrollbar.init();
  192. scrollbar.updateSize();
  193. scrollbar.setTranslate();
  194. }
  195. if (needNavigationInit) {
  196. if (swiper.isElement) {
  197. if (!nextEl || typeof nextEl === 'string') {
  198. nextEl = document.createElement('div');
  199. nextEl.classList.add('swiper-button-next');
  200. setInnerHTML(nextEl, swiper.hostEl.constructor.nextButtonSvg);
  201. nextEl.part.add('button-next');
  202. swiper.el.appendChild(nextEl);
  203. }
  204. if (!prevEl || typeof prevEl === 'string') {
  205. prevEl = document.createElement('div');
  206. prevEl.classList.add('swiper-button-prev');
  207. setInnerHTML(prevEl, swiper.hostEl.constructor.prevButtonSvg);
  208. prevEl.part.add('button-prev');
  209. swiper.el.appendChild(prevEl);
  210. }
  211. }
  212. if (nextEl) currentParams.navigation.nextEl = nextEl;
  213. if (prevEl) currentParams.navigation.prevEl = prevEl;
  214. navigation.init();
  215. navigation.update();
  216. }
  217. if (changedParams.includes('allowSlideNext')) {
  218. swiper.allowSlideNext = passedParams.allowSlideNext;
  219. }
  220. if (changedParams.includes('allowSlidePrev')) {
  221. swiper.allowSlidePrev = passedParams.allowSlidePrev;
  222. }
  223. if (changedParams.includes('direction')) {
  224. swiper.changeDirection(passedParams.direction, false);
  225. }
  226. if (loopNeedDestroy || loopNeedReloop) {
  227. swiper.loopDestroy();
  228. }
  229. if (loopNeedEnable || loopNeedReloop) {
  230. swiper.loopCreate();
  231. }
  232. swiper.update();
  233. }
  234. 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 };