get-params.js 1.6 KB

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