2 * jQuery UI Widget 1.9.2
5 * Copyright 2012 jQuery Foundation and other contributors
6 * Released under the MIT license.
7 * http://jquery.org/license
9 * http://api.jqueryui.com/jQuery.widget/
11 (function( $, undefined ) {
14 slice
= Array
.prototype.slice
,
15 _cleanData
= $.cleanData
;
16 $.cleanData = function( elems
) {
17 for ( var i
= 0, elem
; (elem
= elems
[i
]) != null; i
++ ) {
19 $( elem
).triggerHandler( "remove" );
20 // http://bugs.jquery.com/ticket/8235
26 $.widget = function( name
, base
, prototype ) {
27 var fullName
, existingConstructor
, constructor, basePrototype
,
28 namespace = name
.split( "." )[ 0 ];
30 name
= name
.split( "." )[ 1 ];
31 fullName
= namespace + "-" + name
;
38 // create selector for plugin
39 $.expr
[ ":" ][ fullName
.toLowerCase() ] = function( elem
) {
40 return !!$.data( elem
, fullName
);
43 $[ namespace ] = $[ namespace ] || {};
44 existingConstructor
= $[ namespace ][ name
];
45 constructor = $[ namespace ][ name
] = function( options
, element
) {
46 // allow instantiation without "new" keyword
47 if ( !this._createWidget
) {
48 return new constructor( options
, element
);
51 // allow instantiation without initializing for simple inheritance
52 // must use "new" keyword (the code above always passes args)
53 if ( arguments
.length
) {
54 this._createWidget( options
, element
);
57 // extend with the existing constructor to carry over any static properties
58 $.extend( constructor, existingConstructor
, {
59 version
: prototype.version
,
60 // copy the object used to create the prototype in case we need to
61 // redefine the widget later
62 _proto
: $.extend( {}, prototype ),
63 // track widgets that inherit from this widget in case this widget is
64 // redefined after a widget inherits from it
65 _childConstructors
: []
68 basePrototype
= new base();
69 // we need to make the options hash a property directly on the new instance
70 // otherwise we'll modify the options hash on the prototype that we're
72 basePrototype
.options
= $.widget
.extend( {}, basePrototype
.options
);
73 $.each( prototype, function( prop
, value
) {
74 if ( $.isFunction( value
) ) {
75 prototype[ prop
] = (function() {
76 var _super = function() {
77 return base
.prototype[ prop
].apply( this, arguments
);
79 _superApply = function( args
) {
80 return base
.prototype[ prop
].apply( this, args
);
83 var __super
= this._super
,
84 __superApply
= this._superApply
,
88 this._superApply
= _superApply
;
90 returnValue
= value
.apply( this, arguments
);
92 this._super
= __super
;
93 this._superApply
= __superApply
;
100 constructor.prototype = $.widget
.extend( basePrototype
, {
101 // TODO: remove support for widgetEventPrefix
102 // always use the name + a colon as the prefix, e.g., draggable:start
103 // don't prefix for widgets that aren't DOM-based
104 widgetEventPrefix
: existingConstructor
? basePrototype
.widgetEventPrefix
: name
106 constructor: constructor,
107 namespace: namespace,
109 // TODO remove widgetBaseClass, see #8155
110 widgetBaseClass
: fullName
,
111 widgetFullName
: fullName
114 // If this widget is being redefined then we need to find all widgets that
115 // are inheriting from it and redefine all of them so that they inherit from
116 // the new version of this widget. We're essentially trying to replace one
117 // level in the prototype chain.
118 if ( existingConstructor
) {
119 $.each( existingConstructor
._childConstructors
, function( i
, child
) {
120 var childPrototype
= child
.prototype;
122 // redefine the child widget using the same prototype that was
123 // originally used, but inherit from the new version of the base
124 $.widget( childPrototype
.namespace + "." + childPrototype
.widgetName
, constructor, child
._proto
);
126 // remove the list of existing child constructors from the old constructor
127 // so the old child constructors can be garbage collected
128 delete existingConstructor
._childConstructors
;
130 base
._childConstructors
.push( constructor );
133 $.widget
.bridge( name
, constructor );
136 $.widget
.extend = function( target
) {
137 var input
= slice
.call( arguments
, 1 ),
139 inputLength
= input
.length
,
142 for ( ; inputIndex
< inputLength
; inputIndex
++ ) {
143 for ( key
in input
[ inputIndex
] ) {
144 value
= input
[ inputIndex
][ key
];
145 if ( input
[ inputIndex
].hasOwnProperty( key
) && value
!== undefined ) {
147 if ( $.isPlainObject( value
) ) {
148 target
[ key
] = $.isPlainObject( target
[ key
] ) ?
149 $.widget
.extend( {}, target
[ key
], value
) :
150 // Don't extend strings, arrays, etc. with objects
151 $.widget
.extend( {}, value
);
152 // Copy everything else by reference
154 target
[ key
] = value
;
162 $.widget
.bridge = function( name
, object
) {
163 var fullName
= object
.prototype.widgetFullName
|| name
;
164 $.fn
[ name
] = function( options
) {
165 var isMethodCall
= typeof options
=== "string",
166 args
= slice
.call( arguments
, 1 ),
169 // allow multiple hashes to be passed on init
170 options
= !isMethodCall
&& args
.length
?
171 $.widget
.extend
.apply( null, [ options
].concat(args
) ) :
174 if ( isMethodCall
) {
175 this.each(function() {
177 instance
= $.data( this, fullName
);
179 return $.error( "cannot call methods on " + name
+ " prior to initialization; " +
180 "attempted to call method '" + options
+ "'" );
182 if ( !$.isFunction( instance
[options
] ) || options
.charAt( 0 ) === "_" ) {
183 return $.error( "no such method '" + options
+ "' for " + name
+ " widget instance" );
185 methodValue
= instance
[ options
].apply( instance
, args
);
186 if ( methodValue
!== instance
&& methodValue
!== undefined ) {
187 returnValue
= methodValue
&& methodValue
.jquery
?
188 returnValue
.pushStack( methodValue
.get() ) :
194 this.each(function() {
195 var instance
= $.data( this, fullName
);
197 instance
.option( options
|| {} )._init();
199 $.data( this, fullName
, new object( options
, this ) );
208 $.Widget = function( /* options, element */ ) {};
209 $.Widget
._childConstructors
= [];
211 $.Widget
.prototype = {
212 widgetName
: "widget",
213 widgetEventPrefix
: "",
214 defaultElement
: "<div>",
221 _createWidget: function( options
, element
) {
222 element
= $( element
|| this.defaultElement
|| this )[ 0 ];
223 this.element
= $( element
);
225 this.eventNamespace
= "." + this.widgetName
+ this.uuid
;
226 this.options
= $.widget
.extend( {},
228 this._getCreateOptions(),
232 this.hoverable
= $();
233 this.focusable
= $();
235 if ( element
!== this ) {
237 // TODO remove dual storage
238 $.data( element
, this.widgetName
, this );
239 $.data( element
, this.widgetFullName
, this );
240 this._on( true, this.element
, {
241 remove: function( event
) {
242 if ( event
.target
=== element
) {
247 this.document
= $( element
.style
?
248 // element within the document
249 element
.ownerDocument
:
250 // element is window or document
251 element
.document
|| element
);
252 this.window
= $( this.document
[0].defaultView
|| this.document
[0].parentWindow
);
256 this._trigger( "create", null, this._getCreateEventData() );
259 _getCreateOptions
: $.noop
,
260 _getCreateEventData
: $.noop
,
264 destroy: function() {
266 // we can probably remove the unbind calls in 2.0
267 // all event bindings should go through this._on()
269 .unbind( this.eventNamespace
)
271 // TODO remove dual storage
272 .removeData( this.widgetName
)
273 .removeData( this.widgetFullName
)
274 // support: jquery <1.6.3
275 // http://bugs.jquery.com/ticket/9413
276 .removeData( $.camelCase( this.widgetFullName
) );
278 .unbind( this.eventNamespace
)
279 .removeAttr( "aria-disabled" )
281 this.widgetFullName
+ "-disabled " +
282 "ui-state-disabled" );
284 // clean up events and states
285 this.bindings
.unbind( this.eventNamespace
);
286 this.hoverable
.removeClass( "ui-state-hover" );
287 this.focusable
.removeClass( "ui-state-focus" );
295 option: function( key
, value
) {
301 if ( arguments
.length
=== 0 ) {
302 // don't return a reference to the internal hash
303 return $.widget
.extend( {}, this.options
);
306 if ( typeof key
=== "string" ) {
307 // handle nested keys, e.g., "foo.bar" => { foo: { bar: ___ } }
309 parts
= key
.split( "." );
311 if ( parts
.length
) {
312 curOption
= options
[ key
] = $.widget
.extend( {}, this.options
[ key
] );
313 for ( i
= 0; i
< parts
.length
- 1; i
++ ) {
314 curOption
[ parts
[ i
] ] = curOption
[ parts
[ i
] ] || {};
315 curOption
= curOption
[ parts
[ i
] ];
318 if ( value
=== undefined ) {
319 return curOption
[ key
] === undefined ? null : curOption
[ key
];
321 curOption
[ key
] = value
;
323 if ( value
=== undefined ) {
324 return this.options
[ key
] === undefined ? null : this.options
[ key
];
326 options
[ key
] = value
;
330 this._setOptions( options
);
334 _setOptions: function( options
) {
337 for ( key
in options
) {
338 this._setOption( key
, options
[ key
] );
343 _setOption: function( key
, value
) {
344 this.options
[ key
] = value
;
346 if ( key
=== "disabled" ) {
348 .toggleClass( this.widgetFullName
+ "-disabled ui-state-disabled", !!value
)
349 .attr( "aria-disabled", value
);
350 this.hoverable
.removeClass( "ui-state-hover" );
351 this.focusable
.removeClass( "ui-state-focus" );
358 return this._setOption( "disabled", false );
360 disable: function() {
361 return this._setOption( "disabled", true );
364 _on: function( suppressDisabledCheck
, element
, handlers
) {
368 // no suppressDisabledCheck flag, shuffle arguments
369 if ( typeof suppressDisabledCheck
!== "boolean" ) {
371 element
= suppressDisabledCheck
;
372 suppressDisabledCheck
= false;
375 // no element argument, shuffle and use this.element
378 element
= this.element
;
379 delegateElement
= this.widget();
381 // accept selectors, DOM elements
382 element
= delegateElement
= $( element
);
383 this.bindings
= this.bindings
.add( element
);
386 $.each( handlers
, function( event
, handler
) {
387 function handlerProxy() {
388 // allow widgets to customize the disabled handling
389 // - disabled as an array instead of boolean
390 // - disabled class as method for disabling individual parts
391 if ( !suppressDisabledCheck
&&
392 ( instance
.options
.disabled
=== true ||
393 $( this ).hasClass( "ui-state-disabled" ) ) ) {
396 return ( typeof handler
=== "string" ? instance
[ handler
] : handler
)
397 .apply( instance
, arguments
);
400 // copy the guid so direct unbinding works
401 if ( typeof handler
!== "string" ) {
402 handlerProxy
.guid
= handler
.guid
=
403 handler
.guid
|| handlerProxy
.guid
|| $.guid
++;
406 var match
= event
.match( /^(\w+)\s*(.*)$/ ),
407 eventName
= match
[1] + instance
.eventNamespace
,
410 delegateElement
.delegate( selector
, eventName
, handlerProxy
);
412 element
.bind( eventName
, handlerProxy
);
417 _off: function( element
, eventName
) {
418 eventName
= (eventName
|| "").split( " " ).join( this.eventNamespace
+ " " ) + this.eventNamespace
;
419 element
.unbind( eventName
).undelegate( eventName
);
422 _delay: function( handler
, delay
) {
423 function handlerProxy() {
424 return ( typeof handler
=== "string" ? instance
[ handler
] : handler
)
425 .apply( instance
, arguments
);
428 return setTimeout( handlerProxy
, delay
|| 0 );
431 _hoverable: function( element
) {
432 this.hoverable
= this.hoverable
.add( element
);
434 mouseenter: function( event
) {
435 $( event
.currentTarget
).addClass( "ui-state-hover" );
437 mouseleave: function( event
) {
438 $( event
.currentTarget
).removeClass( "ui-state-hover" );
443 _focusable: function( element
) {
444 this.focusable
= this.focusable
.add( element
);
446 focusin: function( event
) {
447 $( event
.currentTarget
).addClass( "ui-state-focus" );
449 focusout: function( event
) {
450 $( event
.currentTarget
).removeClass( "ui-state-focus" );
455 _trigger: function( type
, event
, data
) {
457 callback
= this.options
[ type
];
460 event
= $.Event( event
);
461 event
.type
= ( type
=== this.widgetEventPrefix
?
463 this.widgetEventPrefix
+ type
).toLowerCase();
464 // the original event may come from any element
465 // so we need to reset the target on the new event
466 event
.target
= this.element
[ 0 ];
468 // copy original event properties over to the new event
469 orig
= event
.originalEvent
;
471 for ( prop
in orig
) {
472 if ( !( prop
in event
) ) {
473 event
[ prop
] = orig
[ prop
];
478 this.element
.trigger( event
, data
);
479 return !( $.isFunction( callback
) &&
480 callback
.apply( this.element
[0], [ event
].concat( data
) ) === false ||
481 event
.isDefaultPrevented() );
485 $.each( { show
: "fadeIn", hide
: "fadeOut" }, function( method
, defaultEffect
) {
486 $.Widget
.prototype[ "_" + method
] = function( element
, options
, callback
) {
487 if ( typeof options
=== "string" ) {
488 options
= { effect
: options
};
491 effectName
= !options
?
493 options
=== true || typeof options
=== "number" ?
495 options
.effect
|| defaultEffect
;
496 options
= options
|| {};
497 if ( typeof options
=== "number" ) {
498 options
= { duration
: options
};
500 hasOptions
= !$.isEmptyObject( options
);
501 options
.complete
= callback
;
502 if ( options
.delay
) {
503 element
.delay( options
.delay
);
505 if ( hasOptions
&& $.effects
&& ( $.effects
.effect
[ effectName
] || $.uiBackCompat
!== false && $.effects
[ effectName
] ) ) {
506 element
[ method
]( options
);
507 } else if ( effectName
!== method
&& element
[ effectName
] ) {
508 element
[ effectName
]( options
.duration
, options
.easing
, callback
);
510 element
.queue(function( next
) {
511 $( this )[ method
]();
513 callback
.call( element
[ 0 ] );
522 if ( $.uiBackCompat
!== false ) {
523 $.Widget
.prototype._getCreateOptions = function() {
524 return $.metadata
&& $.metadata
.get( this.element
[0] )[ this.widgetName
];