Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Timepicker: Default Values & Unit Tests

  • Loading branch information...
commit 4cb59bb2712410e3499f70d20ff29585e475652d 1 parent fed204e
@gnarf gnarf authored
View
2  tests/unit/timepicker/timepicker.html
@@ -35,7 +35,7 @@ <h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">
-<input id="mask1" type="text">
+<input id="timepicker1" type="text">
</div>
</body>
</html>
View
3  tests/unit/timepicker/timepicker_defaults.js
@@ -1,5 +1,8 @@
commonWidgetTests( "timepicker", {
defaults: {
+ ampm: true,
+ disabled: false,
+ seconds: true,
// callbacks
create: null
}
View
48 tests/unit/timepicker/timepicker_events.js
@@ -2,4 +2,52 @@
module( "timepicker: events" );
+// helper funciton to quick "press" a key - keydown/keyup
+function downup( elem, key ) {
+ elem.simulate( "keydown", { keyCode: key } );
+ elem.simulate( "keyup", { keyCode: key } );
+}
+
+test( "keydown: Up/Down/Left/Right behaviors", function() {
+ expect( 11 );
+
+ var i,
+ input = $( "#timepicker1" ).val( "12:00:00 pm" ).timepicker(),
+ timepicker = input.data( "timepicker" ),
+ mask = timepicker.mask;
+
+ mask._caret( 0, 0 );
+ equal( input.val(), "12:00:00 pm", "Sanity Check" );
+ deepEqual( mask._caret(), { begin: 0, end: 0 }, "Caret position correct" );
+
+ downup( input, $.ui.keyCode.UP );
+ equal( input.val(), "01:00:00 pm", "After up keypress in hours field, value went to proper value" );
+
+ downup( input, $.ui.keyCode.DOWN );
+ equal( input.val(), "12:00:00 pm", "After down keypress in hours field, value went to proper value" );
+
+ downup( input, $.ui.keyCode.DOWN );
+ equal( input.val(), "11:00:00 pm", "After down keypress in hours field, value went to proper value" );
+ deepEqual( mask._caret(), { begin: 0, end: 2 }, "Caret position selects hours" );
+
+ downup( input, $.ui.keyCode.RIGHT );
+ deepEqual( mask._caret(), { begin: 3, end: 5 }, "After Right - Caret position selects minutes" );
+
+ for ( i = 0; i < 10; i++ ) {
+ downup( input, $.ui.keyCode.DOWN );
+ }
+ equal( input.val(), "11:50:00 pm", "After 10 down keypress in minutes field, value went to proper value" );
+
+ downup( input, $.ui.keyCode.RIGHT );
+ deepEqual( mask._caret(), { begin: 6, end: 8 }, "After Right - Caret position selects seconds" );
+
+ downup( input, $.ui.keyCode.RIGHT );
+ deepEqual( mask._caret(), { begin: 9, end: 11 }, "After Right - Caret position selects am/pm" );
+
+ downup( input, $.ui.keyCode.DOWN );
+ equal( input.val(), "11:50:00 am", "After down keypress in am/pm field, value went to proper value" );
+
+
+});
+
}( jQuery ) );
View
38 tests/unit/timepicker/timepicker_options.js
@@ -2,4 +2,42 @@
module( "timepicker: options" );
+test( "seconds", function() {
+ expect( 3 );
+
+ var input = $( "#timepicker1" ).val("12:30 am").timepicker({
+ seconds: false
+ });
+
+ equal( input.val(), "12:30 am", "Seconds: false startup option" );
+
+ input.timepicker( "option", "seconds", true );
+
+ equal( input.val(), "12:30:00 am", "Seconds: enabled seconds via option method" );
+
+ input.timepicker( "option", "seconds", false );
+
+ equal( input.val(), "12:30 am", "Seconds: disabled seconds via option method" );
+
+});
+
+test( "ampm", function() {
+ expect( 3 );
+
+ var input = $( "#timepicker1" ).val("01:30:00 pm").timepicker({
+ ampm: true
+ });
+
+ equal( input.val(), "01:30:00 pm", "Sanity Check" );
+
+ input.timepicker( "option", "ampm", false );
+
+ equal( input.val(), "13:30:00", "Disabled ampm option via method" );
+
+ input.timepicker( "option", "ampm", true );
+
+ equal( input.val(), "01:30:00 pm", "Enabled ampm option via method" );
+
+});
+
}( jQuery ) );
View
39 ui/jquery.ui.timepicker.js
@@ -18,6 +18,9 @@
(function( $, undefined ) {
function sixty( value ) {
+ if ( !value ) {
+ return "00";
+ }
value = parseInt( value, 10 );
if ( value >= 0 && value <= 59 ) {
return ( value < 10 ? "0" : "" ) + value;
@@ -26,12 +29,18 @@ function sixty( value ) {
var maskDefinitions = {
hh: function( value ) {
+ if ( value === "" ) {
+ return "12";
+ }
value = parseInt( value, 10 );
if ( value >= 1 && value <= 12 ) {
return ( value < 10 ? "0" : "" ) + value;
}
},
HH: function( value ) {
+ if ( value === "" ) {
+ return "12";
+ }
value = parseInt( value, 10 );
if ( value >= 0 && value <= 23 ) {
return ( value < 10 ? "0" : "" ) + value;
@@ -40,6 +49,9 @@ var maskDefinitions = {
mm: sixty,
ss: sixty,
tt: function( value ) {
+ if ( value === "" ) {
+ return "pm";
+ }
var lower = value.toLowerCase(),
character = lower.charAt( 0 );
if ( lower.length > 1 && lower.charAt( 1 ) !== "m" ) {
@@ -104,10 +116,17 @@ $.widget( "ui.timepicker", {
click: "_checkPosition"
},
_checkPosition: function( event ) {
- this._delay(function() {
+
+ function check() {
var position = this.mask._caret();
this._setField( Math.floor( position.begin / 3 ) );
- }, 0);
+ }
+
+ if ( event.type == "keydown" ) {
+ check.call( this );
+ } else {
+ this._delay( check, 0 );
+ }
},
_setField: function( field ) {
this.currentField = field;
@@ -128,8 +147,13 @@ $.widget( "ui.timepicker", {
}
},
_setOption: function( key, value ) {
+ var was = this.options[ key ];
this._super( "_setOption", key, value );
- console.log( key, value );
+
+ if ( was === value ) {
+ return;
+ }
+
if ( key === "ampm" ) {
var i, buffer, currentHour, currentTT;
buffer = this.mask.buffer;
@@ -156,6 +180,15 @@ $.widget( "ui.timepicker", {
this.element.mask( "option", "mask", this._generateMask() );
}
if ( key === "seconds" ) {
+ if ( value ) {
+ this.element.val( function( index, value ) {
+ return value.substr( 0, 6 ) + ":00" + value.substr( 6 );
+ });
+ } else {
+ this.element.val( function( index, value ) {
+ return value.substr( 0, 6 ) + value.substr( 9 );
+ });
+ }
this.element.mask( "option", "mask", this._generateMask() );
}
},
Please sign in to comment.
Something went wrong with that request. Please try again.