Skip to content

Commit

Permalink
chore(core): move module's utils to separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed Aug 11, 2021
1 parent 1b0165a commit 7e04f5a
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 100 deletions.
2 changes: 1 addition & 1 deletion src/modules/a11y/a11y.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import classesToSelector from '../../shared/classes-to-selector.js';
import $ from '../../shared/dom.js';
import { classesToSelector } from '../../shared/utils.js';

export default function A11y({ swiper, extendParams, on }) {
extendParams({
Expand Down
21 changes: 3 additions & 18 deletions src/modules/effect-cards/effect-cards.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import virtualEffectTransitionEnd from '../../shared/virtual-effect-transition-end.js';

export default function EffectCards({ swiper, extendParams, on }) {
extendParams({
cardsEffect: {
Expand Down Expand Up @@ -88,28 +90,11 @@ export default function EffectCards({ swiper, extendParams, on }) {
};

const setTransition = (duration) => {
const { slides, activeIndex, $wrapperEl } = swiper;
const { transformEl } = swiper.params.cardsEffect;
const $transitionElements = transformEl ? swiper.slides.find(transformEl) : swiper.slides;
$transitionElements.transition(duration);

if (swiper.params.virtualTranslate && duration !== 0) {
let eventTriggered = false;
const $transitionEndTarget = transformEl
? slides.eq(activeIndex).find(transformEl)
: slides.eq(activeIndex);
// eslint-disable-next-line
$transitionEndTarget.transitionEnd(() => {
if (eventTriggered) return;
if (!swiper || swiper.destroyed) return;
eventTriggered = true;
swiper.animating = false;
const triggerEvents = ['webkitTransitionEnd', 'transitionend'];
for (let i = 0; i < triggerEvents.length; i += 1) {
$wrapperEl.trigger(triggerEvents[i]);
}
});
}
virtualEffectTransitionEnd({ swiper, duration, transformEl });
};

on('beforeInit', () => {
Expand Down
21 changes: 3 additions & 18 deletions src/modules/effect-custom/effect-custom.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import virtualEffectTransitionEnd from '../../shared/virtual-effect-transition-end.js';

export default function EffectCustom({ swiper, extendParams, on }) {
extendParams({
customEffect: {
Expand Down Expand Up @@ -90,28 +92,11 @@ export default function EffectCustom({ swiper, extendParams, on }) {
};

const setTransition = (duration) => {
const { slides, activeIndex, $wrapperEl } = swiper;
const { transformEl } = swiper.params.customEffect;
const $transitionElements = transformEl ? swiper.slides.find(transformEl) : swiper.slides;
$transitionElements.transition(duration);

if (swiper.params.virtualTranslate && duration !== 0) {
let eventTriggered = false;
const $transitionEndTarget = transformEl
? slides.eq(activeIndex).find(transformEl)
: slides.eq(activeIndex);
// eslint-disable-next-line
$transitionEndTarget.transitionEnd(() => {
if (eventTriggered) return;
if (!swiper || swiper.destroyed) return;
eventTriggered = true;
swiper.animating = false;
const triggerEvents = ['webkitTransitionEnd', 'transitionend'];
for (let i = 0; i < triggerEvents.length; i += 1) {
$wrapperEl.trigger(triggerEvents[i]);
}
});
}
virtualEffectTransitionEnd({ swiper, duration, transformEl });
};

on('beforeInit', () => {
Expand Down
20 changes: 4 additions & 16 deletions src/modules/effect-fade/effect-fade.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import virtualEffectTransitionEnd from '../../shared/virtual-effect-transition-end.js';

export default function EffectFade({ swiper, extendParams, on }) {
extendParams({
fadeEffect: {
Expand Down Expand Up @@ -32,24 +34,10 @@ export default function EffectFade({ swiper, extendParams, on }) {
}
};
const setTransition = (duration) => {
const { slides, $wrapperEl } = swiper;
const { transformEl } = swiper.params.fadeEffect;
const $transitionElements = transformEl ? slides.find(transformEl) : slides;
const $transitionElements = transformEl ? swiper.slides.find(transformEl) : swiper.slides;
$transitionElements.transition(duration);
if (swiper.params.virtualTranslate && duration !== 0) {
let eventTriggered = false;
const $transitionEndTarget = transformEl ? slides.find(transformEl) : slides;
$transitionEndTarget.transitionEnd(() => {
if (eventTriggered) return;
if (!swiper || swiper.destroyed) return;
eventTriggered = true;
swiper.animating = false;
const triggerEvents = ['webkitTransitionEnd', 'transitionend'];
for (let i = 0; i < triggerEvents.length; i += 1) {
$wrapperEl.trigger(triggerEvents[i]);
}
});
}
virtualEffectTransitionEnd({ swiper, duration, transformEl, allSlides: true });
};

on('beforeInit', () => {
Expand Down
20 changes: 2 additions & 18 deletions src/modules/effect-flip/effect-flip.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import $ from '../../shared/dom.js';
import virtualEffectTransitionEnd from '../../shared/virtual-effect-transition-end.js';

export default function EffectFlip({ swiper, extendParams, on }) {
extendParams({
Expand Down Expand Up @@ -75,7 +76,6 @@ export default function EffectFlip({ swiper, extendParams, on }) {
};

const setTransition = (duration) => {
const { slides, activeIndex, $wrapperEl } = swiper;
const { transformEl } = swiper.params.flipEffect;
const $transitionElements = transformEl ? swiper.slides.find(transformEl) : swiper.slides;
$transitionElements
Expand All @@ -84,23 +84,7 @@ export default function EffectFlip({ swiper, extendParams, on }) {
'.swiper-slide-shadow-top, .swiper-slide-shadow-right, .swiper-slide-shadow-bottom, .swiper-slide-shadow-left',
)
.transition(duration);
if (swiper.params.virtualTranslate && duration !== 0) {
let eventTriggered = false;
const $transitionEndTarget = transformEl
? slides.eq(activeIndex).find(transformEl)
: slides.eq(activeIndex);
// eslint-disable-next-line
$transitionEndTarget.transitionEnd(() => {
if (eventTriggered) return;
if (!swiper || swiper.destroyed) return;
eventTriggered = true;
swiper.animating = false;
const triggerEvents = ['webkitTransitionEnd', 'transitionend'];
for (let i = 0; i < triggerEvents.length; i += 1) {
$wrapperEl.trigger(triggerEvents[i]);
}
});
}
virtualEffectTransitionEnd({ swiper, duration, transformEl });
};

on('beforeInit', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/navigation/navigation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import createElementIfNotDefined from '../../shared/create-element-if-not-defined.js';
import $ from '../../shared/dom.js';
import { createElementIfNotDefined } from '../../shared/utils.js';

export default function Navigation({ swiper, extendParams, on, emit }) {
extendParams({
Expand Down
3 changes: 2 additions & 1 deletion src/modules/pagination/pagination.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import $ from '../../shared/dom.js';
import { classesToSelector, createElementIfNotDefined } from '../../shared/utils.js';
import classesToSelector from '../../shared/classes-to-selector.js';
import createElementIfNotDefined from '../../shared/create-element-if-not-defined.js';

export default function Pagination({ swiper, extendParams, on, emit }) {
const pfx = 'swiper-pagination';
Expand Down
3 changes: 2 additions & 1 deletion src/modules/scrollbar/scrollbar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getDocument } from 'ssr-window';
import $ from '../../shared/dom.js';
import { nextTick, createElementIfNotDefined } from '../../shared/utils.js';
import { nextTick } from '../../shared/utils.js';
import createElementIfNotDefined from '../../shared/create-element-if-not-defined.js';

export default function Scrollbar({ swiper, extendParams, on, emit }) {
const document = getDocument();
Expand Down
6 changes: 6 additions & 0 deletions src/shared/classes-to-selector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function classesToSelector(classes = '') {
return `.${classes
.trim()
.replace(/([\.:!\/])/g, '\\$1') // eslint-disable-line
.replace(/ /g, '.')}`;
}
17 changes: 17 additions & 0 deletions src/shared/create-element-if-not-defined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { getDocument } from 'ssr-window';

export default function createElementIfNotDefined(swiper, originalParams, params, checkProps) {
const document = getDocument();
if (swiper.params.createElements) {
Object.keys(checkProps).forEach((key) => {
if (!params[key] && params.auto === true) {
const element = document.createElement('div');
element.className = checkProps[key];
swiper.$el.append(element);
params[key] = element;
originalParams[key] = element;
}
});
}
return params;
}
27 changes: 1 addition & 26 deletions src/shared/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getDocument, getWindow } from 'ssr-window';
import { getWindow } from 'ssr-window';

function deleteProps(obj) {
const object = obj;
Expand Down Expand Up @@ -133,29 +133,6 @@ function extend(...args) {
return to;
}

function classesToSelector(classes = '') {
return `.${classes
.trim()
.replace(/([\.:!\/])/g, '\\$1') // eslint-disable-line
.replace(/ /g, '.')}`;
}

function createElementIfNotDefined(swiper, originalParams, params, checkProps) {
const document = getDocument();
if (swiper.params.createElements) {
Object.keys(checkProps).forEach((key) => {
if (!params[key] && params.auto === true) {
const element = document.createElement('div');
element.className = checkProps[key];
swiper.$el.append(element);
params[key] = element;
originalParams[key] = element;
}
});
}
return params;
}

function setCSSProperty(el, varName, varValue) {
el.style.setProperty(varName, varValue);
}
Expand Down Expand Up @@ -209,7 +186,5 @@ export {
isObject,
extend,
getComputedStyle,
classesToSelector,
createElementIfNotDefined,
setCSSProperty,
};
24 changes: 24 additions & 0 deletions src/shared/virtual-effect-transition-end.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export default function virtualEffectTransitionEnd({ swiper, duration, transformEl, allSlides }) {
const { slides, activeIndex, $wrapperEl } = swiper;
if (swiper.params.virtualTranslate && duration !== 0) {
let eventTriggered = false;
let $transitionEndTarget;
if (allSlides) {
$transitionEndTarget = transformEl ? slides.find(transformEl) : slides;
} else {
$transitionEndTarget = transformEl
? slides.eq(activeIndex).find(transformEl)
: slides.eq(activeIndex);
}
$transitionEndTarget.transitionEnd(() => {
if (eventTriggered) return;
if (!swiper || swiper.destroyed) return;
eventTriggered = true;
swiper.animating = false;
const triggerEvents = ['webkitTransitionEnd', 'transitionend'];
for (let i = 0; i < triggerEvents.length; i += 1) {
$wrapperEl.trigger(triggerEvents[i]);
}
});
}
}

0 comments on commit 7e04f5a

Please sign in to comment.