swiper
Version:
Most modern mobile touch slider and framework with hardware accelerated transitions
131 lines (125 loc) • 4.19 kB
JavaScript
import { e as extend, p as paramsList, i as isObject, n as needsNavigation, a as needsPagination, b as needsScrollbar } from './update-swiper.mjs';
import { d as defaults } from './swiper-core.mjs';
function getParams(obj, splitEvents) {
if (obj === void 0) {
obj = {};
}
if (splitEvents === void 0) {
splitEvents = true;
}
const params = {
on: {}
};
const events = {};
const passedParams = {};
extend(params, defaults);
params._emitClasses = true;
params.init = false;
const rest = {};
const allowedParams = paramsList.map(key => key.replace(/_/, ''));
const plainObj = Object.assign({}, obj);
Object.keys(plainObj).forEach(key => {
if (typeof obj[key] === 'undefined') return;
if (allowedParams.indexOf(key) >= 0) {
if (isObject(obj[key])) {
params[key] = {};
passedParams[key] = {};
extend(params[key], obj[key]);
extend(passedParams[key], obj[key]);
} else {
params[key] = obj[key];
passedParams[key] = obj[key];
}
} else if (key.search(/on[A-Z]/) === 0 && typeof obj[key] === 'function') {
if (splitEvents) {
events[`${key[2].toLowerCase()}${key.substr(3)}`] = obj[key];
} else {
params.on[`${key[2].toLowerCase()}${key.substr(3)}`] = obj[key];
}
} else {
rest[key] = obj[key];
}
});
['navigation', 'pagination', 'scrollbar'].forEach(key => {
if (params[key] === true) params[key] = {};
if (params[key] === false) delete params[key];
});
return {
params,
passedParams,
rest,
events
};
}
function mountSwiper(_ref, swiperParams) {
let {
el,
nextEl,
prevEl,
paginationEl,
scrollbarEl,
swiper
} = _ref;
if (needsNavigation(swiperParams) && nextEl && prevEl) {
swiper.params.navigation.nextEl = nextEl;
swiper.originalParams.navigation.nextEl = nextEl;
swiper.params.navigation.prevEl = prevEl;
swiper.originalParams.navigation.prevEl = prevEl;
}
if (needsPagination(swiperParams) && paginationEl) {
swiper.params.pagination.el = paginationEl;
swiper.originalParams.pagination.el = paginationEl;
}
if (needsScrollbar(swiperParams) && scrollbarEl) {
swiper.params.scrollbar.el = scrollbarEl;
swiper.originalParams.scrollbar.el = scrollbarEl;
}
swiper.init(el);
}
function getChangedParams(swiperParams, oldParams, children, oldChildren, getKey) {
const keys = [];
if (!oldParams) return keys;
const addKey = key => {
if (keys.indexOf(key) < 0) keys.push(key);
};
if (children && oldChildren) {
const oldChildrenKeys = oldChildren.map(getKey);
const childrenKeys = children.map(getKey);
if (oldChildrenKeys.join('') !== childrenKeys.join('')) addKey('children');
if (oldChildren.length !== children.length) addKey('children');
}
const watchParams = paramsList.filter(key => key[0] === '_').map(key => key.replace(/_/, ''));
watchParams.forEach(key => {
if (key in swiperParams && key in oldParams) {
if (isObject(swiperParams[key]) && isObject(oldParams[key])) {
const newKeys = Object.keys(swiperParams[key]);
const oldKeys = Object.keys(oldParams[key]);
if (newKeys.length !== oldKeys.length) {
addKey(key);
} else {
newKeys.forEach(newKey => {
if (swiperParams[key][newKey] !== oldParams[key][newKey]) {
addKey(key);
}
});
oldKeys.forEach(oldKey => {
if (swiperParams[key][oldKey] !== oldParams[key][oldKey]) addKey(key);
});
}
} else if (swiperParams[key] !== oldParams[key]) {
addKey(key);
}
}
});
return keys;
}
const updateOnVirtualData = swiper => {
if (!swiper || swiper.destroyed || !swiper.params.virtual || swiper.params.virtual && !swiper.params.virtual.enabled) return;
swiper.updateSlides();
swiper.updateProgress();
swiper.updateSlidesClasses();
if (swiper.parallax && swiper.params.parallax && swiper.params.parallax.enabled) {
swiper.parallax.setTranslate();
}
};
export { getChangedParams as a, getParams as g, mountSwiper as m, updateOnVirtualData as u };