get-params.js 1.4 KB

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