commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / bower_components / jquery-ui / ui / widget.js
1 /*!
2 * jQuery UI Widget 1.11.4
3 * http://jqueryui.com
4 *
5 * Copyright jQuery Foundation and other contributors
6 * Released under the MIT license.
7 * http://jquery.org/license
8 *
9 * http://api.jqueryui.com/jQuery.widget/
10 */
11 (function( factory ) {
12 if ( typeof define === "function" && define.amd ) {
13
14 // AMD. Register as an anonymous module.
15 define( [ "jquery" ], factory );
16 } else {
17
18 // Browser globals
19 factory( jQuery );
20 }
21 }(function( $ ) {
22
23 var widget_uuid = 0,
24 widget_slice = Array.prototype.slice;
25
26 $.cleanData = (function( orig ) {
27 return function( elems ) {
28 var events, elem, i;
29 for ( i = 0; (elem = elems[i]) != null; i++ ) {
30 try {
31
32 // Only trigger remove when necessary to save time
33 events = $._data( elem, "events" );
34 if ( events && events.remove ) {
35 $( elem ).triggerHandler( "remove" );
36 }
37
38 // http://bugs.jquery.com/ticket/8235
39 } catch ( e ) {}
40 }
41 orig( elems );
42 };
43 })( $.cleanData );
44
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 ];
51
52 name = name.split( "." )[ 1 ];
53 fullName = namespace + "-" + name;
54
55 if ( !prototype ) {
56 prototype = base;
57 base = $.Widget;
58 }
59
60 // create selector for plugin
61 $.expr[ ":" ][ fullName.toLowerCase() ] = function( elem ) {
62 return !!$.data( elem, fullName );
63 };
64
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 );
71 }
72
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 );
77 }
78 };
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: []
88 });
89
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
93 // inheriting from
94 basePrototype.options = $.widget.extend( {}, basePrototype.options );
95 $.each( prototype, function( prop, value ) {
96 if ( !$.isFunction( value ) ) {
97 proxiedPrototype[ prop ] = value;
98 return;
99 }
100 proxiedPrototype[ prop ] = (function() {
101 var _super = function() {
102 return base.prototype[ prop ].apply( this, arguments );
103 },
104 _superApply = function( args ) {
105 return base.prototype[ prop ].apply( this, args );
106 };
107 return function() {
108 var __super = this._super,
109 __superApply = this._superApply,
110 returnValue;
111
112 this._super = _super;
113 this._superApply = _superApply;
114
115 returnValue = value.apply( this, arguments );
116
117 this._super = __super;
118 this._superApply = __superApply;
119
120 return returnValue;
121 };
122 })();
123 });
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,
132 widgetName: name,
133 widgetFullName: fullName
134 });
135
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;
143
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 );
147 });
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;
151 } else {
152 base._childConstructors.push( constructor );
153 }
154
155 $.widget.bridge( name, constructor );
156
157 return constructor;
158 };
159
160 $.widget.extend = function( target ) {
161 var input = widget_slice.call( arguments, 1 ),
162 inputIndex = 0,
163 inputLength = input.length,
164 key,
165 value;
166 for ( ; inputIndex < inputLength; inputIndex++ ) {
167 for ( key in input[ inputIndex ] ) {
168 value = input[ inputIndex ][ key ];
169 if ( input[ inputIndex ].hasOwnProperty( key ) && value !== undefined ) {
170 // Clone objects
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
177 } else {
178 target[ key ] = value;
179 }
180 }
181 }
182 }
183 return target;
184 };
185
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 ),
191 returnValue = this;
192
193 if ( isMethodCall ) {
194 this.each(function() {
195 var methodValue,
196 instance = $.data( this, fullName );
197 if ( options === "instance" ) {
198 returnValue = instance;
199 return false;
200 }
201 if ( !instance ) {
202 return $.error( "cannot call methods on " + name + " prior to initialization; " +
203 "attempted to call method '" + options + "'" );
204 }
205 if ( !$.isFunction( instance[options] ) || options.charAt( 0 ) === "_" ) {
206 return $.error( "no such method '" + options + "' for " + name + " widget instance" );
207 }
208 methodValue = instance[ options ].apply( instance, args );
209 if ( methodValue !== instance && methodValue !== undefined ) {
210 returnValue = methodValue && methodValue.jquery ?
211 returnValue.pushStack( methodValue.get() ) :
212 methodValue;
213 return false;
214 }
215 });
216 } else {
217
218 // Allow multiple hashes to be passed on init
219 if ( args.length ) {
220 options = $.widget.extend.apply( null, [ options ].concat(args) );
221 }
222
223 this.each(function() {
224 var instance = $.data( this, fullName );
225 if ( instance ) {
226 instance.option( options || {} );
227 if ( instance._init ) {
228 instance._init();
229 }
230 } else {
231 $.data( this, fullName, new object( options, this ) );
232 }
233 });
234 }
235
236 return returnValue;
237 };
238 };
239
240 $.Widget = function( /* options, element */ ) {};
241 $.Widget._childConstructors = [];
242
243 $.Widget.prototype = {
244 widgetName: "widget",
245 widgetEventPrefix: "",
246 defaultElement: "<div>",
247 options: {
248 disabled: false,
249
250 // callbacks
251 create: null
252 },
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;
258
259 this.bindings = $();
260 this.hoverable = $();
261 this.focusable = $();
262
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 ) {
268 this.destroy();
269 }
270 }
271 });
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 );
278 }
279
280 this.options = $.widget.extend( {},
281 this.options,
282 this._getCreateOptions(),
283 options );
284
285 this._create();
286 this._trigger( "create", null, this._getCreateEventData() );
287 this._init();
288 },
289 _getCreateOptions: $.noop,
290 _getCreateEventData: $.noop,
291 _create: $.noop,
292 _init: $.noop,
293
294 destroy: function() {
295 this._destroy();
296 // we can probably remove the unbind calls in 2.0
297 // all event bindings should go through this._on()
298 this.element
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 ) );
304 this.widget()
305 .unbind( this.eventNamespace )
306 .removeAttr( "aria-disabled" )
307 .removeClass(
308 this.widgetFullName + "-disabled " +
309 "ui-state-disabled" );
310
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" );
315 },
316 _destroy: $.noop,
317
318 widget: function() {
319 return this.element;
320 },
321
322 option: function( key, value ) {
323 var options = key,
324 parts,
325 curOption,
326 i;
327
328 if ( arguments.length === 0 ) {
329 // don't return a reference to the internal hash
330 return $.widget.extend( {}, this.options );
331 }
332
333 if ( typeof key === "string" ) {
334 // handle nested keys, e.g., "foo.bar" => { foo: { bar: ___ } }
335 options = {};
336 parts = key.split( "." );
337 key = parts.shift();
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 ] ];
343 }
344 key = parts.pop();
345 if ( arguments.length === 1 ) {
346 return curOption[ key ] === undefined ? null : curOption[ key ];
347 }
348 curOption[ key ] = value;
349 } else {
350 if ( arguments.length === 1 ) {
351 return this.options[ key ] === undefined ? null : this.options[ key ];
352 }
353 options[ key ] = value;
354 }
355 }
356
357 this._setOptions( options );
358
359 return this;
360 },
361 _setOptions: function( options ) {
362 var key;
363
364 for ( key in options ) {
365 this._setOption( key, options[ key ] );
366 }
367
368 return this;
369 },
370 _setOption: function( key, value ) {
371 this.options[ key ] = value;
372
373 if ( key === "disabled" ) {
374 this.widget()
375 .toggleClass( this.widgetFullName + "-disabled", !!value );
376
377 // If the widget is becoming disabled, then nothing is interactive
378 if ( value ) {
379 this.hoverable.removeClass( "ui-state-hover" );
380 this.focusable.removeClass( "ui-state-focus" );
381 }
382 }
383
384 return this;
385 },
386
387 enable: function() {
388 return this._setOptions({ disabled: false });
389 },
390 disable: function() {
391 return this._setOptions({ disabled: true });
392 },
393
394 _on: function( suppressDisabledCheck, element, handlers ) {
395 var delegateElement,
396 instance = this;
397
398 // no suppressDisabledCheck flag, shuffle arguments
399 if ( typeof suppressDisabledCheck !== "boolean" ) {
400 handlers = element;
401 element = suppressDisabledCheck;
402 suppressDisabledCheck = false;
403 }
404
405 // no element argument, shuffle and use this.element
406 if ( !handlers ) {
407 handlers = element;
408 element = this.element;
409 delegateElement = this.widget();
410 } else {
411 element = delegateElement = $( element );
412 this.bindings = this.bindings.add( element );
413 }
414
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" ) ) ) {
423 return;
424 }
425 return ( typeof handler === "string" ? instance[ handler ] : handler )
426 .apply( instance, arguments );
427 }
428
429 // copy the guid so direct unbinding works
430 if ( typeof handler !== "string" ) {
431 handlerProxy.guid = handler.guid =
432 handler.guid || handlerProxy.guid || $.guid++;
433 }
434
435 var match = event.match( /^([\w:-]*)\s*(.*)$/ ),
436 eventName = match[1] + instance.eventNamespace,
437 selector = match[2];
438 if ( selector ) {
439 delegateElement.delegate( selector, eventName, handlerProxy );
440 } else {
441 element.bind( eventName, handlerProxy );
442 }
443 });
444 },
445
446 _off: function( element, eventName ) {
447 eventName = (eventName || "").split( " " ).join( this.eventNamespace + " " ) +
448 this.eventNamespace;
449 element.unbind( eventName ).undelegate( eventName );
450
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() );
455 },
456
457 _delay: function( handler, delay ) {
458 function handlerProxy() {
459 return ( typeof handler === "string" ? instance[ handler ] : handler )
460 .apply( instance, arguments );
461 }
462 var instance = this;
463 return setTimeout( handlerProxy, delay || 0 );
464 },
465
466 _hoverable: function( element ) {
467 this.hoverable = this.hoverable.add( element );
468 this._on( element, {
469 mouseenter: function( event ) {
470 $( event.currentTarget ).addClass( "ui-state-hover" );
471 },
472 mouseleave: function( event ) {
473 $( event.currentTarget ).removeClass( "ui-state-hover" );
474 }
475 });
476 },
477
478 _focusable: function( element ) {
479 this.focusable = this.focusable.add( element );
480 this._on( element, {
481 focusin: function( event ) {
482 $( event.currentTarget ).addClass( "ui-state-focus" );
483 },
484 focusout: function( event ) {
485 $( event.currentTarget ).removeClass( "ui-state-focus" );
486 }
487 });
488 },
489
490 _trigger: function( type, event, data ) {
491 var prop, orig,
492 callback = this.options[ type ];
493
494 data = data || {};
495 event = $.Event( event );
496 event.type = ( type === this.widgetEventPrefix ?
497 type :
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 ];
502
503 // copy original event properties over to the new event
504 orig = event.originalEvent;
505 if ( orig ) {
506 for ( prop in orig ) {
507 if ( !( prop in event ) ) {
508 event[ prop ] = orig[ prop ];
509 }
510 }
511 }
512
513 this.element.trigger( event, data );
514 return !( $.isFunction( callback ) &&
515 callback.apply( this.element[0], [ event ].concat( data ) ) === false ||
516 event.isDefaultPrevented() );
517 }
518 };
519
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 };
524 }
525 var hasOptions,
526 effectName = !options ?
527 method :
528 options === true || typeof options === "number" ?
529 defaultEffect :
530 options.effect || defaultEffect;
531 options = options || {};
532 if ( typeof options === "number" ) {
533 options = { duration: options };
534 }
535 hasOptions = !$.isEmptyObject( options );
536 options.complete = callback;
537 if ( options.delay ) {
538 element.delay( options.delay );
539 }
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 );
544 } else {
545 element.queue(function( next ) {
546 $( this )[ method ]();
547 if ( callback ) {
548 callback.call( element[ 0 ] );
549 }
550 next();
551 });
552 }
553 };
554 });
555
556 return $.widget;
557
558 }));