freqdec / fd-slider

An Unobtrusive Accessible Slider script that can also be used as an HTML5 Input Range polyfill solution
Other
264 stars 55 forks source link

Allow user's manual input to override snap #19

Closed getdave closed 12 years ago

getdave commented 12 years ago

Thanks for the great script.

I had a client request that if his user enters a value directly into the input element, then this should overide the "step" and possibly also the formatting rules.

However, if the slider handle is activated it should behave as normal.

See: https://twitter.com/#!/freqdec/status/184989550270091264

I've hacked the script as suggested and I thought I'd share the result just in case it's of interest.

Global options:

userInputSnap       = options.userInputSnap || false, // Option: Do we want user input to snap or not?
userManualInput     = false,    // boolean: has the user manual entered a value in the input 

Method: onInputChange()

 function onInputChange(e) {                       
    userSet = true;

        // Check options and set userManualInput if required
    userInputSnap === true ? userManualInput = true : userManualInput = false;

    valueToPixels(tagName == "input" ? parseFloat(inp.value) : inp.selectedIndex);
    updateAriaValues();        
    userManualInput = false; // reset the value to default false                                  
}; 

Method: setInputValue():

function setInputValue(val) {                       
    ....

    if(val != "" && userManualInput !== true) { // before we snap check that the user hasn't manually entered number into input.
            val = (min + (Math.round((val - min) / step) * step)).toFixed(precision);                                  
    };

    ....
}; 

I hope that makes sense.

freqdec commented 12 years ago

Hi Dave - I shall integrate it into the code this weekend. Thanks for taking the time to do this.

getdave commented 12 years ago

Hi Brian - not a problem. Just a little tweak to provide a tiny bit more flexibility. I hope it's useful.

Cheers

freqdec commented 12 years ago

Finally, back from vacation and added the code. I've renamed the option "userSnap" though (from the suggested "userInputSnap").

Again, thanks for the input - much appreciated!

getdave commented 12 years ago

No problem