MINI Sh3ll
// Test suggested values and apply margin, step.
function setHandle ( handle, to, noLimitOption ) {
var trigger = handle[0] !== $Handles[0][0] ? 1 : 0,
lowerMargin = $Locations[0] + options.margin,
upperMargin = $Locations[1] - options.margin,
lowerLimit = $Locations[0] + options.limit,
upperLimit = $Locations[1] - options.limit;
// For sliders with multiple handles,
// limit movement to the other handle.
// Apply the margin option by adding it to the handle positions.
if ( $Handles.length > 1 ) {
to = trigger ? Math.max( to, lowerMargin ) : Math.min( to, upperMargin );
}
// The limit option has the opposite effect, limiting handles to a
// maximum distance from another. Limit must be > 0, as otherwise
// handles would be unmoveable. 'noLimitOption' is set to 'false'
// for the .val() method, except for pass 4/4.
if ( noLimitOption !== false && options.limit && $Handles.length > 1 ) {
to = trigger ? Math.min ( to, lowerLimit ) : Math.max( to, upperLimit );
}
// Handle the step option.
to = $Spectrum.getStep( to );
// Limit to 0/100 for .val input, trim anything beyond 7 digits, as
// JavaScript has some issues in its floating point implementation.
to = limit(parseFloat(to.toFixed(7)));
// Return false if handle can't move.
if ( to === $Locations[trigger] ) {
return false;
}
// Set the handle to the new position.
handle.css( options.style, to + '%' );
// Force proper handle stacking
if ( handle.is(':first-child') ) {
handle.toggleClass(Classes[17], to > 50 );
}
// Update locations.
$Locations[trigger] = to;
// Convert the value to the slider stepping/range.
$Values[trigger] = $Spectrum.fromStepping( to );
linkUpdate(triggerPos[trigger]);
return true;
}
// Loop values from value method and apply them.
function setValues ( count, values ) {
var i, trigger, to;
// With the limit option, we'll need another limiting pass.
if ( options.limit ) {
count += 1;
}
// If there are multiple handles to be set run the setting
// mechanism twice for the first handle, to make sure it
// can be bounced of the second one properly.
for ( i = 0; i < count; i += 1 ) {
trigger = i%2;
// Get the current argument from the array.
to = values[trigger];
// Setting with null indicates an 'ignore'.
// Inputting 'false' is invalid.
if ( to !== null && to !== false ) {
// If a formatted number was passed, attemt to decode it.
if ( typeof to === 'number' ) {
to = String(to);
}
to = options.format.from( to );
// Request an update for all links if the value was invalid.
// Do so too if setting the handle fails.
if ( to === false || isNaN(to) || setHandle( $Handles[trigger], $Spectrum.toStepping( to ), i === (3 - options.dir) ) === false ) {
linkUpdate(triggerPos[trigger]);
}
}
}
}
// Set the slider value.
function valueSet ( input ) {
// LibLink: don't accept new values when currently emitting changes.
if ( $Target[0].LinkIsEmitting ) {
return this;
}
var count, values = asArray( input );
// The RTL settings is implemented by reversing the front-end,
// internal mechanisms are the same.
if ( options.dir && options.handles > 1 ) {
values.reverse();
}
// Animation is optional.
// Make sure the initial values where set before using animated
// placement. (no report, unit testing);
if ( options.animate && $Locations[0] !== -1 ) {
addClassFor( $Target, Classes[14], 300 );
}
// Determine how often to set the handles.
count = $Handles.length > 1 ? 3 : 1;
if ( values.length === 1 ) {
count = 1;
}
setValues ( count, values );
// Fire the 'set' event. As of noUiSlider 7,
// this is no longer optional.
fireEvents(['set']);
return this;
}
// Get the slider value.
function valueGet ( ) {
var i, retour = [];
// Get the value from all handles.
for ( i = 0; i < options.handles; i += 1 ){
retour[i] = options.format.to( $Values[i] );
}
return inSliderOrder( retour );
}
// Destroy the slider and unbind all events.
function destroyTarget ( ) {
// Unbind events on the slider, remove all classes and child elements.
$(this).off(namespace)
.removeClass(Classes.join(' '))
.empty();
delete this.LinkUpdate;
delete this.LinkConfirm;
delete this.LinkDefaultFormatter;
delete this.LinkDefaultFlag;
delete this.reappend;
delete this.vGet;
delete this.vSet;
delete this.getCurrentStep;
delete this.getInfo;
delete this.destroy;
// Return the original options from the closure.
return originalOptions;
}
// Get the current step size for the slider.
function getCurrentStep ( ) {
// Check all locations, map them to their stepping point.
// Get the step point, then find it in the input list.
var retour = $.map($Locations, function( location, index ){
var step = $Spectrum.getApplicableStep( location ),
// As per #391, the comparison for the decrement step can have some rounding issues.
// Round the value to the precision used in the step.
stepDecimals = countDecimals(String(step[2])),
// Get the current numeric value
value = $Values[index],
// To move the slider 'one step up', the current step value needs to be added.
// Use null if we are at the maximum slider value.
increment = location === 100 ? null : step[2],
// Going 'one step down' might put the slider in a different sub-range, so we
// need to switch between the current or the previous step.
prev = Number((value - step[2]).toFixed(stepDecimals)),
// If the value fits the step, return the current step value. Otherwise, use the
// previous step. Return null if the slider is at its minimum value.
decrement = location === 0 ? null : (prev >= step[1]) ? step[2] : (step[0] || false);
return [[decrement, increment]];
});
// Return values in the proper order.
return inSliderOrder( retour );
}
// Get the original set of options.
function getOriginalOptions ( ) {
return originalOptions;
}
// Initialize slider
// Throw an error if the slider was already initialized.
if ( $Target.hasClass(Classes[0]) ) {
throw new Error('Slider was already initialized.');
}
// Create the base element, initialise HTML and set classes.
// Add handles and links.
$Base = addSlider( options.dir, options.ort, $Target );
$Handles = addHandles( options.handles, options.dir, $Base );
// Set the connect classes.
addConnection ( options.connect, $Target, $Handles );
// Attach user events.
events( options.events );
// Methods
target.vSet = valueSet;
target.vGet = valueGet;
target.destroy = destroyTarget;
target.getCurrentStep = getCurrentStep;
target.getOriginalOptions = getOriginalOptions;
target.getInfo = function(){
return [
$Spectrum,
options.style,
options.ort
];
};
// Use the public value method to set the start values.
$Target.val( options.start );
OHA YOOOO