1
1
/*jshint jquery:true */
2
+ /*global LOCALE_CODE */
3
+
4
+ if ( typeof LOCALE_CODE != 'undefined' ) {
5
+ var LOCALE_CODE = 'en' ;
6
+ }
2
7
3
8
/** Utility for correlated start / end date time selectors.
4
9
*
7
12
* - http://jonthornton.github.io/jquery-timepicker/
8
13
*/
9
14
10
- var DatePair = function ( shouldSetToNow , dateTimeObjects ) {
15
+ var DatePair = function ( shouldSetToNow , dateTimeObjects , highlightAdvance ) {
11
16
12
17
/** DOM state used by DatePair.
13
18
*
@@ -52,6 +57,14 @@ var DatePair = function (shouldSetToNow, dateTimeObjects) {
52
57
this . verifyStartEnd ( ) ;
53
58
} ;
54
59
60
+ /* Translate a date into a javascript time object
61
+ Assumes that time is in US format '11/13/2013'
62
+ */
63
+ this . dateToTime = function ( value ) {
64
+ var splitval = value . split ( / [ / - ] / ) ;
65
+ return new Date ( splitval [ 2 ] , splitval [ 0 ] - 1 , splitval [ 1 ] ) ;
66
+ } ;
67
+
55
68
/** Set entry time to now with a duration of one hour.
56
69
*/
57
70
this . setToNow = function ( ) {
@@ -72,24 +85,28 @@ var DatePair = function (shouldSetToNow, dateTimeObjects) {
72
85
// Update widget state
73
86
this . setStartDateTime ( now ) ;
74
87
this . setEndDateTime ( inOneHour ) ;
88
+ this . updateEndTime ( now - inOneHour ) ;
75
89
} ;
76
90
77
91
/** Utility configuring widgets associated with DOM elements.
78
92
*/
79
93
this . hookUpWidgets = function ( ) {
80
94
var thisDatePair = this ;
81
95
82
- // Starti date and time
96
+ // Start date and time
83
97
this . startDate . datepicker ( {
84
98
'autoclose' : true ,
85
- 'todayBtn' : true ,
86
99
'todayHighlight' : true ,
87
- 'startDate' : '+0d' ,
88
- 'endDate' : '+2m'
100
+ 'startDate' : new Date ( ) ,
101
+ 'endDate' : '+2m' ,
102
+ 'language' : LOCALE_CODE
89
103
} ) . change ( function ( ) {
90
104
thisDatePair . updateEndTime ( thisDatePair . priorTimeDelta ) ;
91
105
thisDatePair . verifyStartEnd ( ) ; // Update text box
92
- thisDatePair . startTime [ 0 ] . focus ( ) ; // Highlight time stamp
106
+
107
+ if ( highlightAdvance ) {
108
+ thisDatePair . startTime [ 0 ] . focus ( ) ; // Highlight time stamp
109
+ }
93
110
} ) ;
94
111
this . startTime . timepicker ( {
95
112
'timeFormat' : 'g:ia' ,
@@ -102,19 +119,29 @@ var DatePair = function (shouldSetToNow, dateTimeObjects) {
102
119
// End date and time
103
120
this . endDate . datepicker ( {
104
121
'autoclose' : true ,
105
- 'startDate' : '+0d' ,
106
- 'endDate' : '+2m'
122
+ 'startDate' : new Date ( ) ,
123
+ 'endDate' : '+2m' ,
124
+ 'language' : LOCALE_CODE
107
125
} ) . change ( function ( ) {
108
126
thisDatePair . verifyStartEnd ( ) ; // Update text box
109
127
thisDatePair . priorTimeDelta = thisDatePair . getTimeDelta ( ) ;
110
128
thisDatePair . updateEndTime ( thisDatePair . priorTimeDelta ) ;
111
- thisDatePair . endTime [ 0 ] . focus ( ) ; // Highlight time stamp
129
+
130
+ if ( highlightAdvance ) {
131
+ thisDatePair . endTime [ 0 ] . focus ( ) ; // Highlight time stamp
132
+ }
112
133
} ) . data ( 'datepicker' ) ;
113
134
this . endTime . timepicker ( {
114
135
'timeFormat' : 'g:ia' ,
115
136
'scrollDefaultNow' : true
116
137
} ) . change ( function ( ) {
117
- thisDatePair . priorTimeDelta = thisDatePair . getTimeDelta ( ) ;
138
+ // if we accidentally loop around midnight...
139
+ // then subtract 24 hours (86400 seconds) from the delta
140
+ if ( thisDatePair . priorTimeDelta <= 86400 && thisDatePair . getTimeDelta ( ) >= 86400 ) {
141
+ thisDatePair . priorTimeDelta = thisDatePair . getTimeDelta ( ) - 86400 ;
142
+ } else {
143
+ thisDatePair . priorTimeDelta = thisDatePair . getTimeDelta ( ) ;
144
+ }
118
145
thisDatePair . updateEndTime ( thisDatePair . priorTimeDelta ) ;
119
146
thisDatePair . verifyStartEnd ( ) ; // Update text box
120
147
} ) ;
@@ -144,7 +171,7 @@ var DatePair = function (shouldSetToNow, dateTimeObjects) {
144
171
if ( timeDelta < ( this . oneHourMS * 24 / 1000 ) ) {
145
172
startSeconds = this . startTime . timepicker ( 'getSecondsFromMidnight' ) ;
146
173
this . endTime . timepicker ( 'option' , 'minTime' , startSeconds + 30 * 60 ) ;
147
- this . endTime . timepicker ( 'option' , 'maxTime' , '12:00pm ' ) ;
174
+ this . endTime . timepicker ( 'option' , 'maxTime' , '11:30pm ' ) ;
148
175
this . endTime . timepicker ( 'option' , 'durationTime' , startSeconds ) ;
149
176
this . endTime . timepicker ( 'option' , 'showDuration' , true ) ;
150
177
} else {
@@ -174,7 +201,7 @@ var DatePair = function (shouldSetToNow, dateTimeObjects) {
174
201
var date ,
175
202
startDate ,
176
203
startSeconds ;
177
- startDate = new Date ( this . startDate . val ( ) ) ;
204
+ startDate = this . dateToTime ( this . startDate . val ( ) ) ;
178
205
startSeconds = this . startTime . timepicker ( 'getSecondsFromMidnight' ) ;
179
206
date = new Date ( startDate . getTime ( ) + startSeconds * 1000 ) ;
180
207
return date ;
@@ -188,7 +215,7 @@ var DatePair = function (shouldSetToNow, dateTimeObjects) {
188
215
var date ,
189
216
endDate ,
190
217
endSeconds ;
191
- endDate = new Date ( this . endDate . val ( ) ) ;
218
+ endDate = this . dateToTime ( this . endDate . val ( ) ) ;
192
219
endSeconds = this . endTime . timepicker ( 'getSecondsFromMidnight' ) ;
193
220
date = new Date ( endDate . getTime ( ) + endSeconds * 1000 ) ;
194
221
return date ;
@@ -217,13 +244,8 @@ var DatePair = function (shouldSetToNow, dateTimeObjects) {
217
244
* param {Date} End Date object.
218
245
*/
219
246
this . setEndDateTime = function ( date ) {
220
- var endDate = new Date ( date . getFullYear ( ) , date . getMonth ( ) , date . getDate ( ) ) ;
221
-
222
- //if end time is 11:30pm, change end date to start date
223
- if ( ( date . getHours ( ) + ":" + date . getMinutes ( ) ) == "23:30"
224
- && this . getTimeDelta ( ) < ( this . oneHourMS * 24 ) ) {
225
- endDate . setDate ( this . getStartDateTime ( ) . getDate ( ) ) ;
226
- }
247
+ var startDate = this . getStartDateTime ( ) ,
248
+ endDate = new Date ( date . getFullYear ( ) , date . getMonth ( ) , date . getDate ( ) ) ;
227
249
228
250
// Check for NaN.. should be logging an error here
229
251
if ( isNaN ( endDate ) ) {
@@ -284,8 +306,7 @@ var DatePair = function (shouldSetToNow, dateTimeObjects) {
284
306
var configurations ,
285
307
timeCookie ;
286
308
configurations = {
287
- path : '/' , // set globally for the site
288
- expires : 1 // one hour expiration
309
+ path : '/' // set globally for the site
289
310
} ;
290
311
timeCookie = {
291
312
startDateTime : this . getStartDateTime ( ) ,
@@ -313,9 +334,9 @@ var DatePair = function (shouldSetToNow, dateTimeObjects) {
313
334
hourString = ( hourString === '0 hours' ) ? '' : hourString ;
314
335
dateTimeDeltaMinutes -= ( hourInt * 60 ) ;
315
336
316
- minuteString = dateTimeDeltaMinutes + ' minutes ' ;
317
- minuteString = ( minuteString === '1 minutes ' ) ? '1 minute ' : minuteString ;
318
- minuteString = ( minuteString === '0 minutes ' ) ? '' : minuteString ;
337
+ minuteString = dateTimeDeltaMinutes + ' mins ' ;
338
+ minuteString = ( minuteString === '1 min ' ) ? '1 min ' : minuteString ;
339
+ minuteString = ( minuteString === '0 mins ' ) ? '' : minuteString ;
319
340
320
341
$ ( '.output-duration' ) . text ( dateString + ' ' + hourString + ' ' + minuteString ) ;
321
342
} ;
0 commit comments