1/* 2 * Modified version of Stepper v3.0.7 - 2014-05-07 3 * A jQuery plugin for cross browser number inputs. 4 */ 5;(function ( $ ) { 6 "use strict"; 7 8 /** 9 * @options 10 * @param customClass [string] <''> "Class applied to instance" 11 * @param lables.up [string] <'Up'> "Up arrow label" 12 * @param lables.down [string] <'Down'> "Down arrow label" 13 */ 14 var options = { 15 customClass: "", 16 labels: { 17 up: "+", 18 down: "-" 19 } 20 }; 21 22 var pub = { 23 /** 24 * @method 25 * @name defaults 26 * @description Sets default plugin options 27 * @param opts [object] <{}> "Options object" 28 * @example $.stepper("defaults", opts); 29 */ 30 defaults: function ( opts ) { 31 options = $.extend( options, opts || {} ); 32 return $( this ); 33 }, 34 35 /** 36 * @method 37 * @name destroy 38 * @description Removes instance of plugin 39 * @example $(".target").stepper("destroy"); 40 */ 41 destroy: function () { 42 return $( this ).each( function () { 43 var data = $( this ).data( "stepper" ); 44 45 if ( data ) { 46 // Unbind click events 47 data.$stepper.off( ".stepper" ) 48 .find( ".stepper-arrow-container" ) 49 .remove(); 50 51 // Restore DOM 52 data.$input.unwrap() 53 .removeClass( "stepper-input" ); 54 } 55 } ); 56 }, 57 58 /** 59 * @method 60 * @name disable 61 * @description Disables target instance 62 * @example $(".target").stepper("disable"); 63 */ 64 disable: function () { 65 return $( this ).each( function () { 66 var data = $( this ).data( "stepper" ); 67 68 if ( data ) { 69 data.$input.attr( "disabled", "disabled" ); 70 data.$stepper.addClass( "disabled" ); 71 } 72 } ); 73 }, 74 75 /** 76 * @method 77 * @name enable 78 * @description Enables target instance 79 * @example $(".target").stepper("enable"); 80 */ 81 enable: function () { 82 return $( this ).each( function () { 83 var data = $( this ).data( "stepper" ); 84 85 if ( data ) { 86 data.$input.attr( "disabled", null ); 87 data.$stepper.removeClass( "disabled" ); 88 } 89 } ); 90 } 91 }; 92 93 /** 94 * @method private 95 * @name _init 96 * @description Initializes plugin 97 * @param opts [object] "Initialization options" 98 */ 99 function _init( opts ) { 100 // Local options 101 opts = $.extend( {}, options, opts || {} ); 102 103 // Apply to each element 104 var $items = $( this ); 105 for ( var i = 0, count = $items.length; i < count; i++ ) { 106 _build( $items.eq( i ), opts ); 107 } 108 return $items; 109 } 110 111 /** 112 * @method private 113 * @name _build 114 * @description Builds each instance 115 * @param $input {object} "Target jQuery object" 116 * @param opts {object} <{}> "Options object" 117 */ 118 function _build( $input, opts ) { 119 if ( !$input.hasClass( "stepper-input" ) ) { 120 // EXTEND OPTIONS 121 opts = $.extend( {}, opts, $input.data( "stepper-options" ) ); 122 123 // HTML5 attributes 124 var min = parseFloat( $input.attr( "min" ) ), 125 max = parseFloat( $input.attr( "max" ) ), 126 step = parseFloat( $input.attr( "step" ) ) || 1; 127 128 // Modify DOM 129 $input.addClass( "stepper-input" ) 130 .wrap( '<div class="wpim-stepper stepper ' + opts.customClass + '" />' ) 131 .after( '<span class="stepper-arrow-container"><span class="stepper-arrow up">' + opts.labels.up + '</span><span class="stepper-arrow down">' + opts.labels.down + '</span></span>' ); 132 133 // Store data 134 var $stepper = $input.parent( ".stepper" ), 135 data = $.extend( { 136 $stepper: $stepper, 137 $input: $input, 138 $arrow: $stepper.find( ".stepper-arrow" ), 139 min: (typeof min !== undefined && !isNaN( min )) ? min : false, 140 max: (typeof max !== undefined && !isNaN( max )) ? max : false, 141 step: (typeof step !== undefined && !isNaN( step )) ? step : 1, 142 timer: null 143 }, opts ); 144 145 data.digits = _digits( data.step ); 146 147 // Check disabled 148 if ( $input.is( ":disabled" ) ) { 149 $stepper.addClass( "disabled" ); 150 } 151 152 // Bind keyboard events 153 $stepper.on( "keypress", ".stepper-input", data, _onKeyup ); 154 155 // Bind click events 156 $stepper.on( "touchstart.stepper mousedown.stepper", ".stepper-arrow", data, _onMouseDown ) 157 .data( "stepper", data ); 158 } 159 } 160 161 /** 162 * @method private 163 * @name _onKeyup 164 * @description Handles keypress event on inputs 165 * @param e [object] "Event data" 166 */ 167 function _onKeyup( e ) { 168 var data = e.data; 169 170 // If arrow keys 171 if ( e.keyCode === 38 || e.keyCode === 40 ) { 172 e.preventDefault(); 173
174 _step( data, (e.keyCode === 38) ? data.step : -data.step ); 175 } 176 } 177 178 /** 179 * @method private 180 * @name _onMouseDown 181 * @description Handles mousedown event on instance arrows 182 * @param e {object} "Event data" 183 */ 184 function _onMouseDown( e ) { 185 e.preventDefault(); 186 e.stopPropagation(); 187 188 // Make sure we reset the states 189 _onMouseUp( e ); 190 191 var data = e.data; 192 193 if ( !data.$input.is( ':disabled' ) && !data.$stepper.hasClass( "disabled" ) ) { 194 var change = $( e.target ).hasClass( "up" ) ? data.step : -data.step; 195 196 data.timer = _startTimer( data.timer, 125, function () { 197 _step( data, change ); 198 } ); 199 _step( data, change ); 200 201 $( "body" ).on( "touchend.stepper mouseup.stepper", data, _onMouseUp ); 202 } 203 } 204 205 /** 206 * @method private 207 * @name _onMouseUp 208 * @description Handles mouseup event on instance arrows 209 * @param e {object} "Event data" 210 */ 211 function _onMouseUp( e ) { 212 e.preventDefault(); 213 e.stopPropagation(); 214 215 var data = e.data; 216 217 _clearTimer( data.timer ); 218 219 $( "body" ).off( ".stepper" ); 220 } 221 222 /** 223 * @method private 224 * @name _step 225 * @description Steps through values 226 * @param data {object} "Event data" 227 * @param change {string} "Change value" 228 */ 229 function _step( data, change ) { 230 var originalValue = parseFloat( data.$input.val() ), 231 value = change; 232 233 if ( typeof originalValue === undefined || isNaN( originalValue ) ) { 234 if ( data.min !== false ) { 235 value = data.min; 236 } else { 237 value = 0; 238 } 239 } else if ( data.min !== false && originalValue < data.min ) { 240 value = data.min; 241 } else { 242 value += originalValue; 243 } 244 245 var diff = (value - data.min) % data.step; 246 if ( diff !== 0 ) { 247 value -= diff; 248 } 249 250 if ( data.min !== false && value < data.min ) { 251 value = data.min; 252 } 253 254 if ( data.max !== false && value > data.max ) { 255 value -= data.step; 256 } 257 258 if ( value !== originalValue ) { 259 value = _round( value, data.digits ); 260 261 data.$input.val( value ) 262 .trigger( "change" ); 263 } 264 } 265 266 /** 267 * @method private 268 * @name _startTimer 269 * @description Starts an internal timer 270 * @param timer [int] "Timer ID" 271 * @param time [int] "Time until execution" 272 * @param callback [int] "Function to execute" 273 */ 274 function _startTimer( timer, time, callback ) { 275 _clearTimer( timer ); 276 return setInterval( callback, time ); 277 } 278 279 /** 280 * @method private 281 * @name _clearTimer 282 * @description Clears an internal timer 283 * @param timer [int] "Timer ID" 284 */ 285 function _clearTimer( timer ) { 286 if ( timer ) { 287 clearInterval( timer ); 288 timer = null; 289 } 290 } 291 292 /** 293 * @method private 294 * @name _digits 295 * @description Analyzes and returns significant digit count 296 * @param value {float} "Value to analyze" 297 * @return {number} "Number of significant digits" 298 */ 299 function _digits( value ) { 300 var test = String( value ); 301 if ( test.indexOf( "." ) > -1 ) { 302 return test.length - test.indexOf( "." ) - 1; 303 } else { 304 return 0; 305 } 306 } 307 308 /** 309 * @method private 310 * @name _round 311 * @description Rounds a number to a sepcific significant digit count 312 * @param value [float] "Value to round" 313 * @param digits [float] "Digits to round to" 314 * @return {number} "Rounded number" 315 */ 316 function _round( value, digits ) { 317 var exp = Math.pow( 10, digits ); 318 return Math.round( value * exp ) / exp; 319 } 320 321 $.fn.stepper = function ( method ) { 322 if ( pub[ method ] ) { 323 return pub[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ) ); 324 } else if ( typeof method === 'object' || !method ) { 325 return _init.apply( this, arguments ); 326 } 327 return this; 328 }; 329 330 $.stepper = function ( method ) { 331 if ( method === "defaults" ) { 332 pub.defaults.apply( this, Array.prototype.slice.call( arguments, 1 ) ); 333 } 334 }; 335})( jQuery ); 336 337function wpimBindStepper( sel ) { 338 sel = sel || '[class*="wpim"] input[type="number"], [class*="wpinventory"] input[type="number"]';
339 jQuery( sel ).stepper(); 340} 341 342// Automatically apply to any number inputs within WPIM elements 343jQuery( function ( $ ) { 344 wpimBindStepper(); 345} );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.