2
2
* jQuery timepicker addon
3
3
* By: Trent Richardson [http://trentrichardson.com]
4
4
* Version 0.8.1
5
- * Last Modified: 11/15 /2010 by Charles Phillips
5
+ * Last Modified: 11/18 /2010 by Charles Phillips
6
6
*
7
7
* Copyright 2010 Trent Richardson
8
8
* Dual licensed under the MIT and GPL licenses.
@@ -89,6 +89,58 @@ $.extend(Timepicker.prototype, {
89
89
return this ;
90
90
} ,
91
91
92
+ //########################################################################
93
+ // Create a new Timepicker instance
94
+ //########################################################################
95
+ _newInst : function ( $input , o ) {
96
+ var tp_inst = new Timepicker ( ) ,
97
+ inlineSettings = { } ;
98
+
99
+ for ( var attrName in tp_inst . _defaults ) {
100
+ var attrValue = $input . attr ( 'time:' + attrName ) ;
101
+ if ( attrValue ) {
102
+ try {
103
+ inlineSettings [ attrName ] = eval ( attrValue ) ;
104
+ } catch ( err ) {
105
+ inlineSettings [ attrName ] = attrValue ;
106
+ }
107
+ }
108
+ }
109
+ tp_inst . _defaults = $ . extend ( { } , tp_inst . _defaults , inlineSettings , o , {
110
+ beforeShow : function ( input , dp_inst ) {
111
+ tp_inst . hour = tp_inst . _defaults . hour ;
112
+ tp_inst . minute = tp_inst . _defaults . minute ;
113
+ tp_inst . second = tp_inst . _defaults . second ;
114
+ tp_inst . ampm = '' ;
115
+ tp_inst . $input = $ ( input ) ;
116
+ if ( o . altField )
117
+ tp_inst . $altInput = $ ( $ . datepicker . _get ( dp_inst , 'altField' ) )
118
+ . css ( { cursor : 'pointer' } )
119
+ . focus ( function ( ) {
120
+ $input . trigger ( "focus" ) ;
121
+ } ) ;
122
+ tp_inst . inst = dp_inst ;
123
+ tp_inst . _addTimePicker ( ) ;
124
+ if ( $ . isFunction ( o . beforeShow ) )
125
+ o . beforeShow ( input , dp_inst ) ;
126
+ } ,
127
+ onChangeMonthYear : function ( year , month , dp_inst ) {
128
+ // Update the time as well : this prevents the time from disappearing from the $input field.
129
+ tp_inst . _updateDateTime ( dp_inst ) ;
130
+ if ( $ . isFunction ( o . onChangeMonthYear ) )
131
+ o . onChangeMonthYear ( year , month , dp_inst ) ;
132
+ } ,
133
+ onClose : function ( dateText , dp_inst ) {
134
+ if ( tp_inst . timeDefined === true && $input . val ( ) != '' )
135
+ tp_inst . _updateDateTime ( dp_inst ) ;
136
+ if ( $ . isFunction ( o . onClose ) )
137
+ o . onClose ( dateText , dp_inst ) ;
138
+ } ,
139
+ timepicker : tp_inst // add timepicker as a property of datepicker: $.datepicker._get(dp_inst, 'timepicker');
140
+ } ) ;
141
+ return tp_inst ;
142
+ } ,
143
+
92
144
//########################################################################
93
145
// add our sliders to the calendar
94
146
//########################################################################
@@ -108,14 +160,17 @@ $.extend(Timepicker.prototype, {
108
160
order = this . _getFormatPositions ( ) ;
109
161
110
162
if ( ! this . _defaults . timeOnly ) {
111
- //the time should come after x number of characters and a space. x = at least the length of text specified by the date format
163
+ // the time should come after x number of characters and a space.
164
+ // x = at least the length of text specified by the date format
112
165
var dp_dateFormat = $ . datepicker . _get ( this . inst , 'dateFormat' ) ;
113
166
regstr = '.{' + dp_dateFormat . length + ',}\\s+' + regstr ;
114
167
}
115
168
116
169
if ( treg ) {
117
170
if ( order . t !== - 1 )
118
- this . ampm = ( ( treg [ order . t ] === undefined || treg [ order . t ] . length === 0 ) ? '' : ( treg [ order . t ] . charAt ( 0 ) . toUpperCase ( ) == 'A' ) ? 'AM' : 'PM' ) . toUpperCase ( ) ;
171
+ this . ampm = ( ( treg [ order . t ] === undefined || treg [ order . t ] . length === 0 ) ?
172
+ '' :
173
+ ( treg [ order . t ] . charAt ( 0 ) . toUpperCase ( ) == 'A' ) ? 'AM' : 'PM' ) . toUpperCase ( ) ;
119
174
120
175
if ( order . h !== - 1 ) {
121
176
if ( this . ampm == 'AM' && treg [ order . h ] == '12' )
@@ -166,11 +221,13 @@ $.extend(Timepicker.prototype, {
166
221
// Prevent displaying twice
167
222
if ( $dp . find ( "div#ui-timepicker-div-" + dp_id ) . length === 0 ) {
168
223
var noDisplay = ' style="display:none;"' ,
169
- html =
170
- '<div class="ui-timepicker-div" id="ui-timepicker-div-' + dp_id + '"><dl>' +
171
- '<dt class="ui_tpicker_time_label" id="ui_tpicker_time_label_' + dp_id + '"' + ( ( o . showTime ) ? '' : noDisplay ) + '>' + o . timeText + '</dt>' +
172
- '<dd class="ui_tpicker_time" id="ui_tpicker_time_' + dp_id + '"' + ( ( o . showTime ) ? '' : noDisplay ) + '></dd>' +
173
- '<dt class="ui_tpicker_hour_label" id="ui_tpicker_hour_label_' + dp_id + '"' + ( ( o . showHour ) ? '' : noDisplay ) + '>' + o . hourText + '</dt>' ,
224
+ html = '<div class="ui-timepicker-div" id="ui-timepicker-div-' + dp_id + '"><dl>' +
225
+ '<dt class="ui_tpicker_time_label" id="ui_tpicker_time_label_' + dp_id + '"' +
226
+ ( ( o . showTime ) ? '' : noDisplay ) + '>' + o . timeText + '</dt>' +
227
+ '<dd class="ui_tpicker_time" id="ui_tpicker_time_' + dp_id + '"' +
228
+ ( ( o . showTime ) ? '' : noDisplay ) + '></dd>' +
229
+ '<dt class="ui_tpicker_hour_label" id="ui_tpicker_hour_label_' + dp_id + '"' +
230
+ ( ( o . showHour ) ? '' : noDisplay ) + '>' + o . hourText + '</dt>' ,
174
231
hourGridSize = 0 ,
175
232
minuteGridSize = 0 ,
176
233
secondGridSize = 0 ,
@@ -195,13 +252,16 @@ $.extend(Timepicker.prototype, {
195
252
196
253
html += '</tr></table></div>' +
197
254
'</dd>' ;
198
- } else html += '<dd class="ui_tpicker_hour" id="ui_tpicker_hour_' + dp_id + '"' + ( ( o . showHour ) ? '' : noDisplay ) + '></dd>' ;
255
+ } else html += '<dd class="ui_tpicker_hour" id="ui_tpicker_hour_' + dp_id + '"' +
256
+ ( ( o . showHour ) ? '' : noDisplay ) + '></dd>' ;
199
257
200
- html += '<dt class="ui_tpicker_minute_label" id="ui_tpicker_minute_label_' + dp_id + '"' + ( ( o . showMinute ) ? '' : noDisplay ) + '>' + o . minuteText + '</dt>' ;
258
+ html += '<dt class="ui_tpicker_minute_label" id="ui_tpicker_minute_label_' + dp_id + '"' +
259
+ ( ( o . showMinute ) ? '' : noDisplay ) + '>' + o . minuteText + '</dt>' ;
201
260
202
261
if ( o . showMinute && o . minuteGrid > 0 ) {
203
262
html += '<dd class="ui_tpicker_minute ui_tpicker_minute_' + o . minuteGrid + '">' +
204
- '<div id="ui_tpicker_minute_' + dp_id + '"' + ( ( o . showMinute ) ? '' : noDisplay ) + '></div>' +
263
+ '<div id="ui_tpicker_minute_' + dp_id + '"' +
264
+ ( ( o . showMinute ) ? '' : noDisplay ) + '></div>' +
205
265
'<div style="padding-left: 1px"><table><tr>' ;
206
266
207
267
for ( var m = 0 ; m < minMax ; m += o . minuteGrid ) {
@@ -211,13 +271,16 @@ $.extend(Timepicker.prototype, {
211
271
212
272
html += '</tr></table></div>' +
213
273
'</dd>' ;
214
- } else html += '<dd class="ui_tpicker_minute" id="ui_tpicker_minute_' + dp_id + '"' + ( ( o . showMinute ) ? '' : noDisplay ) + '></dd>' ;
274
+ } else html += '<dd class="ui_tpicker_minute" id="ui_tpicker_minute_' + dp_id + '"' +
275
+ ( ( o . showMinute ) ? '' : noDisplay ) + '></dd>' ;
215
276
216
- html += '<dt class="ui_tpicker_second_label" id="ui_tpicker_second_label_' + dp_id + '"' + ( ( o . showSecond ) ? '' : noDisplay ) + '>' + o . secondText + '</dt>' ;
277
+ html += '<dt class="ui_tpicker_second_label" id="ui_tpicker_second_label_' + dp_id + '"' +
278
+ ( ( o . showSecond ) ? '' : noDisplay ) + '>' + o . secondText + '</dt>' ;
217
279
218
280
if ( o . showSecond && o . secondGrid > 0 ) {
219
281
html += '<dd class="ui_tpicker_second ui_tpicker_second_' + o . secondGrid + '">' +
220
- '<div id="ui_tpicker_second_' + dp_id + '"' + ( ( o . showSecond ) ? '' : noDisplay ) + '></div>' +
282
+ '<div id="ui_tpicker_second_' + dp_id + '"' +
283
+ ( ( o . showSecond ) ? '' : noDisplay ) + '></div>' +
221
284
'<div style="padding-left: 1px"><table><tr>' ;
222
285
223
286
for ( var s = 0 ; s < secMax ; s += o . secondGrid ) {
@@ -227,7 +290,8 @@ $.extend(Timepicker.prototype, {
227
290
228
291
html += '</tr></table></div>' +
229
292
'</dd>' ;
230
- } else html += '<dd class="ui_tpicker_second" id="ui_tpicker_second_' + dp_id + '"' + ( ( o . showSecond ) ? '' : noDisplay ) + '></dd>' ;
293
+ } else html += '<dd class="ui_tpicker_second" id="ui_tpicker_second_' + dp_id + '"' +
294
+ ( ( o . showSecond ) ? '' : noDisplay ) + '></dd>' ;
231
295
232
296
html += '</dl></div>' ;
233
297
$tp = $ ( html ) ;
@@ -348,8 +412,8 @@ $.extend(Timepicker.prototype, {
348
412
} ) ;
349
413
}
350
414
351
- var buttonPanel = $dp . find ( '.ui-datepicker-buttonpane' ) ;
352
- if ( buttonPanel . length > 0 ) $dp . find ( '.ui-datepicker-buttonpane' ) . before ( $tp ) ;
415
+ var $ buttonPanel = $dp . find ( '.ui-datepicker-buttonpane' ) ;
416
+ if ( $ buttonPanel. length ) $buttonPanel . before ( $tp ) ;
353
417
else $dp . append ( $tp ) ;
354
418
355
419
this . $timeObj = $ ( '#ui_tpicker_time_' + dp_id ) ;
@@ -363,7 +427,7 @@ $.extend(Timepicker.prototype, {
363
427
} ,
364
428
365
429
//########################################################################
366
- // when a slider moves..
430
+ // when a slider moves...
367
431
// on time change is also called when the time is updated in the text field
368
432
//########################################################################
369
433
_onTimeChange : function ( force ) {
@@ -397,8 +461,8 @@ $.extend(Timepicker.prototype, {
397
461
// format the time all pretty...
398
462
//########################################################################
399
463
_formatTime : function ( ) {
400
- var tmptime = this . _defaults . timeFormat . toString ( ) ;
401
- var hour12 = ( ( this . ampm == 'AM' ) ? ( this . hour ) : ( this . hour % 12 ) ) ;
464
+ var tmptime = this . _defaults . timeFormat . toString ( ) ,
465
+ hour12 = ( ( this . ampm == 'AM' ) ? ( this . hour ) : ( this . hour % 12 ) ) ;
402
466
hour12 = ( Number ( hour12 ) === 0 ) ? 12 : hour12 ;
403
467
404
468
if ( this . _defaults . ampm === true ) {
@@ -460,13 +524,13 @@ $.fn.extend({
460
524
// shorthand just to use timepicker..
461
525
//########################################################################
462
526
timepicker : function ( o ) {
527
+ o = o || { } ;
463
528
var tmp_args = arguments ;
464
529
465
- if ( o === undefined ) o = { timeOnly : true } ;
466
- else if ( typeof o == 'object' ) o = $ . extend ( o , { timeOnly : true } ) ;
530
+ if ( typeof o == 'object' ) tmp_args [ 0 ] = $ . extend ( o , { timeOnly : true } ) ;
467
531
468
532
return this . each ( function ( ) {
469
- $ ( this ) . datetimepicker ( o , tmp_args [ 1 ] , tmp_args [ 2 ] , tmp_args [ 3 ] , tmp_args [ 4 ] ) ;
533
+ $ . fn . datepicker . apply ( this , tmp_args ) ;
470
534
} ) ;
471
535
} ,
472
536
@@ -478,75 +542,14 @@ $.fn.extend({
478
542
var $input = this ,
479
543
tmp_args = arguments ;
480
544
481
- if ( typeof ( o ) == 'string' ) {
482
- if ( o == 'setDate' ) return this . each ( function ( ) {
483
- return $ ( this ) . datepicker ( o , tmp_args [ 1 ] ) ;
484
- } ) ;
485
- else if ( o == 'option' && typeof ( tmp_args [ 1 ] ) == 'string' ) return this . each ( function ( ) {
486
- return $ ( this ) . datepicker ( o , tmp_args [ 1 ] , tmp_args [ 2 ] ) ;
487
- } ) ;
488
- else if ( o == 'dialog' ) return this . each ( function ( ) {
489
- return $ ( this ) . datepicker ( o , tmp_args [ 1 ] , tmp_args [ 2 ] , tmp_args [ 3 ] , tmp_args [ 4 ] ) ;
490
- } ) ;
491
- else return this . each ( function ( ) {
492
- return $ ( this ) . datepicker ( o ) ;
493
- } ) ;
494
- } else {
495
- var tp_inst = new Timepicker ( ) ,
496
- inlineSettings = { } ;
497
-
498
- for ( var attrName in tp_inst . _defaults ) {
499
- var attrValue = $input . attr ( 'time:' + attrName ) ;
500
- if ( attrValue ) {
501
- try {
502
- inlineSettings [ attrName ] = eval ( attrValue ) ;
503
- } catch ( err ) {
504
- inlineSettings [ attrName ] = attrValue ;
505
- }
506
- }
507
- }
508
- tp_inst . _defaults = $ . extend ( { } , tp_inst . _defaults , inlineSettings , o , {
509
- beforeShow : function ( input , dp_inst ) {
510
- tp_inst . hour = tp_inst . _defaults . hour ;
511
- tp_inst . minute = tp_inst . _defaults . minute ;
512
- tp_inst . second = tp_inst . _defaults . second ;
513
- tp_inst . ampm = '' ;
514
- tp_inst . $input = $ ( input ) ;
515
- if ( o . altField ) {
516
- tp_inst . $altInput = $ ( $ . datepicker . _get ( dp_inst , 'altField' ) )
517
- . css ( { cursor : 'pointer' } )
518
- . focus ( function ( ) {
519
- $input . trigger ( "focus" ) ;
520
- } ) ;
521
- }
522
- tp_inst . inst = dp_inst ;
523
- tp_inst . _addTimePicker ( ) ;
524
- if ( $ . isFunction ( o . beforeShow ) ) {
525
- o . beforeShow ( input , dp_inst ) ;
526
- }
527
- } ,
528
- onChangeMonthYear : function ( year , month , dp_inst ) {
529
- // Update the time as well : this prevents the time from disappearing from the $input field.
530
- tp_inst . _updateDateTime ( dp_inst ) ;
531
- if ( $ . isFunction ( o . onChangeMonthYear ) ) {
532
- o . onChangeMonthYear ( year , month , dp_inst ) ;
533
- }
534
- } ,
535
- onClose : function ( dateText , dp_inst ) {
536
- if ( tp_inst . timeDefined === true && $input . val ( ) != '' ) {
537
- tp_inst . _updateDateTime ( dp_inst ) ;
538
- }
539
- if ( $ . isFunction ( o . onClose ) ) {
540
- o . onClose ( dateText , dp_inst ) ;
541
- }
542
- } ,
543
- timepicker : tp_inst // add timepicker as a property of datepicker: $.datepicker._get(dp_inst, 'timepicker');
545
+ if ( typeof ( o ) == 'string' )
546
+ return this . each ( function ( ) {
547
+ $ . fn . datepicker . apply ( this , tmp_args ) ;
544
548
} ) ;
545
-
549
+ else
546
550
return this . each ( function ( ) {
547
- $ ( this ) . datepicker ( tp_inst . _defaults ) ;
551
+ $ ( this ) . datepicker ( $ . timepicker . _newInst ( $input , o ) . _defaults ) ;
548
552
} ) ;
549
- }
550
553
}
551
554
} ) ;
552
555
@@ -601,9 +604,9 @@ $.datepicker._doKeyPress = function(event) {
601
604
602
605
if ( tp_inst ) {
603
606
if ( $ . datepicker . _get ( inst , 'constrainInput' ) ) {
604
- var dateChars = $ . datepicker . _possibleChars ( $ . datepicker . _get ( inst , 'dateFormat' ) ) ;
605
- var chr = String . fromCharCode ( event . charCode === undefined ? event . keyCode : event . charCode ) ;
606
- var chrl = chr . toLowerCase ( ) ;
607
+ var dateChars = $ . datepicker . _possibleChars ( $ . datepicker . _get ( inst , 'dateFormat' ) ) ,
608
+ chr = String . fromCharCode ( event . charCode === undefined ? event . keyCode : event . charCode ) ,
609
+ chrl = chr . toLowerCase ( ) ;
607
610
// keyCode == 58 => ":"
608
611
// keyCode == 32 => " "
609
612
return event . ctrlKey || ( chr < ' ' || ! dateChars || dateChars . indexOf ( chr ) > - 1 || event . keyCode == 58 || event . keyCode == 32 || chr == ':' || chr == ' ' || chrl == 'a' || chrl == 'p' || chrl == 'm' ) ;
@@ -649,7 +652,7 @@ $.datepicker._gotoToday = function(id) {
649
652
$ . datepicker . _setTime = function ( inst , date ) {
650
653
var tp_inst = $ . datepicker . _get ( inst , 'timepicker' ) ;
651
654
652
- if ( tp_inst ) {
655
+ if ( tp_inst ) {
653
656
var hour = date . getHours ( ) ,
654
657
minute = date . getMinutes ( ) ,
655
658
second = date . getSeconds ( ) ;
@@ -696,7 +699,7 @@ $.datepicker._base_getDate = $.datepicker._getDate;
696
699
$ . datepicker . _getDate = function ( inst ) {
697
700
var tp_inst = $ . datepicker . _get ( inst , 'timepicker' ) ;
698
701
if ( tp_inst )
699
- return startDate = ( ! inst . currentYear || ( inst . input && inst . input . val ( ) == '' ) ) ?
702
+ return ( ! inst . currentYear || ( inst . input && inst . input . val ( ) == '' ) ) ?
700
703
null :
701
704
( new Date ( inst . currentYear , inst . currentMonth , inst . currentDay , tp_inst . hour , tp_inst . minute , tp_inst . second ) ) ;
702
705
else return $ . datepicker . _base_getDate ( inst ) ;
@@ -709,7 +712,7 @@ $.datepicker._getDate = function(inst) {
709
712
function extendRemove ( target , props ) {
710
713
$ . extend ( target , props ) ;
711
714
for ( var name in props )
712
- if ( props [ name ] == null || props [ name ] == undefined )
715
+ if ( props [ name ] === null || props [ name ] = == undefined )
713
716
target [ name ] = props [ name ] ;
714
717
return target ;
715
718
}
@@ -718,4 +721,3 @@ $.timepicker = new Timepicker(); // singleton instance
718
721
$ . timepicker . version = "0.8.1" ;
719
722
720
723
} ) ( jQuery ) ;
721
-
0 commit comments