4 * Copyright (c) 2008 Paul Bakaus (ui.jquery.com)
5 * Dual licensed under the MIT (MIT-LICENSE.txt)
6 * and GPL (GPL-LICENSE.txt) licenses.
8 * http://docs.jquery.com/UI
12 /** jQuery core modifications and additions **/
43 var _remove = $.fn.remove;
44 var isFF2 = $.browser.mozilla && (parseFloat($.browser.version) < 1.9);
47 //Helper functions and ui object
52 // $.ui.plugin is deprecated. Use the proxy pattern instead.
54 add: function(module, option, set) {
55 var proto = $.ui[module].prototype;
57 proto.plugins[i] = proto.plugins[i] || [];
58 proto.plugins[i].push([option, set[i]]);
61 call: function(instance, name, args) {
62 var set = instance.plugins[name];
65 for (var i = 0; i < set.length; i++) {
66 if (instance.options[set[i][0]]) {
67 set[i][1].apply(instance.element, args);
75 if ($.ui.cssCache[name]) { return $.ui.cssCache[name]; }
76 var tmp = $('<div class="ui-gen">').addClass(name).css({position:'absolute', top:'-5000px', left:'-5000px', display:'block'}).appendTo('body');
78 //if (!$.browser.safari)
79 //tmp.appendTo('body');
81 //Opera and Safari set width and height to 0px instead of auto
82 //Safari returns rgba(0,0,0,0) when bgcolor is not set
83 $.ui.cssCache[name] = !!(
84 (!(/auto|default/).test(tmp.css('cursor')) || (/^[1-9]/).test(tmp.css('height')) || (/^[1-9]/).test(tmp.css('width')) ||
85 !(/none/).test(tmp.css('backgroundImage')) || !(/transparent|rgba\(0, 0, 0, 0\)/).test(tmp.css('backgroundColor')))
87 try { $('body').get(0).removeChild(tmp.get(0)); } catch(e){}
88 return $.ui.cssCache[name];
91 hasScroll: function(e, a) {
93 //If overflow is hidden, the element might have extra content, but the user wants to hide it
94 if ($(e).css('overflow') == 'hidden') { return false; }
96 var scroll = (a && a == 'left') ? 'scrollLeft' : 'scrollTop',
99 if (e[scroll] > 0) { return true; }
101 // TODO: determine which cases actually cause this to happen
102 // if the element doesn't have the scroll set, see if it's possible to
105 has = (e[scroll] > 0);
116 // Safari has a native remove event which actually removes DOM elements,
117 // so we have to use triggerHandler instead of trigger (#3037).
118 $("*", this).add(this).each(function() {
119 $(this).triggerHandler("remove");
121 return _remove.apply(this, arguments );
124 enableSelection: function() {
126 .attr('unselectable', 'off')
127 .css('MozUserSelect', '')
128 .unbind('selectstart.ui');
131 disableSelection: function() {
133 .attr('unselectable', 'on')
134 .css('MozUserSelect', 'none')
135 .bind('selectstart.ui', function() { return false; });
138 // WAI-ARIA Semantics
139 ariaRole: function(role) {
140 return (role !== undefined
143 ? this.attr("role", isFF2 ? "wairole:" + role : role)
146 : (this.attr("role") || "").replace(/^wairole:/, ""));
149 ariaState: function(state, value) {
150 return (value !== undefined
153 ? this.each(function(i, el) {
155 ? el.setAttributeNS("http://www.w3.org/2005/07/aaa",
156 "aaa:" + state, value)
157 : $(el).attr("aria-" + state, value));
161 : this.attr(isFF2 ? "aaa:" + state : "aria-" + state));
167 //Additional selectors
168 $.extend($.expr[':'], {
170 data: function(a, i, m) {
171 return $.data(a, m[3]);
174 // TODO: add support for object, area
175 tabbable: function(a, i, m) {
177 var nodeName = a.nodeName.toLowerCase();
178 var isVisible = function(element) {
179 function checkStyles(element) {
180 var style = element.style;
181 return (style.display != 'none' && style.visibility != 'hidden');
184 var visible = checkStyles(element);
186 (visible && $.each($.dir(element, 'parentNode'), function() {
187 return (visible = checkStyles(this));
197 ( // filter node types that participate in the tab order
200 ('a' == nodeName && a.href) ||
202 // enabled form element
203 (/input|select|textarea|button/.test(nodeName) &&
204 'hidden' != a.type && !a.disabled)
216 // $.widget is a factory to create jQuery plugins
217 // taking some boilerplate code out of the plugin code
218 // created by Scott González and Jörn Zaefferer
219 function getter(namespace, plugin, method, args) {
220 function getMethods(type) {
221 var methods = $[namespace][plugin][type] || [];
222 return (typeof methods == 'string' ? methods.split(/,?\s+/) : methods);
225 var methods = getMethods('getter');
226 if (args.length == 1 && typeof args[0] == 'string') {
227 methods = methods.concat(getMethods('getterSetter'));
229 return ($.inArray(method, methods) != -1);
232 $.widget = function(name, prototype) {
233 var namespace = name.split(".")[0];
234 name = name.split(".")[1];
236 // create plugin method
237 $.fn[name] = function(options) {
238 var isMethodCall = (typeof options == 'string'),
239 args = Array.prototype.slice.call(arguments, 1);
241 // prevent calls to internal methods
242 if (isMethodCall && options.substring(0, 1) == '_') {
246 // handle getter methods
247 if (isMethodCall && getter(namespace, name, options, args)) {
248 var instance = $.data(this[0], name);
249 return (instance ? instance[options].apply(instance, args)
253 // handle initialization and non-getter methods
254 return this.each(function() {
255 var instance = $.data(this, name);
258 (!instance && !isMethodCall &&
259 $.data(this, name, new $[namespace][name](this, options)));
262 (instance && isMethodCall && $.isFunction(instance[options]) &&
263 instance[options].apply(instance, args));
267 // create widget constructor
268 $[namespace] = $[namespace] || {};
269 $[namespace][name] = function(element, options) {
272 this.widgetName = name;
273 this.widgetEventPrefix = $[namespace][name].eventPrefix || name;
274 this.widgetBaseClass = namespace + '-' + name;
276 this.options = $.extend({},
278 $[namespace][name].defaults,
279 $.metadata && $.metadata.get(element)[name],
282 this.element = $(element)
283 .bind('setData.' + name, function(e, key, value) {
284 return self._setData(key, value);
286 .bind('getData.' + name, function(e, key) {
287 return self._getData(key);
289 .bind('remove', function() {
290 return self.destroy();
296 // add widget prototype
297 $[namespace][name].prototype = $.extend({}, $.widget.prototype, prototype);
299 // TODO: merge getter and getterSetter properties from widget prototype
300 // and plugin prototype
301 $[namespace][name].getterSetter = 'option';
304 $.widget.prototype = {
305 _init: function() {},
306 destroy: function() {
307 this.element.removeData(this.widgetName);
310 option: function(key, value) {
314 if (typeof key == "string") {
315 if (value === undefined) {
316 return this._getData(key);
319 options[key] = value;
322 $.each(options, function(key, value) {
323 self._setData(key, value);
326 _getData: function(key) {
327 return this.options[key];
329 _setData: function(key, value) {
330 this.options[key] = value;
332 if (key == 'disabled') {
333 this.element[value ? 'addClass' : 'removeClass'](
334 this.widgetBaseClass + '-disabled');
339 this._setData('disabled', false);
341 disable: function() {
342 this._setData('disabled', true);
345 _trigger: function(type, e, data) {
346 var eventName = (type == this.widgetEventPrefix
347 ? type : this.widgetEventPrefix + type);
348 e = e || $.event.fix({ type: eventName, target: this.element[0] });
349 return this.element.triggerHandler(eventName, [e, data], this.options[type]);
353 $.widget.defaults = {
358 /** Mouse Interaction Plugin **/
361 _mouseInit: function() {
365 .bind('mousedown.'+this.widgetName, function(e) {
366 return self._mouseDown(e);
368 .bind('click.'+this.widgetName, function(e) {
369 if(self._preventClickEvent) {
370 self._preventClickEvent = false;
375 // Prevent text selection in IE
376 if ($.browser.msie) {
377 this._mouseUnselectable = this.element.attr('unselectable');
378 this.element.attr('unselectable', 'on');
381 this.started = false;
384 // TODO: make sure destroying one instance of mouse doesn't mess with
385 // other instances of mouse
386 _mouseDestroy: function() {
387 this.element.unbind('.'+this.widgetName);
389 // Restore text selection in IE
391 && this.element.attr('unselectable', this._mouseUnselectable));
394 _mouseDown: function(e) {
395 // we may have missed mouseup (out of window)
396 (this._mouseStarted && this._mouseUp(e));
398 this._mouseDownEvent = e;
401 btnIsLeft = (e.which == 1),
402 elIsCancel = (typeof this.options.cancel == "string" ? $(e.target).parents().add(e.target).filter(this.options.cancel).length : false);
403 if (!btnIsLeft || elIsCancel || !this._mouseCapture(e)) {
407 this.mouseDelayMet = !this.options.delay;
408 if (!this.mouseDelayMet) {
409 this._mouseDelayTimer = setTimeout(function() {
410 self.mouseDelayMet = true;
411 }, this.options.delay);
414 if (this._mouseDistanceMet(e) && this._mouseDelayMet(e)) {
415 this._mouseStarted = (this._mouseStart(e) !== false);
416 if (!this._mouseStarted) {
422 // these delegates are required to keep context
423 this._mouseMoveDelegate = function(e) {
424 return self._mouseMove(e);
426 this._mouseUpDelegate = function(e) {
427 return self._mouseUp(e);
430 .bind('mousemove.'+this.widgetName, this._mouseMoveDelegate)
431 .bind('mouseup.'+this.widgetName, this._mouseUpDelegate);
436 _mouseMove: function(e) {
437 // IE mouseup check - mouseup happened when mouse was out of window
438 if ($.browser.msie && !e.button) {
439 return this._mouseUp(e);
442 if (this._mouseStarted) {
447 if (this._mouseDistanceMet(e) && this._mouseDelayMet(e)) {
449 (this._mouseStart(this._mouseDownEvent, e) !== false);
450 (this._mouseStarted ? this._mouseDrag(e) : this._mouseUp(e));
453 return !this._mouseStarted;
456 _mouseUp: function(e) {
458 .unbind('mousemove.'+this.widgetName, this._mouseMoveDelegate)
459 .unbind('mouseup.'+this.widgetName, this._mouseUpDelegate);
461 if (this._mouseStarted) {
462 this._mouseStarted = false;
463 this._preventClickEvent = true;
470 _mouseDistanceMet: function(e) {
472 Math.abs(this._mouseDownEvent.pageX - e.pageX),
473 Math.abs(this._mouseDownEvent.pageY - e.pageY)
474 ) >= this.options.distance
478 _mouseDelayMet: function(e) {
479 return this.mouseDelayMet;
482 // These are placeholder methods, to be overriden by extending plugin
483 _mouseStart: function(e) {},
484 _mouseDrag: function(e) {},
485 _mouseStop: function(e) {},
486 _mouseCapture: function(e) { return true; }
489 $.ui.mouse.defaults = {