update-on-virtual-data.mjs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { e as extend, p as paramsList, i as isObject, n as needsNavigation, a as needsPagination, b as needsScrollbar } from './update-swiper.mjs';
  2. import { d as defaults } from './swiper-core.mjs';
  3. function getParams(obj, splitEvents) {
  4. if (obj === void 0) {
  5. obj = {};
  6. }
  7. if (splitEvents === void 0) {
  8. splitEvents = true;
  9. }
  10. const params = {
  11. on: {}
  12. };
  13. const events = {};
  14. const passedParams = {};
  15. extend(params, defaults);
  16. params._emitClasses = true;
  17. params.init = false;
  18. const rest = {};
  19. const allowedParams = paramsList.map(key => key.replace(/_/, ''));
  20. const plainObj = Object.assign({}, obj);
  21. Object.keys(plainObj).forEach(key => {
  22. if (typeof obj[key] === 'undefined') return;
  23. if (allowedParams.indexOf(key) >= 0) {
  24. if (isObject(obj[key])) {
  25. params[key] = {};
  26. passedParams[key] = {};
  27. extend(params[key], obj[key]);
  28. extend(passedParams[key], obj[key]);
  29. } else {
  30. params[key] = obj[key];
  31. passedParams[key] = obj[key];
  32. }
  33. } else if (key.search(/on[A-Z]/) === 0 && typeof obj[key] === 'function') {
  34. if (splitEvents) {
  35. events[`${key[2].toLowerCase()}${key.substr(3)}`] = obj[key];
  36. } else {
  37. params.on[`${key[2].toLowerCase()}${key.substr(3)}`] = obj[key];
  38. }
  39. } else {
  40. rest[key] = obj[key];
  41. }
  42. });
  43. ['navigation', 'pagination', 'scrollbar'].forEach(key => {
  44. if (params[key] === true) params[key] = {};
  45. if (params[key] === false) delete params[key];
  46. });
  47. return {
  48. params,
  49. passedParams,
  50. rest,
  51. events
  52. };
  53. }
  54. function mountSwiper(_ref, swiperParams) {
  55. let {
  56. el,
  57. nextEl,
  58. prevEl,
  59. paginationEl,
  60. scrollbarEl,
  61. swiper
  62. } = _ref;
  63. if (needsNavigation(swiperParams) && nextEl && prevEl) {
  64. swiper.params.navigation.nextEl = nextEl;
  65. swiper.originalParams.navigation.nextEl = nextEl;
  66. swiper.params.navigation.prevEl = prevEl;
  67. swiper.originalParams.navigation.prevEl = prevEl;
  68. }
  69. if (needsPagination(swiperParams) && paginationEl) {
  70. swiper.params.pagination.el = paginationEl;
  71. swiper.originalParams.pagination.el = paginationEl;
  72. }
  73. if (needsScrollbar(swiperParams) && scrollbarEl) {
  74. swiper.params.scrollbar.el = scrollbarEl;
  75. swiper.originalParams.scrollbar.el = scrollbarEl;
  76. }
  77. swiper.init(el);
  78. }
  79. function getChangedParams(swiperParams, oldParams, children, oldChildren, getKey) {
  80. const keys = [];
  81. if (!oldParams) return keys;
  82. const addKey = key => {
  83. if (keys.indexOf(key) < 0) keys.push(key);
  84. };
  85. if (children && oldChildren) {
  86. const oldChildrenKeys = oldChildren.map(getKey);
  87. const childrenKeys = children.map(getKey);
  88. if (oldChildrenKeys.join('') !== childrenKeys.join('')) addKey('children');
  89. if (oldChildren.length !== children.length) addKey('children');
  90. }
  91. const watchParams = paramsList.filter(key => key[0] === '_').map(key => key.replace(/_/, ''));
  92. watchParams.forEach(key => {
  93. if (key in swiperParams && key in oldParams) {
  94. if (isObject(swiperParams[key]) && isObject(oldParams[key])) {
  95. const newKeys = Object.keys(swiperParams[key]);
  96. const oldKeys = Object.keys(oldParams[key]);
  97. if (newKeys.length !== oldKeys.length) {
  98. addKey(key);
  99. } else {
  100. newKeys.forEach(newKey => {
  101. if (swiperParams[key][newKey] !== oldParams[key][newKey]) {
  102. addKey(key);
  103. }
  104. });
  105. oldKeys.forEach(oldKey => {
  106. if (swiperParams[key][oldKey] !== oldParams[key][oldKey]) addKey(key);
  107. });
  108. }
  109. } else if (swiperParams[key] !== oldParams[key]) {
  110. addKey(key);
  111. }
  112. }
  113. });
  114. return keys;
  115. }
  116. const updateOnVirtualData = swiper => {
  117. if (!swiper || swiper.destroyed || !swiper.params.virtual || swiper.params.virtual && !swiper.params.virtual.enabled) return;
  118. swiper.updateSlides();
  119. swiper.updateProgress();
  120. swiper.updateSlidesClasses();
  121. if (swiper.parallax && swiper.params.parallax && swiper.params.parallax.enabled) {
  122. swiper.parallax.setTranslate();
  123. }
  124. };
  125. export { getChangedParams as a, getParams as g, mountSwiper as m, updateOnVirtualData as u };