Skip to content
Permalink
Browse files

Spinner: Added ability to specify custom incremental function.

  • Loading branch information...
scottgonzalez committed Sep 10, 2011
1 parent 4387d19 commit 7216c08b2fd9ad5e3cb90500f2b13bed6339f2ba
Showing with 74 additions and 41 deletions.
  1. +65 −38 tests/unit/spinner/spinner_options.js
  2. +9 −3 ui/jquery.ui.spinner.js
@@ -2,6 +2,71 @@

module( "spinner: options" );

test( "incremental, false", function() {
expect( 100 );

var i, diff,
prev = 0,
element = $( "#spin" ).val( prev ).spinner({
incremental: false,
spin: function( event, ui ) {
equal( ui.value - prev, 1 );
prev = ui.value;
}
});

for ( i = 0; i < 100; i++ ) {
element.simulate( "keydown", { keyCode: $.ui.keyCode.UP } );
}
});

test( "incremental, true", function() {
expect( 100 );

function fill( num, val ) {
return $.map( new Array( num ), function() {
return val;
});
}

var i, diff,
prev = 0,
expected = [].concat( fill( 18, 1 ), fill( 37, 2 ), fill( 14, 3 ),
fill( 9, 4 ), fill( 6, 5 ), fill( 5, 6 ), fill ( 5, 7 ),
fill( 4, 8 ), fill( 2, 9 ) ),
element = $( "#spin" ).val( prev ).spinner({
incremental: true,
spin: function( event, ui ) {
equal( ui.value - prev, expected[ i ] );
prev = ui.value;
}
});

for ( i = 0; i < 100; i++ ) {
element.simulate( "keydown", { keyCode: $.ui.keyCode.UP } );
}
});

test( "incremental, function", function() {
expect( 100 );

var i,
prev = 0,
element = $( "#spin" ).val( prev ).spinner({
incremental: function( i ) {
return i;
},
spin: function( event, ui ) {
equal( ui.value - prev, i + 1 );
prev = ui.value;
}
});

for ( i = 0; i < 100; i++ ) {
element.simulate( "keydown", { keyCode: $.ui.keyCode.UP } );
}
});

test( "numberFormat, number", function() {
expect( 2 );
var element = $( "#spin" ).val( 0 ).spinner({ numberFormat: "n" });
@@ -26,44 +91,6 @@ test( "numberFormat, currency", function() {
equal( element.val(), "$1.00", "formatted after step" );
});

/* TODO figure out how to test this properly
test("incremental - false (default)", function() {
var el = $("#spin").spinner({ incremental:false });
for ( var i = 1 ; i<=120 ; i++ ) {
el.simulate("keydown",{keyCode:$.ui.keyCode.UP});
}
el.simulate("keyup",{keyCode:$.ui.keyCode.UP});
equals(el.val(), 120, "incremental false - keydown 120 times");
for ( var i = 1 ; i<=210 ; i++ ) {
el.simulate("keydown",{keyCode:$.ui.keyCode.DOWN});
}
el.simulate("keyup",{keyCode:$.ui.keyCode.DOWN});
equals(el.val(), -90, "incremental false - keydown 210 times");
});
test("incremental - true (default)", function() {
var el = $("#spin").spinner();
for ( var i = 1 ; i<=120 ; i++ ) {
el.simulate("keydown",{keyCode:$.ui.keyCode.UP});
}
el.simulate("keyup",{keyCode:$.ui.keyCode.UP});
equals(el.val(), 300, "incremental true - keydown 120 times (100+20*10)");
for ( var i = 1 ; i<=210 ; i++ ) {
el.simulate("keydown",{keyCode:$.ui.keyCode.DOWN});
}
el.simulate("keyup",{keyCode:$.ui.keyCode.DOWN});
equals(el.val(), -1800, "incremental true - keydown 210 times (300-100-100*10-10*100)");
});
*/

test( "max", function() {
expect( 3 );
var element = $( "#spin" ).val( 1000 ).spinner({ max: 100 });
@@ -233,9 +233,15 @@ $.widget( "ui.spinner", {
},

_increment: function( i ) {
return this.options.incremental ?
Math.floor( i*i*i/50000 - i*i/500 + 17*i/200 + 1 ) :
1;
var incremental = this.options.incremental;

if ( incremental ) {
return $.isFunction( incremental ) ?
incremental( i ) :
Math.floor( i*i*i/50000 - i*i/500 + 17*i/200 + 1 );
}

return 1;
},

_precision: function( num ) {

0 comments on commit 7216c08

Please sign in to comment.
You can’t perform that action at this time.