You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

150 line
4.0 KiB

  1. import _toConsumableArray from 'babel-runtime/helpers/toConsumableArray';
  2. import { findDOMNode } from 'react-dom';
  3. import keyCode from 'rc-util/es/KeyCode';
  4. export function isEventFromHandle(e, handles) {
  5. try {
  6. return Object.keys(handles).some(function (key) {
  7. return e.target === findDOMNode(handles[key]);
  8. });
  9. } catch (error) {
  10. return false;
  11. }
  12. }
  13. export function isValueOutOfRange(value, _ref) {
  14. var min = _ref.min,
  15. max = _ref.max;
  16. return value < min || value > max;
  17. }
  18. export function isNotTouchEvent(e) {
  19. return e.touches.length > 1 || e.type.toLowerCase() === 'touchend' && e.touches.length > 0;
  20. }
  21. export function getClosestPoint(val, _ref2) {
  22. var marks = _ref2.marks,
  23. step = _ref2.step,
  24. min = _ref2.min,
  25. max = _ref2.max;
  26. var points = Object.keys(marks).map(parseFloat);
  27. if (step !== null) {
  28. var maxSteps = Math.floor((max - min) / step);
  29. var steps = Math.min((val - min) / step, maxSteps);
  30. var closestStep = Math.round(steps) * step + min;
  31. points.push(closestStep);
  32. }
  33. var diffs = points.map(function (point) {
  34. return Math.abs(val - point);
  35. });
  36. return points[diffs.indexOf(Math.min.apply(Math, _toConsumableArray(diffs)))];
  37. }
  38. export function getPrecision(step) {
  39. var stepString = step.toString();
  40. var precision = 0;
  41. if (stepString.indexOf('.') >= 0) {
  42. precision = stepString.length - stepString.indexOf('.') - 1;
  43. }
  44. return precision;
  45. }
  46. export function getMousePosition(vertical, e) {
  47. return vertical ? e.clientY : e.pageX;
  48. }
  49. export function getTouchPosition(vertical, e) {
  50. return vertical ? e.touches[0].clientY : e.touches[0].pageX;
  51. }
  52. export function getHandleCenterPosition(vertical, handle) {
  53. var coords = handle.getBoundingClientRect();
  54. return vertical ? coords.top + coords.height * 0.5 : window.pageXOffset + coords.left + coords.width * 0.5;
  55. }
  56. export function ensureValueInRange(val, _ref3) {
  57. var max = _ref3.max,
  58. min = _ref3.min;
  59. if (val <= min) {
  60. return min;
  61. }
  62. if (val >= max) {
  63. return max;
  64. }
  65. return val;
  66. }
  67. export function ensureValuePrecision(val, props) {
  68. var step = props.step;
  69. var closestPoint = isFinite(getClosestPoint(val, props)) ? getClosestPoint(val, props) : 0; // eslint-disable-line
  70. return step === null ? closestPoint : parseFloat(closestPoint.toFixed(getPrecision(step)));
  71. }
  72. export function pauseEvent(e) {
  73. e.stopPropagation();
  74. e.preventDefault();
  75. }
  76. export function calculateNextValue(func, value, props) {
  77. var operations = {
  78. increase: function increase(a, b) {
  79. return a + b;
  80. },
  81. decrease: function decrease(a, b) {
  82. return a - b;
  83. }
  84. };
  85. var indexToGet = operations[func](Object.keys(props.marks).indexOf(JSON.stringify(value)), 1);
  86. var keyToGet = Object.keys(props.marks)[indexToGet];
  87. if (props.step) {
  88. return operations[func](value, props.step);
  89. } else if (!!Object.keys(props.marks).length && !!props.marks[keyToGet]) {
  90. return props.marks[keyToGet];
  91. }
  92. return value;
  93. }
  94. export function getKeyboardValueMutator(e, vertical, reverse) {
  95. var increase = 'increase';
  96. var decrease = 'decrease';
  97. var method = increase;
  98. switch (e.keyCode) {
  99. case keyCode.UP:
  100. method = vertical && reverse ? decrease : increase;break;
  101. case keyCode.RIGHT:
  102. method = !vertical && reverse ? decrease : increase;break;
  103. case keyCode.DOWN:
  104. method = vertical && reverse ? increase : decrease;break;
  105. case keyCode.LEFT:
  106. method = !vertical && reverse ? increase : decrease;break;
  107. case keyCode.END:
  108. return function (value, props) {
  109. return props.max;
  110. };
  111. case keyCode.HOME:
  112. return function (value, props) {
  113. return props.min;
  114. };
  115. case keyCode.PAGE_UP:
  116. return function (value, props) {
  117. return value + props.step * 2;
  118. };
  119. case keyCode.PAGE_DOWN:
  120. return function (value, props) {
  121. return value - props.step * 2;
  122. };
  123. default:
  124. return undefined;
  125. }
  126. return function (value, props) {
  127. return calculateNextValue(method, value, props);
  128. };
  129. }