2 * jQuery UI Widget 1.11.4
5 * Copyright 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( factory
) {
12 if ( typeof define
=== "function" && define
.amd
) {
14 // AMD. Register as an anonymous module.
15 define( [ "jquery" ], factory
);
24 widget_slice
= Array
.prototype.slice
;
26 $.cleanData
= (function( orig
) {
27 return function( elems
) {
29 for ( i
= 0; (elem
= elems
[i
]) != null; i
++ ) {
32 // Only trigger remove when necessary to save time
33 events
= $._data( elem
, "events" );
34 if ( events
&& events
.remove
) {
35 $( elem
).triggerHandler( "remove" );
38 // http://bugs.jquery.com/ticket/8235
45 $.widget = function( name
, base
, prototype ) {
46 var fullName
, existingConstructor
, constructor, basePrototype
,
47 // proxiedPrototype allows the provided prototype to remain unmodified
48 // so that it can be used as a mixin for multiple widgets (#8876)
49 proxiedPrototype
= {},
50 namespace = name
.split( "." )[ 0 ];
52 name
= name
.split( "." )[ 1 ];
53 fullName
= namespace + "-" + name
;
60 // create selector for plugin
61 $.expr
[ ":" ][ fullName
.toLowerCase() ] = function( elem
) {
62 return !!$.data( elem
, fullName
);
65 $[ namespace ] = $[ namespace ] || {};
66 existingConstructor
= $[ namespace ][ name
];
67 constructor = $[ namespace ][ name
] = function( options
, element
) {
68 // allow instantiation without "new" keyword
69 if ( !this._createWidget
) {
70 return new constructor( options
, element
);
73 // allow instantiation without initializing for simple inheritance
74 // must use "new" keyword (the code above always passes args)
75 if ( arguments
.length
) {
76 this._createWidget( options
, element
);
79 // extend with the existing constructor to carry over any static properties
80 $.extend( constructor, existingConstructor
, {
81 version
: prototype.version
,
82 // copy the object used to create the prototype in case we need to
83 // redefine the widget later
84 _proto
: $.extend( {}, prototype ),
85 // track widgets that inherit from this widget in case this widget is
86 // redefined after a widget inherits from it
87 _childConstructors
: []
90 basePrototype
= new base();
91 // we need to make the options hash a property directly on the new instance
92 // otherwise we'll modify the options hash on the prototype that we're
94 basePrototype
.options
= $.widget
.extend( {}, basePrototype
.options
);
95 $.each( prototype, function( prop
, value
) {
96 if ( !$.isFunction( value
) ) {
97 proxiedPrototype
[ prop
] = value
;
100 proxiedPrototype
[ prop
] = (function() {
101 var _super = function() {
102 return base
.prototype[ prop
].apply( this, arguments
);
104 _superApply = function( args
) {
105 return base
.prototype[ prop
].apply( this, args
);
108 var __super
= this._super
,
109 __superApply
= this._superApply
,
112 this._super
= _super
;
113 this._superApply
= _superApply
;
115 returnValue
= value
.apply( this, arguments
);
117 this._super
= __super
;
118 this._superApply
= __superApply
;
124 constructor.prototype = $.widget
.extend( basePrototype
, {
125 // TODO: remove support for widgetEventPrefix
126 // always use the name + a colon as the prefix, e.g., draggable:start
127 // don't prefix for widgets that aren't DOM-based
128 widgetEventPrefix
: existingConstructor
? (basePrototype
.widgetEventPrefix
|| name
) : name
129 }, proxiedPrototype
, {
130 constructor: constructor,
131 namespace: namespace,
133 widgetFullName
: fullName
136 // If this widget is being redefined then we need to find all widgets that
137 // are inheriting from it and redefine all of them so that they inherit from
138 // the new version of this widget. We're essentially trying to replace one
139 // level in the prototype chain.
140 if ( existingConstructor
) {
141 $.each( existingConstructor
._childConstructors
, function( i
, child
) {
142 var childPrototype
= child
.prototype;
144 // redefine the child widget using the same prototype that was
145 // originally used, but inherit from the new version of the base
146 $.widget( childPrototype
.namespace + "." + childPrototype
.widgetName
, constructor, child
._proto
);
148 // remove the list of existing child constructors from the old constructor
149 // so the old child constructors can be garbage collected
150 delete existingConstructor
._childConstructors
;
152 base
._childConstructors
.push( constructor );
155 $.widget
.bridge( name
, constructor );
160 $.widget
.extend = function( target
) {
161 var input
= widget_slice
.call( arguments
, 1 ),
163 inputLength
= input
.length
,
166 for ( ; inputIndex
< inputLength
; inputIndex
++ ) {
167 for ( key
in input
[ inputIndex
] ) {
168 value
= input
[ inputIndex
][ key
];
169 if ( input
[ inputIndex
].hasOwnProperty( key
) && value
!== undefined ) {
171 if ( $.isPlainObject( value
) ) {
172 target
[ key
] = $.isPlainObject( target
[ key
] ) ?
173 $.widget
.extend( {}, target
[ key
], value
) :
174 // Don't extend strings, arrays, etc. with objects
175 $.widget
.extend( {}, value
);
176 // Copy everything else by reference
178 target
[ key
] = value
;
186 $.widget
.bridge = function( name
, object
) {
187 var fullName
= object
.prototype.widgetFullName
|| name
;
188 $.fn
[ name
] = function( options
) {
189 var isMethodCall
= typeof options
=== "string",
190 args
= widget_slice
.call( arguments
, 1 ),
193 if ( isMethodCall
) {
194 this.each(function() {
196 instance
= $.data( this, fullName
);
197 if ( options
=== "instance" ) {
198 returnValue
= instance
;
202 return $.error( "cannot call methods on " + name
+ " prior to initialization; " +
203 "attempted to call method '" + options
+ "'" );
205 if ( !$.isFunction( instance
[options
] ) || options
.charAt( 0 ) === "_" ) {
206 return $.error( "no such method '" + options
+ "' for " + name
+ " widget instance" );
208 methodValue
= instance
[ options
].apply( instance
, args
);
209 if ( methodValue
!== instance
&& methodValue
!== undefined ) {
210 returnValue
= methodValue
&& methodValue
.jquery
?
211 returnValue
.pushStack( methodValue
.get() ) :
218 // Allow multiple hashes to be passed on init
220 options
= $.widget
.extend
.apply( null, [ options
].concat(args
) );
223 this.each(function() {
224 var instance
= $.data( this, fullName
);
226 instance
.option( options
|| {} );
227 if ( instance
._init
) {
231 $.data( this, fullName
, new object( options
, this ) );
240 $.Widget = function( /* options, element */ ) {};
241 $.Widget
._childConstructors
= [];
243 $.Widget
.prototype = {
244 widgetName
: "widget",
245 widgetEventPrefix
: "",
246 defaultElement
: "<div>",
253 _createWidget: function( options
, element
) {
254 element
= $( element
|| this.defaultElement
|| this )[ 0 ];
255 this.element
= $( element
);
256 this.uuid
= widget_uuid
++;
257 this.eventNamespace
= "." + this.widgetName
+ this.uuid
;
260 this.hoverable
= $();
261 this.focusable
= $();
263 if ( element
!== this ) {
264 $.data( element
, this.widgetFullName
, this );
265 this._on( true, this.element
, {
266 remove: function( event
) {
267 if ( event
.target
=== element
) {
272 this.document
= $( element
.style
?
273 // element within the document
274 element
.ownerDocument
:
275 // element is window or document
276 element
.document
|| element
);
277 this.window
= $( this.document
[0].defaultView
|| this.document
[0].parentWindow
);
280 this.options
= $.widget
.extend( {},
282 this._getCreateOptions(),
286 this._trigger( "create", null, this._getCreateEventData() );
289 _getCreateOptions
: $.noop
,
290 _getCreateEventData
: $.noop
,
294 destroy: function() {
296 // we can probably remove the unbind calls in 2.0
297 // all event bindings should go through this._on()
299 .unbind( this.eventNamespace
)
300 .removeData( this.widgetFullName
)
301 // support: jquery <1.6.3
302 // http://bugs.jquery.com/ticket/9413
303 .removeData( $.camelCase( this.widgetFullName
) );
305 .unbind( this.eventNamespace
)
306 .removeAttr( "aria-disabled" )
308 this.widgetFullName
+ "-disabled " +
309 "ui-state-disabled" );
311 // clean up events and states
312 this.bindings
.unbind( this.eventNamespace
);
313 this.hoverable
.removeClass( "ui-state-hover" );
314 this.focusable
.removeClass( "ui-state-focus" );
322 option: function( key
, value
) {
328 if ( arguments
.length
=== 0 ) {
329 // don't return a reference to the internal hash
330 return $.widget
.extend( {}, this.options
);
333 if ( typeof key
=== "string" ) {
334 // handle nested keys, e.g., "foo.bar" => { foo: { bar: ___ } }
336 parts
= key
.split( "." );
338 if ( parts
.length
) {
339 curOption
= options
[ key
] = $.widget
.extend( {}, this.options
[ key
] );
340 for ( i
= 0; i
< parts
.length
- 1; i
++ ) {
341 curOption
[ parts
[ i
] ] = curOption
[ parts
[ i
] ] || {};
342 curOption
= curOption
[ parts
[ i
] ];
345 if ( arguments
.length
=== 1 ) {
346 return curOption
[ key
] === undefined ? null : curOption
[ key
];
348 curOption
[ key
] = value
;
350 if ( arguments
.length
=== 1 ) {
351 return this.options
[ key
] === undefined ? null : this.options
[ key
];
353 options
[ key
] = value
;
357 this._setOptions( options
);
361 _setOptions: function( options
) {
364 for ( key
in options
) {
365 this._setOption( key
, options
[ key
] );
370 _setOption: function( key
, value
) {
371 this.options
[ key
] = value
;
373 if ( key
=== "disabled" ) {
375 .toggleClass( this.widgetFullName
+ "-disabled", !!value
);
377 // If the widget is becoming disabled, then nothing is interactive
379 this.hoverable
.removeClass( "ui-state-hover" );
380 this.focusable
.removeClass( "ui-state-focus" );
388 return this._setOptions({ disabled
: false });
390 disable: function() {
391 return this._setOptions({ disabled
: true });
394 _on: function( suppressDisabledCheck
, element
, handlers
) {
398 // no suppressDisabledCheck flag, shuffle arguments
399 if ( typeof suppressDisabledCheck
!== "boolean" ) {
401 element
= suppressDisabledCheck
;
402 suppressDisabledCheck
= false;
405 // no element argument, shuffle and use this.element
408 element
= this.element
;
409 delegateElement
= this.widget();
411 element
= delegateElement
= $( element
);
412 this.bindings
= this.bindings
.add( element
);
415 $.each( handlers
, function( event
, handler
) {
416 function handlerProxy() {
417 // allow widgets to customize the disabled handling
418 // - disabled as an array instead of boolean
419 // - disabled class as method for disabling individual parts
420 if ( !suppressDisabledCheck
&&
421 ( instance
.options
.disabled
=== true ||
422 $( this ).hasClass( "ui-state-disabled" ) ) ) {
425 return ( typeof handler
=== "string" ? instance
[ handler
] : handler
)
426 .apply( instance
, arguments
);
429 // copy the guid so direct unbinding works
430 if ( typeof handler
!== "string" ) {
431 handlerProxy
.guid
= handler
.guid
=
432 handler
.guid
|| handlerProxy
.guid
|| $.guid
++;
435 var match
= event
.match( /^([\w:-]*)\s*(.*)$/ ),
436 eventName
= match
[1] + instance
.eventNamespace
,
439 delegateElement
.delegate( selector
, eventName
, handlerProxy
);
441 element
.bind( eventName
, handlerProxy
);
446 _off: function( element
, eventName
) {
447 eventName
= (eventName
|| "").split( " " ).join( this.eventNamespace
+ " " ) +
449 element
.unbind( eventName
).undelegate( eventName
);
451 // Clear the stack to avoid memory leaks (#10056)
452 this.bindings
= $( this.bindings
.not( element
).get() );
453 this.focusable
= $( this.focusable
.not( element
).get() );
454 this.hoverable
= $( this.hoverable
.not( element
).get() );
457 _delay: function( handler
, delay
) {
458 function handlerProxy() {
459 return ( typeof handler
=== "string" ? instance
[ handler
] : handler
)
460 .apply( instance
, arguments
);
463 return setTimeout( handlerProxy
, delay
|| 0 );
466 _hoverable: function( element
) {
467 this.hoverable
= this.hoverable
.add( element
);
469 mouseenter: function( event
) {
470 $( event
.currentTarget
).addClass( "ui-state-hover" );
472 mouseleave: function( event
) {
473 $( event
.currentTarget
).removeClass( "ui-state-hover" );
478 _focusable: function( element
) {
479 this.focusable
= this.focusable
.add( element
);
481 focusin: function( event
) {
482 $( event
.currentTarget
).addClass( "ui-state-focus" );
484 focusout: function( event
) {
485 $( event
.currentTarget
).removeClass( "ui-state-focus" );
490 _trigger: function( type
, event
, data
) {
492 callback
= this.options
[ type
];
495 event
= $.Event( event
);
496 event
.type
= ( type
=== this.widgetEventPrefix
?
498 this.widgetEventPrefix
+ type
).toLowerCase();
499 // the original event may come from any element
500 // so we need to reset the target on the new event
501 event
.target
= this.element
[ 0 ];
503 // copy original event properties over to the new event
504 orig
= event
.originalEvent
;
506 for ( prop
in orig
) {
507 if ( !( prop
in event
) ) {
508 event
[ prop
] = orig
[ prop
];
513 this.element
.trigger( event
, data
);
514 return !( $.isFunction( callback
) &&
515 callback
.apply( this.element
[0], [ event
].concat( data
) ) === false ||
516 event
.isDefaultPrevented() );
520 $.each( { show
: "fadeIn", hide
: "fadeOut" }, function( method
, defaultEffect
) {
521 $.Widget
.prototype[ "_" + method
] = function( element
, options
, callback
) {
522 if ( typeof options
=== "string" ) {
523 options
= { effect
: options
};
526 effectName
= !options
?
528 options
=== true || typeof options
=== "number" ?
530 options
.effect
|| defaultEffect
;
531 options
= options
|| {};
532 if ( typeof options
=== "number" ) {
533 options
= { duration
: options
};
535 hasOptions
= !$.isEmptyObject( options
);
536 options
.complete
= callback
;
537 if ( options
.delay
) {
538 element
.delay( options
.delay
);
540 if ( hasOptions
&& $.effects
&& $.effects
.effect
[ effectName
] ) {
541 element
[ method
]( options
);
542 } else if ( effectName
!== method
&& element
[ effectName
] ) {
543 element
[ effectName
]( options
.duration
, options
.easing
, callback
);
545 element
.queue(function( next
) {
546 $( this )[ method
]();
548 callback
.call( element
[ 0 ] );