client/player/js/ui.slider.js
changeset 57 3a3c15c462f8
parent 48 44d58d2e90b5
parent 56 da0957782d03
child 58 ec1d0e7dcb53
equal deleted inserted replaced
48:44d58d2e90b5 57:3a3c15c462f8
     1 /*
       
     2  * jQuery UI Slider 1.7.2
       
     3  *
       
     4  * Copyright (c) 2009 AUTHORS.txt (http://jqueryui.com/about)
       
     5  * Dual licensed under the MIT (MIT-LICENSE.txt)
       
     6  * and GPL (GPL-LICENSE.txt) licenses.
       
     7  *
       
     8  * http://docs.jquery.com/UI/Slider
       
     9  *
       
    10  * Depends:
       
    11  *	ui.core.js
       
    12  */
       
    13 
       
    14 (function($) {
       
    15 
       
    16 $.widget("ui.slider", $.extend({}, $.ui.mouse, {
       
    17 
       
    18 	_init: function() {
       
    19 
       
    20 		var self = this, o = this.options;
       
    21 		this._keySliding = false;
       
    22 		this._handleIndex = null;
       
    23 		this._detectOrientation();
       
    24 		this._mouseInit();
       
    25 
       
    26 		this.element
       
    27 			.addClass("ui-slider"
       
    28 				+ " ui-slider-" + this.orientation
       
    29 				+ " ui-widget"
       
    30 				+ " ui-widget-content"
       
    31 				+ " ui-corner-all");
       
    32 
       
    33 		this.range = $([]);
       
    34 
       
    35 		if (o.range) {
       
    36 
       
    37 			if (o.range === true) {
       
    38 				this.range = $('<div></div>');
       
    39 				if (!o.values) o.values = [this._valueMin(), this._valueMin()];
       
    40 				if (o.values.length && o.values.length != 2) {
       
    41 					o.values = [o.values[0], o.values[0]];
       
    42 				}
       
    43 			} else {
       
    44 				this.range = $('<div></div>');
       
    45 			}
       
    46 
       
    47 			this.range
       
    48 				.appendTo(this.element)
       
    49 				.addClass("ui-slider-range");
       
    50 
       
    51 			if (o.range == "min" || o.range == "max") {
       
    52 				this.range.addClass("ui-slider-range-" + o.range);
       
    53 			}
       
    54 
       
    55 			// note: this isn't the most fittingly semantic framework class for this element,
       
    56 			// but worked best visually with a variety of themes
       
    57 			this.range.addClass("ui-widget-header");
       
    58 
       
    59 		}
       
    60 
       
    61 		if ($(".ui-slider-handle", this.element).length == 0)
       
    62 			$('<a href="#"></a>')
       
    63 				.appendTo(this.element)
       
    64 				.addClass("ui-slider-handle");
       
    65 
       
    66 		if (o.values && o.values.length) {
       
    67 			while ($(".ui-slider-handle", this.element).length < o.values.length)
       
    68 				$('<a href="#"></a>')
       
    69 					.appendTo(this.element)
       
    70 					.addClass("ui-slider-handle");
       
    71 		}
       
    72 
       
    73 		this.handles = $(".ui-slider-handle", this.element)
       
    74 			.addClass("ui-state-default"
       
    75 				+ " ui-corner-all");
       
    76 
       
    77 		this.handle = this.handles.eq(0);
       
    78 
       
    79 		this.handles.add(this.range).filter("a")
       
    80 			.click(function(event) {
       
    81 				event.preventDefault();
       
    82 			})
       
    83 			.hover(function() {
       
    84 				if (!o.disabled) {
       
    85 					$(this).addClass('ui-state-hover');
       
    86 				}
       
    87 			}, function() {
       
    88 				$(this).removeClass('ui-state-hover');
       
    89 			})
       
    90 			.focus(function() {
       
    91 				if (!o.disabled) {
       
    92 					$(".ui-slider .ui-state-focus").removeClass('ui-state-focus'); $(this).addClass('ui-state-focus');
       
    93 				} else {
       
    94 					$(this).blur();
       
    95 				}
       
    96 			})
       
    97 			.blur(function() {
       
    98 				$(this).removeClass('ui-state-focus');
       
    99 			});
       
   100 
       
   101 		this.handles.each(function(i) {
       
   102 			$(this).data("index.ui-slider-handle", i);
       
   103 		});
       
   104 
       
   105 		this.handles.keydown(function(event) {
       
   106 
       
   107 			var ret = true;
       
   108 
       
   109 			var index = $(this).data("index.ui-slider-handle");
       
   110 
       
   111 			if (self.options.disabled)
       
   112 				return;
       
   113 
       
   114 			switch (event.keyCode) {
       
   115 				case $.ui.keyCode.HOME:
       
   116 				case $.ui.keyCode.END:
       
   117 				case $.ui.keyCode.UP:
       
   118 				case $.ui.keyCode.RIGHT:
       
   119 				case $.ui.keyCode.DOWN:
       
   120 				case $.ui.keyCode.LEFT:
       
   121 					ret = false;
       
   122 					if (!self._keySliding) {
       
   123 						self._keySliding = true;
       
   124 						$(this).addClass("ui-state-active");
       
   125 						self._start(event, index);
       
   126 					}
       
   127 					break;
       
   128 			}
       
   129 
       
   130 			var curVal, newVal, step = self._step();
       
   131 			if (self.options.values && self.options.values.length) {
       
   132 				curVal = newVal = self.values(index);
       
   133 			} else {
       
   134 				curVal = newVal = self.value();
       
   135 			}
       
   136 
       
   137 			switch (event.keyCode) {
       
   138 				case $.ui.keyCode.HOME:
       
   139 					newVal = self._valueMin();
       
   140 					break;
       
   141 				case $.ui.keyCode.END:
       
   142 					newVal = self._valueMax();
       
   143 					break;
       
   144 				case $.ui.keyCode.UP:
       
   145 				case $.ui.keyCode.RIGHT:
       
   146 					if(curVal == self._valueMax()) return;
       
   147 					newVal = curVal + step;
       
   148 					break;
       
   149 				case $.ui.keyCode.DOWN:
       
   150 				case $.ui.keyCode.LEFT:
       
   151 					if(curVal == self._valueMin()) return;
       
   152 					newVal = curVal - step;
       
   153 					break;
       
   154 			}
       
   155 
       
   156 			self._slide(event, index, newVal);
       
   157 
       
   158 			return ret;
       
   159 
       
   160 		}).keyup(function(event) {
       
   161 
       
   162 			var index = $(this).data("index.ui-slider-handle");
       
   163 
       
   164 			if (self._keySliding) {
       
   165 				self._stop(event, index);
       
   166 				self._change(event, index);
       
   167 				self._keySliding = false;
       
   168 				$(this).removeClass("ui-state-active");
       
   169 			}
       
   170 
       
   171 		});
       
   172 
       
   173 		this._refreshValue();
       
   174 
       
   175 	},
       
   176 
       
   177 	destroy: function() {
       
   178 
       
   179 		this.handles.remove();
       
   180 		this.range.remove();
       
   181 
       
   182 		this.element
       
   183 			.removeClass("ui-slider"
       
   184 				+ " ui-slider-horizontal"
       
   185 				+ " ui-slider-vertical"
       
   186 				+ " ui-slider-disabled"
       
   187 				+ " ui-widget"
       
   188 				+ " ui-widget-content"
       
   189 				+ " ui-corner-all")
       
   190 			.removeData("slider")
       
   191 			.unbind(".slider");
       
   192 
       
   193 		this._mouseDestroy();
       
   194 
       
   195 	},
       
   196 
       
   197 	_mouseCapture: function(event) {
       
   198 
       
   199 		var o = this.options;
       
   200 
       
   201 		if (o.disabled)
       
   202 			return false;
       
   203 
       
   204 		this.elementSize = {
       
   205 			width: this.element.outerWidth(),
       
   206 			height: this.element.outerHeight()
       
   207 		};
       
   208 		this.elementOffset = this.element.offset();
       
   209 
       
   210 		var position = { x: event.pageX, y: event.pageY };
       
   211 		var normValue = this._normValueFromMouse(position);
       
   212 
       
   213 		var distance = this._valueMax() - this._valueMin() + 1, closestHandle;
       
   214 		var self = this, index;
       
   215 		this.handles.each(function(i) {
       
   216 			var thisDistance = Math.abs(normValue - self.values(i));
       
   217 			if (distance > thisDistance) {
       
   218 				distance = thisDistance;
       
   219 				closestHandle = $(this);
       
   220 				index = i;
       
   221 			}
       
   222 		});
       
   223 
       
   224 		// workaround for bug #3736 (if both handles of a range are at 0,
       
   225 		// the first is always used as the one with least distance,
       
   226 		// and moving it is obviously prevented by preventing negative ranges)
       
   227 		if(o.range == true && this.values(1) == o.min) {
       
   228 			closestHandle = $(this.handles[++index]);
       
   229 		}
       
   230 
       
   231 		this._start(event, index);
       
   232 
       
   233 		self._handleIndex = index;
       
   234 
       
   235 		closestHandle
       
   236 			.addClass("ui-state-active")
       
   237 			.focus();
       
   238 		
       
   239 		var offset = closestHandle.offset();
       
   240 		var mouseOverHandle = !$(event.target).parents().andSelf().is('.ui-slider-handle');
       
   241 		this._clickOffset = mouseOverHandle ? { left: 0, top: 0 } : {
       
   242 			left: event.pageX - offset.left - (closestHandle.width() / 2),
       
   243 			top: event.pageY - offset.top
       
   244 				- (closestHandle.height() / 2)
       
   245 				- (parseInt(closestHandle.css('borderTopWidth'),10) || 0)
       
   246 				- (parseInt(closestHandle.css('borderBottomWidth'),10) || 0)
       
   247 				+ (parseInt(closestHandle.css('marginTop'),10) || 0)
       
   248 		};
       
   249 
       
   250 		normValue = this._normValueFromMouse(position);
       
   251 		this._slide(event, index, normValue);
       
   252 		return true;
       
   253 
       
   254 	},
       
   255 
       
   256 	_mouseStart: function(event) {
       
   257 		return true;
       
   258 	},
       
   259 
       
   260 	_mouseDrag: function(event) {
       
   261 
       
   262 		var position = { x: event.pageX, y: event.pageY };
       
   263 		var normValue = this._normValueFromMouse(position);
       
   264 		
       
   265 		this._slide(event, this._handleIndex, normValue);
       
   266 
       
   267 		return false;
       
   268 
       
   269 	},
       
   270 
       
   271 	_mouseStop: function(event) {
       
   272 
       
   273 		this.handles.removeClass("ui-state-active");
       
   274 		this._stop(event, this._handleIndex);
       
   275 		this._change(event, this._handleIndex);
       
   276 		this._handleIndex = null;
       
   277 		this._clickOffset = null;
       
   278 
       
   279 		return false;
       
   280 
       
   281 	},
       
   282 	
       
   283 	_detectOrientation: function() {
       
   284 		this.orientation = this.options.orientation == 'vertical' ? 'vertical' : 'horizontal';
       
   285 	},
       
   286 
       
   287 	_normValueFromMouse: function(position) {
       
   288 
       
   289 		var pixelTotal, pixelMouse;
       
   290 		if ('horizontal' == this.orientation) {
       
   291 			pixelTotal = this.elementSize.width;
       
   292 			pixelMouse = position.x - this.elementOffset.left - (this._clickOffset ? this._clickOffset.left : 0);
       
   293 		} else {
       
   294 			pixelTotal = this.elementSize.height;
       
   295 			pixelMouse = position.y - this.elementOffset.top - (this._clickOffset ? this._clickOffset.top : 0);
       
   296 		}
       
   297 
       
   298 		var percentMouse = (pixelMouse / pixelTotal);
       
   299 		if (percentMouse > 1) percentMouse = 1;
       
   300 		if (percentMouse < 0) percentMouse = 0;
       
   301 		if ('vertical' == this.orientation)
       
   302 			percentMouse = 1 - percentMouse;
       
   303 
       
   304 		var valueTotal = this._valueMax() - this._valueMin(),
       
   305 			valueMouse = percentMouse * valueTotal,
       
   306 			valueMouseModStep = valueMouse % this.options.step,
       
   307 			normValue = this._valueMin() + valueMouse - valueMouseModStep;
       
   308 
       
   309 		if (valueMouseModStep > (this.options.step / 2))
       
   310 			normValue += this.options.step;
       
   311 
       
   312 		// Since JavaScript has problems with large floats, round
       
   313 		// the final value to 5 digits after the decimal point (see #4124)
       
   314 		return parseFloat(normValue.toFixed(5));
       
   315 
       
   316 	},
       
   317 
       
   318 	_start: function(event, index) {
       
   319 		var uiHash = {
       
   320 			handle: this.handles[index],
       
   321 			value: this.value()
       
   322 		};
       
   323 		if (this.options.values && this.options.values.length) {
       
   324 			uiHash.value = this.values(index);
       
   325 			uiHash.values = this.values();
       
   326 		}
       
   327 		this._trigger("start", event, uiHash);
       
   328 	},
       
   329 
       
   330 	_slide: function(event, index, newVal) {
       
   331 
       
   332 		var handle = this.handles[index];
       
   333 
       
   334 		if (this.options.values && this.options.values.length) {
       
   335 
       
   336 			var otherVal = this.values(index ? 0 : 1);
       
   337 
       
   338 			if ((this.options.values.length == 2 && this.options.range === true) && 
       
   339 				((index == 0 && newVal > otherVal) || (index == 1 && newVal < otherVal))){
       
   340  				newVal = otherVal;
       
   341 			}
       
   342 
       
   343 			if (newVal != this.values(index)) {
       
   344 				var newValues = this.values();
       
   345 				newValues[index] = newVal;
       
   346 				// A slide can be canceled by returning false from the slide callback
       
   347 				var allowed = this._trigger("slide", event, {
       
   348 					handle: this.handles[index],
       
   349 					value: newVal,
       
   350 					values: newValues
       
   351 				});
       
   352 				var otherVal = this.values(index ? 0 : 1);
       
   353 				if (allowed !== false) {
       
   354 					this.values(index, newVal, ( event.type == 'mousedown' && this.options.animate ), true);
       
   355 				}
       
   356 			}
       
   357 
       
   358 		} else {
       
   359 
       
   360 			if (newVal != this.value()) {
       
   361 				// A slide can be canceled by returning false from the slide callback
       
   362 				var allowed = this._trigger("slide", event, {
       
   363 					handle: this.handles[index],
       
   364 					value: newVal
       
   365 				});
       
   366 				if (allowed !== false) {
       
   367 					this._setData('value', newVal, ( event.type == 'mousedown' && this.options.animate ));
       
   368 				}
       
   369 					
       
   370 			}
       
   371 
       
   372 		}
       
   373 
       
   374 	},
       
   375 
       
   376 	_stop: function(event, index) {
       
   377 		var uiHash = {
       
   378 			handle: this.handles[index],
       
   379 			value: this.value()
       
   380 		};
       
   381 		if (this.options.values && this.options.values.length) {
       
   382 			uiHash.value = this.values(index);
       
   383 			uiHash.values = this.values();
       
   384 		}
       
   385 		this._trigger("stop", event, uiHash);
       
   386 	},
       
   387 
       
   388 	_change: function(event, index) {
       
   389 		var uiHash = {
       
   390 			handle: this.handles[index],
       
   391 			value: this.value()
       
   392 		};
       
   393 		if (this.options.values && this.options.values.length) {
       
   394 			uiHash.value = this.values(index);
       
   395 			uiHash.values = this.values();
       
   396 		}
       
   397 		this._trigger("change", event, uiHash);
       
   398 	},
       
   399 
       
   400 	value: function(newValue) {
       
   401 
       
   402 		if (arguments.length) {
       
   403 			this._setData("value", newValue);
       
   404 			this._change(null, 0);
       
   405 		}
       
   406 
       
   407 		return this._value();
       
   408 
       
   409 	},
       
   410 
       
   411 	values: function(index, newValue, animated, noPropagation) {
       
   412 
       
   413 		if (arguments.length > 1) {
       
   414 			this.options.values[index] = newValue;
       
   415 			this._refreshValue(animated);
       
   416 			if(!noPropagation) this._change(null, index);
       
   417 		}
       
   418 
       
   419 		if (arguments.length) {
       
   420 			if (this.options.values && this.options.values.length) {
       
   421 				return this._values(index);
       
   422 			} else {
       
   423 				return this.value();
       
   424 			}
       
   425 		} else {
       
   426 			return this._values();
       
   427 		}
       
   428 
       
   429 	},
       
   430 
       
   431 	_setData: function(key, value, animated) {
       
   432 
       
   433 		$.widget.prototype._setData.apply(this, arguments);
       
   434 
       
   435 		switch (key) {
       
   436 			case 'disabled':
       
   437 				if (value) {
       
   438 					this.handles.filter(".ui-state-focus").blur();
       
   439 					this.handles.removeClass("ui-state-hover");
       
   440 					this.handles.attr("disabled", "disabled");
       
   441 				} else {
       
   442 					this.handles.removeAttr("disabled");
       
   443 				}
       
   444 			case 'orientation':
       
   445 
       
   446 				this._detectOrientation();
       
   447 				
       
   448 				this.element
       
   449 					.removeClass("ui-slider-horizontal ui-slider-vertical")
       
   450 					.addClass("ui-slider-" + this.orientation);
       
   451 				this._refreshValue(animated);
       
   452 				break;
       
   453 			case 'value':
       
   454 				this._refreshValue(animated);
       
   455 				break;
       
   456 		}
       
   457 
       
   458 	},
       
   459 
       
   460 	_step: function() {
       
   461 		var step = this.options.step;
       
   462 		return step;
       
   463 	},
       
   464 
       
   465 	_value: function() {
       
   466 
       
   467 		var val = this.options.value;
       
   468 		if (val < this._valueMin()) val = this._valueMin();
       
   469 		if (val > this._valueMax()) val = this._valueMax();
       
   470 
       
   471 		return val;
       
   472 
       
   473 	},
       
   474 
       
   475 	_values: function(index) {
       
   476 
       
   477 		if (arguments.length) {
       
   478 			var val = this.options.values[index];
       
   479 			if (val < this._valueMin()) val = this._valueMin();
       
   480 			if (val > this._valueMax()) val = this._valueMax();
       
   481 
       
   482 			return val;
       
   483 		} else {
       
   484 			return this.options.values;
       
   485 		}
       
   486 
       
   487 	},
       
   488 
       
   489 	_valueMin: function() {
       
   490 		var valueMin = this.options.min;
       
   491 		return valueMin;
       
   492 	},
       
   493 
       
   494 	_valueMax: function() {
       
   495 		var valueMax = this.options.max;
       
   496 		return valueMax;
       
   497 	},
       
   498 
       
   499 	_refreshValue: function(animate) {
       
   500 
       
   501 		var oRange = this.options.range, o = this.options, self = this;
       
   502 
       
   503 		if (this.options.values && this.options.values.length) {
       
   504 			var vp0, vp1;
       
   505 			this.handles.each(function(i, j) {
       
   506 				var valPercent = (self.values(i) - self._valueMin()) / (self._valueMax() - self._valueMin()) * 100;
       
   507 				var _set = {}; _set[self.orientation == 'horizontal' ? 'left' : 'bottom'] = valPercent + '%';
       
   508 				$(this).stop(1,1)[animate ? 'animate' : 'css'](_set, o.animate);
       
   509 				if (self.options.range === true) {
       
   510 					if (self.orientation == 'horizontal') {
       
   511 						(i == 0) && self.range.stop(1,1)[animate ? 'animate' : 'css']({ left: valPercent + '%' }, o.animate);
       
   512 						(i == 1) && self.range[animate ? 'animate' : 'css']({ width: (valPercent - lastValPercent) + '%' }, { queue: false, duration: o.animate });
       
   513 					} else {
       
   514 						(i == 0) && self.range.stop(1,1)[animate ? 'animate' : 'css']({ bottom: (valPercent) + '%' }, o.animate);
       
   515 						(i == 1) && self.range[animate ? 'animate' : 'css']({ height: (valPercent - lastValPercent) + '%' }, { queue: false, duration: o.animate });
       
   516 					}
       
   517 				}
       
   518 				lastValPercent = valPercent;
       
   519 			});
       
   520 		} else {
       
   521 			var value = this.value(),
       
   522 				valueMin = this._valueMin(),
       
   523 				valueMax = this._valueMax(),
       
   524 				valPercent = valueMax != valueMin
       
   525 					? (value - valueMin) / (valueMax - valueMin) * 100
       
   526 					: 0;
       
   527 			var _set = {}; _set[self.orientation == 'horizontal' ? 'left' : 'bottom'] = valPercent + '%';
       
   528 			this.handle.stop(1,1)[animate ? 'animate' : 'css'](_set, o.animate);
       
   529 
       
   530 			(oRange == "min") && (this.orientation == "horizontal") && this.range.stop(1,1)[animate ? 'animate' : 'css']({ width: valPercent + '%' }, o.animate);
       
   531 			(oRange == "max") && (this.orientation == "horizontal") && this.range[animate ? 'animate' : 'css']({ width: (100 - valPercent) + '%' }, { queue: false, duration: o.animate });
       
   532 			(oRange == "min") && (this.orientation == "vertical") && this.range.stop(1,1)[animate ? 'animate' : 'css']({ height: valPercent + '%' }, o.animate);
       
   533 			(oRange == "max") && (this.orientation == "vertical") && this.range[animate ? 'animate' : 'css']({ height: (100 - valPercent) + '%' }, { queue: false, duration: o.animate });
       
   534 		}
       
   535 
       
   536 	}
       
   537 	
       
   538 }));
       
   539 
       
   540 $.extend($.ui.slider, {
       
   541 	getter: "value values",
       
   542 	version: "1.7.2",
       
   543 	eventPrefix: "slide",
       
   544 	defaults: {
       
   545 		animate: false,
       
   546 		delay: 0,
       
   547 		distance: 0,
       
   548 		max: 100,
       
   549 		min: 0,
       
   550 		orientation: 'horizontal',
       
   551 		range: false,
       
   552 		step: 1,
       
   553 		value: 0,
       
   554 		values: null
       
   555 	}
       
   556 });
       
   557 
       
   558 })(jQuery);