get-element-params.mjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { e as extend, i as isObject, c as attrToProp, p as paramsList } from './update-swiper.mjs';
  2. import { d as defaults } from './swiper-core.mjs';
  3. const formatValue = val => {
  4. if (parseFloat(val) === Number(val)) return Number(val);
  5. if (val === 'true') return true;
  6. if (val === '') return true;
  7. if (val === 'false') return false;
  8. if (val === 'null') return null;
  9. if (val === 'undefined') return undefined;
  10. if (typeof val === 'string' && val.includes('{') && val.includes('}') && val.includes('"')) {
  11. let v;
  12. try {
  13. v = JSON.parse(val);
  14. } catch (err) {
  15. v = val;
  16. }
  17. return v;
  18. }
  19. return val;
  20. };
  21. const modulesParamsList = ['a11y', 'autoplay', 'controller', 'cards-effect', 'coverflow-effect', 'creative-effect', 'cube-effect', 'fade-effect', 'flip-effect', 'free-mode', 'grid', 'hash-navigation', 'history', 'keyboard', 'mousewheel', 'navigation', 'pagination', 'parallax', 'scrollbar', 'thumbs', 'virtual', 'zoom'];
  22. function getParams(element, propName, propValue) {
  23. const params = {};
  24. const passedParams = {};
  25. extend(params, defaults);
  26. const localParamsList = [...paramsList, 'on'];
  27. const allowedParams = localParamsList.map(key => key.replace(/_/, ''));
  28. // First check props
  29. localParamsList.forEach(paramName => {
  30. paramName = paramName.replace('_', '');
  31. if (typeof element[paramName] !== 'undefined') {
  32. passedParams[paramName] = element[paramName];
  33. }
  34. });
  35. // Attributes
  36. const attrsList = [...element.attributes];
  37. if (typeof propName === 'string' && typeof propValue !== 'undefined') {
  38. attrsList.push({
  39. name: propName,
  40. value: isObject(propValue) ? {
  41. ...propValue
  42. } : propValue
  43. });
  44. }
  45. attrsList.forEach(attr => {
  46. const moduleParam = modulesParamsList.filter(mParam => attr.name.indexOf(`${mParam}-`) === 0)[0];
  47. if (moduleParam) {
  48. const parentObjName = attrToProp(moduleParam);
  49. const subObjName = attrToProp(attr.name.split(`${moduleParam}-`)[1]);
  50. if (typeof passedParams[parentObjName] === 'undefined') passedParams[parentObjName] = {};
  51. if (passedParams[parentObjName] === true) {
  52. passedParams[parentObjName] = {
  53. enabled: true
  54. };
  55. }
  56. passedParams[parentObjName][subObjName] = formatValue(attr.value);
  57. } else {
  58. const name = attrToProp(attr.name);
  59. if (!allowedParams.includes(name)) return;
  60. const value = formatValue(attr.value);
  61. if (passedParams[name] && modulesParamsList.includes(attr.name) && !isObject(value)) {
  62. if (passedParams[name].constructor !== Object) {
  63. passedParams[name] = {};
  64. }
  65. passedParams[name].enabled = !!value;
  66. } else {
  67. passedParams[name] = value;
  68. }
  69. }
  70. });
  71. extend(params, passedParams);
  72. if (params.navigation) {
  73. params.navigation = {
  74. prevEl: '.swiper-button-prev',
  75. nextEl: '.swiper-button-next',
  76. ...(params.navigation !== true ? params.navigation : {})
  77. };
  78. } else if (params.navigation === false) {
  79. delete params.navigation;
  80. }
  81. if (params.scrollbar) {
  82. params.scrollbar = {
  83. el: '.swiper-scrollbar',
  84. ...(params.scrollbar !== true ? params.scrollbar : {})
  85. };
  86. } else if (params.scrollbar === false) {
  87. delete params.scrollbar;
  88. }
  89. if (params.pagination) {
  90. params.pagination = {
  91. el: '.swiper-pagination',
  92. ...(params.pagination !== true ? params.pagination : {})
  93. };
  94. } else if (params.pagination === false) {
  95. delete params.pagination;
  96. }
  97. return {
  98. params,
  99. passedParams
  100. };
  101. }
  102. export { getParams as g };