1f82d3a32efadf87bd494a2c98a3e528af30a7f1
[atutor.git] / mods / atutor_opencaps / opencaps / js / jquery / ui.core.js
1 /*\r
2  * jQuery UI 1.7.2\r
3  *\r
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
7  *\r
8  * http://docs.jquery.com/UI\r
9  */\r
10 ;jQuery.ui || (function($) {\r
11 \r
12 var _remove = $.fn.remove,\r
13         isFF2 = $.browser.mozilla && (parseFloat($.browser.version) < 1.9);\r
14 \r
15 //Helper functions and ui object\r
16 $.ui = {\r
17         version: "1.7.2",\r
18 \r
19         // $.ui.plugin is deprecated.  Use the proxy pattern instead.\r
20         plugin: {\r
21                 add: function(module, option, set) {\r
22                         var proto = $.ui[module].prototype;\r
23                         for(var i in set) {\r
24                                 proto.plugins[i] = proto.plugins[i] || [];\r
25                                 proto.plugins[i].push([option, set[i]]);\r
26                         }\r
27                 },\r
28                 call: function(instance, name, args) {\r
29                         var set = instance.plugins[name];\r
30                         if(!set || !instance.element[0].parentNode) { return; }\r
31 \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
35                                 }\r
36                         }\r
37                 }\r
38         },\r
39 \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
44         },\r
45 \r
46         hasScroll: function(el, a) {\r
47 \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
50 \r
51                 var scroll = (a && a == 'left') ? 'scrollLeft' : 'scrollTop',\r
52                         has = false;\r
53 \r
54                 if (el[scroll] > 0) { return true; }\r
55 \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
58                 // set the scroll\r
59                 el[scroll] = 1;\r
60                 has = (el[scroll] > 0);\r
61                 el[scroll] = 0;\r
62                 return has;\r
63         },\r
64 \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
68         },\r
69 \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
73         },\r
74 \r
75         keyCode: {\r
76                 BACKSPACE: 8,\r
77                 CAPS_LOCK: 20,\r
78                 COMMA: 188,\r
79                 CONTROL: 17,\r
80                 DELETE: 46,\r
81                 DOWN: 40,\r
82                 END: 35,\r
83                 ENTER: 13,\r
84                 ESCAPE: 27,\r
85                 HOME: 36,\r
86                 INSERT: 45,\r
87                 LEFT: 37,\r
88                 NUMPAD_ADD: 107,\r
89                 NUMPAD_DECIMAL: 110,\r
90                 NUMPAD_DIVIDE: 111,\r
91                 NUMPAD_ENTER: 108,\r
92                 NUMPAD_MULTIPLY: 106,\r
93                 NUMPAD_SUBTRACT: 109,\r
94                 PAGE_DOWN: 34,\r
95                 PAGE_UP: 33,\r
96                 PERIOD: 190,\r
97                 RIGHT: 39,\r
98                 SHIFT: 16,\r
99                 SPACE: 32,\r
100                 TAB: 9,\r
101                 UP: 38\r
102         }\r
103 };\r
104 \r
105 // WAI-ARIA normalization\r
106 if (isFF2) {\r
107         var attr = $.attr,\r
108                 removeAttr = $.fn.removeAttr,\r
109                 ariaNS = "http://www.w3.org/2005/07/aaa",\r
110                 ariaState = /^aria-/,\r
111                 ariaRole = /^wairole:/;\r
112 \r
113         $.attr = function(elem, name, value) {\r
114                 var set = value !== undefined;\r
115 \r
116                 return (name == 'role'\r
117                         ? (set\r
118                                 ? attr.call(this, elem, name, "wairole:" + value)\r
119                                 : (attr.apply(this, arguments) || "").replace(ariaRole, ""))\r
120                         : (ariaState.test(name)\r
121                                 ? (set\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
126         };\r
127 \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
133         };\r
134 }\r
135 \r
136 //jQuery plugins\r
137 $.fn.extend({\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
143                 });\r
144                 return _remove.apply(this, arguments );\r
145         },\r
146 \r
147         enableSelection: function() {\r
148                 return this\r
149                         .attr('unselectable', 'off')\r
150                         .css('MozUserSelect', '')\r
151                         .unbind('selectstart.ui');\r
152         },\r
153 \r
154         disableSelection: function() {\r
155                 return this\r
156                         .attr('unselectable', 'on')\r
157                         .css('MozUserSelect', 'none')\r
158                         .bind('selectstart.ui', function() { return false; });\r
159         },\r
160 \r
161         scrollParent: function() {\r
162                 var scrollParent;\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
166                         }).eq(0);\r
167                 } else {\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
170                         }).eq(0);\r
171                 }\r
172 \r
173                 return (/fixed/).test(this.css('position')) || !scrollParent.length ? $(document) : scrollParent;\r
174         }\r
175 });\r
176 \r
177 \r
178 //Additional selectors\r
179 $.extend($.expr[':'], {\r
180         data: function(elem, i, match) {\r
181                 return !!$.data(elem, match[3]);\r
182         },\r
183 \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
195         },\r
196 \r
197         tabbable: function(element) {\r
198                 var tabIndex = $.attr(element, 'tabindex');\r
199                 return (isNaN(tabIndex) || tabIndex >= 0) && $(element).is(':focusable');\r
200         }\r
201 });\r
202 \r
203 \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
210         }\r
211 \r
212         var methods = getMethods('getter');\r
213         if (args.length == 1 && typeof args[0] == 'string') {\r
214                 methods = methods.concat(getMethods('getterSetter'));\r
215         }\r
216         return ($.inArray(method, methods) != -1);\r
217 }\r
218 \r
219 $.widget = function(name, prototype) {\r
220         var namespace = name.split(".")[0];\r
221         name = name.split(".")[1];\r
222 \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
227 \r
228                 // prevent calls to internal methods\r
229                 if (isMethodCall && options.substring(0, 1) == '_') {\r
230                         return this;\r
231                 }\r
232 \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
237                                 : undefined);\r
238                 }\r
239 \r
240                 // handle initialization and non-getter methods\r
241                 return this.each(function() {\r
242                         var instance = $.data(this, name);\r
243 \r
244                         // constructor\r
245                         (!instance && !isMethodCall &&\r
246                                 $.data(this, name, new $[namespace][name](this, options))._init());\r
247 \r
248                         // method call\r
249                         (instance && isMethodCall && $.isFunction(instance[options]) &&\r
250                                 instance[options].apply(instance, args));\r
251                 });\r
252         };\r
253 \r
254         // create widget constructor\r
255         $[namespace] = $[namespace] || {};\r
256         $[namespace][name] = function(element, options) {\r
257                 var self = this;\r
258 \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
263 \r
264                 this.options = $.extend({},\r
265                         $.widget.defaults,\r
266                         $[namespace][name].defaults,\r
267                         $.metadata && $.metadata.get(element)[name],\r
268                         options);\r
269 \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
274                                 }\r
275                         })\r
276                         .bind('getData.' + name, function(event, key) {\r
277                                 if (event.target == element) {\r
278                                         return self._getData(key);\r
279                                 }\r
280                         })\r
281                         .bind('remove', function() {\r
282                                 return self.destroy();\r
283                         });\r
284         };\r
285 \r
286         // add widget prototype\r
287         $[namespace][name].prototype = $.extend({}, $.widget.prototype, prototype);\r
288 \r
289         // TODO: merge getter and getterSetter properties from widget prototype\r
290         // and plugin prototype\r
291         $[namespace][name].getterSetter = 'option';\r
292 };\r
293 \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
300         },\r
301 \r
302         option: function(key, value) {\r
303                 var options = key,\r
304                         self = this;\r
305 \r
306                 if (typeof key == "string") {\r
307                         if (value === undefined) {\r
308                                 return this._getData(key);\r
309                         }\r
310                         options = {};\r
311                         options[key] = value;\r
312                 }\r
313 \r
314                 $.each(options, function(key, value) {\r
315                         self._setData(key, value);\r
316                 });\r
317         },\r
318         _getData: function(key) {\r
319                 return this.options[key];\r
320         },\r
321         _setData: function(key, value) {\r
322                 this.options[key] = value;\r
323 \r
324                 if (key == 'disabled') {\r
325                         this.element\r
326                                 [value ? 'addClass' : 'removeClass'](\r
327                                         this.widgetBaseClass + '-disabled' + ' ' +\r
328                                         this.namespace + '-state-disabled')\r
329                                 .attr("aria-disabled", value);\r
330                 }\r
331         },\r
332 \r
333         enable: function() {\r
334                 this._setData('disabled', false);\r
335         },\r
336         disable: function() {\r
337                 this._setData('disabled', true);\r
338         },\r
339 \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
344 \r
345                 event = $.Event(event);\r
346                 event.type = eventName;\r
347 \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
355                         }\r
356                 }\r
357 \r
358                 this.element.trigger(event, data);\r
359 \r
360                 return !($.isFunction(callback) && callback.call(this.element[0], event, data) === false\r
361                         || event.isDefaultPrevented());\r
362         }\r
363 };\r
364 \r
365 $.widget.defaults = {\r
366         disabled: false\r
367 };\r
368 \r
369 \r
370 /** Mouse Interaction Plugin **/\r
371 \r
372 $.ui.mouse = {\r
373         _mouseInit: function() {\r
374                 var self = this;\r
375 \r
376                 this.element\r
377                         .bind('mousedown.'+this.widgetName, function(event) {\r
378                                 return self._mouseDown(event);\r
379                         })\r
380                         .bind('click.'+this.widgetName, function(event) {\r
381                                 if(self._preventClickEvent) {\r
382                                         self._preventClickEvent = false;\r
383                                         event.stopImmediatePropagation();\r
384                                         return false;\r
385                                 }\r
386                         });\r
387 \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
392                 }\r
393 \r
394                 this.started = false;\r
395         },\r
396 \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
401 \r
402                 // Restore text selection in IE\r
403                 ($.browser.msie\r
404                         && this.element.attr('unselectable', this._mouseUnselectable));\r
405         },\r
406 \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
412 \r
413                 // we may have missed mouseup (out of window)\r
414                 (this._mouseStarted && this._mouseUp(event));\r
415 \r
416                 this._mouseDownEvent = event;\r
417 \r
418                 var self = this,\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
422                         return true;\r
423                 }\r
424 \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
430                 }\r
431 \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
436                                 return true;\r
437                         }\r
438                 }\r
439 \r
440                 // these delegates are required to keep context\r
441                 this._mouseMoveDelegate = function(event) {\r
442                         return self._mouseMove(event);\r
443                 };\r
444                 this._mouseUpDelegate = function(event) {\r
445                         return self._mouseUp(event);\r
446                 };\r
447                 $(document)\r
448                         .bind('mousemove.'+this.widgetName, this._mouseMoveDelegate)\r
449                         .bind('mouseup.'+this.widgetName, this._mouseUpDelegate);\r
450 \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
455 \r
456                 event.originalEvent.mouseHandled = true;\r
457                 return true;\r
458         },\r
459 \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
464                 }\r
465 \r
466                 if (this._mouseStarted) {\r
467                         this._mouseDrag(event);\r
468                         return event.preventDefault();\r
469                 }\r
470 \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
475                 }\r
476 \r
477                 return !this._mouseStarted;\r
478         },\r
479 \r
480         _mouseUp: function(event) {\r
481                 $(document)\r
482                         .unbind('mousemove.'+this.widgetName, this._mouseMoveDelegate)\r
483                         .unbind('mouseup.'+this.widgetName, this._mouseUpDelegate);\r
484 \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
489                 }\r
490 \r
491                 return false;\r
492         },\r
493 \r
494         _mouseDistanceMet: function(event) {\r
495                 return (Math.max(\r
496                                 Math.abs(this._mouseDownEvent.pageX - event.pageX),\r
497                                 Math.abs(this._mouseDownEvent.pageY - event.pageY)\r
498                         ) >= this.options.distance\r
499                 );\r
500         },\r
501 \r
502         _mouseDelayMet: function(event) {\r
503                 return this.mouseDelayMet;\r
504         },\r
505 \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
511 };\r
512 \r
513 $.ui.mouse.defaults = {\r
514         cancel: null,\r
515         distance: 1,\r
516         delay: 0\r
517 };\r
518 \r
519 })(jQuery);\r