get-changed-params.js 1.4 KB

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