/*! nouislider - 9.2.0 - 2017-01-11 10:35:34 */ (function (factory) { if ( typeof define === 'function' && define.amd ) { // AMD. Register as an anonymous module. define([], factory); } else if ( typeof exports === 'object' ) { // Node/CommonJS module.exports = factory(); } else { // Browser globals window.noUiSlider = factory(); } }(function( ){ 'use strict'; var VERSION = '9.2.0'; // Creates a node, adds it to target, returns the new node. function addNodeTo ( target, className ) { var div = document.createElement('div'); addClass(div, className); target.appendChild(div); return div; } // Removes duplicates from an array. function unique ( array ) { return array.filter(function(a){ return !this[a] ? this[a] = true : false; }, {}); } // Round a value to the closest 'to'. function closest ( value, to ) { return Math.round(value / to) * to; } // Current position of an element relative to the document. function offset ( elem, orientation ) { var rect = elem.getBoundingClientRect(), doc = elem.ownerDocument, docElem = doc.documentElement, pageOffset = getPageOffset(); // getBoundingClientRect contains left scroll in Chrome on Android. // I haven't found a feature detection that proves this. Worst case // scenario on mis-match: the 'tap' feature on horizontal sliders breaks. if ( /webkit.*Chrome.*Mobile/i.test(navigator.userAgent) ) { pageOffset.x = 0; } return orientation ? (rect.top + pageOffset.y - docElem.clientTop) : (rect.left + pageOffset.x - docElem.clientLeft); } // Checks whether a value is numerical. function isNumeric ( a ) { return typeof a === 'number' && !isNaN( a ) && isFinite( a ); } // Sets a class and removes it after [duration] ms. function addClassFor ( element, className, duration ) { if (duration > 0) { addClass(element, className); setTimeout(function(){ removeClass(element, className); }, duration); } } // Limits a value to 0 - 100 function limit ( a ) { return Math.max(Math.min(a, 100), 0); } // Wraps a variable as an array, if it isn't one yet. // Note that an input array is returned by reference! function asArray ( a ) { return Array.isArray(a) ? a : [a]; } // Counts decimals function countDecimals ( numStr ) { numStr = String(numStr); var pieces = numStr.split("."); return pieces.length > 1 ? pieces[1].length : 0; } // http://youmightnotneedjquery.com/#add_class function addClass ( el, className ) { if ( el.classList ) { el.classList.add(className); } else { el.className += ' ' + className; } } // http://youmightnotneedjquery.com/#remove_class function removeClass ( el, className ) { if ( el.classList ) { el.classList.remove(className); } else { el.className = el.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' '); } } // https://plainjs.com/javascript/attributes/adding-removing-and-testing-for-classes-9/ function hasClass ( el, className ) { return el.classList ? el.classList.contains(className) : new RegExp('\\b' + className + '\\b').test(el.className); } // https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollY#Notes function getPageOffset ( ) { var supportPageOffset = window.pageXOffset !== undefined, isCSS1Compat = ((document.compatMode || "") === "CSS1Compat"), x = supportPageOffset ? window.pageXOffset : isCSS1Compat ? document.documentElement.scrollLeft : document.body.scrollLeft, y = supportPageOffset ? window.pageYOffset : isCSS1Compat ? document.documentElement.scrollTop : document.body.scrollTop; return { x: x, y: y }; } // we provide a function to compute constants instead // of accessing window.* as soon as the module needs it // so that we do not compute anything if not needed function getActions ( ) { // Determine the events to bind. IE11 implements pointerEvents without // a prefix, which breaks compatibility with the IE10 implementation. return window.navigator.pointerEnabled ? { start: 'pointerdown', move: 'pointermove', end: 'pointerup' } : window.navigator.msPointerEnabled ? { start: 'MSPointerDown', move: 'MSPointerMove', end: 'MSPointerUp' } : { start: 'mousedown touchstart', move: 'mousemove touchmove', end: 'mouseup touchend' }; } // Value calculation // Determine the size of a sub-range in relation to a full range. function subRangeRatio ( pa, pb ) { return (100 / (pb - pa)); } // (percentage) How many percent is this value of this range? function fromPercentage ( range, value ) { return (value * 100) / ( range[1] - range[0] ); } // (percentage) Where is this value on this range? function toPercentage ( range, value ) { return fromPercentage( range, range[0] < 0 ? value + Math.abs(range[0]) : value - range[0] ); } // (value) How much is this percentage on this range? function isPercentage ( range, value ) { return ((value * ( range[1] - range[0] )) / 100) + range[0]; } // Range conversion function getJ ( value, arr ) { var j = 1; while ( value >= arr[j] ){ j += 1; } return j; } // (percentage) Input a value, find where, on a scale of 0-100, it applies. function toStepping ( xVal, xPct, value ) { if ( value >= xVal.slice(-1)[0] ){ return 100; } var j = getJ( value, xVal ), va, vb, pa, pb; va = xVal[j-1]; vb = xVal[j]; pa = xPct[j-1]; pb = xPct[j]; return pa + (toPercentage([va, vb], value) / subRangeRatio (pa, pb)); } // (value) Input a percentage, find where it is on the specified range. function fromStepping ( xVal, xPct, value ) { // There is no range group that fits 100 if ( value >= 100 ){ return xVal.slice(-1)[0]; } var j = getJ( value, xPct ), va, vb, pa, pb; va = xVal[j-1]; vb = xVal[j]; pa = xPct[j-1]; pb = xPct[j]; return isPercentage([va, vb], (value - pa) * subRangeRatio (pa, pb)); } // (percentage) Get the step that applies at a certain value. function getStep ( xPct, xSteps, snap, value ) { if ( value === 100 ) { return value; } var j = getJ( value, xPct ), a, b; // If 'snap' is set, steps are used as fixed points on the slider. if ( snap ) { a = xPct[j-1]; b = xPct[j]; // Find the closest position, a or b. if ((value - a) > ((b-a)/2)){ return b; } return a; } if ( !xSteps[j-1] ){ return value; } return xPct[j-1] + closest( value - xPct[j-1], xSteps[j-1] ); } // Entry parsing function handleEntryPoint ( index, value, that ) { var percentage; // Wrap numerical input in an array. if ( typeof value === "number" ) { value = [value]; } // Reject any invalid input, by testing whether value is an array. if ( Object.prototype.toString.call( value ) !== '[object Array]' ){ throw new Error("noUiSlider (" + VERSION + "): 'range' contains invalid value."); } // Covert min/max syntax to 0 and 100. if ( index === 'min' ) { percentage = 0; } else if ( index === 'max' ) { percentage = 100; } else { percentage = parseFloat( index ); } // Check for correct input. if ( !isNumeric( percentage ) || !isNumeric( value[0] ) ) { throw new Error("noUiSlider (" + VERSION + "): 'range' value isn't numeric."); } // Store values. that.xPct.push( percentage ); that.xVal.push( value[0] ); // NaN will evaluate to false too, but to keep // logging clear, set step explicitly. Make sure // not to override the 'step' setting with false. if ( !percentage ) { if ( !isNaN( value[1] ) ) { that.xSteps[0] = value[1]; } } else { that.xSteps.push( isNaN(value[1]) ? false : value[1] ); } that.xHighestCompleteStep.push(0); } function handleStepPoint ( i, n, that ) { // Ignore 'false' stepping. if ( !n ) { return true; } // Factor to range ratio that.xSteps[i] = fromPercentage([ that.xVal[i] ,that.xVal[i+1] ], n) / subRangeRatio ( that.xPct[i], that.xPct[i+1] ); var totalSteps = (that.xVal[i+1] - that.xVal[i]) / that.xNumSteps[i]; var highestStep = Math.ceil(Number(totalSteps.toFixed(3)) - 1); var step = that.xVal[i] + (that.xNumSteps[i] * highestStep); that.xHighestCompleteStep[i] = step; } // Interface // The interface to Spectrum handles all direction-based // conversions, so the above values are unaware. function Spectrum ( entry, snap, direction, singleStep ) { this.xPct = []; this.xVal = []; this.xSteps = [ singleStep || false ]; this.xNumSteps = [ false ]; this.xHighestCompleteStep = []; this.snap = snap; this.direction = direction; var index, ordered = [ /* [0, 'min'], [1, '50%'], [2, 'max'] */ ]; // Map the object keys to an array. for ( index in entry ) { if ( entry.hasOwnProperty(index) ) { ordered.push([entry[index], index]); } } // Sort all entries by value (numeric sort). if ( ordered.length && typeof ordered[0][0] === "object" ) { ordered.sort(function(a, b) { return a[0][0] - b[0][0]; }); } else { ordered.sort(function(a, b) { return a[0] - b[0]; }); } // Convert all entries to subranges. for ( index = 0; index < ordered.length; index++ ) { handleEntryPoint(ordered[index][1], ordered[index][0], this); } // Store the actual step values. // xSteps is sorted in the same order as xPct and xVal. this.xNumSteps = this.xSteps.slice(0); // Convert all numeric steps to the percentage of the subrange they represent. for ( index = 0; index < this.xNumSteps.length; index++ ) { handleStepPoint(index, this.xNumSteps[index], this); } } Spectrum.prototype.getMargin = function ( value ) { var step = this.xNumSteps[0]; if ( step && ((value / step) % 1) !== 0 ) { throw new Error("noUiSlider (" + VERSION + "): 'limit', 'margin' and 'padding' must be divisible by step."); } return this.xPct.length === 2 ? fromPercentage(this.xVal, value) : false; }; Spectrum.prototype.toStepping = function ( value ) { value = toStepping( this.xVal, this.xPct, value ); return value; }; Spectrum.prototype.fromStepping = function ( value ) { return fromStepping( this.xVal, this.xPct, value ); }; Spectrum.prototype.getStep = function ( value ) { value = getStep(this.xPct, this.xSteps, this.snap, value ); return value; }; Spectrum.prototype.getNearbySteps = function ( value ) { var j = getJ(value, this.xPct); return { stepBefore: { startValue: this.xVal[j-2], step: this.xNumSteps[j-2], highestStep: this.xHighestCompleteStep[j-2] }, thisStep: { startValue: this.xVal[j-1], step: this.xNumSteps[j-1], highestStep: this.xHighestCompleteStep[j-1] }, stepAfter: { startValue: this.xVal[j-0], step: this.xNumSteps[j-0], highestStep: this.xHighestCompleteStep[j-0] } }; }; Spectrum.prototype.countStepDecimals = function () { var stepDecimals = this.xNumSteps.map(countDecimals); return Math.max.apply(null, stepDecimals); }; // Outside testing Spectrum.prototype.convert = function ( value ) { return this.getStep(this.toStepping(value)); }; /* Every input option is tested and parsed. This'll prevent endless validation in internal methods. These tests are structured with an item for every option available. An option can be marked as required by setting the 'r' flag. The testing function is provided with three arguments: - The provided value for the option; - A reference to the options object; - The name for the option; The testing function returns false when an error is detected, or true when everything is OK. It can also modify the option object, to make sure all values can be correctly looped elsewhere. */ var defaultFormatter = { 'to': function( value ){ return value !== undefined && value.toFixed(2); }, 'from': Number }; function testStep ( parsed, entry ) { if ( !isNumeric( entry ) ) { throw new Error("noUiSlider (" + VERSION + "): 'step' is not numeric."); } // The step option can still be used to set stepping // for linear sliders. Overwritten if set in 'range'. parsed.singleStep = entry; } function testRange ( parsed, entry ) { // Filter incorrect input. if ( typeof entry !== 'object' || Array.isArray(entry) ) { throw new Error("noUiSlider (" + VERSION + "): 'range' is not an object."); } // Catch missing start or end. if ( entry.min === undefined || entry.max === undefined ) { throw new Error("noUiSlider (" + VERSION + "): Missing 'min' or 'max' in 'range'."); } // Catch equal start or end. if ( entry.min === entry.max ) { throw new Error("noUiSlider (" + VERSION + "): 'range' 'min' and 'max' cannot be equal."); } parsed.spectrum = new Spectrum(entry, parsed.snap, parsed.dir, parsed.singleStep); } function testStart ( parsed, entry ) { entry = asArray(entry); // Validate input. Values aren't tested, as the public .val method // will always provide a valid location. if ( !Array.isArray( entry ) || !entry.length ) { throw new Error("noUiSlider (" + VERSION + "): 'start' option is incorrect."); } // Store the number of handles. parsed.handles = entry.length; // When the slider is initialized, the .val method will // be called with the start options. parsed.start = entry; } function testSnap ( parsed, entry ) { // Enforce 100% stepping within subranges. parsed.snap = entry; if ( typeof entry !== 'boolean' ){ throw new Error("noUiSlider (" + VERSION + "): 'snap' option must be a boolean."); } } function testAnimate ( parsed, entry ) { // Enforce 100% stepping within subranges. parsed.animate = entry; if ( typeof entry !== 'boolean' ){ throw new Error("noUiSlider (" + VERSION + "): 'animate' option must be a boolean."); } } function testAnimationDuration ( parsed, entry ) { parsed.animationDuration = entry; if ( typeof entry !== 'number' ){ throw new Error("noUiSlider (" + VERSION + "): 'animationDuration' option must be a number."); } } function testConnect ( parsed, entry ) { var connect = [false]; var i; // Map legacy options if ( entry === 'lower' ) { entry = [true, false]; } else if ( entry === 'upper' ) { entry = [false, true]; } // Handle boolean options if ( entry === true || entry === false ) { for ( i = 1; i < parsed.handles; i++ ) { connect.push(entry); } connect.push(false); } // Reject invalid input else if ( !Array.isArray( entry ) || !entry.length || entry.length !== parsed.handles + 1 ) { throw new Error("noUiSlider (" + VERSION + "): 'connect' option doesn't match handle count."); } else { connect = entry; } parsed.connect = connect; } function testOrientation ( parsed, entry ) { // Set orientation to an a numerical value for easy // array selection. switch ( entry ){ case 'horizontal': parsed.ort = 0; break; case 'vertical': parsed.ort = 1; break; default: throw new Error("noUiSlider (" + VERSION + "): 'orientation' option is invalid."); } } function testMargin ( parsed, entry ) { if ( !isNumeric(entry) ){ throw new Error("noUiSlider (" + VERSION + "): 'margin' option must be numeric."); } // Issue #582 if ( entry === 0 ) { return; } parsed.margin = parsed.spectrum.getMargin(entry); if ( !parsed.margin ) { throw new Error("noUiSlider (" + VERSION + "): 'margin' option is only supported on linear sliders."); } } function testLimit ( parsed, entry ) { if ( !isNumeric(entry) ){ throw new Error("noUiSlider (" + VERSION + "): 'limit' option must be numeric."); } parsed.limit = parsed.spectrum.getMargin(entry); if ( !parsed.limit || parsed.handles < 2 ) { throw new Error("noUiSlider (" + VERSION + "): 'limit' option is only supported on linear sliders with 2 or more handles."); } } function testPadding ( parsed, entry ) { if ( !isNumeric(entry) ){ throw new Error("noUiSlider (" + VERSION + "): 'padding' option must be numeric."); } if ( entry === 0 ) { return; } parsed.padding = parsed.spectrum.getMargin(entry); if ( !parsed.padding ) { throw new Error("noUiSlider (" + VERSION + "): 'padding' option is only supported on linear sliders."); } if ( parsed.padding < 0 ) { throw new Error("noUiSlider (" + VERSION + "): 'padding' option must be a positive number."); } if ( parsed.padding >= 50 ) { throw new Error("noUiSlider (" + VERSION + "): 'padding' option must be less than half the range."); } } function testDirection ( parsed, entry ) { // Set direction as a numerical value for easy parsing. // Invert connection for RTL sliders, so that the proper // handles get the connect/background classes. switch ( entry ) { case 'ltr': parsed.dir = 0; break; case 'rtl': parsed.dir = 1; break; default: throw new Error("noUiSlider (" + VERSION + "): 'direction' option was not recognized."); } } function testBehaviour ( parsed, entry ) { // Make sure the input is a string. if ( typeof entry !== 'string' ) { throw new Error("noUiSlider (" + VERSION + "): 'behaviour' must be a string containing options."); } // Check if the string contains any keywords. // None are required. var tap = entry.indexOf('tap') >= 0; var drag = entry.indexOf('drag') >= 0; var fixed = entry.indexOf('fixed') >= 0; var snap = entry.indexOf('snap') >= 0; var hover = entry.indexOf('hover') >= 0; if ( fixed ) { if ( parsed.handles !== 2 ) { throw new Error("noUiSlider (" + VERSION + "): 'fixed' behaviour must be used with 2 handles"); } // Use margin to enforce fixed state testMargin(parsed, parsed.start[1] - parsed.start[0]); } parsed.events = { tap: tap || snap, drag: drag, fixed: fixed, snap: snap, hover: hover }; } function testTooltips ( parsed, entry ) { if ( entry === false ) { return; } else if ( entry === true ) { parsed.tooltips = []; for ( var i = 0; i < parsed.handles; i++ ) { parsed.tooltips.push(true); } } else { parsed.tooltips = asArray(entry); if ( parsed.tooltips.length !== parsed.handles ) { throw new Error("noUiSlider (" + VERSION + "): must pass a formatter for all handles."); } parsed.tooltips.forEach(function(formatter){ if ( typeof formatter !== 'boolean' && (typeof formatter !== 'object' || typeof formatter.to !== 'function') ) { throw new Error("noUiSlider (" + VERSION + "): 'tooltips' must be passed a formatter or 'false'."); } }); } } function testFormat ( parsed, entry ) { parsed.format = entry; // Any object with a to and from method is supported. if ( typeof entry.to === 'function' && typeof entry.from === 'function' ) { return true; } throw new Error("noUiSlider (" + VERSION + "): 'format' requires 'to' and 'from' methods."); } function testCssPrefix ( parsed, entry ) { if ( entry !== undefined && typeof entry !== 'string' && entry !== false ) { throw new Error("noUiSlider (" + VERSION + "): 'cssPrefix' must be a string or `false`."); } parsed.cssPrefix = entry; } function testCssClasses ( parsed, entry ) { if ( entry !== undefined && typeof entry !== 'object' ) { throw new Error("noUiSlider (" + VERSION + "): 'cssClasses' must be an object."); } if ( typeof parsed.cssPrefix === 'string' ) { parsed.cssClasses = {}; for ( var key in entry ) { if ( !entry.hasOwnProperty(key) ) { continue; } parsed.cssClasses[key] = parsed.cssPrefix + entry[key]; } } else { parsed.cssClasses = entry; } } function testUseRaf ( parsed, entry ) { if ( entry === true || entry === false ) { parsed.useRequestAnimationFrame = entry; } else { throw new Error("noUiSlider (" + VERSION + "): 'useRequestAnimationFrame' option should be true (default) or false."); } } // Test all developer settings and parse to assumption-safe values. function testOptions ( options ) { // To prove a fix for #537, freeze options here. // If the object is modified, an error will be thrown. // Object.freeze(options); var parsed = { margin: 0, limit: 0, padding: 0, animate: true, animationDuration: 300, format: defaultFormatter }; // Tests are executed in the order they are presented here. var tests = { 'step': { r: false, t: testStep }, 'start': { r: true, t: testStart }, 'connect': { r: true, t: testConnect }, 'direction': { r: true, t: testDirection }, 'snap': { r: false, t: testSnap }, 'animate': { r: false, t: testAnimate }, 'animationDuration': { r: false, t: testAnimationDuration }, 'range': { r: true, t: testRange }, 'orientation': { r: false, t: testOrientation }, 'margin': { r: false, t: testMargin }, 'limit': { r: false, t: testLimit }, 'padding': { r: false, t: testPadding }, 'behaviour': { r: true, t: testBehaviour }, 'format': { r: false, t: testFormat }, 'tooltips': { r: false, t: testTooltips }, 'cssPrefix': { r: false, t: testCssPrefix }, 'cssClasses': { r: false, t: testCssClasses }, 'useRequestAnimationFrame': { r: false, t: testUseRaf } }; var defaults = { 'connect': false, 'direction': 'ltr', 'behaviour': 'tap', 'orientation': 'horizontal', 'cssPrefix' : 'noUi-', 'cssClasses': { target: 'target', base: 'base', origin: 'origin', handle: 'handle', handleLower: 'handle-lower', handleUpper: 'handle-upper', horizontal: 'horizontal', vertical: 'vertical', background: 'background', connect: 'connect', ltr: 'ltr', rtl: 'rtl', draggable: 'draggable', drag: 'state-drag', tap: 'state-tap', active: 'active', tooltip: 'tooltip', pips: 'pips', pipsHorizontal: 'pips-horizontal', pipsVertical: 'pips-vertical', marker: 'marker', markerHorizontal: 'marker-horizontal', markerVertical: 'marker-vertical', markerNormal: 'marker-normal', markerLarge: 'marker-large', markerSub: 'marker-sub', value: 'value', valueHorizontal: 'value-horizontal', valueVertical: 'value-vertical', valueNormal: 'value-normal', valueLarge: 'value-large', valueSub: 'value-sub' }, 'useRequestAnimationFrame': true }; // Run all options through a testing mechanism to ensure correct // input. It should be noted that options might get modified to // be handled properly. E.g. wrapping integers in arrays. Object.keys(tests).forEach(function( name ){ // If the option isn't set, but it is required, throw an error. if ( options[name] === undefined && defaults[name] === undefined ) { if ( tests[name].r ) { throw new Error("noUiSlider (" + VERSION + "): '" + name + "' is required."); } return true; } tests[name].t( parsed, options[name] === undefined ? defaults[name] : options[name] ); }); // Forward pips options parsed.pips = options.pips; var styles = [['left', 'top'], ['right', 'bottom']]; // Pre-define the styles. parsed.style = styles[parsed.dir][parsed.ort]; parsed.styleOposite = styles[parsed.dir?0:1][parsed.ort]; return parsed; } function closure ( target, options, originalOptions ){ var actions = getActions( ); // All variables local to 'closure' are prefixed with 'scope_' var scope_Target = target; var scope_Locations = []; var scope_Base; var scope_Handles; var scope_HandleNumbers = []; var scope_ActiveHandle = false; var scope_Connects; var scope_Spectrum = options.spectrum; var scope_Values = []; var scope_Events = {}; var scope_Self; // Append a origin to the base function addOrigin ( base, handleNumber ) { var origin = addNodeTo(base, options.cssClasses.origin); var handle = addNodeTo(origin, options.cssClasses.handle); handle.setAttribute('data-handle', handleNumber); if ( handleNumber === 0 ) { addClass(handle, options.cssClasses.handleLower); } else if ( handleNumber === options.handles - 1 ) { addClass(handle, options.cssClasses.handleUpper); } return origin; } // Insert nodes for connect elements function addConnect ( base, add ) { if ( !add ) { return false; } return addNodeTo(base, options.cssClasses.connect); } // Add handles to the slider base. function addElements ( connectOptions, base ) { scope_Handles = []; scope_Connects = []; scope_Connects.push(addConnect(base, connectOptions[0])); // [::::O====O====O====] // connectOptions = [0, 1, 1, 1] for ( var i = 0; i < options.handles; i++ ) { // Keep a list of all added handles. scope_Handles.push(addOrigin(base, i)); scope_HandleNumbers[i] = i; scope_Connects.push(addConnect(base, connectOptions[i + 1])); } } // Initialize a single slider. function addSlider ( target ) { // Apply classes and data to the target. addClass(target, options.cssClasses.target); if ( options.dir === 0 ) { addClass(target, options.cssClasses.ltr); } else { addClass(target, options.cssClasses.rtl); } if ( options.ort === 0 ) { addClass(target, options.cssClasses.horizontal); } else { addClass(target, options.cssClasses.vertical); } scope_Base = addNodeTo(target, options.cssClasses.base); } function addTooltip ( handle, handleNumber ) { if ( !options.tooltips[handleNumber] ) { return false; } return addNodeTo(handle.firstChild, options.cssClasses.tooltip); } // The tooltips option is a shorthand for using the 'update' event. function tooltips ( ) { // Tooltips are added with options.tooltips in original order. var tips = scope_Handles.map(addTooltip); bindEvent('update', function(values, handleNumber, unencoded) { if ( !tips[handleNumber] ) { return; } var formattedValue = values[handleNumber]; if ( options.tooltips[handleNumber] !== true ) { formattedValue = options.tooltips[handleNumber].to(unencoded[handleNumber]); } tips[handleNumber].innerHTML = formattedValue; }); } function getGroup ( mode, values, stepped ) { // Use the range. if ( mode === 'range' || mode === 'steps' ) { return scope_Spectrum.xVal; } if ( mode === 'count' ) { if ( !values ) { throw new Error("noUiSlider (" + VERSION + "): 'values' required for mode 'count'."); } // Divide 0 - 100 in 'count' parts. var spread = ( 100 / (values - 1) ); var v; var i = 0; values = []; // List these parts and have them handled as 'positions'. while ( (v = i++ * spread) <= 100 ) { values.push(v); } mode = 'positions'; } if ( mode === 'positions' ) { // Map all percentages to on-range values. return values.map(function( value ){ return scope_Spectrum.fromStepping( stepped ? scope_Spectrum.getStep( value ) : value ); }); } if ( mode === 'values' ) { // If the value must be stepped, it needs to be converted to a percentage first. if ( stepped ) { return values.map(function( value ){ // Convert to percentage, apply step, return to value. return scope_Spectrum.fromStepping( scope_Spectrum.getStep( scope_Spectrum.toStepping( value ) ) ); }); } // Otherwise, we can simply use the values. return values; } } function generateSpread ( density, mode, group ) { function safeIncrement(value, increment) { // Avoid floating point variance by dropping the smallest decimal places. return (value + increment).toFixed(7) / 1; } var indexes = {}; var firstInRange = scope_Spectrum.xVal[0]; var lastInRange = scope_Spectrum.xVal[scope_Spectrum.xVal.length-1]; var ignoreFirst = false; var ignoreLast = false; var prevPct = 0; // Create a copy of the group, sort it and filter away all duplicates. group = unique(group.slice().sort(function(a, b){ return a - b; })); // Make sure the range starts with the first element. if ( group[0] !== firstInRange ) { group.unshift(firstInRange); ignoreFirst = true; } // Likewise for the last one. if ( group[group.length - 1] !== lastInRange ) { group.push(lastInRange); ignoreLast = true; } group.forEach(function ( current, index ) { // Get the current step and the lower + upper positions. var step; var i; var q; var low = current; var high = group[index+1]; var newPct; var pctDifference; var pctPos; var type; var steps; var realSteps; var stepsize; // When using 'steps' mode, use the provided steps. // Otherwise, we'll step on to the next subrange. if ( mode === 'steps' ) { step = scope_Spectrum.xNumSteps[ index ]; } // Default to a 'full' step. if ( !step ) { step = high-low; } // Low can be 0, so test for false. If high is undefined, // we are at the last subrange. Index 0 is already handled. if ( low === false || high === undefined ) { return; } // Make sure step isn't 0, which would cause an infinite loop (#654) step = Math.max(step, 0.0000001); // Find all steps in the subrange. for ( i = low; i <= high; i = safeIncrement(i, step) ) { // Get the percentage value for the current step, // calculate the size for the subrange. newPct = scope_Spectrum.toStepping( i ); pctDifference = newPct - prevPct; steps = pctDifference / density; realSteps = Math.round(steps); // This ratio represents the ammount of percentage-space a point indicates. // For a density 1 the points/percentage = 1. For density 2, that percentage needs to be re-devided. // Round the percentage offset to an even number, then divide by two // to spread the offset on both sides of the range. stepsize = pctDifference/realSteps; // Divide all points evenly, adding the correct number to this subrange. // Run up to <= so that 100% gets a point, event if ignoreLast is set. for ( q = 1; q <= realSteps; q += 1 ) { // The ratio between the rounded value and the actual size might be ~1% off. // Correct the percentage offset by the number of points // per subrange. density = 1 will result in 100 points on the // full range, 2 for 50, 4 for 25, etc. pctPos = prevPct + ( q * stepsize ); indexes[pctPos.toFixed(5)] = ['x', 0]; } // Determine the point type. type = (group.indexOf(i) > -1) ? 1 : ( mode === 'steps' ? 2 : 0 ); // Enforce the 'ignoreFirst' option by overwriting the type for 0. if ( !index && ignoreFirst ) { type = 0; } if ( !(i === high && ignoreLast)) { // Mark the 'type' of this point. 0 = plain, 1 = real value, 2 = step value. indexes[newPct.toFixed(5)] = [i, type]; } // Update the percentage count. prevPct = newPct; } }); return indexes; } function addMarking ( spread, filterFunc, formatter ) { var element = document.createElement('div'); var out = ''; var valueSizeClasses = [ options.cssClasses.valueNormal, options.cssClasses.valueLarge, options.cssClasses.valueSub ]; var markerSizeClasses = [ options.cssClasses.markerNormal, options.cssClasses.markerLarge, options.cssClasses.markerSub ]; var valueOrientationClasses = [ options.cssClasses.valueHorizontal, options.cssClasses.valueVertical ]; var markerOrientationClasses = [ options.cssClasses.markerHorizontal, options.cssClasses.markerVertical ]; addClass(element, options.cssClasses.pips); addClass(element, options.ort === 0 ? options.cssClasses.pipsHorizontal : options.cssClasses.pipsVertical); function getClasses( type, source ){ var a = source === options.cssClasses.value; var orientationClasses = a ? valueOrientationClasses : markerOrientationClasses; var sizeClasses = a ? valueSizeClasses : markerSizeClasses; return source + ' ' + orientationClasses[options.ort] + ' ' + sizeClasses[type]; } function getTags( offset, source, values ) { return 'class="' + getClasses(values[1], source) + '" style="' + options.style + ': ' + offset + '%"'; } function addSpread ( offset, values ){ // Apply the filter function, if it is set. values[1] = (values[1] && filterFunc) ? filterFunc(values[0], values[1]) : values[1]; // Add a marker for every point out += '
'; // Values are only appended for points marked '1' or '2'. if ( values[1] ) { out += '