util.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import _typeof from 'babel-runtime/helpers/typeof';
  2. import contains from '../vc-util/Dom/contains';
  3. export function buffer(fn, ms) {
  4. var timer = void 0;
  5. function clear() {
  6. if (timer) {
  7. clearTimeout(timer);
  8. timer = null;
  9. }
  10. }
  11. function bufferFn() {
  12. clear();
  13. timer = setTimeout(fn, ms);
  14. }
  15. bufferFn.clear = clear;
  16. return bufferFn;
  17. }
  18. export function isSamePoint(prev, next) {
  19. if (prev === next) return true;
  20. if (!prev || !next) return false;
  21. if ('pageX' in next && 'pageY' in next) {
  22. return prev.pageX === next.pageX && prev.pageY === next.pageY;
  23. }
  24. if ('clientX' in next && 'clientY' in next) {
  25. return prev.clientX === next.clientX && prev.clientY === next.clientY;
  26. }
  27. return false;
  28. }
  29. export function isWindow(obj) {
  30. return obj && (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object' && obj.window === obj;
  31. }
  32. export function isSimilarValue(val1, val2) {
  33. var int1 = Math.floor(val1);
  34. var int2 = Math.floor(val2);
  35. return Math.abs(int1 - int2) <= 1;
  36. }
  37. export function restoreFocus(activeElement, container) {
  38. // Focus back if is in the container
  39. if (activeElement !== document.activeElement && contains(container, activeElement)) {
  40. activeElement.focus();
  41. }
  42. }