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