[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 /*! 2 * jQuery UI Slider 1.12.1 3 * http://jqueryui.com 4 * 5 * Copyright jQuery Foundation and other contributors 6 * Released under the MIT license. 7 * http://jquery.org/license 8 */ 9 10 //>>label: Slider 11 //>>group: Widgets 12 //>>description: Displays a flexible slider with ranges and accessibility via keyboard. 13 //>>docs: http://api.jqueryui.com/slider/ 14 //>>demos: http://jqueryui.com/slider/ 15 //>>css.structure: ../../themes/base/core.css 16 //>>css.structure: ../../themes/base/slider.css 17 //>>css.theme: ../../themes/base/theme.css 18 19 ( function( factory ) { 20 if ( typeof define === "function" && define.amd ) { 21 22 // AMD. Register as an anonymous module. 23 define( [ 24 "jquery", 25 "./mouse", 26 "./core" 27 ], factory ); 28 } else { 29 30 // Browser globals 31 factory( jQuery ); 32 } 33 }( function( $ ) { 34 35 return $.widget( "ui.slider", $.ui.mouse, { 36 version: "1.12.1", 37 widgetEventPrefix: "slide", 38 39 options: { 40 animate: false, 41 classes: { 42 "ui-slider": "ui-corner-all", 43 "ui-slider-handle": "ui-corner-all", 44 45 // Note: ui-widget-header isn't the most fittingly semantic framework class for this 46 // element, but worked best visually with a variety of themes 47 "ui-slider-range": "ui-corner-all ui-widget-header" 48 }, 49 distance: 0, 50 max: 100, 51 min: 0, 52 orientation: "horizontal", 53 range: false, 54 step: 1, 55 value: 0, 56 values: null, 57 58 // Callbacks 59 change: null, 60 slide: null, 61 start: null, 62 stop: null 63 }, 64 65 // Number of pages in a slider 66 // (how many times can you page up/down to go through the whole range) 67 numPages: 5, 68 69 _create: function() { 70 this._keySliding = false; 71 this._mouseSliding = false; 72 this._animateOff = true; 73 this._handleIndex = null; 74 this._detectOrientation(); 75 this._mouseInit(); 76 this._calculateNewMax(); 77 78 this._addClass( "ui-slider ui-slider-" + this.orientation, 79 "ui-widget ui-widget-content" ); 80 81 this._refresh(); 82 83 this._animateOff = false; 84 }, 85 86 _refresh: function() { 87 this._createRange(); 88 this._createHandles(); 89 this._setupEvents(); 90 this._refreshValue(); 91 }, 92 93 _createHandles: function() { 94 var i, handleCount, 95 options = this.options, 96 existingHandles = this.element.find( ".ui-slider-handle" ), 97 handle = "<span tabindex='0'></span>", 98 handles = []; 99 100 handleCount = ( options.values && options.values.length ) || 1; 101 102 if ( existingHandles.length > handleCount ) { 103 existingHandles.slice( handleCount ).remove(); 104 existingHandles = existingHandles.slice( 0, handleCount ); 105 } 106 107 for ( i = existingHandles.length; i < handleCount; i++ ) { 108 handles.push( handle ); 109 } 110 111 this.handles = existingHandles.add( $( handles.join( "" ) ).appendTo( this.element ) ); 112 113 this._addClass( this.handles, "ui-slider-handle", "ui-state-default" ); 114 115 this.handle = this.handles.eq( 0 ); 116 117 this.handles.each( function( i ) { 118 $( this ) 119 .data( "ui-slider-handle-index", i ) 120 .attr( "tabIndex", 0 ); 121 } ); 122 }, 123 124 _createRange: function() { 125 var options = this.options; 126 127 if ( options.range ) { 128 if ( options.range === true ) { 129 if ( !options.values ) { 130 options.values = [ this._valueMin(), this._valueMin() ]; 131 } else if ( options.values.length && options.values.length !== 2 ) { 132 options.values = [ options.values[ 0 ], options.values[ 0 ] ]; 133 } else if ( $.isArray( options.values ) ) { 134 options.values = options.values.slice( 0 ); 135 } 136 } 137 138 if ( !this.range || !this.range.length ) { 139 this.range = $( "<div>" ) 140 .appendTo( this.element ); 141 142 this._addClass( this.range, "ui-slider-range" ); 143 } else { 144 this._removeClass( this.range, "ui-slider-range-min ui-slider-range-max" ); 145 146 // Handle range switching from true to min/max 147 this.range.css( { 148 "left": "", 149 "bottom": "" 150 } ); 151 } 152 if ( options.range === "min" || options.range === "max" ) { 153 this._addClass( this.range, "ui-slider-range-" + options.range ); 154 } 155 } else { 156 if ( this.range ) { 157 this.range.remove(); 158 } 159 this.range = null; 160 } 161 }, 162 163 _setupEvents: function() { 164 this._off( this.handles ); 165 this._on( this.handles, this._handleEvents ); 166 this._hoverable( this.handles ); 167 this._focusable( this.handles ); 168 }, 169 170 _destroy: function() { 171 this.handles.remove(); 172 if ( this.range ) { 173 this.range.remove(); 174 } 175 176 this._mouseDestroy(); 177 }, 178 179 _mouseCapture: function( event ) { 180 var position, normValue, distance, closestHandle, index, allowed, offset, mouseOverHandle, 181 that = this, 182 o = this.options; 183 184 if ( o.disabled ) { 185 return false; 186 } 187 188 this.elementSize = { 189 width: this.element.outerWidth(), 190 height: this.element.outerHeight() 191 }; 192 this.elementOffset = this.element.offset(); 193 194 position = { x: event.pageX, y: event.pageY }; 195 normValue = this._normValueFromMouse( position ); 196 distance = this._valueMax() - this._valueMin() + 1; 197 this.handles.each( function( i ) { 198 var thisDistance = Math.abs( normValue - that.values( i ) ); 199 if ( ( distance > thisDistance ) || 200 ( distance === thisDistance && 201 ( i === that._lastChangedValue || that.values( i ) === o.min ) ) ) { 202 distance = thisDistance; 203 closestHandle = $( this ); 204 index = i; 205 } 206 } ); 207 208 allowed = this._start( event, index ); 209 if ( allowed === false ) { 210 return false; 211 } 212 this._mouseSliding = true; 213 214 this._handleIndex = index; 215 216 this._addClass( closestHandle, null, "ui-state-active" ); 217 closestHandle.trigger( "focus" ); 218 219 offset = closestHandle.offset(); 220 mouseOverHandle = !$( event.target ).parents().addBack().is( ".ui-slider-handle" ); 221 this._clickOffset = mouseOverHandle ? { left: 0, top: 0 } : { 222 left: event.pageX - offset.left - ( closestHandle.width() / 2 ), 223 top: event.pageY - offset.top - 224 ( closestHandle.height() / 2 ) - 225 ( parseInt( closestHandle.css( "borderTopWidth" ), 10 ) || 0 ) - 226 ( parseInt( closestHandle.css( "borderBottomWidth" ), 10 ) || 0 ) + 227 ( parseInt( closestHandle.css( "marginTop" ), 10 ) || 0 ) 228 }; 229 230 if ( !this.handles.hasClass( "ui-state-hover" ) ) { 231 this._slide( event, index, normValue ); 232 } 233 this._animateOff = true; 234 return true; 235 }, 236 237 _mouseStart: function() { 238 return true; 239 }, 240 241 _mouseDrag: function( event ) { 242 var position = { x: event.pageX, y: event.pageY }, 243 normValue = this._normValueFromMouse( position ); 244 245 this._slide( event, this._handleIndex, normValue ); 246 247 return false; 248 }, 249 250 _mouseStop: function( event ) { 251 this._removeClass( this.handles, null, "ui-state-active" ); 252 this._mouseSliding = false; 253 254 this._stop( event, this._handleIndex ); 255 this._change( event, this._handleIndex ); 256 257 this._handleIndex = null; 258 this._clickOffset = null; 259 this._animateOff = false; 260 261 return false; 262 }, 263 264 _detectOrientation: function() { 265 this.orientation = ( this.options.orientation === "vertical" ) ? "vertical" : "horizontal"; 266 }, 267 268 _normValueFromMouse: function( position ) { 269 var pixelTotal, 270 pixelMouse, 271 percentMouse, 272 valueTotal, 273 valueMouse; 274 275 if ( this.orientation === "horizontal" ) { 276 pixelTotal = this.elementSize.width; 277 pixelMouse = position.x - this.elementOffset.left - 278 ( this._clickOffset ? this._clickOffset.left : 0 ); 279 } else { 280 pixelTotal = this.elementSize.height; 281 pixelMouse = position.y - this.elementOffset.top - 282 ( this._clickOffset ? this._clickOffset.top : 0 ); 283 } 284 285 percentMouse = ( pixelMouse / pixelTotal ); 286 if ( percentMouse > 1 ) { 287 percentMouse = 1; 288 } 289 if ( percentMouse < 0 ) { 290 percentMouse = 0; 291 } 292 if ( this.orientation === "vertical" ) { 293 percentMouse = 1 - percentMouse; 294 } 295 296 valueTotal = this._valueMax() - this._valueMin(); 297 valueMouse = this._valueMin() + percentMouse * valueTotal; 298 299 return this._trimAlignValue( valueMouse ); 300 }, 301 302 _uiHash: function( index, value, values ) { 303 var uiHash = { 304 handle: this.handles[ index ], 305 handleIndex: index, 306 value: value !== undefined ? value : this.value() 307 }; 308 309 if ( this._hasMultipleValues() ) { 310 uiHash.value = value !== undefined ? value : this.values( index ); 311 uiHash.values = values || this.values(); 312 } 313 314 return uiHash; 315 }, 316 317 _hasMultipleValues: function() { 318 return this.options.values && this.options.values.length; 319 }, 320 321 _start: function( event, index ) { 322 return this._trigger( "start", event, this._uiHash( index ) ); 323 }, 324 325 _slide: function( event, index, newVal ) { 326 var allowed, otherVal, 327 currentValue = this.value(), 328 newValues = this.values(); 329 330 if ( this._hasMultipleValues() ) { 331 otherVal = this.values( index ? 0 : 1 ); 332 currentValue = this.values( index ); 333 334 if ( this.options.values.length === 2 && this.options.range === true ) { 335 newVal = index === 0 ? Math.min( otherVal, newVal ) : Math.max( otherVal, newVal ); 336 } 337 338 newValues[ index ] = newVal; 339 } 340 341 if ( newVal === currentValue ) { 342 return; 343 } 344 345 allowed = this._trigger( "slide", event, this._uiHash( index, newVal, newValues ) ); 346 347 // A slide can be canceled by returning false from the slide callback 348 if ( allowed === false ) { 349 return; 350 } 351 352 if ( this._hasMultipleValues() ) { 353 this.values( index, newVal ); 354 } else { 355 this.value( newVal ); 356 } 357 }, 358 359 _stop: function( event, index ) { 360 this._trigger( "stop", event, this._uiHash( index ) ); 361 }, 362 363 _change: function( event, index ) { 364 if ( !this._keySliding && !this._mouseSliding ) { 365 366 //store the last changed value index for reference when handles overlap 367 this._lastChangedValue = index; 368 this._trigger( "change", event, this._uiHash( index ) ); 369 } 370 }, 371 372 value: function( newValue ) { 373 if ( arguments.length ) { 374 this.options.value = this._trimAlignValue( newValue ); 375 this._refreshValue(); 376 this._change( null, 0 ); 377 return; 378 } 379 380 return this._value(); 381 }, 382 383 values: function( index, newValue ) { 384 var vals, 385 newValues, 386 i; 387 388 if ( arguments.length > 1 ) { 389 this.options.values[ index ] = this._trimAlignValue( newValue ); 390 this._refreshValue(); 391 this._change( null, index ); 392 return; 393 } 394 395 if ( arguments.length ) { 396 if ( $.isArray( arguments[ 0 ] ) ) { 397 vals = this.options.values; 398 newValues = arguments[ 0 ]; 399 for ( i = 0; i < vals.length; i += 1 ) { 400 vals[ i ] = this._trimAlignValue( newValues[ i ] ); 401 this._change( null, i ); 402 } 403 this._refreshValue(); 404 } else { 405 if ( this._hasMultipleValues() ) { 406 return this._values( index ); 407 } else { 408 return this.value(); 409 } 410 } 411 } else { 412 return this._values(); 413 } 414 }, 415 416 _setOption: function( key, value ) { 417 var i, 418 valsLength = 0; 419 420 if ( key === "range" && this.options.range === true ) { 421 if ( value === "min" ) { 422 this.options.value = this._values( 0 ); 423 this.options.values = null; 424 } else if ( value === "max" ) { 425 this.options.value = this._values( this.options.values.length - 1 ); 426 this.options.values = null; 427 } 428 } 429 430 if ( $.isArray( this.options.values ) ) { 431 valsLength = this.options.values.length; 432 } 433 434 this._super( key, value ); 435 436 switch ( key ) { 437 case "orientation": 438 this._detectOrientation(); 439 this._removeClass( "ui-slider-horizontal ui-slider-vertical" ) 440 ._addClass( "ui-slider-" + this.orientation ); 441 this._refreshValue(); 442 if ( this.options.range ) { 443 this._refreshRange( value ); 444 } 445 446 // Reset positioning from previous orientation 447 this.handles.css( value === "horizontal" ? "bottom" : "left", "" ); 448 break; 449 case "value": 450 this._animateOff = true; 451 this._refreshValue(); 452 this._change( null, 0 ); 453 this._animateOff = false; 454 break; 455 case "values": 456 this._animateOff = true; 457 this._refreshValue(); 458 459 // Start from the last handle to prevent unreachable handles (#9046) 460 for ( i = valsLength - 1; i >= 0; i-- ) { 461 this._change( null, i ); 462 } 463 this._animateOff = false; 464 break; 465 case "step": 466 case "min": 467 case "max": 468 this._animateOff = true; 469 this._calculateNewMax(); 470 this._refreshValue(); 471 this._animateOff = false; 472 break; 473 case "range": 474 this._animateOff = true; 475 this._refresh(); 476 this._animateOff = false; 477 break; 478 } 479 }, 480 481 _setOptionDisabled: function( value ) { 482 this._super( value ); 483 484 this._toggleClass( null, "ui-state-disabled", !!value ); 485 }, 486 487 //internal value getter 488 // _value() returns value trimmed by min and max, aligned by step 489 _value: function() { 490 var val = this.options.value; 491 val = this._trimAlignValue( val ); 492 493 return val; 494 }, 495 496 //internal values getter 497 // _values() returns array of values trimmed by min and max, aligned by step 498 // _values( index ) returns single value trimmed by min and max, aligned by step 499 _values: function( index ) { 500 var val, 501 vals, 502 i; 503 504 if ( arguments.length ) { 505 val = this.options.values[ index ]; 506 val = this._trimAlignValue( val ); 507 508 return val; 509 } else if ( this._hasMultipleValues() ) { 510 511 // .slice() creates a copy of the array 512 // this copy gets trimmed by min and max and then returned 513 vals = this.options.values.slice(); 514 for ( i = 0; i < vals.length; i += 1 ) { 515 vals[ i ] = this._trimAlignValue( vals[ i ] ); 516 } 517 518 return vals; 519 } else { 520 return []; 521 } 522 }, 523 524 // Returns the step-aligned value that val is closest to, between (inclusive) min and max 525 _trimAlignValue: function( val ) { 526 if ( val <= this._valueMin() ) { 527 return this._valueMin(); 528 } 529 if ( val >= this._valueMax() ) { 530 return this._valueMax(); 531 } 532 var step = ( this.options.step > 0 ) ? this.options.step : 1, 533 valModStep = ( val - this._valueMin() ) % step, 534 alignValue = val - valModStep; 535 536 if ( Math.abs( valModStep ) * 2 >= step ) { 537 alignValue += ( valModStep > 0 ) ? step : ( -step ); 538 } 539 540 // Since JavaScript has problems with large floats, round 541 // the final value to 5 digits after the decimal point (see #4124) 542 return parseFloat( alignValue.toFixed( 5 ) ); 543 }, 544 545 _calculateNewMax: function() { 546 var max = this.options.max, 547 min = this._valueMin(), 548 step = this.options.step, 549 aboveMin = Math.round( ( max - min ) / step ) * step; 550 max = aboveMin + min; 551 if ( max > this.options.max ) { 552 553 //If max is not divisible by step, rounding off may increase its value 554 max -= step; 555 } 556 this.max = parseFloat( max.toFixed( this._precision() ) ); 557 }, 558 559 _precision: function() { 560 var precision = this._precisionOf( this.options.step ); 561 if ( this.options.min !== null ) { 562 precision = Math.max( precision, this._precisionOf( this.options.min ) ); 563 } 564 return precision; 565 }, 566 567 _precisionOf: function( num ) { 568 var str = num.toString(), 569 decimal = str.indexOf( "." ); 570 return decimal === -1 ? 0 : str.length - decimal - 1; 571 }, 572 573 _valueMin: function() { 574 return this.options.min; 575 }, 576 577 _valueMax: function() { 578 return this.max; 579 }, 580 581 _refreshRange: function( orientation ) { 582 if ( orientation === "vertical" ) { 583 this.range.css( { "width": "", "left": "" } ); 584 } 585 if ( orientation === "horizontal" ) { 586 this.range.css( { "height": "", "bottom": "" } ); 587 } 588 }, 589 590 _refreshValue: function() { 591 var lastValPercent, valPercent, value, valueMin, valueMax, 592 oRange = this.options.range, 593 o = this.options, 594 that = this, 595 animate = ( !this._animateOff ) ? o.animate : false, 596 _set = {}; 597 598 if ( this._hasMultipleValues() ) { 599 this.handles.each( function( i ) { 600 valPercent = ( that.values( i ) - that._valueMin() ) / ( that._valueMax() - 601 that._valueMin() ) * 100; 602 _set[ that.orientation === "horizontal" ? "left" : "bottom" ] = valPercent + "%"; 603 $( this ).stop( 1, 1 )[ animate ? "animate" : "css" ]( _set, o.animate ); 604 if ( that.options.range === true ) { 605 if ( that.orientation === "horizontal" ) { 606 if ( i === 0 ) { 607 that.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { 608 left: valPercent + "%" 609 }, o.animate ); 610 } 611 if ( i === 1 ) { 612 that.range[ animate ? "animate" : "css" ]( { 613 width: ( valPercent - lastValPercent ) + "%" 614 }, { 615 queue: false, 616 duration: o.animate 617 } ); 618 } 619 } else { 620 if ( i === 0 ) { 621 that.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { 622 bottom: ( valPercent ) + "%" 623 }, o.animate ); 624 } 625 if ( i === 1 ) { 626 that.range[ animate ? "animate" : "css" ]( { 627 height: ( valPercent - lastValPercent ) + "%" 628 }, { 629 queue: false, 630 duration: o.animate 631 } ); 632 } 633 } 634 } 635 lastValPercent = valPercent; 636 } ); 637 } else { 638 value = this.value(); 639 valueMin = this._valueMin(); 640 valueMax = this._valueMax(); 641 valPercent = ( valueMax !== valueMin ) ? 642 ( value - valueMin ) / ( valueMax - valueMin ) * 100 : 643 0; 644 _set[ this.orientation === "horizontal" ? "left" : "bottom" ] = valPercent + "%"; 645 this.handle.stop( 1, 1 )[ animate ? "animate" : "css" ]( _set, o.animate ); 646 647 if ( oRange === "min" && this.orientation === "horizontal" ) { 648 this.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { 649 width: valPercent + "%" 650 }, o.animate ); 651 } 652 if ( oRange === "max" && this.orientation === "horizontal" ) { 653 this.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { 654 width: ( 100 - valPercent ) + "%" 655 }, o.animate ); 656 } 657 if ( oRange === "min" && this.orientation === "vertical" ) { 658 this.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { 659 height: valPercent + "%" 660 }, o.animate ); 661 } 662 if ( oRange === "max" && this.orientation === "vertical" ) { 663 this.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { 664 height: ( 100 - valPercent ) + "%" 665 }, o.animate ); 666 } 667 } 668 }, 669 670 _handleEvents: { 671 keydown: function( event ) { 672 var allowed, curVal, newVal, step, 673 index = $( event.target ).data( "ui-slider-handle-index" ); 674 675 switch ( event.keyCode ) { 676 case $.ui.keyCode.HOME: 677 case $.ui.keyCode.END: 678 case $.ui.keyCode.PAGE_UP: 679 case $.ui.keyCode.PAGE_DOWN: 680 case $.ui.keyCode.UP: 681 case $.ui.keyCode.RIGHT: 682 case $.ui.keyCode.DOWN: 683 case $.ui.keyCode.LEFT: 684 event.preventDefault(); 685 if ( !this._keySliding ) { 686 this._keySliding = true; 687 this._addClass( $( event.target ), null, "ui-state-active" ); 688 allowed = this._start( event, index ); 689 if ( allowed === false ) { 690 return; 691 } 692 } 693 break; 694 } 695 696 step = this.options.step; 697 if ( this._hasMultipleValues() ) { 698 curVal = newVal = this.values( index ); 699 } else { 700 curVal = newVal = this.value(); 701 } 702 703 switch ( event.keyCode ) { 704 case $.ui.keyCode.HOME: 705 newVal = this._valueMin(); 706 break; 707 case $.ui.keyCode.END: 708 newVal = this._valueMax(); 709 break; 710 case $.ui.keyCode.PAGE_UP: 711 newVal = this._trimAlignValue( 712 curVal + ( ( this._valueMax() - this._valueMin() ) / this.numPages ) 713 ); 714 break; 715 case $.ui.keyCode.PAGE_DOWN: 716 newVal = this._trimAlignValue( 717 curVal - ( ( this._valueMax() - this._valueMin() ) / this.numPages ) ); 718 break; 719 case $.ui.keyCode.UP: 720 case $.ui.keyCode.RIGHT: 721 if ( curVal === this._valueMax() ) { 722 return; 723 } 724 newVal = this._trimAlignValue( curVal + step ); 725 break; 726 case $.ui.keyCode.DOWN: 727 case $.ui.keyCode.LEFT: 728 if ( curVal === this._valueMin() ) { 729 return; 730 } 731 newVal = this._trimAlignValue( curVal - step ); 732 break; 733 } 734 735 this._slide( event, index, newVal ); 736 }, 737 keyup: function( event ) { 738 var index = $( event.target ).data( "ui-slider-handle-index" ); 739 740 if ( this._keySliding ) { 741 this._keySliding = false; 742 this._stop( event, index ); 743 this._change( event, index ); 744 this._removeClass( $( event.target ), null, "ui-state-active" ); 745 } 746 } 747 } 748 } ); 749 750 } ) );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Feb 26 01:00:04 2021 | Cross-referenced by PHPXref 0.7.1 |