/
Range.js
419 lines (372 loc) · 13 KB
/
Range.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _toConsumableArray from 'babel-runtime/helpers/toConsumableArray';
import _extends from 'babel-runtime/helpers/extends';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
/* eslint-disable react/prop-types */
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import shallowEqual from 'shallowequal';
import warning from 'warning';
import Track from './common/Track';
import createSlider from './common/createSlider';
import * as utils from './utils';
var Range = function (_React$Component) {
_inherits(Range, _React$Component);
function Range(props) {
_classCallCheck(this, Range);
var _this = _possibleConstructorReturn(this, (Range.__proto__ || Object.getPrototypeOf(Range)).call(this, props));
_this.onEnd = function () {
_this.setState({ handle: null });
_this.removeDocumentEvents();
_this.props.onAfterChange(_this.getValue());
};
var count = props.count,
min = props.min,
max = props.max;
var initialValue = Array.apply(null, Array(count + 1)).map(function () {
return min;
});
var defaultValue = 'defaultValue' in props ? props.defaultValue : initialValue;
var value = props.value !== undefined ? props.value : defaultValue;
var bounds = value.map(function (v) {
return _this.trimAlignValue(v);
});
var recent = bounds[0] === max ? 0 : bounds.length - 1;
_this.state = {
handle: null,
recent: recent,
bounds: bounds
};
return _this;
}
_createClass(Range, [{
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
var _this2 = this;
if (!('value' in nextProps || 'min' in nextProps || 'max' in nextProps)) return;
if (this.props.min === nextProps.min && this.props.max === nextProps.max && shallowEqual(this.props.value, nextProps.value)) {
return;
}
var bounds = this.state.bounds;
var value = nextProps.value || bounds;
var nextBounds = value.map(function (v) {
return _this2.trimAlignValue(v, nextProps);
});
if (nextBounds.length === bounds.length && nextBounds.every(function (v, i) {
return v === bounds[i];
})) return;
this.setState({ bounds: nextBounds });
if (bounds.some(function (v) {
return utils.isValueOutOfRange(v, nextProps);
})) {
this.props.onChange(nextBounds);
}
}
}, {
key: 'onChange',
value: function onChange(state) {
var props = this.props;
var isNotControlled = !('value' in props);
if (isNotControlled) {
this.setState(state);
} else if (state.handle !== undefined) {
this.setState({ handle: state.handle });
}
var data = _extends({}, this.state, state);
var changedValue = data.bounds;
props.onChange(changedValue);
}
}, {
key: 'onStart',
value: function onStart(position) {
var props = this.props;
var state = this.state;
var bounds = this.getValue();
props.onBeforeChange(bounds);
var value = this.calcValueByPos(position);
this.startValue = value;
this.startPosition = position;
var closestBound = this.getClosestBound(value);
var boundNeedMoving = this.getBoundNeedMoving(value, closestBound);
this.setState({
handle: boundNeedMoving,
recent: boundNeedMoving
});
var prevValue = bounds[boundNeedMoving];
if (value === prevValue) return;
var nextBounds = [].concat(_toConsumableArray(state.bounds));
nextBounds[boundNeedMoving] = value;
this.onChange({ bounds: nextBounds });
}
}, {
key: 'onMove',
value: function onMove(e, position) {
utils.pauseEvent(e);
var props = this.props;
var state = this.state;
var value = this.calcValueByPos(position);
var oldValue = state.bounds[state.handle];
if (value === oldValue) return;
var nextBounds = [].concat(_toConsumableArray(state.bounds));
nextBounds[state.handle] = value;
var nextHandle = state.handle;
if (props.pushable !== false) {
var originalValue = state.bounds[nextHandle];
this.pushSurroundingHandles(nextBounds, nextHandle, originalValue);
} else if (props.allowCross) {
nextBounds.sort(function (a, b) {
return a - b;
});
nextHandle = nextBounds.indexOf(value);
}
this.onChange({
handle: nextHandle,
bounds: nextBounds
});
}
}, {
key: 'onKeyboard',
value: function onKeyboard() {
warning(true, 'Keyboard support is not yet supported for ranges.');
}
}, {
key: 'getValue',
value: function getValue() {
return this.state.bounds;
}
}, {
key: 'getClosestBound',
value: function getClosestBound(value) {
var bounds = this.state.bounds;
var closestBound = 0;
for (var i = 1; i < bounds.length - 1; ++i) {
if (value > bounds[i]) {
closestBound = i;
}
}
if (Math.abs(bounds[closestBound + 1] - value) < Math.abs(bounds[closestBound] - value)) {
closestBound = closestBound + 1;
}
return closestBound;
}
}, {
key: 'getBoundNeedMoving',
value: function getBoundNeedMoving(value, closestBound) {
var _state = this.state,
bounds = _state.bounds,
recent = _state.recent;
var boundNeedMoving = closestBound;
var isAtTheSamePoint = bounds[closestBound + 1] === bounds[closestBound];
if (isAtTheSamePoint && bounds[recent] === bounds[closestBound]) {
boundNeedMoving = recent;
}
if (isAtTheSamePoint && value !== bounds[closestBound + 1]) {
boundNeedMoving = value < bounds[closestBound + 1] ? closestBound : closestBound + 1;
}
return boundNeedMoving;
}
}, {
key: 'getLowerBound',
value: function getLowerBound() {
return this.state.bounds[0];
}
}, {
key: 'getUpperBound',
value: function getUpperBound() {
var bounds = this.state.bounds;
return bounds[bounds.length - 1];
}
/**
* Returns an array of possible slider points, taking into account both
* `marks` and `step`. The result is cached.
*/
}, {
key: 'getPoints',
value: function getPoints() {
var _props = this.props,
marks = _props.marks,
step = _props.step,
min = _props.min,
max = _props.max;
var cache = this._getPointsCache;
if (!cache || cache.marks !== marks || cache.step !== step) {
var pointsObject = _extends({}, marks);
if (step !== null) {
for (var point = min; point <= max; point += step) {
pointsObject[point] = point;
}
}
var points = Object.keys(pointsObject).map(parseFloat);
points.sort(function (a, b) {
return a - b;
});
this._getPointsCache = { marks: marks, step: step, points: points };
}
return this._getPointsCache.points;
}
}, {
key: 'pushSurroundingHandles',
value: function pushSurroundingHandles(bounds, handle, originalValue) {
var threshold = this.props.pushable;
var value = bounds[handle];
var direction = 0;
if (bounds[handle + 1] - value < threshold) {
direction = +1; // push to right
}
if (value - bounds[handle - 1] < threshold) {
direction = -1; // push to left
}
if (direction === 0) {
return;
}
var nextHandle = handle + direction;
var diffToNext = direction * (bounds[nextHandle] - value);
if (!this.pushHandle(bounds, nextHandle, direction, threshold - diffToNext)) {
// revert to original value if pushing is impossible
bounds[handle] = originalValue;
}
}
}, {
key: 'pushHandle',
value: function pushHandle(bounds, handle, direction, amount) {
var originalValue = bounds[handle];
var currentValue = bounds[handle];
while (direction * (currentValue - originalValue) < amount) {
if (!this.pushHandleOnePoint(bounds, handle, direction)) {
// can't push handle enough to create the needed `amount` gap, so we
// revert its position to the original value
bounds[handle] = originalValue;
return false;
}
currentValue = bounds[handle];
}
// the handle was pushed enough to create the needed `amount` gap
return true;
}
}, {
key: 'pushHandleOnePoint',
value: function pushHandleOnePoint(bounds, handle, direction) {
var points = this.getPoints();
var pointIndex = points.indexOf(bounds[handle]);
var nextPointIndex = pointIndex + direction;
if (nextPointIndex >= points.length || nextPointIndex < 0) {
// reached the minimum or maximum available point, can't push anymore
return false;
}
var nextHandle = handle + direction;
var nextValue = points[nextPointIndex];
var threshold = this.props.pushable;
var diffToNext = direction * (bounds[nextHandle] - nextValue);
if (!this.pushHandle(bounds, nextHandle, direction, threshold - diffToNext)) {
// couldn't push next handle, so we won't push this one either
return false;
}
// push the handle
bounds[handle] = nextValue;
return true;
}
}, {
key: 'trimAlignValue',
value: function trimAlignValue(v) {
var nextProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var mergedProps = _extends({}, this.props, nextProps);
var valInRange = utils.ensureValueInRange(v, mergedProps);
var valNotConflict = this.ensureValueNotConflict(valInRange, mergedProps);
return utils.ensureValuePrecision(valNotConflict, mergedProps);
}
}, {
key: 'ensureValueNotConflict',
value: function ensureValueNotConflict(val, _ref) {
var allowCross = _ref.allowCross;
var state = this.state || {};
var handle = state.handle,
bounds = state.bounds;
/* eslint-disable eqeqeq */
if (!allowCross && handle != null) {
if (handle > 0 && val <= bounds[handle - 1]) {
return bounds[handle - 1];
}
if (handle < bounds.length - 1 && val >= bounds[handle + 1]) {
return bounds[handle + 1];
}
}
/* eslint-enable eqeqeq */
return val;
}
}, {
key: 'render',
value: function render() {
var _this3 = this;
var _state2 = this.state,
handle = _state2.handle,
bounds = _state2.bounds;
var _props2 = this.props,
prefixCls = _props2.prefixCls,
vertical = _props2.vertical,
included = _props2.included,
disabled = _props2.disabled,
min = _props2.min,
max = _props2.max,
handleGenerator = _props2.handle,
trackStyle = _props2.trackStyle,
handleStyle = _props2.handleStyle;
var offsets = bounds.map(function (v) {
return _this3.calcOffset(v);
});
var handleClassName = prefixCls + '-handle';
var handles = bounds.map(function (v, i) {
var _classNames;
return handleGenerator({
className: classNames((_classNames = {}, _defineProperty(_classNames, handleClassName, true), _defineProperty(_classNames, handleClassName + '-' + (i + 1), true), _classNames)),
vertical: vertical,
offset: offsets[i],
value: v,
dragging: handle === i,
index: i,
min: min,
max: max,
disabled: disabled,
style: handleStyle[i],
ref: function ref(h) {
return _this3.saveHandle(i, h);
}
});
});
var tracks = bounds.slice(0, -1).map(function (_, index) {
var _classNames2;
var i = index + 1;
var trackClassName = classNames((_classNames2 = {}, _defineProperty(_classNames2, prefixCls + '-track', true), _defineProperty(_classNames2, prefixCls + '-track-' + i, true), _classNames2));
return React.createElement(Track, {
className: trackClassName,
vertical: vertical,
included: included,
offset: offsets[i - 1],
length: offsets[i] - offsets[i - 1],
style: trackStyle[index],
key: i
});
});
return { tracks: tracks, handles: handles };
}
}]);
return Range;
}(React.Component);
Range.displayName = 'Range';
Range.propTypes = {
defaultValue: PropTypes.arrayOf(PropTypes.number),
value: PropTypes.arrayOf(PropTypes.number),
count: PropTypes.number,
pushable: PropTypes.oneOfType([PropTypes.bool, PropTypes.number]),
allowCross: PropTypes.bool,
disabled: PropTypes.bool
};
Range.defaultProps = {
count: 1,
allowCross: true,
pushable: false
};
export default createSlider(Range);