Skip to content

Commit

Permalink
feat(core): convert FreeMode to functional module
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed Aug 4, 2021
1 parent 2ddd6e9 commit 4d307c0
Showing 1 changed file with 29 additions and 33 deletions.
62 changes: 29 additions & 33 deletions src/modules/free-mode/free-mode.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { bindModuleMethods, now } from '../../shared/utils.js';
import { now } from '../../shared/utils.js';

const FreeMode = {
onTouchMove() {
const swiper = this;
export default function freeMode({ swiper, extendParams, emit }) {
extendParams({
freeMode: {
enabled: false,
momentum: true,
momentumRatio: 1,
momentumBounce: true,
momentumBounceRatio: 1,
momentumVelocityRatio: 1,
sticky: false,
minimumVelocity: 0.02,
},
});

function onTouchMove() {
const { touchEventsData: data, touches } = swiper;
// Velocity
if (data.velocities.length === 0) {
Expand All @@ -15,11 +27,10 @@ const FreeMode = {
position: touches[swiper.isHorizontal() ? 'currentX' : 'currentY'],
time: now(),
});
},
onTouchEnd({ currentPos }) {
const swiper = this;
const { params, $wrapperEl, rtlTranslate: rtl, snapGrid } = swiper;
const data = swiper.touchEventsData;
}

function onTouchEnd({ currentPos }) {
const { params, $wrapperEl, rtlTranslate: rtl, snapGrid, touchEventsData: data } = swiper;
// Time diff
const touchEndTime = now();
const timeDiff = touchEndTime - data.touchStartTime;
Expand Down Expand Up @@ -157,7 +168,7 @@ const FreeMode = {
swiper.animating = true;
$wrapperEl.transitionEnd(() => {
if (!swiper || swiper.destroyed || !data.allowMomentumBounce) return;
swiper.emit('momentumBounce');
emit('momentumBounce');
swiper.setTransition(params.speed);
setTimeout(() => {
swiper.setTranslate(afterBouncePosition);
Expand All @@ -168,7 +179,7 @@ const FreeMode = {
}, 0);
});
} else if (swiper.velocity) {
swiper.emit('_freeModeNoMomentumRelease');
emit('_freeModeNoMomentumRelease');
swiper.updateProgress(newPosition);
swiper.setTransition(momentumDuration);
swiper.setTranslate(newPosition);
Expand All @@ -190,35 +201,20 @@ const FreeMode = {
swiper.slideToClosest();
return;
} else if (params.freeMode) {
swiper.emit('_freeModeNoMomentumRelease');
emit('_freeModeNoMomentumRelease');
}

if (!params.freeMode.momentum || timeDiff >= params.longSwipesMs) {
swiper.updateProgress();
swiper.updateActiveIndex();
swiper.updateSlidesClasses();
}
},
};
}

export default {
name: 'free-mode',
params: {
Object.assign(swiper, {
freeMode: {
enabled: false,
momentum: true,
momentumRatio: 1,
momentumBounce: true,
momentumBounceRatio: 1,
momentumVelocityRatio: 1,
sticky: false,
minimumVelocity: 0.02,
onTouchMove,
onTouchEnd,
},
},
create() {
const swiper = this;

bindModuleMethods(swiper, { freeMode: FreeMode });
},
on: {},
};
});
}

0 comments on commit 4d307c0

Please sign in to comment.