get-changed-params.js 1.4 KB

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