1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import { __assign, __extends } from "tslib";
|
17 | import classNames from "classnames";
|
18 | import * as React from "react";
|
19 | import { AbstractPureComponent, Classes, Utils } from "../../common";
|
20 | import { DISPLAYNAME_PREFIX } from "../../common/props";
|
21 | import { clamp } from "../../common/utils";
|
22 | import { formatPercentage } from "./sliderUtils";
|
23 |
|
24 | var NUMBER_PROPS = ["max", "min", "stepSize", "tickSize", "value"];
|
25 |
|
26 | var Handle = (function (_super) {
|
27 | __extends(Handle, _super);
|
28 | function Handle() {
|
29 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
30 | _this.state = {
|
31 | isMoving: false,
|
32 | };
|
33 | _this.handleElement = null;
|
34 | _this.refHandlers = {
|
35 | handle: function (el) { return (_this.handleElement = el); },
|
36 | };
|
37 | _this.beginHandleMovement = function (event) {
|
38 | document.addEventListener("mousemove", _this.handleHandleMovement);
|
39 | document.addEventListener("mouseup", _this.endHandleMovement);
|
40 | _this.setState({ isMoving: true });
|
41 | _this.changeValue(_this.clientToValue(_this.mouseEventClientOffset(event)));
|
42 | };
|
43 | _this.beginHandleTouchMovement = function (event) {
|
44 | document.addEventListener("touchmove", _this.handleHandleTouchMovement);
|
45 | document.addEventListener("touchend", _this.endHandleTouchMovement);
|
46 | document.addEventListener("touchcancel", _this.endHandleTouchMovement);
|
47 | _this.setState({ isMoving: true });
|
48 | _this.changeValue(_this.clientToValue(_this.touchEventClientOffset(event)));
|
49 | };
|
50 | _this.getStyleProperties = function () {
|
51 | if (_this.handleElement == null) {
|
52 | return {};
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | var _a = _this.props, _b = _a.min, min = _b === void 0 ? 0 : _b, tickSizeRatio = _a.tickSizeRatio, value = _a.value, vertical = _a.vertical;
|
60 | var handleMidpoint = _this.getHandleMidpointAndOffset(_this.handleElement, true).handleMidpoint;
|
61 | var offsetRatio = (value - min) * tickSizeRatio;
|
62 | var offsetCalc = "calc(".concat(formatPercentage(offsetRatio), " - ").concat(handleMidpoint, "px)");
|
63 | return vertical ? { bottom: offsetCalc } : { left: offsetCalc };
|
64 | };
|
65 | _this.endHandleMovement = function (event) {
|
66 | _this.handleMoveEndedAt(_this.mouseEventClientOffset(event));
|
67 | };
|
68 | _this.endHandleTouchMovement = function (event) {
|
69 | _this.handleMoveEndedAt(_this.touchEventClientOffset(event));
|
70 | };
|
71 | _this.handleMoveEndedAt = function (clientPixel) {
|
72 | var _a, _b;
|
73 | _this.removeDocumentEventListeners();
|
74 | _this.setState({ isMoving: false });
|
75 |
|
76 | var finalValue = _this.changeValue(_this.clientToValue(clientPixel));
|
77 | (_b = (_a = _this.props).onRelease) === null || _b === void 0 ? void 0 : _b.call(_a, finalValue);
|
78 | };
|
79 | _this.handleHandleMovement = function (event) {
|
80 | _this.handleMovedTo(_this.mouseEventClientOffset(event));
|
81 | };
|
82 | _this.handleHandleTouchMovement = function (event) {
|
83 | _this.handleMovedTo(_this.touchEventClientOffset(event));
|
84 | };
|
85 | _this.handleMovedTo = function (clientPixel) {
|
86 | if (_this.state.isMoving && !_this.props.disabled) {
|
87 | _this.changeValue(_this.clientToValue(clientPixel));
|
88 | }
|
89 | };
|
90 | _this.handleKeyDown = function (event) {
|
91 | var _a = _this.props, stepSize = _a.stepSize, value = _a.value;
|
92 | var key = event.key;
|
93 | if (key === "ArrowDown" || key === "ArrowLeft") {
|
94 | _this.changeValue(value - stepSize);
|
95 |
|
96 | event.preventDefault();
|
97 | }
|
98 | else if (key === "ArrowUp" || key === "ArrowRight") {
|
99 | _this.changeValue(value + stepSize);
|
100 | event.preventDefault();
|
101 | }
|
102 | };
|
103 | _this.handleKeyUp = function (event) {
|
104 | var _a, _b;
|
105 | if (Utils.isArrowKey(event)) {
|
106 | (_b = (_a = _this.props).onRelease) === null || _b === void 0 ? void 0 : _b.call(_a, _this.props.value);
|
107 | }
|
108 | };
|
109 | return _this;
|
110 | }
|
111 | Handle.prototype.componentDidMount = function () {
|
112 |
|
113 |
|
114 | this.forceUpdate();
|
115 | };
|
116 | Handle.prototype.render = function () {
|
117 | var _a;
|
118 | var _b = this.props, className = _b.className, disabled = _b.disabled, label = _b.label, min = _b.min, max = _b.max, value = _b.value, vertical = _b.vertical, htmlProps = _b.htmlProps;
|
119 | var isMoving = this.state.isMoving;
|
120 | return (React.createElement("span", __assign({ role: "slider", tabIndex: 0 }, htmlProps, { className: classNames(Classes.SLIDER_HANDLE, (_a = {}, _a[Classes.ACTIVE] = isMoving, _a), className), onKeyDown: disabled ? undefined : this.handleKeyDown, onKeyUp: disabled ? undefined : this.handleKeyUp, onMouseDown: disabled ? undefined : this.beginHandleMovement, onTouchStart: disabled ? undefined : this.beginHandleTouchMovement, ref: this.refHandlers.handle, style: this.getStyleProperties(), "aria-valuemin": min, "aria-valuemax": max, "aria-valuenow": value, "aria-orientation": vertical ? "vertical" : "horizontal" }), label == null ? null : React.createElement("span", { className: Classes.SLIDER_LABEL }, label)));
|
121 | };
|
122 | Handle.prototype.componentWillUnmount = function () {
|
123 | this.removeDocumentEventListeners();
|
124 | };
|
125 |
|
126 | Handle.prototype.clientToValue = function (clientPixel) {
|
127 | var _a = this.props, stepSize = _a.stepSize, tickSize = _a.tickSize, value = _a.value, vertical = _a.vertical;
|
128 | if (this.handleElement == null) {
|
129 | return value;
|
130 | }
|
131 |
|
132 |
|
133 | var clientPixelNormalized = vertical ? window.innerHeight - clientPixel : clientPixel;
|
134 | var handleCenterPixel = this.getHandleElementCenterPixel(this.handleElement);
|
135 | var pixelDelta = clientPixelNormalized - handleCenterPixel;
|
136 | if (isNaN(pixelDelta)) {
|
137 | return value;
|
138 | }
|
139 |
|
140 | return value + Math.round(pixelDelta / (tickSize * stepSize)) * stepSize;
|
141 | };
|
142 | Handle.prototype.mouseEventClientOffset = function (event) {
|
143 | return this.props.vertical ? event.clientY : event.clientX;
|
144 | };
|
145 | Handle.prototype.touchEventClientOffset = function (event) {
|
146 | var touch = event.changedTouches[0];
|
147 | return this.props.vertical ? touch.clientY : touch.clientX;
|
148 | };
|
149 | Handle.prototype.validateProps = function (props) {
|
150 | for (var _i = 0, NUMBER_PROPS_1 = NUMBER_PROPS; _i < NUMBER_PROPS_1.length; _i++) {
|
151 | var prop = NUMBER_PROPS_1[_i];
|
152 | if (typeof props[prop] !== "number") {
|
153 | throw new Error("[Blueprint] <Handle> requires number value for ".concat(prop, " prop"));
|
154 | }
|
155 | }
|
156 | };
|
157 |
|
158 | Handle.prototype.changeValue = function (newValue, callback) {
|
159 | if (callback === void 0) { callback = this.props.onChange; }
|
160 | newValue = this.clamp(newValue);
|
161 | if (!isNaN(newValue) && this.props.value !== newValue) {
|
162 | callback === null || callback === void 0 ? void 0 : callback(newValue);
|
163 | }
|
164 | return newValue;
|
165 | };
|
166 |
|
167 | Handle.prototype.clamp = function (value) {
|
168 | return clamp(value, this.props.min, this.props.max);
|
169 | };
|
170 | Handle.prototype.getHandleElementCenterPixel = function (handleElement) {
|
171 | var _a = this.getHandleMidpointAndOffset(handleElement), handleMidpoint = _a.handleMidpoint, handleOffset = _a.handleOffset;
|
172 | return handleOffset + handleMidpoint;
|
173 | };
|
174 | Handle.prototype.getHandleMidpointAndOffset = function (handleElement, useOppositeDimension) {
|
175 | if (useOppositeDimension === void 0) { useOppositeDimension = false; }
|
176 | if (handleElement == null) {
|
177 | return { handleMidpoint: 0, handleOffset: 0 };
|
178 | }
|
179 | var vertical = this.props.vertical;
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | var handleRect = handleElement.getBoundingClientRect();
|
185 | handleRect.width = handleElement.offsetWidth;
|
186 | handleRect.height = handleElement.offsetHeight;
|
187 | var sizeKey = vertical
|
188 | ? useOppositeDimension
|
189 | ? "width"
|
190 | : "height"
|
191 | : useOppositeDimension
|
192 | ? "height"
|
193 | : "width";
|
194 |
|
195 |
|
196 | var handleOffset = vertical ? window.innerHeight - (handleRect.top + handleRect[sizeKey]) : handleRect.left;
|
197 | return { handleMidpoint: handleRect[sizeKey] / 2, handleOffset: handleOffset };
|
198 | };
|
199 | Handle.prototype.removeDocumentEventListeners = function () {
|
200 | document.removeEventListener("mousemove", this.handleHandleMovement);
|
201 | document.removeEventListener("mouseup", this.endHandleMovement);
|
202 | document.removeEventListener("touchmove", this.handleHandleTouchMovement);
|
203 | document.removeEventListener("touchend", this.endHandleTouchMovement);
|
204 | document.removeEventListener("touchcancel", this.endHandleTouchMovement);
|
205 | };
|
206 | Handle.displayName = "".concat(DISPLAYNAME_PREFIX, ".SliderHandle");
|
207 | return Handle;
|
208 | }(AbstractPureComponent));
|
209 | export { Handle };
|
210 |
|
\ | No newline at end of file |