1
+ 'use strict' ;
2
+
3
+ Object . defineProperty ( exports , "__esModule" , {
4
+ value : true
5
+ } ) ;
6
+ exports . Stopwatch = undefined ;
7
+
8
+ var _getPrototypeOf = require ( 'babel-runtime/core-js/object/get-prototype-of' ) ;
9
+
10
+ var _getPrototypeOf2 = _interopRequireDefault ( _getPrototypeOf ) ;
11
+
12
+ var _classCallCheck2 = require ( 'babel-runtime/helpers/classCallCheck' ) ;
13
+
14
+ var _classCallCheck3 = _interopRequireDefault ( _classCallCheck2 ) ;
15
+
16
+ var _createClass2 = require ( 'babel-runtime/helpers/createClass' ) ;
17
+
18
+ var _createClass3 = _interopRequireDefault ( _createClass2 ) ;
19
+
20
+ var _possibleConstructorReturn2 = require ( 'babel-runtime/helpers/possibleConstructorReturn' ) ;
21
+
22
+ var _possibleConstructorReturn3 = _interopRequireDefault ( _possibleConstructorReturn2 ) ;
23
+
24
+ var _inherits2 = require ( 'babel-runtime/helpers/inherits' ) ;
25
+
26
+ var _inherits3 = _interopRequireDefault ( _inherits2 ) ;
27
+
28
+ var _react = require ( 'react' ) ;
29
+
30
+ var _react2 = _interopRequireDefault ( _react ) ;
31
+
32
+ var _sprintf = require ( 'sprintf-js/src/sprintf' ) ;
33
+
34
+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
35
+
36
+ var Stopwatch = exports . Stopwatch = function ( _React$Component ) {
37
+ ( 0 , _inherits3 . default ) ( Stopwatch , _React$Component ) ;
38
+
39
+ function Stopwatch ( ) {
40
+ var _ref ;
41
+
42
+ var _temp , _this , _ret ;
43
+
44
+ ( 0 , _classCallCheck3 . default ) ( this , Stopwatch ) ;
45
+
46
+ for ( var _len = arguments . length , args = Array ( _len ) , _key = 0 ; _key < _len ; _key ++ ) {
47
+ args [ _key ] = arguments [ _key ] ;
48
+ }
49
+
50
+ return _ret = ( _temp = ( _this = ( 0 , _possibleConstructorReturn3 . default ) ( this , ( _ref = Stopwatch . __proto__ || ( 0 , _getPrototypeOf2 . default ) ( Stopwatch ) ) . call . apply ( _ref , [ this ] . concat ( args ) ) ) , _this ) , _this . state = {
51
+ elapsed : 0 ,
52
+ startTime : new Date ( ) ,
53
+ timer : null
54
+ } , _this . calculateElapsedSeconds = function ( startTime ) {
55
+ var now = new Date ( ) ;
56
+ var timeDiff = now . getTime ( ) - startTime . getTime ( ) ;
57
+
58
+ return Math . abs ( timeDiff / 1000 ) ;
59
+ } , _this . getTimeDiffString = function ( ) {
60
+ var seconds = Math . floor ( _this . state . elapsed % 60 ) ;
61
+ var minutes = Math . floor ( _this . state . elapsed / 60 ) % 60 ;
62
+ var hours = Math . floor ( _this . state . elapsed / 3600 ) ;
63
+
64
+ var timeString = '' ;
65
+
66
+ if ( hours > 0 ) {
67
+ timeString += ( 0 , _sprintf . sprintf ) ( '%02d' , hours ) + ':' ;
68
+ }
69
+
70
+ timeString += ( 0 , _sprintf . sprintf ) ( '%02d' , minutes ) + ':' + ( 0 , _sprintf . sprintf ) ( '%02d' , seconds ) ;
71
+
72
+ return timeString ;
73
+ } , _temp ) , ( 0 , _possibleConstructorReturn3 . default ) ( _this , _ret ) ;
74
+ }
75
+
76
+ ( 0 , _createClass3 . default ) ( Stopwatch , [ {
77
+ key : 'componentWillMount' ,
78
+ value : function componentWillMount ( ) {
79
+ var _this2 = this ;
80
+
81
+ var startTime = this . props . start || new Date ( ) ;
82
+ this . setState ( {
83
+ elapsed : this . calculateElapsedSeconds ( startTime ) ,
84
+ startTime : startTime ,
85
+ timer : setInterval ( function ( ) {
86
+ _this2 . setState ( {
87
+ elapsed : _this2 . calculateElapsedSeconds ( _this2 . state . startTime )
88
+ } ) ;
89
+ } , 500 )
90
+ } ) ;
91
+ }
92
+ } , {
93
+ key : 'componentWillUnmount' ,
94
+ value : function componentWillUnmount ( ) {
95
+ clearInterval ( this . state . timer ) ;
96
+ }
97
+ } , {
98
+ key : 'render' ,
99
+ value : function render ( ) {
100
+ return _react2 . default . createElement (
101
+ 'span' ,
102
+ { style : this . props . style , className : this . props . className } ,
103
+ this . getTimeDiffString ( )
104
+ ) ;
105
+ }
106
+ } ] ) ;
107
+ return Stopwatch ;
108
+ } ( _react2 . default . Component ) ;
109
+
110
+ Stopwatch . propTypes = {
111
+ style : _react2 . default . PropTypes . object ,
112
+ className : _react2 . default . PropTypes . string ,
113
+ start : _react2 . default . PropTypes . instanceOf ( Date )
114
+ } ;
115
+ Stopwatch . defaultProps = {
116
+ style : { }
117
+ } ;
118
+ exports . default = Stopwatch ;
0 commit comments