PageSourceSearch

https://kunde.linja.no/Touch/Scripts/slider/jquery.ui.slider.js

js linja.no collected 2026-10-04 09:42:57 UTC 18,890 bytes, 709 lines download raw bytes

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

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.