]> source.dussan.org Git - jquery-ui.git/commitdiff
Spinner: Added ability to specify custom incremental function.
authorScott González <scott.gonzalez@gmail.com>
Sat, 10 Sep 2011 00:08:51 +0000 (20:08 -0400)
committerScott González <scott.gonzalez@gmail.com>
Sat, 10 Sep 2011 00:08:51 +0000 (20:08 -0400)
tests/unit/spinner/spinner_options.js
ui/jquery.ui.spinner.js

index 647455fe5ac2e77282855f276bc2293faa012fd3..48bdf96a9e2b2cb2281117907abed2ba4bf18e0c 100644 (file)
@@ -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 });
index 97ee20e5455e9c23a7cf676eb8b23ea4439d5907..a7fb30cc365116c3a5f529cd7f7afe9ae3918630 100644 (file)
@@ -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 ) {