1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | };
|
9 | return function (d, b) {
|
10 | extendStatics(d, b);
|
11 | function __() { this.constructor = d; }
|
12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
13 | };
|
14 | })();
|
15 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
16 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
17 | };
|
18 | var __importStar = (this && this.__importStar) || function (mod) {
|
19 | if (mod && mod.__esModule) return mod;
|
20 | var result = {};
|
21 | if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
22 | result["default"] = mod;
|
23 | return result;
|
24 | };
|
25 | Object.defineProperty(exports, "__esModule", { value: true });
|
26 | var withStyles_1 = __importDefault(require("@material-ui/core/styles/withStyles"));
|
27 | var PropTypes = __importStar(require("prop-types"));
|
28 | var React = __importStar(require("react"));
|
29 | var createStyles_1 = __importDefault(require("@material-ui/core/styles/createStyles"));
|
30 | var time_utils_1 = require("../../_helpers/time-utils");
|
31 | var ClockType_1 = __importDefault(require("../../constants/ClockType"));
|
32 | var ClockPointer_1 = __importDefault(require("./ClockPointer"));
|
33 | var Clock = (function (_super) {
|
34 | __extends(Clock, _super);
|
35 | function Clock() {
|
36 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
37 | _this.isMoving = false;
|
38 | _this.handleTouchMove = function (e) {
|
39 | _this.isMoving = true;
|
40 | _this.setTime(e);
|
41 | };
|
42 | _this.handleTouchEnd = function (e) {
|
43 | if (_this.isMoving) {
|
44 | _this.setTime(e, true);
|
45 | _this.isMoving = false;
|
46 | }
|
47 | };
|
48 | _this.handleMove = function (e) {
|
49 | e.preventDefault();
|
50 | e.stopPropagation();
|
51 |
|
52 | var isButtonPressed = typeof e.buttons === 'undefined' ? e.nativeEvent.which === 1 : e.buttons === 1;
|
53 | if (isButtonPressed) {
|
54 | _this.setTime(e.nativeEvent, false);
|
55 | }
|
56 | };
|
57 | _this.handleMouseUp = function (e) {
|
58 | if (_this.isMoving) {
|
59 | _this.isMoving = false;
|
60 | }
|
61 | _this.setTime(e.nativeEvent, true);
|
62 | };
|
63 | _this.hasSelected = function () {
|
64 | var _a = _this.props, type = _a.type, value = _a.value;
|
65 | if (type === ClockType_1.default.HOURS) {
|
66 | return true;
|
67 | }
|
68 | return value % 5 === 0;
|
69 | };
|
70 | return _this;
|
71 | }
|
72 | Clock.prototype.setTime = function (e, isFinish) {
|
73 | if (isFinish === void 0) { isFinish = false; }
|
74 | var offsetX = e.offsetX, offsetY = e.offsetY;
|
75 | if (typeof offsetX === 'undefined') {
|
76 | var rect = e.target.getBoundingClientRect();
|
77 | offsetX = e.changedTouches[0].clientX - rect.left;
|
78 | offsetY = e.changedTouches[0].clientY - rect.top;
|
79 | }
|
80 | var value = this.props.type === ClockType_1.default.SECONDS || this.props.type === ClockType_1.default.MINUTES
|
81 | ? time_utils_1.getMinutes(offsetX, offsetY, this.props.minutesStep)
|
82 | : time_utils_1.getHours(offsetX, offsetY, Boolean(this.props.ampm));
|
83 | this.props.onChange(value, isFinish);
|
84 | };
|
85 | Clock.prototype.render = function () {
|
86 | var _a = this.props, classes = _a.classes, value = _a.value, children = _a.children, type = _a.type, ampm = _a.ampm;
|
87 | var isPointerInner = !ampm && type === ClockType_1.default.HOURS && (value < 1 || value > 12);
|
88 | return (React.createElement("div", { className: classes.container },
|
89 | React.createElement("div", { className: classes.clock },
|
90 | React.createElement("div", { role: "menu", tabIndex: -1, className: classes.squareMask, onTouchMove: this.handleTouchMove, onTouchEnd: this.handleTouchEnd, onMouseUp: this.handleMouseUp, onMouseMove: this.handleMove }),
|
91 | React.createElement("div", { className: classes.pin }),
|
92 | React.createElement(ClockPointer_1.default, { type: type, value: value, isInner: isPointerInner, hasSelected: this.hasSelected() }),
|
93 | children)));
|
94 | };
|
95 | Clock.propTypes = {
|
96 | type: PropTypes.oneOf(Object.keys(ClockType_1.default).map(function (key) { return ClockType_1.default[key]; })).isRequired,
|
97 | value: PropTypes.number.isRequired,
|
98 | onChange: PropTypes.func.isRequired,
|
99 | children: PropTypes.arrayOf(PropTypes.node).isRequired,
|
100 | ampm: PropTypes.bool,
|
101 | minutesStep: PropTypes.number,
|
102 | innerRef: PropTypes.any,
|
103 | };
|
104 | Clock.defaultProps = {
|
105 | ampm: false,
|
106 | minutesStep: 1,
|
107 | };
|
108 | return Clock;
|
109 | }(React.Component));
|
110 | exports.Clock = Clock;
|
111 | exports.styles = function (theme) {
|
112 | return createStyles_1.default({
|
113 | container: {
|
114 | display: 'flex',
|
115 | justifyContent: 'center',
|
116 | alignItems: 'flex-end',
|
117 | margin: theme.spacing.unit * 4 + "px 0 " + theme.spacing.unit + "px",
|
118 | },
|
119 | clock: {
|
120 | backgroundColor: 'rgba(0,0,0,.07)',
|
121 | borderRadius: '50%',
|
122 | height: 260,
|
123 | width: 260,
|
124 | position: 'relative',
|
125 | pointerEvents: 'none',
|
126 | zIndex: 1,
|
127 | },
|
128 | squareMask: {
|
129 | width: '100%',
|
130 | height: '100%',
|
131 | position: 'absolute',
|
132 | pointerEvents: 'auto',
|
133 | outline: 'none',
|
134 | touchActions: 'none',
|
135 | userSelect: 'none',
|
136 | '&:active': {
|
137 | cursor: 'move',
|
138 | },
|
139 | },
|
140 | pin: {
|
141 | width: 6,
|
142 | height: 6,
|
143 | borderRadius: '50%',
|
144 | backgroundColor: theme.palette.primary.main,
|
145 | position: 'absolute',
|
146 | top: '50%',
|
147 | left: '50%',
|
148 | transform: 'translate(-50%, -50%)',
|
149 | },
|
150 | });
|
151 | };
|
152 | exports.default = withStyles_1.default(exports.styles, {
|
153 | name: 'MuiPickersClock',
|
154 | })(Clock);
|