util.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _typeof2 = require('babel-runtime/helpers/typeof');
  6. var _typeof3 = _interopRequireDefault(_typeof2);
  7. exports.buffer = buffer;
  8. exports.isSamePoint = isSamePoint;
  9. exports.isWindow = isWindow;
  10. exports.isSimilarValue = isSimilarValue;
  11. exports.restoreFocus = restoreFocus;
  12. var _contains = require('../vc-util/Dom/contains');
  13. var _contains2 = _interopRequireDefault(_contains);
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  15. function buffer(fn, ms) {
  16. var timer = void 0;
  17. function clear() {
  18. if (timer) {
  19. clearTimeout(timer);
  20. timer = null;
  21. }
  22. }
  23. function bufferFn() {
  24. clear();
  25. timer = setTimeout(fn, ms);
  26. }
  27. bufferFn.clear = clear;
  28. return bufferFn;
  29. }
  30. function isSamePoint(prev, next) {
  31. if (prev === next) return true;
  32. if (!prev || !next) return false;
  33. if ('pageX' in next && 'pageY' in next) {
  34. return prev.pageX === next.pageX && prev.pageY === next.pageY;
  35. }
  36. if ('clientX' in next && 'clientY' in next) {
  37. return prev.clientX === next.clientX && prev.clientY === next.clientY;
  38. }
  39. return false;
  40. }
  41. function isWindow(obj) {
  42. return obj && (typeof obj === 'undefined' ? 'undefined' : (0, _typeof3['default'])(obj)) === 'object' && obj.window === obj;
  43. }
  44. function isSimilarValue(val1, val2) {
  45. var int1 = Math.floor(val1);
  46. var int2 = Math.floor(val2);
  47. return Math.abs(int1 - int2) <= 1;
  48. }
  49. function restoreFocus(activeElement, container) {
  50. // Focus back if is in the container
  51. if (activeElement !== document.activeElement && (0, _contains2['default'])(container, activeElement)) {
  52. activeElement.focus();
  53. }
  54. }