2 * jQuery UI Widget 1.11.2
5 * Copyright 2014 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 // allow multiple hashes to be passed on init
194 options = !isMethodCall && args.length ?
195 $.widget.extend.apply( null, [ options ].concat(args) ) :
198 if ( isMethodCall ) {
199 this.each(function() {
201 instance = $.data( this, fullName );
202 if ( options === "instance" ) {
203 returnValue = instance;
207 return $.error( "cannot call methods on " + name + " prior to initialization; " +
208 "attempted to call method '" + options + "'" );
210 if ( !$.isFunction( instance[options] ) || options.charAt( 0 ) === "_" ) {
211 return $.error( "no such method '" + options + "' for " + name + " widget instance" );
213 methodValue = instance[ options ].apply( instance, args );
214 if ( methodValue !== instance && methodValue !== undefined ) {
215 returnValue = methodValue && methodValue.jquery ?
216 returnValue.pushStack( methodValue.get() ) :
222 this.each(function() {
223 var instance = $.data( this, fullName );
225 instance.option( options || {} );
226 if ( instance._init ) {
230 $.data( this, fullName, new object( options, this ) );
239 $.Widget = function( /* options, element */ ) {};
240 $.Widget._childConstructors = [];
242 $.Widget.prototype = {
243 widgetName: "widget",
244 widgetEventPrefix: "",
245 defaultElement: "<div>",
252 _createWidget: function( options, element ) {
253 element = $( element || this.defaultElement || this )[ 0 ];
254 this.element = $( element );
255 this.uuid = widget_uuid++;
256 this.eventNamespace = "." + this.widgetName + this.uuid;
259 this.hoverable = $();
260 this.focusable = $();
262 if ( element !== this ) {
263 $.data( element, this.widgetFullName, this );
264 this._on( true, this.element, {
265 remove: function( event ) {
266 if ( event.target === element ) {
271 this.document = $( element.style ?
272 // element within the document
273 element.ownerDocument :
274 // element is window or document
275 element.document || element );
276 this.window = $( this.document[0].defaultView || this.document[0].parentWindow );
279 this.options = $.widget.extend( {},
281 this._getCreateOptions(),
285 this._trigger( "create", null, this._getCreateEventData() );
288 _getCreateOptions: $.noop,
289 _getCreateEventData: $.noop,
293 destroy: function() {
295 // we can probably remove the unbind calls in 2.0
296 // all event bindings should go through this._on()
298 .unbind( this.eventNamespace )
299 .removeData( this.widgetFullName )
300 // support: jquery <1.6.3
301 // http://bugs.jquery.com/ticket/9413
302 .removeData( $.camelCase( this.widgetFullName ) );
304 .unbind( this.eventNamespace )
305 .removeAttr( "aria-disabled" )
307 this.widgetFullName + "-disabled " +
308 "ui-state-disabled" );
310 // clean up events and states
311 this.bindings.unbind( this.eventNamespace );
312 this.hoverable.removeClass( "ui-state-hover" );
313 this.focusable.removeClass( "ui-state-focus" );
321 option: function( key, value ) {
327 if ( arguments.length === 0 ) {
328 // don't return a reference to the internal hash
329 return $.widget.extend( {}, this.options );
332 if ( typeof key === "string" ) {
333 // handle nested keys, e.g., "foo.bar" => { foo: { bar: ___ } }
335 parts = key.split( "." );
337 if ( parts.length ) {
338 curOption = options[ key ] = $.widget.extend( {}, this.options[ key ] );
339 for ( i = 0; i < parts.length - 1; i++ ) {
340 curOption[ parts[ i ] ] = curOption[ parts[ i ] ] || {};
341 curOption = curOption[ parts[ i ] ];
344 if ( arguments.length === 1 ) {
345 return curOption[ key ] === undefined ? null : curOption[ key ];
347 curOption[ key ] = value;
349 if ( arguments.length === 1 ) {
350 return this.options[ key ] === undefined ? null : this.options[ key ];
352 options[ key ] = value;
356 this._setOptions( options );
360 _setOptions: function( options ) {
363 for ( key in options ) {
364 this._setOption( key, options[ key ] );
369 _setOption: function( key, value ) {
370 this.options[ key ] = value;
372 if ( key === "disabled" ) {
374 .toggleClass( this.widgetFullName + "-disabled", !!value );
376 // If the widget is becoming disabled, then nothing is interactive
378 this.hoverable.removeClass( "ui-state-hover" );
379 this.focusable.removeClass( "ui-state-focus" );
387 return this._setOptions({ disabled: false });
389 disable: function() {
390 return this._setOptions({ disabled: true });
393 _on: function( suppressDisabledCheck, element, handlers ) {
397 // no suppressDisabledCheck flag, shuffle arguments
398 if ( typeof suppressDisabledCheck !== "boolean" ) {
400 element = suppressDisabledCheck;
401 suppressDisabledCheck = false;
404 // no element argument, shuffle and use this.element
407 element = this.element;
408 delegateElement = this.widget();
410 element = delegateElement = $( element );
411 this.bindings = this.bindings.add( element );
414 $.each( handlers, function( event, handler ) {
415 function handlerProxy() {
416 // allow widgets to customize the disabled handling
417 // - disabled as an array instead of boolean
418 // - disabled class as method for disabling individual parts
419 if ( !suppressDisabledCheck &&
420 ( instance.options.disabled === true ||
421 $( this ).hasClass( "ui-state-disabled" ) ) ) {
424 return ( typeof handler === "string" ? instance[ handler ] : handler )
425 .apply( instance, arguments );
428 // copy the guid so direct unbinding works
429 if ( typeof handler !== "string" ) {
430 handlerProxy.guid = handler.guid =
431 handler.guid || handlerProxy.guid || $.guid++;
434 var match = event.match( /^([\w:-]*)\s*(.*)$/ ),
435 eventName = match[1] + instance.eventNamespace,
438 delegateElement.delegate( selector, eventName, handlerProxy );
440 element.bind( eventName, handlerProxy );
445 _off: function( element, eventName ) {
446 eventName = (eventName || "").split( " " ).join( this.eventNamespace + " " ) +
448 element.unbind( eventName ).undelegate( eventName );
450 // Clear the stack to avoid memory leaks (#10056)
451 this.bindings = $( this.bindings.not( element ).get() );
452 this.focusable = $( this.focusable.not( element ).get() );
453 this.hoverable = $( this.hoverable.not( element ).get() );
456 _delay: function( handler, delay ) {
457 function handlerProxy() {
458 return ( typeof handler === "string" ? instance[ handler ] : handler )
459 .apply( instance, arguments );
462 return setTimeout( handlerProxy, delay || 0 );
465 _hoverable: function( element ) {
466 this.hoverable = this.hoverable.add( element );
468 mouseenter: function( event ) {
469 $( event.currentTarget ).addClass( "ui-state-hover" );
471 mouseleave: function( event ) {
472 $( event.currentTarget ).removeClass( "ui-state-hover" );
477 _focusable: function( element ) {
478 this.focusable = this.focusable.add( element );
480 focusin: function( event ) {
481 $( event.currentTarget ).addClass( "ui-state-focus" );
483 focusout: function( event ) {
484 $( event.currentTarget ).removeClass( "ui-state-focus" );
489 _trigger: function( type, event, data ) {
491 callback = this.options[ type ];
494 event = $.Event( event );
495 event.type = ( type === this.widgetEventPrefix ?
497 this.widgetEventPrefix + type ).toLowerCase();
498 // the original event may come from any element
499 // so we need to reset the target on the new event
500 event.target = this.element[ 0 ];
502 // copy original event properties over to the new event
503 orig = event.originalEvent;
505 for ( prop in orig ) {
506 if ( !( prop in event ) ) {
507 event[ prop ] = orig[ prop ];
512 this.element.trigger( event, data );
513 return !( $.isFunction( callback ) &&
514 callback.apply( this.element[0], [ event ].concat( data ) ) === false ||
515 event.isDefaultPrevented() );
519 $.each( { show: "fadeIn", hide: "fadeOut" }, function( method, defaultEffect ) {
520 $.Widget.prototype[ "_" + method ] = function( element, options, callback ) {
521 if ( typeof options === "string" ) {
522 options = { effect: options };
525 effectName = !options ?
527 options === true || typeof options === "number" ?
529 options.effect || defaultEffect;
530 options = options || {};
531 if ( typeof options === "number" ) {
532 options = { duration: options };
534 hasOptions = !$.isEmptyObject( options );
535 options.complete = callback;
536 if ( options.delay ) {
537 element.delay( options.delay );
539 if ( hasOptions && $.effects && $.effects.effect[ effectName ] ) {
540 element[ method ]( options );
541 } else if ( effectName !== method && element[ effectName ] ) {
542 element[ effectName ]( options.duration, options.easing, callback );
544 element.queue(function( next ) {
545 $( this )[ method ]();
547 callback.call( element[ 0 ] );