Skip to content
Browse files

Timepicker: Fixing up tests and removing some unneeded code surroundi…

…ng focus
  • Loading branch information...
1 parent 009347c commit 23bd8812d80446b4a0e12705e3ea022b5a2cd19f @gnarf gnarf committed Oct 25, 2011
View
1 tests/unit/timepicker/timepicker.html
@@ -20,6 +20,7 @@
<script src="../../jquery.simulate.js"></script>
<script src="../testsuite.js"></script>
+ <script src="timepicker_test_helpers.js"></script>
<script src="timepicker_core.js"></script>
<script src="timepicker_defaults.js"></script>
<script src="timepicker_events.js"></script>
View
6 tests/unit/timepicker/timepicker_events.js
@@ -2,12 +2,6 @@
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( 12 );
View
51 tests/unit/timepicker/timepicker_test_helpers.js
@@ -0,0 +1,51 @@
+// helper funciton to quick "press" a key - keydown/keyup
+function downup( element, key ) {
+ element.simulate( "keydown", { keyCode: key } );
+ element.simulate( "keyup", { keyCode: key } );
+}
+
+function press( element, key ) {
+ var code = key.charCodeAt( 0 );
+
+ element.simulate( "keypress", {
+ charCode: code,
+ which: code
+ });
+}
+
+function focus( element ) {
+ var triggered = false;
+
+ function trigger() {
+ triggered = true;
+ }
+
+ element.bind( "focus", trigger );
+ element[ 0 ].focus();
+
+ if ( !triggered ) {
+ element.triggerHandler( "focus" );
+ }
+ element.unbind( "focus", trigger );
+}
+
+function blur( element ) {
+ var triggered = false;
+
+ function trigger() {
+ triggered = true;
+ }
+
+ element.bind( "blur", trigger );
+ element[ 0 ].blur();
+
+ // Some versions of IE don't actually .blur() on an element - so we focus the body
+ if ( element[ 0 ].ownerDocument.activeElement === element[ 0 ] ) {
+ element[ 0 ].ownerDocument.body.focus();
+ }
+
+ if ( !triggered ) {
+ element.triggerHandler( "blur" );
+ }
+ element.unbind( "blur", trigger );
+}
View
42 ui/jquery.ui.timepicker.js
@@ -113,38 +113,25 @@ $.widget( "ui.timepicker", {
this.element.spinner( "destroy" );
},
_events: {
- blur: function() {
- this._focusing = false;
- },
- click: function() {
- if ( this._focusing ) {
- this._focusing = false;
- this._setPosition( this.currentField );
- } else {
- this._checkPosition();
- this._setPosition();
- }
- },
- keydown: "_checkPosition",
- mousedown: function( event ) {
- if ( document.activeElement !== this.element[0] ) {
- this._focusing = true;
- }
- }
+ click: "_checkPosition",
+ keydown: "_checkPosition"
},
_checkPosition: function( event ) {
var position = this.mask._caret(),
field = Math.floor( position.begin / 3 );
- if ( !this._focusing ) {
- this._setField( field );
- }
+ this._setField( field );
- // if they ever select the first slot only, ensure that the caret
+ // if the cursor is left of the first field, ensure that the selection
// covers the first field to make overtyping make more sense
if ( position.begin === position.end && position.begin === 0 ) {
this.mask._caret( 0, 2 );
}
+
+ // after detecting the new position on a click, we should highlight the new field
+ if ( event.type === "click" ) {
+ this._highlightField();
+ }
},
_generateMask: function() {
var mask = "";
@@ -170,6 +157,9 @@ $.widget( "ui.timepicker", {
}
return mask;
},
+ _highlightField: function( field ) {
+ this.mask._caretSelect( this.currentField * 3 );
+ },
_setField: function( field ) {
this.currentField = field;
switch( field ) {
@@ -247,14 +237,6 @@ $.widget( "ui.timepicker", {
this.element.mask( "option", "mask", this._generateMask() );
}
},
- _setPosition: function( field ) {
- if ( field == null ) {
- field = this.currentField;
- } else {
- this._setField( field );
- }
- this.mask._caretSelect( field * 3 );
- },
_spinnerParse: function( val ) {
val = this.mask.buffer[ this.currentField * 3 ].value;
if ( this.currentField === ( this.options.seconds ? 3 : 2 ) ) {

0 comments on commit 23bd881

Please sign in to comment.
Something went wrong with that request. Please try again.