Skip to content

Commit

Permalink
feat(core): remove object-syntax modules support
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed Aug 4, 2021
1 parent 71dc7d8 commit 287d14a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 114 deletions.
92 changes: 14 additions & 78 deletions src/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { getBrowser } from '../shared/get-browser.js';
import Resize from './modules/resize/resize.js';
import Observer from './modules/observer/observer.js';

import modular from './modular.js';
import eventsEmitter from './events-emitter.js';

import update from './update/index.js';
Expand All @@ -26,9 +25,9 @@ import images from './images/index.js';
import checkOverflow from './check-overflow/index.js';

import defaults from './defaults.js';
import moduleExtendParams from './moduleExtendParams.js';

const prototypes = {
modular,
eventsEmitter,
update,
translate,
Expand Down Expand Up @@ -86,78 +85,25 @@ class Swiper {
if (typeof swiper.modules === 'undefined') {
swiper.modules = [];
}
if (params.modules && Array.isArray(params.modules)) {
swiper.modules.push(...params.modules);
}

const modulesParams = {};
const allModulesParams = {};

swiper.modules.forEach((mod) => {
if (typeof mod === 'function') {
const extendParams = (obj = {}) => {
const moduleParamName = Object.keys(obj)[0];
const moduleParams = obj[moduleParamName];
if (typeof moduleParams !== 'object' || moduleParams === null) {
extend(modulesParams, obj);
return;
}
if (
['navigation', 'pagination', 'scrollbar'].indexOf(moduleParamName) >= 0 &&
params[moduleParamName] === true
) {
params[moduleParamName] = { auto: true };
}
if (!(moduleParamName in params && 'enabled' in moduleParams)) {
extend(modulesParams, obj);
return;
}
if (params[moduleParamName] === true) {
params[moduleParamName] = { enabled: true };
}
if (
typeof params[moduleParamName] === 'object' &&
!('enabled' in params[moduleParamName])
) {
params[moduleParamName].enabled = true;
}
if (!params[moduleParamName]) params[moduleParamName] = { enabled: false };
extend(modulesParams, obj);
};
mod({
swiper,
extendParams,
on: swiper.on.bind(swiper),
once: swiper.once.bind(swiper),
off: swiper.off.bind(swiper),
emit: swiper.emit.bind(swiper),
});
return;
}
// TODO: remove after all modules conversion
if (mod.params) {
const moduleParamName = Object.keys(mod.params)[0];
const moduleParams = mod.params[moduleParamName];
if (typeof moduleParams !== 'object' || moduleParams === null) return;
if (
['navigation', 'pagination', 'scrollbar'].indexOf(moduleParamName) >= 0 &&
params[moduleParamName] === true
) {
params[moduleParamName] = { auto: true };
}
if (!(moduleParamName in params && 'enabled' in moduleParams)) return;
if (params[moduleParamName] === true) {
params[moduleParamName] = { enabled: true };
}
if (
typeof params[moduleParamName] === 'object' &&
!('enabled' in params[moduleParamName])
) {
params[moduleParamName].enabled = true;
}
if (!params[moduleParamName]) params[moduleParamName] = { enabled: false };
}
mod({
swiper,
extendParams: moduleExtendParams,
on: swiper.on.bind(swiper),
once: swiper.once.bind(swiper),
off: swiper.off.bind(swiper),
emit: swiper.emit.bind(swiper),
});
});

// Extend defaults with modules params
const swiperParams = extend({}, defaults, modulesParams);
swiper.useParams(swiperParams);
const swiperParams = extend({}, defaults, allModulesParams);

// Extend defaults with passed params
swiper.params = extend({}, swiperParams, extendedDefaults, params);
Expand Down Expand Up @@ -278,9 +224,6 @@ class Swiper {
imagesLoaded: 0,
});

// Install Modules
swiper.useModules();

swiper.emit('_swiper');

// Init
Expand Down Expand Up @@ -676,14 +619,7 @@ class Swiper {

if (typeof mod === 'function' && modules.indexOf(mod) < 0) {
modules.push(mod);
return;
}
const name = mod.name;
const sameNameModule = modules.filter((m) => m.name === name)[0];
if (sameNameModule) {
modules.splice(modules.indexOf(sameNameModule), 1);
}
modules.push(mod);
}

static use(module) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/events/onTouchMove.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getDocument } from 'ssr-window';
import $ from '../../shared/dom.js';
import { extend, now } from '../../shared/utils.js';
import { now } from '../../shared/utils.js';

export default function onTouchMove(event) {
const document = getDocument();
Expand Down Expand Up @@ -30,7 +30,7 @@ export default function onTouchMove(event) {
// isMoved = true;
swiper.allowClick = false;
if (data.isTouched) {
extend(touches, {
Object.assign(touches, {
startX: pageX,
startY: pageY,
currentX: pageX,
Expand Down
34 changes: 0 additions & 34 deletions src/core/modular.js

This file was deleted.

30 changes: 30 additions & 0 deletions src/core/moduleExtendParams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { extend } from '../shared/utils.js';

export default function moduleExtendParams(params, allModulesParams) {
return function extendParams(obj = {}) {
const moduleParamName = Object.keys(obj)[0];
const moduleParams = obj[moduleParamName];
if (typeof moduleParams !== 'object' || moduleParams === null) {
extend(allModulesParams, obj);
return;
}
if (
['navigation', 'pagination', 'scrollbar'].indexOf(moduleParamName) >= 0 &&
params[moduleParamName] === true
) {
params[moduleParamName] = { auto: true };
}
if (!(moduleParamName in params && 'enabled' in moduleParams)) {
extend(allModulesParams, obj);
return;
}
if (params[moduleParamName] === true) {
params[moduleParamName] = { enabled: true };
}
if (typeof params[moduleParamName] === 'object' && !('enabled' in params[moduleParamName])) {
params[moduleParamName].enabled = true;
}
if (!params[moduleParamName]) params[moduleParamName] = { enabled: false };
extend(allModulesParams, obj);
};
}

0 comments on commit 287d14a

Please sign in to comment.