app/jquery/jquery-ui.core.js
changeset 839 3e50c012a4a8
child 2420 645f4de26f99
equal deleted inserted replaced
838:58da949dd64c 839:3e50c012a4a8
       
     1 /*
       
     2  * jQuery UI @VERSION
       
     3  *
       
     4  * Copyright (c) 2009 AUTHORS.txt (http://ui.jquery.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
       
     9  */
       
    10 ;(function($) {
       
    11 
       
    12 var _remove = $.fn.remove,
       
    13 	isFF2 = $.browser.mozilla && (parseFloat($.browser.version) < 1.9);
       
    14 
       
    15 //Helper functions and ui object
       
    16 $.ui = {
       
    17 	version: "@VERSION",
       
    18 
       
    19 	// $.ui.plugin is deprecated.  Use the proxy pattern instead.
       
    20 	plugin: {
       
    21 		add: function(module, option, set) {
       
    22 			var proto = $.ui[module].prototype;
       
    23 			for(var i in set) {
       
    24 				proto.plugins[i] = proto.plugins[i] || [];
       
    25 				proto.plugins[i].push([option, set[i]]);
       
    26 			}
       
    27 		},
       
    28 		call: function(instance, name, args) {
       
    29 			var set = instance.plugins[name];
       
    30 			if(!set) { return; }
       
    31 
       
    32 			for (var i = 0; i < set.length; i++) {
       
    33 				if (instance.options[set[i][0]]) {
       
    34 					set[i][1].apply(instance.element, args);
       
    35 				}
       
    36 			}
       
    37 		}
       
    38 	},
       
    39 
       
    40 	contains: function(a, b) {
       
    41 		return document.compareDocumentPosition
       
    42 			? a.compareDocumentPosition(b) & 16
       
    43 			: a !== b && a.contains(b);
       
    44 	},
       
    45 
       
    46 	cssCache: {},
       
    47 	css: function(name) {
       
    48 		if ($.ui.cssCache[name]) { return $.ui.cssCache[name]; }
       
    49 		var tmp = $('<div class="ui-gen"></div>').addClass(name).css({position:'absolute', top:'-5000px', left:'-5000px', display:'block'}).appendTo('body');
       
    50 
       
    51 		//if (!$.browser.safari)
       
    52 			//tmp.appendTo('body');
       
    53 
       
    54 		//Opera and Safari set width and height to 0px instead of auto
       
    55 		//Safari returns rgba(0,0,0,0) when bgcolor is not set
       
    56 		$.ui.cssCache[name] = !!(
       
    57 			(!(/auto|default/).test(tmp.css('cursor')) || (/^[1-9]/).test(tmp.css('height')) || (/^[1-9]/).test(tmp.css('width')) ||
       
    58 			!(/none/).test(tmp.css('backgroundImage')) || !(/transparent|rgba\(0, 0, 0, 0\)/).test(tmp.css('backgroundColor')))
       
    59 		);
       
    60 		try { $('body').get(0).removeChild(tmp.get(0));	} catch(e){}
       
    61 		return $.ui.cssCache[name];
       
    62 	},
       
    63 
       
    64 	hasScroll: function(el, a) {
       
    65 
       
    66 		//If overflow is hidden, the element might have extra content, but the user wants to hide it
       
    67 		if ($(el).css('overflow') == 'hidden') { return false; }
       
    68 
       
    69 		var scroll = (a && a == 'left') ? 'scrollLeft' : 'scrollTop',
       
    70 			has = false;
       
    71 
       
    72 		if (el[scroll] > 0) { return true; }
       
    73 
       
    74 		// TODO: determine which cases actually cause this to happen
       
    75 		// if the element doesn't have the scroll set, see if it's possible to
       
    76 		// set the scroll
       
    77 		el[scroll] = 1;
       
    78 		has = (el[scroll] > 0);
       
    79 		el[scroll] = 0;
       
    80 		return has;
       
    81 	},
       
    82 
       
    83 	isOverAxis: function(x, reference, size) {
       
    84 		//Determines when x coordinate is over "b" element axis
       
    85 		return (x > reference) && (x < (reference + size));
       
    86 	},
       
    87 
       
    88 	isOver: function(y, x, top, left, height, width) {
       
    89 		//Determines when x, y coordinates is over "b" element
       
    90 		return $.ui.isOverAxis(y, top, height) && $.ui.isOverAxis(x, left, width);
       
    91 	},
       
    92 
       
    93 	keyCode: {
       
    94 		BACKSPACE: 8,
       
    95 		CAPS_LOCK: 20,
       
    96 		COMMA: 188,
       
    97 		CONTROL: 17,
       
    98 		DELETE: 46,
       
    99 		DOWN: 40,
       
   100 		END: 35,
       
   101 		ENTER: 13,
       
   102 		ESCAPE: 27,
       
   103 		HOME: 36,
       
   104 		INSERT: 45,
       
   105 		LEFT: 37,
       
   106 		NUMPAD_ADD: 107,
       
   107 		NUMPAD_DECIMAL: 110,
       
   108 		NUMPAD_DIVIDE: 111,
       
   109 		NUMPAD_ENTER: 108,
       
   110 		NUMPAD_MULTIPLY: 106,
       
   111 		NUMPAD_SUBTRACT: 109,
       
   112 		PAGE_DOWN: 34,
       
   113 		PAGE_UP: 33,
       
   114 		PERIOD: 190,
       
   115 		RIGHT: 39,
       
   116 		SHIFT: 16,
       
   117 		SPACE: 32,
       
   118 		TAB: 9,
       
   119 		UP: 38
       
   120 	}
       
   121 };
       
   122 
       
   123 // WAI-ARIA normalization
       
   124 if (isFF2) {
       
   125 	var attr = $.attr,
       
   126 		removeAttr = $.fn.removeAttr,
       
   127 		ariaNS = "http://www.w3.org/2005/07/aaa",
       
   128 		ariaState = /^aria-/,
       
   129 		ariaRole = /^wairole:/;
       
   130 
       
   131 	$.attr = function(elem, name, value) {
       
   132 		var set = value !== undefined;
       
   133 
       
   134 		return (name == 'role'
       
   135 			? (set
       
   136 				? attr.call(this, elem, name, "wairole:" + value)
       
   137 				: (attr.apply(this, arguments) || "").replace(ariaRole, ""))
       
   138 			: (ariaState.test(name)
       
   139 				? (set
       
   140 					? elem.setAttributeNS(ariaNS,
       
   141 						name.replace(ariaState, "aaa:"), value)
       
   142 					: attr.call(this, elem, name.replace(ariaState, "aaa:")))
       
   143 				: attr.apply(this, arguments)));
       
   144 	};
       
   145 
       
   146 	$.fn.removeAttr = function(name) {
       
   147 		return (ariaState.test(name)
       
   148 			? this.each(function() {
       
   149 				this.removeAttributeNS(ariaNS, name.replace(ariaState, ""));
       
   150 			}) : removeAttr.call(this, name));
       
   151 	};
       
   152 }
       
   153 
       
   154 //jQuery plugins
       
   155 $.fn.extend({
       
   156 	remove: function() {
       
   157 		// Safari has a native remove event which actually removes DOM elements,
       
   158 		// so we have to use triggerHandler instead of trigger (#3037).
       
   159 		$("*", this).add(this).each(function() {
       
   160 			$(this).triggerHandler("remove");
       
   161 		});
       
   162 		return _remove.apply(this, arguments );
       
   163 	},
       
   164 
       
   165 	enableSelection: function() {
       
   166 		return this
       
   167 			.attr('unselectable', 'off')
       
   168 			.css('MozUserSelect', '')
       
   169 			.unbind('selectstart.ui');
       
   170 	},
       
   171 
       
   172 	disableSelection: function() {
       
   173 		return this
       
   174 			.attr('unselectable', 'on')
       
   175 			.css('MozUserSelect', 'none')
       
   176 			.bind('selectstart.ui', function() { return false; });
       
   177 	},
       
   178 
       
   179 	scrollParent: function() {
       
   180 		var scrollParent;
       
   181 		if(($.browser.msie && (/(static|relative)/).test(this.css('position'))) || (/absolute/).test(this.css('position'))) {
       
   182 			scrollParent = this.parents().filter(function() {
       
   183 				return (/(relative|absolute|fixed)/).test($.curCSS(this,'position',1)) && (/(auto|scroll)/).test($.curCSS(this,'overflow',1)+$.curCSS(this,'overflow-y',1)+$.curCSS(this,'overflow-x',1));
       
   184 			}).eq(0);
       
   185 		} else {
       
   186 			scrollParent = this.parents().filter(function() {
       
   187 				return (/(auto|scroll)/).test($.curCSS(this,'overflow',1)+$.curCSS(this,'overflow-y',1)+$.curCSS(this,'overflow-x',1));
       
   188 			}).eq(0);
       
   189 		}
       
   190 
       
   191 		return (/fixed/).test(this.css('position')) || !scrollParent.length ? $(document) : scrollParent;
       
   192 	}
       
   193 });
       
   194 
       
   195 
       
   196 //Additional selectors
       
   197 $.extend($.expr[':'], {
       
   198 	data: function(elem, i, match) {
       
   199 		return !!$.data(elem, match[3]);
       
   200 	},
       
   201 
       
   202 	// TODO: add support for object, area
       
   203 	tabbable: function(elem) {
       
   204 		var nodeName = elem.nodeName.toLowerCase();
       
   205 		function isVisible(element) {
       
   206 			return !($(element).is(':hidden') || $(element).parents(':hidden').length);
       
   207 		}
       
   208 
       
   209 		return (
       
   210 			// in tab order
       
   211 			elem.tabIndex >= 0 &&
       
   212 
       
   213 			( // filter node types that participate in the tab order
       
   214 
       
   215 				// anchor tag
       
   216 				('a' == nodeName && elem.href) ||
       
   217 
       
   218 				// enabled form element
       
   219 				(/input|select|textarea|button/.test(nodeName) &&
       
   220 					'hidden' != elem.type && !elem.disabled)
       
   221 			) &&
       
   222 
       
   223 			// visible on page
       
   224 			isVisible(elem)
       
   225 		);
       
   226 	}
       
   227 });
       
   228 
       
   229 
       
   230 // $.widget is a factory to create jQuery plugins
       
   231 // taking some boilerplate code out of the plugin code
       
   232 function getter(namespace, plugin, method, args) {
       
   233 	function getMethods(type) {
       
   234 		var methods = $[namespace][plugin][type] || [];
       
   235 		return (typeof methods == 'string' ? methods.split(/,?\s+/) : methods);
       
   236 	}
       
   237 
       
   238 	var methods = getMethods('getter');
       
   239 	if (args.length == 1 && typeof args[0] == 'string') {
       
   240 		methods = methods.concat(getMethods('getterSetter'));
       
   241 	}
       
   242 	return ($.inArray(method, methods) != -1);
       
   243 }
       
   244 
       
   245 $.widget = function(name, prototype) {
       
   246 	var namespace = name.split(".")[0];
       
   247 	name = name.split(".")[1];
       
   248 
       
   249 	// create plugin method
       
   250 	$.fn[name] = function(options) {
       
   251 		var isMethodCall = (typeof options == 'string'),
       
   252 			args = Array.prototype.slice.call(arguments, 1);
       
   253 
       
   254 		// prevent calls to internal methods
       
   255 		if (isMethodCall && options.substring(0, 1) == '_') {
       
   256 			return this;
       
   257 		}
       
   258 
       
   259 		// handle getter methods
       
   260 		if (isMethodCall && getter(namespace, name, options, args)) {
       
   261 			var instance = $.data(this[0], name);
       
   262 			return (instance ? instance[options].apply(instance, args)
       
   263 				: undefined);
       
   264 		}
       
   265 
       
   266 		// handle initialization and non-getter methods
       
   267 		return this.each(function() {
       
   268 			var instance = $.data(this, name);
       
   269 
       
   270 			// constructor
       
   271 			(!instance && !isMethodCall &&
       
   272 				$.data(this, name, new $[namespace][name](this, options)));
       
   273 
       
   274 			// method call
       
   275 			(instance && isMethodCall && $.isFunction(instance[options]) &&
       
   276 				instance[options].apply(instance, args));
       
   277 		});
       
   278 	};
       
   279 
       
   280 	// create widget constructor
       
   281 	$[namespace] = $[namespace] || {};
       
   282 	$[namespace][name] = function(element, options) {
       
   283 		var self = this;
       
   284 
       
   285 		this.namespace = namespace;
       
   286 		this.widgetName = name;
       
   287 		this.widgetEventPrefix = $[namespace][name].eventPrefix || name;
       
   288 		this.widgetBaseClass = namespace + '-' + name;
       
   289 
       
   290 		this.options = $.extend({},
       
   291 			$.widget.defaults,
       
   292 			$[namespace][name].defaults,
       
   293 			$.metadata && $.metadata.get(element)[name],
       
   294 			options);
       
   295 
       
   296 		this.element = $(element)
       
   297 			.bind('setData.' + name, function(event, key, value) {
       
   298 				if (event.target == element) {
       
   299 					return self._setData(key, value);
       
   300 				}
       
   301 			})
       
   302 			.bind('getData.' + name, function(event, key) {
       
   303 				if (event.target == element) {
       
   304 					return self._getData(key);
       
   305 				}
       
   306 			})
       
   307 			.bind('remove', function() {
       
   308 				return self.destroy();
       
   309 			});
       
   310 
       
   311 		this._init();
       
   312 	};
       
   313 
       
   314 	// add widget prototype
       
   315 	$[namespace][name].prototype = $.extend({}, $.widget.prototype, prototype);
       
   316 
       
   317 	// TODO: merge getter and getterSetter properties from widget prototype
       
   318 	// and plugin prototype
       
   319 	$[namespace][name].getterSetter = 'option';
       
   320 };
       
   321 
       
   322 $.widget.prototype = {
       
   323 	_init: function() {},
       
   324 	destroy: function() {
       
   325 		this.element.removeData(this.widgetName)
       
   326 			.removeClass(this.widgetBaseClass + '-disabled' + ' ' + this.namespace + '-state-disabled')
       
   327 			.removeAttr('aria-disabled');
       
   328 	},
       
   329 
       
   330 	option: function(key, value) {
       
   331 		var options = key,
       
   332 			self = this;
       
   333 
       
   334 		if (typeof key == "string") {
       
   335 			if (value === undefined) {
       
   336 				return this._getData(key);
       
   337 			}
       
   338 			options = {};
       
   339 			options[key] = value;
       
   340 		}
       
   341 
       
   342 		$.each(options, function(key, value) {
       
   343 			self._setData(key, value);
       
   344 		});
       
   345 	},
       
   346 	_getData: function(key) {
       
   347 		return this.options[key];
       
   348 	},
       
   349 	_setData: function(key, value) {
       
   350 		this.options[key] = value;
       
   351 
       
   352 		if (key == 'disabled') {
       
   353 			this.element
       
   354 				[value ? 'addClass' : 'removeClass'](
       
   355 					this.widgetBaseClass + '-disabled' + ' ' +
       
   356 					this.namespace + '-state-disabled')
       
   357 				.attr("aria-disabled", value);
       
   358 		}
       
   359 	},
       
   360 
       
   361 	enable: function() {
       
   362 		this._setData('disabled', false);
       
   363 	},
       
   364 	disable: function() {
       
   365 		this._setData('disabled', true);
       
   366 	},
       
   367 
       
   368 	_trigger: function(type, event, data) {
       
   369 		var callback = this.options[type],
       
   370 			eventName = (type == this.widgetEventPrefix
       
   371 				? type : this.widgetEventPrefix + type);
       
   372 
       
   373 		event = event ? $.event.fix(event) : $.Event();
       
   374 		event.type = eventName;
       
   375 
       
   376 		this.element.trigger(event, data);
       
   377 
       
   378 		return !(callback && callback.call(this.element[0], event, data) === false
       
   379 			|| event.isDefaultPrevented());
       
   380 	}
       
   381 };
       
   382 
       
   383 $.widget.defaults = {
       
   384 	disabled: false
       
   385 };
       
   386 
       
   387 
       
   388 /** Mouse Interaction Plugin **/
       
   389 
       
   390 $.ui.mouse = {
       
   391 	_mouseInit: function() {
       
   392 		var self = this;
       
   393 
       
   394 		this.element
       
   395 			.bind('mousedown.'+this.widgetName, function(event) {
       
   396 				return self._mouseDown(event);
       
   397 			})
       
   398 			.bind('click.'+this.widgetName, function(event) {
       
   399 				if(self._preventClickEvent) {
       
   400 					self._preventClickEvent = false;
       
   401 					return false;
       
   402 				}
       
   403 			});
       
   404 
       
   405 		// Prevent text selection in IE
       
   406 		if ($.browser.msie) {
       
   407 			this._mouseUnselectable = this.element.attr('unselectable');
       
   408 			this.element.attr('unselectable', 'on');
       
   409 		}
       
   410 
       
   411 		this.started = false;
       
   412 	},
       
   413 
       
   414 	// TODO: make sure destroying one instance of mouse doesn't mess with
       
   415 	// other instances of mouse
       
   416 	_mouseDestroy: function() {
       
   417 		this.element.unbind('.'+this.widgetName);
       
   418 
       
   419 		// Restore text selection in IE
       
   420 		($.browser.msie
       
   421 			&& this.element.attr('unselectable', this._mouseUnselectable));
       
   422 	},
       
   423 
       
   424 	_mouseDown: function(event) {
       
   425 		// we may have missed mouseup (out of window)
       
   426 		(this._mouseStarted && this._mouseUp(event));
       
   427 
       
   428 		this._mouseDownEvent = event;
       
   429 
       
   430 		var self = this,
       
   431 			btnIsLeft = (event.which == 1),
       
   432 			elIsCancel = (typeof this.options.cancel == "string" ? $(event.target).parents().add(event.target).filter(this.options.cancel).length : false);
       
   433 		if (!btnIsLeft || elIsCancel || !this._mouseCapture(event)) {
       
   434 			return true;
       
   435 		}
       
   436 
       
   437 		this.mouseDelayMet = !this.options.delay;
       
   438 		if (!this.mouseDelayMet) {
       
   439 			this._mouseDelayTimer = setTimeout(function() {
       
   440 				self.mouseDelayMet = true;
       
   441 			}, this.options.delay);
       
   442 		}
       
   443 
       
   444 		if (this._mouseDistanceMet(event) && this._mouseDelayMet(event)) {
       
   445 			this._mouseStarted = (this._mouseStart(event) !== false);
       
   446 			if (!this._mouseStarted) {
       
   447 				event.preventDefault();
       
   448 				return true;
       
   449 			}
       
   450 		}
       
   451 
       
   452 		// these delegates are required to keep context
       
   453 		this._mouseMoveDelegate = function(event) {
       
   454 			return self._mouseMove(event);
       
   455 		};
       
   456 		this._mouseUpDelegate = function(event) {
       
   457 			return self._mouseUp(event);
       
   458 		};
       
   459 		$(document)
       
   460 			.bind('mousemove.'+this.widgetName, this._mouseMoveDelegate)
       
   461 			.bind('mouseup.'+this.widgetName, this._mouseUpDelegate);
       
   462 
       
   463 		// preventDefault() is used to prevent the selection of text here -
       
   464 		// however, in Safari, this causes select boxes not to be selectable
       
   465 		// anymore, so this fix is needed
       
   466 		($.browser.safari || event.preventDefault());
       
   467 		
       
   468 		return true;
       
   469 	},
       
   470 
       
   471 	_mouseMove: function(event) {
       
   472 		// IE mouseup check - mouseup happened when mouse was out of window
       
   473 		if ($.browser.msie && !event.button) {
       
   474 			return this._mouseUp(event);
       
   475 		}
       
   476 
       
   477 		if (this._mouseStarted) {
       
   478 			this._mouseDrag(event);
       
   479 			return event.preventDefault();
       
   480 		}
       
   481 
       
   482 		if (this._mouseDistanceMet(event) && this._mouseDelayMet(event)) {
       
   483 			this._mouseStarted =
       
   484 				(this._mouseStart(this._mouseDownEvent, event) !== false);
       
   485 			(this._mouseStarted ? this._mouseDrag(event) : this._mouseUp(event));
       
   486 		}
       
   487 
       
   488 		return !this._mouseStarted;
       
   489 	},
       
   490 
       
   491 	_mouseUp: function(event) {
       
   492 		$(document)
       
   493 			.unbind('mousemove.'+this.widgetName, this._mouseMoveDelegate)
       
   494 			.unbind('mouseup.'+this.widgetName, this._mouseUpDelegate);
       
   495 
       
   496 		if (this._mouseStarted) {
       
   497 			this._mouseStarted = false;
       
   498 			this._preventClickEvent = true;
       
   499 			this._mouseStop(event);
       
   500 		}
       
   501 
       
   502 		return false;
       
   503 	},
       
   504 
       
   505 	_mouseDistanceMet: function(event) {
       
   506 		return (Math.max(
       
   507 				Math.abs(this._mouseDownEvent.pageX - event.pageX),
       
   508 				Math.abs(this._mouseDownEvent.pageY - event.pageY)
       
   509 			) >= this.options.distance
       
   510 		);
       
   511 	},
       
   512 
       
   513 	_mouseDelayMet: function(event) {
       
   514 		return this.mouseDelayMet;
       
   515 	},
       
   516 
       
   517 	// These are placeholder methods, to be overriden by extending plugin
       
   518 	_mouseStart: function(event) {},
       
   519 	_mouseDrag: function(event) {},
       
   520 	_mouseStop: function(event) {},
       
   521 	_mouseCapture: function(event) { return true; }
       
   522 };
       
   523 
       
   524 $.ui.mouse.defaults = {
       
   525 	cancel: null,
       
   526 	distance: 1,
       
   527 	delay: 0
       
   528 };
       
   529 
       
   530 })(jQuery);