-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsource.js
More file actions
266 lines (224 loc) · 9.31 KB
/
source.js
File metadata and controls
266 lines (224 loc) · 9.31 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
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
"use strict";
(function (global, factory) {
if (typeof exports === "object" && typeof module !== "undefined") module.exports = factory();
if (typeof define === "function" && define.amd) define(factory);
(global || window).AReactTimepicker = factory();
})(this, function () {
var React = typeof require === "function" ? require("react") : window.React;
var CLOCK_SIZE = 222;
var TimePicker = React.createClass({
getInitialState: function getInitialState() {
return {
visible: false,
hour: 12,
minute: 0,
am: true,
position: {
top: 0,
left: 0
}
};
},
componentWillMount: function componentWillMount() {
document.addEventListener("click", this.hideOnDocumentClick);
},
componentWillUnmount: function componentWillUnmount() {
document.removeEventListener("click", this.hideOnDocumentClick);
},
show: function show() {
var trigger = this.refs.trigger.getDOMNode(),
rect = trigger.getBoundingClientRect(),
isTopHalf = rect.top > window.innerHeight / 2;
this.setState({
visible: true,
position: {
top: isTopHalf ? rect.top + window.scrollY - CLOCK_SIZE - 3 : rect.top + trigger.clientHeight + window.scrollY + 3,
left: rect.left
}
});
},
hide: function hide() {
this.setState({
visible: false
});
},
hideOnDocumentClick: function hideOnDocumentClick(e) {
if (!this.parentsHaveClassName(e.target, "time-picker")) this.hide();
},
parentsHaveClassName: function parentsHaveClassName(element, className) {
var parent = element;
while (parent) {
if (parent.className && parent.className.indexOf(className) > -1) return true;
parent = parent.parentNode;
}
return false;
},
onTimeChanged: function onTimeChanged(hour, minute, am) {
this.setState({
hour: hour,
minute: minute,
am: am
});
},
onDone: function onDone() {
this.hide();
},
render: function render() {
return <div className="time-picker">
<input ref="trigger" type="text" readOnly="true" value={_getTimeString(this.state.hour, this.state.minute, this.state.am)} onClick={this.show} />
<Clock visible={this.state.visible} position={this.state.position} onTimeChanged={this.onTimeChanged} onDone={this.onDone} hour={this.state.hour} minute={this.state.minute} am={this.state.am} />
</div>;
}
});
var Clock = React.createClass({
getInitialState: function getInitialState() {
return {
hoursVisible: true,
minutesVisible: false,
position: "below"
};
},
componentWillReceiveProps: function componentWillReceiveProps(props) {
if (this.props.visible && !props.visible) this.setState({
hoursVisible: true,
minutesVisible: false,
amPmVisible: false
});
},
getTime: function getTime() {
return {
hour: this.props.hour,
minute: this.props.minute,
am: this.props.am
};
},
onHourChanged: function onHourChanged(hour) {
this._hour = hour;
this.setState({
hoursVisible: false,
minutesVisible: true
});
},
onHoursHidden: function onHoursHidden() {
this.props.onTimeChanged(this._hour, this.props.minute, this.props.am);
},
onMinuteChanged: function onMinuteChanged(minute) {
this.props.onDone();
this._minute = minute;
this.setState({
minutesVisible: false,
amPmVisible: true
});
},
onMinutesHidden: function onMinutesHidden() {
this.props.onTimeChanged(this.props.hour, this._minute, this.props.am);
},
onAmPmChanged: function onAmPmChanged(am) {
this.props.onTimeChanged(this.props.hour, this.props.minute, am);
},
style: function style() {
return {
top: this.props.position.top,
left: this.props.position.left
};
},
render: function render() {
return <div className={"clock " + (this.props.visible ? "clock-show" : "clock-hide")} style={this.style()}>
<div className="clock-face-wrapper">
<Hours visible={this.state.hoursVisible} time={this.getTime()} onClick={this.onHourChanged} onHidden={this.onHoursHidden} />
<Minutes visible={this.state.minutesVisible} time={this.getTime()} onClick={this.onMinuteChanged} onHidden={this.onMinutesHidden} />
</div>
<AmPmInfo time={this.getTime()} onChange={this.onAmPmChanged} />
</div>;
}
});
var AmPmInfo = React.createClass({
render: function render() {
var time = this.props.time;
return <div className="am-pm-info">
<div className={"am" + (time.am ? " selected" : "")} onClick={this.props.onChange.bind(null, true)}>AM</div>
<div className="time">{_getTimeString(time.hour, time.minute, time.am)}</div>
<div className={"pm" + (!time.am ? " selected" : "")} onClick={this.props.onChange.bind(null, false)}>PM</div>
</div>;
}
});
var Hours = React.createClass({
buildHours: function buildHours() {
var hours = [];
for (var i = 1; i <= 12; i++) hours.push(i);
return hours;
},
render: function() {
var { time, ...props } = this.props;
return <Face {...props} type="hours" values={this.buildHours()} selected={this.props.time.hour} />;
}
});
var Minutes = React.createClass({
buildMinutes: function buildMinutes() {
var minutes = [];
for (var i = 1; i <= 12; i++) minutes.push(_pad((i === 12 ? 0 : i) * 5));
return minutes;
},
render: function() {
var { time, ...props } = this.props;
return <Face {...props} type="minutes" values={this.buildMinutes()} selected={this.props.time.minute} />;
}
});
var Face = React.createClass({
componentDidMount: function componentDidMount() {
this.refs.face.getDOMNode().addEventListener("transitionend", this.onTransitionEnd);
},
componentWillUnmount: function componentWillUnmount() {
this.refs.face.getDOMNode().removeEventListener("transitionend", this.onTransitionEnd);
},
onTransitionEnd: function onTransitionEnd(e) {
if (e.propertyName === "opacity" && e.target.className.indexOf("face-hide") > -1) this.props.onHidden();
},
render: function render() {
return <div ref="face" className={"face " + this.props.type + (this.props.visible ? " face-show" : " face-hide")}>
{this.props.values.map(function(value, i) {
return <div key={i} className={"position position-" + (i + 1) + (parseInt(this.props.selected) === parseInt(value) ? " selected" : "")} onClick={this.props.onClick.bind(null, value)}>
{_pad(value)}
</div>
}.bind(this))}
<LongHand type={this.props.type} selected={this.props.selected} />
<div className="inner-face"></div>
<Ticks />
</div>;
}
});
var LongHand = React.createClass({
render: function render() {
var deg = (this.props.selected / (this.props.type === "hours" ? 12 : 60) * 360);
return <div>
<div className="long-hand" style={{ transform: "rotate(" + deg + "deg) scale(1, 0.8)", WebkitTransform: "rotate(" + deg + "deg) scale(1, 0.8)" }}></div>
<div className="long-hand-attachment"></div>
</div>;
}
});
var Ticks = React.createClass({
buildTick: function buildTick(index) {
return React.createElement(
"div",
{ key: index, className: "tick " + (index % 5 === 0 ? "big " : ""), style: { transform: "rotate(" + index * 6 + "deg)", WebkitTransform: "rotate(" + index * 6 + "deg)" } },
React.createElement("div", null)
);
},
render: function render() {
var ticks = [];
for (var i = 0; i < 60; i++)
ticks.push(this.buildTick(i));
return <div className="ticks">
{ticks}
</div>;
}
});
function _pad(value) {
value = value.toString();
return value.length === 1 ? "0" + value : value;
}
function _getTimeString(hour, minute, am) {
return hour + ":" + _pad(minute) + " " + (am ? "AM" : "PM");
}
return TimePicker;
});