get-params.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import Swiper from '../../index.js';
  2. import {
  3. isObject,
  4. extend
  5. } from './utils.js';
  6. import {
  7. paramsList
  8. } from './params-list.js';
  9. function getParams(obj = {}) {
  10. const params = {
  11. on: {},
  12. };
  13. const passedParams = {};
  14. extend(params, Swiper.defaults);
  15. extend(params, Swiper.extendedDefaults);
  16. params._emitClasses = true;
  17. params.init = false;
  18. const rest = {};
  19. const allowedParams = paramsList.map((key) => key.replace(/_/, ''));
  20. // Prevent empty Object.keys(obj) array on ios.
  21. const plainObj = Object.assign({}, obj);
  22. Object.keys(plainObj).forEach((key) => {
  23. if (typeof obj[key] === 'undefined') return;
  24. if (allowedParams.indexOf(key) >= 0) {
  25. if (isObject(obj[key])) {
  26. params[key] = {};
  27. passedParams[key] = {};
  28. extend(params[key], obj[key]);
  29. extend(passedParams[key], obj[key]);
  30. } else {
  31. params[key] = obj[key];
  32. passedParams[key] = obj[key];
  33. }
  34. } else if (key.search(/on[A-Z]/) === 0 && typeof obj[key] === 'function') {
  35. params.on[`${key[2].toLowerCase()}${key.substr(3)}`] = obj[key];
  36. } else {
  37. rest[key] = obj[key];
  38. }
  39. });
  40. ['navigation', 'pagination', 'scrollbar'].forEach((key) => {
  41. if (params[key] === true) params[key] = {};
  42. if (params[key] === false) delete params[key];
  43. });
  44. return {
  45. params,
  46. passedParams,
  47. rest
  48. };
  49. }
  50. export {
  51. getParams
  52. };