123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /**
  2. * Adopted from Swiper
  3. * Most modern mobile touch slider and framework with hardware
  4. * accelerated transitions.
  5. *
  6. * http://www.idangero.us/swiper/
  7. *
  8. * Copyright 2016, Vladimir Kharlampidi
  9. * The iDangero.us
  10. * http://www.idangero.us/
  11. *
  12. * Licensed under MIT
  13. */
  14. import { CLS, addClass, eachChild, inlineStyle, isHorizontal, maxTranslate, minTranslate, removeClass, round, updateSlidesOffset } from './swiper-utils';
  15. import { setWrapperTransition, setWrapperTranslate } from './swiper-transition';
  16. import { updateProgress } from './swiper-progress';
  17. import { updateClasses } from './swiper-classes';
  18. import { parallaxSetTranslate } from './swiper-parallax';
  19. import { updateActiveIndex, updateRealIndex } from './swiper-index';
  20. import { SWIPER_EFFECTS } from './swiper-effects';
  21. import { updatePagination } from './swiper-pagination';
  22. import { resetZoomEvents } from './swiper-zoom';
  23. export function initSwiper(s, plt) {
  24. // Classname
  25. s._classNames = [];
  26. /*=========================
  27. Preparation - Define Container, Wrapper and Pagination
  28. ===========================*/
  29. if (!s.container) {
  30. return;
  31. }
  32. // Save instance in container HTML Element and in data
  33. s.container.swiper = s;
  34. const containerModifierClass = CLS.containerModifier;
  35. s._classNames.push(containerModifierClass + s.direction);
  36. if (s.freeMode) {
  37. s._classNames.push(containerModifierClass + 'free-mode');
  38. }
  39. if (s.autoHeight) {
  40. s._classNames.push(containerModifierClass + 'autoheight');
  41. }
  42. // Enable slides progress when required
  43. if (s.parallax || s.watchSlidesVisibility) {
  44. s.watchSlidesProgress = true;
  45. }
  46. // Max resistance when touchReleaseOnEdges
  47. if (s.touchReleaseOnEdges) {
  48. s.resistanceRatio = 0;
  49. }
  50. var effect = s.effect;
  51. // Coverflow / 3D
  52. if (['cube', 'coverflow', 'flip'].indexOf(effect) >= 0) {
  53. s.watchSlidesProgress = true;
  54. s._classNames.push(containerModifierClass + '3d');
  55. }
  56. if (effect !== 'slide') {
  57. s._classNames.push(containerModifierClass + effect);
  58. }
  59. if (effect === 'cube') {
  60. s.resistanceRatio = 0;
  61. s.slidesPerView = 1;
  62. s.slidesPerColumn = 1;
  63. s.slidesPerGroup = 1;
  64. s.centeredSlides = false;
  65. s.spaceBetween = 0;
  66. s.virtualTranslate = true;
  67. s.setWrapperSize = false;
  68. }
  69. if (effect === 'fade' || effect === 'flip') {
  70. s.slidesPerView = 1;
  71. s.slidesPerColumn = 1;
  72. s.slidesPerGroup = 1;
  73. s.watchSlidesProgress = true;
  74. s.spaceBetween = 0;
  75. s.setWrapperSize = false;
  76. s.virtualTranslate = true;
  77. }
  78. // Wrapper
  79. s._wrapper = s.container.querySelector('.' + CLS.wrapper);
  80. // Pagination
  81. if (s.paginationType) {
  82. s._paginationContainer = s.container.querySelector('.swiper-pagination');
  83. if (s.paginationType === 'bullets') {
  84. s._paginationContainer.classList.add(CLS.paginationModifier + 'clickable');
  85. }
  86. s._paginationContainer.classList.add(CLS.paginationModifier + s.paginationType);
  87. }
  88. // Next/Prev Buttons
  89. // if (s.nextButton || s.prevButton) {
  90. // if (s.nextButton) {
  91. // s.nextButton = <any>s.container.closest('ion-content').querySelector(s.nextButton);
  92. // }
  93. // if (s.prevButton) {
  94. // s.prevButton = <any>s.container.closest('ion-content').querySelector(s.prevButton);
  95. // }
  96. // }
  97. // RTL
  98. s._rtl = isHorizontal(s) && (s.container.dir.toLowerCase() === 'rtl' || s.container.style.direction === 'rtl');
  99. if (s._rtl) {
  100. s._classNames.push(containerModifierClass + 'rtl');
  101. }
  102. // Columns
  103. if (s.slidesPerColumn > 1) {
  104. s._classNames.push(containerModifierClass + 'multirow');
  105. }
  106. // Check for Android
  107. if (plt.is('android')) {
  108. s._classNames.push(containerModifierClass + 'android');
  109. }
  110. // Add classes
  111. s._classNames.forEach(clsName => {
  112. s.container.classList.add(clsName);
  113. });
  114. // Translate
  115. s._translate = 0;
  116. // Progress
  117. s.progress = 0;
  118. // Velocity
  119. s.velocity = 0;
  120. /*=========================
  121. Autoplay
  122. ===========================*/
  123. s._autoplayTimeoutId = undefined;
  124. s._autoplaying = false;
  125. s._autoplayPaused = false;
  126. s._allowClick = true;
  127. // Animating Flag
  128. s._animating = false;
  129. // Touches information
  130. s._touches = {
  131. startX: 0,
  132. startY: 0,
  133. currentX: 0,
  134. currentY: 0,
  135. diff: 0
  136. };
  137. if (s.loop) {
  138. createLoop(s);
  139. }
  140. updateContainerSize(s, plt);
  141. updateSlidesSize(s, plt);
  142. updatePagination(s);
  143. if (effect !== 'slide' && SWIPER_EFFECTS[effect]) {
  144. if (!s.loop) {
  145. updateProgress(s);
  146. }
  147. SWIPER_EFFECTS[effect].setTranslate(s, plt);
  148. }
  149. if (s.loop) {
  150. slideTo(s, plt, s.initialSlide + s.loopedSlides, 0, s.runCallbacksOnInit);
  151. }
  152. else {
  153. slideTo(s, plt, s.initialSlide, 0, s.runCallbacksOnInit);
  154. if (s.initialSlide === 0) {
  155. parallaxSetTranslate(s);
  156. }
  157. }
  158. if (s.autoplay) {
  159. startAutoplay(s, plt);
  160. }
  161. }
  162. /*=========================
  163. Autoplay
  164. ===========================*/
  165. function autoplay(s, plt) {
  166. var autoplayDelay = s.autoplay;
  167. var activeSlide = s._slides[s._activeIndex];
  168. if (activeSlide.hasAttribute('data-swiper-autoplay')) {
  169. autoplayDelay = (activeSlide.getAttribute('data-swiper-autoplay') || s.autoplay);
  170. }
  171. s._autoplayTimeoutId = plt.timeout(() => {
  172. s._zone.run(() => {
  173. if (s.loop) {
  174. fixLoop(s, plt);
  175. slideNext(s, plt, true, undefined, true);
  176. s.ionSlideAutoplay.emit(s);
  177. }
  178. else {
  179. if (!s._isEnd) {
  180. slideNext(s, plt, true, undefined, true);
  181. s.ionSlideAutoplay.emit(s);
  182. }
  183. else {
  184. if (!s.autoplayStopOnLast) {
  185. slideTo(s, plt, 0);
  186. s.ionSlideAutoplay.emit(s);
  187. }
  188. else {
  189. stopAutoplay(s);
  190. }
  191. }
  192. }
  193. });
  194. }, autoplayDelay);
  195. }
  196. export function startAutoplay(s, plt) {
  197. if (typeof s._autoplayTimeoutId !== 'undefined')
  198. return false;
  199. if (!s.autoplay || s._autoplaying) {
  200. return false;
  201. }
  202. s._autoplaying = true;
  203. s._zone.run(() => {
  204. s.ionSlideAutoplayStart.emit(s);
  205. });
  206. autoplay(s, plt);
  207. }
  208. export function stopAutoplay(s) {
  209. if (!s._autoplayTimeoutId)
  210. return;
  211. if (s._autoplayTimeoutId)
  212. clearTimeout(s._autoplayTimeoutId);
  213. s._autoplaying = false;
  214. s._autoplayTimeoutId = undefined;
  215. s._zone.run(() => {
  216. s.ionSlideAutoplayStop.emit(s);
  217. });
  218. }
  219. export function pauseAutoplay(s, plt, speed) {
  220. if (s._autoplayPaused)
  221. return;
  222. if (s._autoplayTimeoutId)
  223. clearTimeout(s._autoplayTimeoutId);
  224. s._autoplayPaused = true;
  225. if (speed === 0) {
  226. s._autoplayPaused = false;
  227. autoplay(s, plt);
  228. }
  229. else {
  230. plt.transitionEnd(s._wrapper, () => {
  231. if (!s)
  232. return;
  233. s._autoplayPaused = false;
  234. if (!s._autoplaying) {
  235. stopAutoplay(s);
  236. }
  237. else {
  238. autoplay(s, plt);
  239. }
  240. });
  241. }
  242. }
  243. /*=========================
  244. Slider/slides sizes
  245. ===========================*/
  246. export function updateAutoHeight(s) {
  247. var activeSlides = [];
  248. var newHeight = 0;
  249. var i;
  250. // Find slides currently in view
  251. if (s.slidesPerView !== 'auto' && s.slidesPerView > 1) {
  252. for (i = 0; i < Math.ceil(s.slidesPerView); i++) {
  253. var index = s._activeIndex + i;
  254. if (index > s._slides.length)
  255. break;
  256. activeSlides.push(s._slides[index]);
  257. }
  258. }
  259. else {
  260. activeSlides.push(s._slides[s._activeIndex]);
  261. }
  262. // Find new height from heighest slide in view
  263. for (i = 0; i < activeSlides.length; i++) {
  264. if (typeof activeSlides[i] !== 'undefined') {
  265. var height = activeSlides[i].offsetHeight;
  266. newHeight = height > newHeight ? height : newHeight;
  267. }
  268. }
  269. // Update Height
  270. if (newHeight) {
  271. s._wrapper.style.height = newHeight + 'px';
  272. }
  273. }
  274. export function updateContainerSize(s, plt) {
  275. const container = s.container;
  276. let width;
  277. let height;
  278. if (typeof s.width !== 'undefined') {
  279. // manually assign user width
  280. width = s.width;
  281. }
  282. else {
  283. width = container.clientWidth;
  284. }
  285. if (typeof s.renderedHeight !== 'undefined') {
  286. // manually assign user height
  287. height = s.renderedHeight;
  288. }
  289. else {
  290. height = container.clientHeight;
  291. }
  292. if (width === 0 && isHorizontal(s) || height === 0 && !isHorizontal(s)) {
  293. return;
  294. }
  295. // Subtract paddings
  296. const containerStyles = plt.getElementComputedStyle(container);
  297. width = width - parseInt(containerStyles.paddingLeft, 10) - parseInt(containerStyles.paddingRight, 10);
  298. height = height - parseInt(containerStyles.paddingTop, 10) - parseInt(containerStyles.paddingBottom, 10);
  299. // Store values
  300. s.renderedWidth = width;
  301. s.renderedHeight = height;
  302. s._renderedSize = isHorizontal(s) ? width : height;
  303. }
  304. export function updateSlidesSize(s, plt) {
  305. s._slides = s._wrapper.querySelectorAll('.' + CLS.slide);
  306. s._snapGrid = [];
  307. s._slidesGrid = [];
  308. s._slidesSizesGrid = [];
  309. var spaceBetween = s.spaceBetween;
  310. var slidePosition = -s.slidesOffsetBefore;
  311. var i;
  312. var prevSlideSize = 0;
  313. var index = 0;
  314. if (typeof s._renderedSize === 'undefined')
  315. return;
  316. if (typeof spaceBetween === 'string' && spaceBetween.indexOf('%') >= 0) {
  317. spaceBetween = parseFloat(spaceBetween.replace('%', '')) / 100 * s._renderedSize;
  318. }
  319. s._virtualSize = -spaceBetween;
  320. // reset margins
  321. if (s._rtl) {
  322. inlineStyle(s._slides, { marginLeft: '', marginTop: '' });
  323. }
  324. else {
  325. inlineStyle(s._slides, { marginRight: '', marginBottom: '' });
  326. }
  327. var slidesNumberEvenToRows;
  328. if (s.slidesPerColumn > 1) {
  329. if (Math.floor(s._slides.length / s.slidesPerColumn) === s._slides.length / s.slidesPerColumn) {
  330. slidesNumberEvenToRows = s._slides.length;
  331. }
  332. else {
  333. slidesNumberEvenToRows = Math.ceil(s._slides.length / s.slidesPerColumn) * s.slidesPerColumn;
  334. }
  335. if (s.slidesPerView !== 'auto' && s.slidesPerColumnFill === 'row') {
  336. slidesNumberEvenToRows = Math.max(slidesNumberEvenToRows, s.slidesPerView * s.slidesPerColumn);
  337. }
  338. }
  339. // Calc slides
  340. var slideSize;
  341. var slidesPerColumn = s.slidesPerColumn;
  342. var slidesPerRow = slidesNumberEvenToRows / slidesPerColumn;
  343. var numFullColumns = slidesPerRow - (s.slidesPerColumn * slidesPerRow - s._slides.length);
  344. for (i = 0; i < s._slides.length; i++) {
  345. slideSize = 0;
  346. var slide = s._slides[i];
  347. if (s.slidesPerColumn > 1) {
  348. // Set slides order
  349. var newSlideOrderIndex;
  350. var column;
  351. var row;
  352. if (s.slidesPerColumnFill === 'column') {
  353. column = Math.floor(i / slidesPerColumn);
  354. row = i - column * slidesPerColumn;
  355. if (column > numFullColumns || (column === numFullColumns && row === slidesPerColumn - 1)) {
  356. if (++row >= slidesPerColumn) {
  357. row = 0;
  358. column++;
  359. }
  360. }
  361. newSlideOrderIndex = column + row * slidesNumberEvenToRows / slidesPerColumn;
  362. inlineStyle(slide, {
  363. '-webkit-box-ordinal-group': newSlideOrderIndex,
  364. '-moz-box-ordinal-group': newSlideOrderIndex,
  365. '-ms-flex-order': newSlideOrderIndex,
  366. '-webkit-order': newSlideOrderIndex,
  367. 'order': newSlideOrderIndex
  368. });
  369. }
  370. else {
  371. row = Math.floor(i / slidesPerRow);
  372. column = i - row * slidesPerRow;
  373. }
  374. var cssVal = (row !== 0 && s.spaceBetween) && (s.spaceBetween + 'px');
  375. var cssObj = {};
  376. if (isHorizontal(s)) {
  377. cssObj['marginTop'] = cssVal;
  378. }
  379. else {
  380. cssObj['marginLeft'] = cssVal;
  381. }
  382. inlineStyle(slide, cssObj);
  383. slide.setAttribute('data-swiper-column', column);
  384. slide.setAttribute('data-swiper-row', row);
  385. }
  386. if (slide.style.display === 'none') {
  387. continue;
  388. }
  389. if (s.slidesPerView === 'auto') {
  390. var styles = plt.getElementComputedStyle(slide);
  391. if (isHorizontal(s)) {
  392. slideSize = slide.offsetWidth + parseFloat(styles.marginRight) + parseFloat(styles.marginLeft);
  393. }
  394. else {
  395. slideSize = slide.offsetHeight + parseFloat(styles.marginTop) + parseFloat(styles.marginBottom);
  396. }
  397. if (s.roundLengths)
  398. slideSize = round(slideSize);
  399. }
  400. else {
  401. slideSize = (s._renderedSize - (s.slidesPerView - 1) * spaceBetween) / s.slidesPerView;
  402. if (s.roundLengths)
  403. slideSize = round(slideSize);
  404. if (isHorizontal(s)) {
  405. s._slides[i].style.width = slideSize + 'px';
  406. }
  407. else {
  408. s._slides[i].style.height = slideSize + 'px';
  409. }
  410. }
  411. s._slides[i].swiperSlideSize = slideSize;
  412. s._slidesSizesGrid.push(slideSize);
  413. if (s.centeredSlides) {
  414. slidePosition = slidePosition + slideSize / 2 + prevSlideSize / 2 + spaceBetween;
  415. if (i === 0)
  416. slidePosition = slidePosition - s._renderedSize / 2 - spaceBetween;
  417. if (Math.abs(slidePosition) < 1 / 1000)
  418. slidePosition = 0;
  419. if ((index) % s.slidesPerGroup === 0)
  420. s._snapGrid.push(slidePosition);
  421. s._slidesGrid.push(slidePosition);
  422. }
  423. else {
  424. if ((index) % s.slidesPerGroup === 0)
  425. s._snapGrid.push(slidePosition);
  426. s._slidesGrid.push(slidePosition);
  427. slidePosition = slidePosition + slideSize + spaceBetween;
  428. }
  429. s._virtualSize += slideSize + spaceBetween;
  430. prevSlideSize = slideSize;
  431. index++;
  432. }
  433. s._virtualSize = Math.max(s._virtualSize, s._renderedSize) + s.slidesOffsetAfter;
  434. var newSlidesGrid;
  435. if (s._rtl && (s.effect === 'slide' || s.effect === 'coverflow')) {
  436. inlineStyle(s._wrapper, { width: s._virtualSize + s.spaceBetween + 'px' });
  437. }
  438. if (s.setWrapperSize) {
  439. if (isHorizontal(s)) {
  440. inlineStyle(s._wrapper, { width: s._virtualSize + s.spaceBetween + 'px' });
  441. }
  442. else {
  443. inlineStyle(s._wrapper, { height: s._virtualSize + s.spaceBetween + 'px' });
  444. }
  445. }
  446. if (s.slidesPerColumn > 1) {
  447. s._virtualSize = (slideSize + s.spaceBetween) * slidesNumberEvenToRows;
  448. s._virtualSize = Math.ceil(s._virtualSize / s.slidesPerColumn) - s.spaceBetween;
  449. if (isHorizontal(s)) {
  450. inlineStyle(s._wrapper, { width: s._virtualSize + s.spaceBetween + 'px' });
  451. }
  452. else {
  453. inlineStyle(s._wrapper, { height: s._virtualSize + s.spaceBetween + 'px' });
  454. }
  455. if (s.centeredSlides) {
  456. newSlidesGrid = [];
  457. for (i = 0; i < s._snapGrid.length; i++) {
  458. if (s._snapGrid[i] < s._virtualSize + s._snapGrid[0])
  459. newSlidesGrid.push(s._snapGrid[i]);
  460. }
  461. s._snapGrid = newSlidesGrid;
  462. }
  463. }
  464. // Remove last grid elements depending on width
  465. if (!s.centeredSlides) {
  466. newSlidesGrid = [];
  467. for (i = 0; i < s._snapGrid.length; i++) {
  468. if (s._snapGrid[i] <= s._virtualSize - s._renderedSize) {
  469. newSlidesGrid.push(s._snapGrid[i]);
  470. }
  471. }
  472. s._snapGrid = newSlidesGrid;
  473. if (Math.floor(s._virtualSize - s._renderedSize) - Math.floor(s._snapGrid[s._snapGrid.length - 1]) > 1) {
  474. s._snapGrid.push(s._virtualSize - s._renderedSize);
  475. }
  476. }
  477. if (s._snapGrid.length === 0)
  478. s._snapGrid = [0];
  479. if (s.spaceBetween !== 0) {
  480. if (isHorizontal(s)) {
  481. if (s._rtl) {
  482. inlineStyle(s._slides, { marginLeft: spaceBetween + 'px' });
  483. }
  484. else {
  485. inlineStyle(s._slides, { marginRight: spaceBetween + 'px' });
  486. }
  487. }
  488. else {
  489. inlineStyle(s._slides, { marginBottom: spaceBetween + 'px' });
  490. }
  491. }
  492. if (s.watchSlidesProgress) {
  493. updateSlidesOffset(s);
  494. }
  495. }
  496. /*=========================
  497. Dynamic Slides Per View
  498. ===========================*/
  499. export function currentSlidesPerView(s) {
  500. var spv = 1;
  501. var i;
  502. var j;
  503. if (s.centeredSlides) {
  504. var size = s._slides[s._activeIndex].swiperSlideSize;
  505. var breakLoop;
  506. for (i = s._activeIndex + 1; i < s._slides.length; i++) {
  507. if (s._slides[i] && !breakLoop) {
  508. size += s._slides[i].swiperSlideSize;
  509. spv++;
  510. if (size > s._renderedSize)
  511. breakLoop = true;
  512. }
  513. }
  514. for (j = s._activeIndex - 1; j >= 0; j--) {
  515. if (s._slides[j] && !breakLoop) {
  516. size += s._slides[j].swiperSlideSize;
  517. spv++;
  518. if (size > s._renderedSize)
  519. breakLoop = true;
  520. }
  521. }
  522. }
  523. else {
  524. for (i = s._activeIndex + 1; i < s._slides.length; i++) {
  525. if (s._slidesGrid[i] - s._slidesGrid[s._activeIndex] < s._renderedSize) {
  526. spv++;
  527. }
  528. }
  529. }
  530. return spv;
  531. }
  532. /*=========================
  533. Common update method
  534. ===========================*/
  535. export function update(s, plt, updateTranslate) {
  536. if (!s)
  537. return;
  538. updateContainerSize(s, plt);
  539. updateSlidesSize(s, plt);
  540. updateProgress(s);
  541. updatePagination(s);
  542. updateClasses(s);
  543. if (s.zoom) {
  544. resetZoomEvents(s, plt);
  545. }
  546. var translated;
  547. var newTranslate;
  548. function forceSetTranslate() {
  549. newTranslate = Math.min(Math.max(s._translate, maxTranslate(s)), minTranslate(s));
  550. setWrapperTranslate(s, plt, newTranslate);
  551. updateActiveIndex(s);
  552. updateClasses(s);
  553. }
  554. if (updateTranslate) {
  555. if (s._spline) {
  556. s._spline = undefined;
  557. }
  558. if (s.freeMode) {
  559. forceSetTranslate();
  560. if (s.autoHeight) {
  561. updateAutoHeight(s);
  562. }
  563. }
  564. else {
  565. if ((s.slidesPerView === 'auto' || s.slidesPerView > 1) && s._isEnd && !s.centeredSlides) {
  566. translated = slideTo(s, plt, s._slides.length - 1, 0, false, true);
  567. }
  568. else {
  569. translated = slideTo(s, plt, s._activeIndex, 0, false, true);
  570. }
  571. if (!translated) {
  572. forceSetTranslate();
  573. }
  574. }
  575. }
  576. else if (s.autoHeight) {
  577. updateAutoHeight(s);
  578. }
  579. }
  580. /*=========================
  581. Loop
  582. ===========================*/
  583. // Create looped slides
  584. function createLoop(s) {
  585. // Remove duplicated slides
  586. eachChild(s._wrapper, '.' + CLS.slide + '.' + CLS.slideDuplicate, ele => {
  587. ele.parentElement.removeChild(ele);
  588. });
  589. var slides = s._wrapper.querySelectorAll('.' + CLS.slide);
  590. if (s.slidesPerView === 'auto' && !s.loopedSlides) {
  591. s.loopedSlides = slides.length;
  592. }
  593. s.loopedSlides = parseInt((s.loopedSlides || s.slidesPerView), 10);
  594. s.loopedSlides = s.loopedSlides + s.loopAdditionalSlides;
  595. if (s.loopedSlides > slides.length) {
  596. s.loopedSlides = slides.length;
  597. }
  598. var prependSlides = [];
  599. var appendSlides = [];
  600. for (var i = 0; i < slides.length; i++) {
  601. var slide = slides[i];
  602. if (i < s.loopedSlides)
  603. appendSlides.push(slide);
  604. if (i < slides.length && i >= slides.length - s.loopedSlides)
  605. prependSlides.push(slide);
  606. slide.setAttribute('data-swiper-slide-index', i);
  607. }
  608. for (i = 0; i < appendSlides.length; i++) {
  609. var appendClone = appendSlides[i].cloneNode(true);
  610. addClass(appendClone, CLS.slideDuplicate);
  611. s._wrapper.appendChild(appendClone);
  612. }
  613. for (i = prependSlides.length - 1; i >= 0; i--) {
  614. var prependClone = prependSlides[i].cloneNode(true);
  615. addClass(prependClone, CLS.slideDuplicate);
  616. s._wrapper.insertBefore(prependClone, s._wrapper.firstElementChild);
  617. }
  618. }
  619. function destroyLoop(s) {
  620. eachChild(s._wrapper, '.' + CLS.slide + '.' + CLS.slideDuplicate, ele => {
  621. ele.parentElement.removeChild(ele);
  622. });
  623. if (s._slides) {
  624. for (var i = 0; i < s._slides.length; i++) {
  625. s._slides[i].removeAttribute('data-swiper-slide-index');
  626. }
  627. }
  628. }
  629. export function fixLoop(s, plt) {
  630. var newIndex;
  631. if (s._activeIndex < s.loopedSlides) {
  632. // Fix For Negative Oversliding
  633. newIndex = s._slides.length - s.loopedSlides * 3 + s._activeIndex;
  634. newIndex = newIndex + s.loopedSlides;
  635. slideTo(s, plt, newIndex, 0, false, true);
  636. }
  637. else if ((s.slidesPerView === 'auto' && s._activeIndex >= s.loopedSlides * 2) || (s._activeIndex > s._slides.length - s.slidesPerView * 2)) {
  638. // Fix For Positive Oversliding
  639. newIndex = -s._slides.length + s._activeIndex + s.loopedSlides;
  640. newIndex = newIndex + s.loopedSlides;
  641. slideTo(s, plt, newIndex, 0, false, true);
  642. }
  643. }
  644. /*=========================
  645. Transitions
  646. ===========================*/
  647. export function slideTo(s, plt, slideIndex, speed, runCallbacks = true, internal) {
  648. if (typeof slideIndex === 'undefined')
  649. slideIndex = 0;
  650. if (slideIndex < 0)
  651. slideIndex = 0;
  652. s._snapIndex = Math.floor(slideIndex / s.slidesPerGroup);
  653. if (s._snapIndex >= s._snapGrid.length)
  654. s._snapIndex = s._snapGrid.length - 1;
  655. var translate = -s._snapGrid[s._snapIndex];
  656. // Stop autoplay
  657. if (s.autoplay && s._autoplaying) {
  658. if (internal || !s.autoplayDisableOnInteraction) {
  659. pauseAutoplay(s, plt, speed);
  660. }
  661. else {
  662. stopAutoplay(s);
  663. }
  664. }
  665. // Update progress
  666. updateProgress(s, translate);
  667. // Directions locks
  668. if (!s._allowSwipeToNext && translate < s._translate && translate < minTranslate(s)) {
  669. return false;
  670. }
  671. if (!s._allowSwipeToPrev && translate > s._translate && translate > maxTranslate(s)) {
  672. if ((s._activeIndex || 0) !== slideIndex)
  673. return false;
  674. }
  675. // Update Index
  676. if (typeof speed === 'undefined')
  677. speed = s.speed;
  678. s._previousIndex = s._activeIndex || 0;
  679. s._activeIndex = slideIndex;
  680. updateRealIndex(s);
  681. if ((s._rtl && -translate === s._translate) || (!s._rtl && translate === s._translate)) {
  682. // Update Height
  683. if (s.autoHeight) {
  684. updateAutoHeight(s);
  685. }
  686. updateClasses(s);
  687. if (s.effect !== 'slide') {
  688. setWrapperTranslate(s, plt, translate);
  689. }
  690. return false;
  691. }
  692. updateClasses(s);
  693. onTransitionStart(s, runCallbacks);
  694. if (speed === 0) {
  695. setWrapperTranslate(s, plt, translate);
  696. setWrapperTransition(s, plt, 0);
  697. onTransitionEnd(s, plt, runCallbacks);
  698. }
  699. else {
  700. setWrapperTranslate(s, plt, translate);
  701. setWrapperTransition(s, plt, speed);
  702. if (!s._animating) {
  703. s._animating = true;
  704. plt.transitionEnd(s._wrapper, () => {
  705. if (!s)
  706. return;
  707. onTransitionEnd(s, plt, runCallbacks);
  708. });
  709. }
  710. }
  711. return true;
  712. }
  713. export function onTransitionStart(s, runCallbacks = true) {
  714. if (s.autoHeight) {
  715. updateAutoHeight(s);
  716. }
  717. if (runCallbacks) {
  718. s._zone.run(() => {
  719. s.ionSlideTransitionStart.emit(s);
  720. if (s._activeIndex !== s._previousIndex) {
  721. s.ionSlideWillChange.emit(s);
  722. if (s._activeIndex > s._previousIndex) {
  723. s.ionSlideNextStart.emit(s);
  724. }
  725. else {
  726. s.ionSlidePrevStart.emit(s);
  727. }
  728. }
  729. });
  730. }
  731. }
  732. export function onTransitionEnd(s, plt, runCallbacks = true) {
  733. s._animating = false;
  734. setWrapperTransition(s, plt, 0);
  735. if (runCallbacks) {
  736. s._zone.run(() => {
  737. s.ionSlideTransitionEnd.emit(s);
  738. if (s._activeIndex !== s._previousIndex) {
  739. s.ionSlideDidChange.emit(s);
  740. if (s._activeIndex > s._previousIndex) {
  741. s.ionSlideNextEnd.emit(s);
  742. }
  743. else {
  744. s.ionSlidePrevEnd.emit(s);
  745. }
  746. }
  747. });
  748. }
  749. }
  750. export function slideNext(s, plt, runCallbacks, speed, internal) {
  751. if (s.loop) {
  752. if (s._animating)
  753. return false;
  754. fixLoop(s, plt);
  755. s.container.clientLeft;
  756. return slideTo(s, plt, s._activeIndex + s.slidesPerGroup, speed, runCallbacks, internal);
  757. }
  758. var nextSlide = s._activeIndex + s.slidesPerGroup;
  759. if (nextSlide < s._slides.length) {
  760. return slideTo(s, plt, nextSlide, speed, runCallbacks, internal);
  761. }
  762. return false;
  763. }
  764. export function slidePrev(s, plt, runCallbacks, speed, internal) {
  765. if (s.loop) {
  766. if (s._animating)
  767. return false;
  768. fixLoop(s, plt);
  769. s.container.clientLeft;
  770. return slideTo(s, plt, s._activeIndex - 1, speed, runCallbacks, internal);
  771. }
  772. var previousSlide = s._activeIndex - 1;
  773. if (previousSlide >= 0) {
  774. return slideTo(s, plt, s._activeIndex - 1, speed, runCallbacks, internal);
  775. }
  776. return false;
  777. }
  778. export function slideReset(s, plt, runCallbacks, speed) {
  779. return slideTo(s, plt, s._activeIndex, speed, runCallbacks, true);
  780. }
  781. export function disableTouchControl(s) {
  782. s.onlyExternal = true;
  783. return true;
  784. }
  785. export function enableTouchControl(s) {
  786. s.onlyExternal = false;
  787. return true;
  788. }
  789. /*=========================
  790. Translate/transition helpers
  791. ===========================*/
  792. // Cleanup dynamic styles
  793. function cleanupStyles(s) {
  794. if (!s.container || !s._wrapper) {
  795. // fix #10830
  796. return;
  797. }
  798. // Container
  799. if (s.container) {
  800. removeClass(s.container, s._classNames);
  801. s.container.removeAttribute('style');
  802. }
  803. // Wrapper
  804. s._wrapper.removeAttribute('style');
  805. // Slides
  806. if (s._slides && s._slides.length) {
  807. removeClass(s._slides, [
  808. CLS.slideVisible,
  809. CLS.slideActive,
  810. CLS.slideNext,
  811. CLS.slidePrev
  812. ]);
  813. for (var i = 0; i < s._slides.length; i++) {
  814. var slide = s._slides[i];
  815. slide.removeAttribute('style');
  816. slide.removeAttribute('data-swiper-column');
  817. slide.removeAttribute('data-swiper-row');
  818. }
  819. }
  820. // Pagination/Bullets
  821. removeClass(s._bullets, CLS.bulletActive);
  822. // Buttons
  823. removeClass(s.prevButton, CLS.buttonDisabled);
  824. removeClass(s.nextButton, CLS.buttonDisabled);
  825. }
  826. // Destroy
  827. export function destroySwiper(s) {
  828. // Stop autoplay
  829. stopAutoplay(s);
  830. // Destroy loop
  831. if (s.loop) {
  832. destroyLoop(s);
  833. }
  834. // Cleanup styles
  835. cleanupStyles(s);
  836. }
  837. //# sourceMappingURL=swiper.js.map