-
-
Notifications
You must be signed in to change notification settings - Fork 776
Expand file tree
/
Copy pathutils.js
More file actions
145 lines (123 loc) · 4.16 KB
/
utils.js
File metadata and controls
145 lines (123 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { findDOMNode } from 'react-dom';
import keyCode from 'rc-util/lib/KeyCode';
const MAX_PRECISION_FOR_OPERATIONS = 15;
export function isEventFromHandle(e, handles) {
try {
return Object.keys(handles)
.some(key => e.target === findDOMNode(handles[key]));
} catch(error) {
return false;
}
}
export function isValueOutOfRange(value, { min, max }) {
return value < min || value > max;
}
export function isNotTouchEvent(e) {
return e.touches.length > 1 ||
(e.type.toLowerCase() === 'touchend' && e.touches.length > 0);
}
export function getPrecision(step) {
const stepString = step.toString();
let precision = 0;
if (stepString.indexOf('.') >= 0) {
precision = stepString.length - stepString.indexOf('.') - 1;
}
return precision;
}
function withPrecision(value, precision) {
return parseFloat(value.toFixed(precision));
}
// safeDivideBy and safeMultiply: if either term is a float,
// then round the result to the combined precision
function safeDivideBy(a, b) {
const precision = Math.min(
getPrecision(a) + getPrecision(b),
MAX_PRECISION_FOR_OPERATIONS
);
return precision === 0 ? a / b : withPrecision(a / b, precision);
}
function safeMultiply(a, b) {
const precision = Math.min(
getPrecision(a) + getPrecision(b),
MAX_PRECISION_FOR_OPERATIONS
);
return withPrecision(a * b, precision);
}
export function getClosestPoint(val, { marks, step, min, max }) {
const points = Object.keys(marks).map(parseFloat);
if (step !== null) {
const maxSteps = Math.floor(safeDivideBy(max - min, step));
const steps = Math.min(safeDivideBy(val - min, step), maxSteps);
const closestStep =
safeMultiply(Math.round(steps), step) + min;
points.push(closestStep);
}
const diffs = points.map(point => Math.abs(val - point));
return points[diffs.indexOf(Math.min(...diffs))];
}
export function getMousePosition(vertical, e) {
return vertical ? e.clientY : e.pageX;
}
export function getTouchPosition(vertical, e) {
return vertical ? e.touches[0].clientY : e.touches[0].pageX;
}
export function getHandleCenterPosition(vertical, handle) {
const coords = handle.getBoundingClientRect();
return vertical ?
coords.top + (coords.height * 0.5) :
window.pageXOffset + coords.left + (coords.width * 0.5);
}
export function ensureValueInRange(val, { max, min }) {
if (val <= min) {
return min;
}
if (val >= max) {
return max;
}
return val;
}
export function ensureValuePrecision(val, props) {
const { step } = props;
const closestPoint = isFinite(getClosestPoint(val, props)) ? getClosestPoint(val, props) : 0; // eslint-disable-line
return step === null ? closestPoint :
withPrecision(closestPoint, getPrecision(step));
}
export function pauseEvent(e) {
e.stopPropagation();
e.preventDefault();
}
export function calculateNextValue(func, value, props) {
const operations = {
increase: (a, b) => a + b,
decrease: (a, b) => a - b,
};
const indexToGet = operations[func](Object.keys(props.marks).indexOf(JSON.stringify(value)), 1);
const keyToGet = Object.keys(props.marks)[indexToGet];
if (props.step) {
return operations[func](value, props.step);
} else if (!!Object.keys(props.marks).length && !!props.marks[keyToGet]) {
return props.marks[keyToGet];
}
return value;
}
export function getKeyboardValueMutator(e, vertical, reverse) {
const increase = 'increase';
const decrease = 'decrease';
let method = increase;
switch (e.keyCode) {
case keyCode.UP:
method = vertical && reverse ? decrease: increase; break;
case keyCode.RIGHT:
method = !vertical && reverse ? decrease: increase; break;
case keyCode.DOWN:
method = vertical && reverse ? increase: decrease; break;
case keyCode.LEFT:
method = !vertical && reverse ? increase: decrease; break;
case keyCode.END: return (value, props) => props.max;
case keyCode.HOME: return (value, props) => props.min;
case keyCode.PAGE_UP: return (value, props) => value + props.step * 2;
case keyCode.PAGE_DOWN: return (value, props) => value - props.step * 2;
default: return undefined;
}
return (value, props) => calculateNextValue(method, value, props);
}