loop.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import Swiper from '../../index.js';
  2. function calcLoopedSlides(slides, swiperParams) {
  3. let slidesPerViewParams = swiperParams.slidesPerView;
  4. if (swiperParams.breakpoints) {
  5. const breakpoint = Swiper.prototype.getBreakpoint(swiperParams.breakpoints);
  6. const breakpointOnlyParams =
  7. breakpoint in swiperParams.breakpoints ? swiperParams.breakpoints[breakpoint] : undefined;
  8. if (breakpointOnlyParams && breakpointOnlyParams.slidesPerView) {
  9. slidesPerViewParams = breakpointOnlyParams.slidesPerView;
  10. }
  11. }
  12. let loopedSlides = Math.ceil(parseFloat(swiperParams.loopedSlides || slidesPerViewParams, 10));
  13. loopedSlides += swiperParams.loopAdditionalSlides;
  14. if (loopedSlides > slides.length) {
  15. loopedSlides = slides.length;
  16. }
  17. return loopedSlides;
  18. }
  19. function renderLoop(native, swiperParams, data) {
  20. const modifiedValue = data;
  21. if (swiperParams.loopFillGroupWithBlank) {
  22. const blankSlidesNum =
  23. swiperParams.slidesPerGroup - (modifiedValue.length % swiperParams.slidesPerGroup);
  24. if (blankSlidesNum !== swiperParams.slidesPerGroup) {
  25. for (let i = 0; i < blankSlidesNum; i += 1) {
  26. const blankSlide = h('div', {
  27. class: `${swiperParams.slideClass} ${swiperParams.slideBlankClass}`,
  28. });
  29. modifiedValue.push(blankSlide);
  30. }
  31. }
  32. }
  33. if (swiperParams.slidesPerView === 'auto' && !swiperParams.loopedSlides) {
  34. swiperParams.loopedSlides = modifiedValue.length;
  35. }
  36. const loopedSlides = calcLoopedSlides(modifiedValue, swiperParams);
  37. const prependSlides = [];
  38. const appendSlides = [];
  39. const prependValue = [];
  40. const appendValue = [];
  41. modifiedValue.forEach((child, index) => {
  42. if (index < loopedSlides) {
  43. if (!native.loopUpdateData) {
  44. appendValue.push(child);
  45. }
  46. }
  47. if (index < modifiedValue.length && index >= modifiedValue.length - loopedSlides) {
  48. if (!native.loopUpdateData) {
  49. prependValue.push(child);
  50. }
  51. }
  52. })
  53. if (native) {
  54. if (!native.originalDataList) native.originalDataList = [];
  55. native.originalDataList = [...prependValue, ...modifiedValue, ...appendValue];
  56. }
  57. return {
  58. data: [...prependValue, ...modifiedValue, ...appendValue]
  59. };
  60. }
  61. export {
  62. calcLoopedSlides,
  63. renderLoop
  64. };