get-changed-params.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import {
  2. paramsList
  3. } from './params-list.js';
  4. import {
  5. isObject
  6. } from './utils.js';
  7. function getChangedParams(swiperParams, oldParams, children, oldChildren) {
  8. const keys = [];
  9. if (!oldParams) return keys;
  10. const addKey = (key) => {
  11. if (keys.indexOf(key) < 0) keys.push(key);
  12. };
  13. const oldChildrenKeys = oldChildren.map((child) => child.props && child.props.key);
  14. const childrenKeys = children.map((child) => child.props && child.props.key);
  15. if (oldChildrenKeys.join('') !== childrenKeys.join('')) keys.push('children');
  16. if (oldChildren.length !== children.length) keys.push('children');
  17. const watchParams = paramsList.filter((key) => key[0] === '_').map((key) => key.replace(/_/, ''));
  18. watchParams.forEach((key) => {
  19. if (key in swiperParams && key in oldParams) {
  20. if (isObject(swiperParams[key]) && isObject(oldParams[key])) {
  21. const newKeys = Object.keys(swiperParams[key]);
  22. const oldKeys = Object.keys(oldParams[key]);
  23. if (newKeys.length !== oldKeys.length) {
  24. addKey(key);
  25. } else {
  26. newKeys.forEach((newKey) => {
  27. if (swiperParams[key][newKey] !== oldParams[key][newKey]) {
  28. addKey(key);
  29. }
  30. });
  31. oldKeys.forEach((oldKey) => {
  32. if (swiperParams[key][oldKey] !== oldParams[key][oldKey]) addKey(key);
  33. });
  34. }
  35. } else if (swiperParams[key] !== oldParams[key]) {
  36. addKey(key);
  37. }
  38. } else if (key in swiperParams && !(key in oldParams)) {
  39. addKey(key);
  40. } else if (!(key in swiperParams) && key in oldParams) {
  41. addKey(key);
  42. }
  43. });
  44. return keys;
  45. }
  46. export {
  47. getChangedParams
  48. };