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.
8 * http://docs.jquery.com/UI
10 ;jQuery.ui || (function($) {
12 var _remove = $.fn.remove,
13 isFF2 = $.browser.mozilla && (parseFloat($.browser.version) < 1.9);
15 //Helper functions and ui object
19 // $.ui.plugin is deprecated. Use the proxy pattern instead.
21 add: function(module, option, set) {
22 var proto = $.ui[module].prototype;
24 proto.plugins[i] = proto.plugins[i] || [];
25 proto.plugins[i].push([option, set[i]]);
28 call: function(instance, name, args) {
29 var set = instance.plugins[name];
30 if(!set || !instance.element[0].parentNode) { return; }
32 for (var i = 0; i < set.length; i++) {
33 if (instance.options[set[i][0]]) {
34 set[i][1].apply(instance.element, args);
40 contains: function(a, b) {
41 return document.compareDocumentPosition
42 ? a.compareDocumentPosition(b) & 16
43 : a !== b && a.contains(b);
46 hasScroll: function(el, a) {
48 //If overflow is hidden, the element might have extra content, but the user wants to hide it
49 if ($(el).css('overflow') == 'hidden') { return false; }
51 var scroll = (a && a == 'left') ? 'scrollLeft' : 'scrollTop',
54 if (el[scroll] > 0) { return true; }
56 // TODO: determine which cases actually cause this to happen
57 // if the element doesn't have the scroll set, see if it's possible to
60 has = (el[scroll] > 0);
65 isOverAxis: function(x, reference, size) {
66 //Determines when x coordinate is over "b" element axis
67 return (x > reference) && (x < (reference + size));
70 isOver: function(y, x, top, left, height, width) {
71 //Determines when x, y coordinates is over "b" element
72 return $.ui.isOverAxis(y, top, height) && $.ui.isOverAxis(x, left, width);
105 // WAI-ARIA normalization
108 removeAttr = $.fn.removeAttr,
109 ariaNS = "http://www.w3.org/2005/07/aaa",
110 ariaState = /^aria-/,
111 ariaRole = /^wairole:/;
113 $.attr = function(elem, name, value) {
114 var set = value !== undefined;
116 return (name == 'role'
118 ? attr.call(this, elem, name, "wairole:" + value)
119 : (attr.apply(this, arguments) || "").replace(ariaRole, ""))
120 : (ariaState.test(name)
122 ? elem.setAttributeNS(ariaNS,
123 name.replace(ariaState, "aaa:"), value)
124 : attr.call(this, elem, name.replace(ariaState, "aaa:")))
125 : attr.apply(this, arguments)));
128 $.fn.removeAttr = function(name) {
129 return (ariaState.test(name)
130 ? this.each(function() {
131 this.removeAttributeNS(ariaNS, name.replace(ariaState, ""));
132 }) : removeAttr.call(this, name));
139 // Safari has a native remove event which actually removes DOM elements,
140 // so we have to use triggerHandler instead of trigger (#3037).
141 $("*", this).add(this).each(function() {
142 $(this).triggerHandler("remove");
144 return _remove.apply(this, arguments );
147 enableSelection: function() {
149 .attr('unselectable', 'off')
150 .css('MozUserSelect', '')
151 .unbind('selectstart.ui');
154 disableSelection: function() {
156 .attr('unselectable', 'on')
157 .css('MozUserSelect', 'none')
158 .bind('selectstart.ui', function() { return false; });
161 scrollParent: function() {
163 if(($.browser.msie && (/(static|relative)/).test(this.css('position'))) || (/absolute/).test(this.css('position'))) {
164 scrollParent = this.parents().filter(function() {
165 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));
168 scrollParent = this.parents().filter(function() {
169 return (/(auto|scroll)/).test($.curCSS(this,'overflow',1)+$.curCSS(this,'overflow-y',1)+$.curCSS(this,'overflow-x',1));
173 return (/fixed/).test(this.css('position')) || !scrollParent.length ? $(document) : scrollParent;
178 //Additional selectors
179 $.extend($.expr[':'], {
180 data: function(elem, i, match) {
181 return !!$.data(elem, match[3]);
184 focusable: function(element) {
185 var nodeName = element.nodeName.toLowerCase(),
186 tabIndex = $.attr(element, 'tabindex');
187 return (/input|select|textarea|button|object/.test(nodeName)
189 : 'a' == nodeName || 'area' == nodeName
190 ? element.href || !isNaN(tabIndex)
192 // the element and all of its ancestors must be visible
193 // the browser may report that the area is hidden
194 && !$(element)['area' == nodeName ? 'parents' : 'closest'](':hidden').length;
197 tabbable: function(element) {
198 var tabIndex = $.attr(element, 'tabindex');
199 return (isNaN(tabIndex) || tabIndex >= 0) && $(element).is(':focusable');
204 // $.widget is a factory to create jQuery plugins
205 // taking some boilerplate code out of the plugin code
206 function getter(namespace, plugin, method, args) {
207 function getMethods(type) {
208 var methods = $[namespace][plugin][type] || [];
209 return (typeof methods == 'string' ? methods.split(/,?\s+/) : methods);
212 var methods = getMethods('getter');
213 if (args.length == 1 && typeof args[0] == 'string') {
214 methods = methods.concat(getMethods('getterSetter'));
216 return ($.inArray(method, methods) != -1);
219 $.widget = function(name, prototype) {
220 var namespace = name.split(".")[0];
221 name = name.split(".")[1];
223 // create plugin method
224 $.fn[name] = function(options) {
225 var isMethodCall = (typeof options == 'string'),
226 args = Array.prototype.slice.call(arguments, 1);
228 // prevent calls to internal methods
229 if (isMethodCall && options.substring(0, 1) == '_') {
233 // handle getter methods
234 if (isMethodCall && getter(namespace, name, options, args)) {
235 var instance = $.data(this[0], name);
236 return (instance ? instance[options].apply(instance, args)
240 // handle initialization and non-getter methods
241 return this.each(function() {
242 var instance = $.data(this, name);
245 (!instance && !isMethodCall &&
246 $.data(this, name, new $[namespace][name](this, options))._init());
249 (instance && isMethodCall && $.isFunction(instance[options]) &&
250 instance[options].apply(instance, args));
254 // create widget constructor
255 $[namespace] = $[namespace] || {};
256 $[namespace][name] = function(element, options) {
259 this.namespace = namespace;
260 this.widgetName = name;
261 this.widgetEventPrefix = $[namespace][name].eventPrefix || name;
262 this.widgetBaseClass = namespace + '-' + name;
264 this.options = $.extend({},
266 $[namespace][name].defaults,
267 $.metadata && $.metadata.get(element)[name],
270 this.element = $(element)
271 .bind('setData.' + name, function(event, key, value) {
272 if (event.target == element) {
273 return self._setData(key, value);
276 .bind('getData.' + name, function(event, key) {
277 if (event.target == element) {
278 return self._getData(key);
281 .bind('remove', function() {
282 return self.destroy();
286 // add widget prototype
287 $[namespace][name].prototype = $.extend({}, $.widget.prototype, prototype);
289 // TODO: merge getter and getterSetter properties from widget prototype
290 // and plugin prototype
291 $[namespace][name].getterSetter = 'option';
294 $.widget.prototype = {
295 _init: function() {},
296 destroy: function() {
297 this.element.removeData(this.widgetName)
298 .removeClass(this.widgetBaseClass + '-disabled' + ' ' + this.namespace + '-state-disabled')
299 .removeAttr('aria-disabled');
302 option: function(key, value) {
306 if (typeof key == "string") {
307 if (value === undefined) {
308 return this._getData(key);
311 options[key] = value;
314 $.each(options, function(key, value) {
315 self._setData(key, value);
318 _getData: function(key) {
319 return this.options[key];
321 _setData: function(key, value) {
322 this.options[key] = value;
324 if (key == 'disabled') {
326 [value ? 'addClass' : 'removeClass'](
327 this.widgetBaseClass + '-disabled' + ' ' +
328 this.namespace + '-state-disabled')
329 .attr("aria-disabled", value);
334 this._setData('disabled', false);
336 disable: function() {
337 this._setData('disabled', true);
340 _trigger: function(type, event, data) {
341 var callback = this.options[type],
342 eventName = (type == this.widgetEventPrefix
343 ? type : this.widgetEventPrefix + type);
345 event = $.Event(event);
346 event.type = eventName;
348 // copy original event properties over to the new event
349 // this would happen if we could call $.event.fix instead of $.Event
350 // but we don't have a way to force an event to be fixed multiple times
351 if (event.originalEvent) {
352 for (var i = $.event.props.length, prop; i;) {
353 prop = $.event.props[--i];
354 event[prop] = event.originalEvent[prop];
358 this.element.trigger(event, data);
360 return !($.isFunction(callback) && callback.call(this.element[0], event, data) === false
361 || event.isDefaultPrevented());
365 $.widget.defaults = {
370 /** Mouse Interaction Plugin **/
373 _mouseInit: function() {
377 .bind('mousedown.'+this.widgetName, function(event) {
378 return self._mouseDown(event);
380 .bind('click.'+this.widgetName, function(event) {
381 if(self._preventClickEvent) {
382 self._preventClickEvent = false;
383 event.stopImmediatePropagation();
388 // Prevent text selection in IE
389 if ($.browser.msie) {
390 this._mouseUnselectable = this.element.attr('unselectable');
391 this.element.attr('unselectable', 'on');
394 this.started = false;
397 // TODO: make sure destroying one instance of mouse doesn't mess with
398 // other instances of mouse
399 _mouseDestroy: function() {
400 this.element.unbind('.'+this.widgetName);
402 // Restore text selection in IE
404 && this.element.attr('unselectable', this._mouseUnselectable));
407 _mouseDown: function(event) {
408 // don't let more than one widget handle mouseStart
409 // TODO: figure out why we have to use originalEvent
410 event.originalEvent = event.originalEvent || {};
411 if (event.originalEvent.mouseHandled) { return; }
413 // we may have missed mouseup (out of window)
414 (this._mouseStarted && this._mouseUp(event));
416 this._mouseDownEvent = event;
419 btnIsLeft = (event.which == 1),
420 elIsCancel = (typeof this.options.cancel == "string" ? $(event.target).parents().add(event.target).filter(this.options.cancel).length : false);
421 if (!btnIsLeft || elIsCancel || !this._mouseCapture(event)) {
425 this.mouseDelayMet = !this.options.delay;
426 if (!this.mouseDelayMet) {
427 this._mouseDelayTimer = setTimeout(function() {
428 self.mouseDelayMet = true;
429 }, this.options.delay);
432 if (this._mouseDistanceMet(event) && this._mouseDelayMet(event)) {
433 this._mouseStarted = (this._mouseStart(event) !== false);
434 if (!this._mouseStarted) {
435 event.preventDefault();
440 // these delegates are required to keep context
441 this._mouseMoveDelegate = function(event) {
442 return self._mouseMove(event);
444 this._mouseUpDelegate = function(event) {
445 return self._mouseUp(event);
448 .bind('mousemove.'+this.widgetName, this._mouseMoveDelegate)
449 .bind('mouseup.'+this.widgetName, this._mouseUpDelegate);
451 // preventDefault() is used to prevent the selection of text here -
452 // however, in Safari, this causes select boxes not to be selectable
453 // anymore, so this fix is needed
454 ($.browser.safari || event.preventDefault());
456 event.originalEvent.mouseHandled = true;
460 _mouseMove: function(event) {
461 // IE mouseup check - mouseup happened when mouse was out of window
462 if ($.browser.msie && !event.button) {
463 return this._mouseUp(event);
466 if (this._mouseStarted) {
467 this._mouseDrag(event);
468 return event.preventDefault();
471 if (this._mouseDistanceMet(event) && this._mouseDelayMet(event)) {
473 (this._mouseStart(this._mouseDownEvent, event) !== false);
474 (this._mouseStarted ? this._mouseDrag(event) : this._mouseUp(event));
477 return !this._mouseStarted;
480 _mouseUp: function(event) {
482 .unbind('mousemove.'+this.widgetName, this._mouseMoveDelegate)
483 .unbind('mouseup.'+this.widgetName, this._mouseUpDelegate);
485 if (this._mouseStarted) {
486 this._mouseStarted = false;
487 this._preventClickEvent = (event.target == this._mouseDownEvent.target);
488 this._mouseStop(event);
494 _mouseDistanceMet: function(event) {
496 Math.abs(this._mouseDownEvent.pageX - event.pageX),
497 Math.abs(this._mouseDownEvent.pageY - event.pageY)
498 ) >= this.options.distance
502 _mouseDelayMet: function(event) {
503 return this.mouseDelayMet;
506 // These are placeholder methods, to be overriden by extending plugin
507 _mouseStart: function(event) {},
508 _mouseDrag: function(event) {},
509 _mouseStop: function(event) {},
510 _mouseCapture: function(event) { return true; }
513 $.ui.mouse.defaults = {