Debian Codegraph @

ui/widget.js

raw symbols SCIP
1 /*!
2 * jQuery UI Widget 1.14.2
3 * https://jqueryui.com
4 *
5 * Copyright OpenJS Foundation and other contributors
6 * Released under the MIT license.
7 * https://jquery.org/license
8 */
9
10 //>>label: Widget
11 //>>group: Core
12 //>>description: Provides a factory for creating stateful widgets with a common API.
13 //>>docs: https://api.jqueryui.com/jQuery.widget/
14 //>>demos: https://jqueryui.com/widget/
15
16 ( function( factory ) {
17 "use strict";
18
19 if ( typeof define === "function" && define.amd ) {
20
21 // AMD. Register as an anonymous module.
22 define( [ "jquery", "./version" ], factory );
23 } else {
24
25 // Browser globals
26 factory( jQuery );
27 }
28 } )( function( $ ) {
29 "use strict";
30
31 var widgetUuid = 0;
32 var widgetHasOwnProperty = Array.prototype.hasOwnProperty;
33 var widgetSlice = Array.prototype.slice;
34
35 $.cleanData = ( function( orig ) {
36 return function( elems ) {
37 var events, elem, i;
38 for ( i = 0; ( elem = elems[ i ] ) != null; i++ ) {
39
40 // Only trigger remove when necessary to save time
41 events = $._data( elem, "events" );
42 if ( events && events.remove ) {
43 $( elem ).triggerHandler( "remove" );
44 }
45 }
46 orig( elems );
47 };
48 } )( $.cleanData );
49
50 $.widget = function( name, base, prototype ) {
51 var existingConstructor, constructor, basePrototype;
52
53 // ProxiedPrototype allows the provided prototype to remain unmodified
54 // so that it can be used as a mixin for multiple widgets (#8876)
55 var proxiedPrototype = {};
56
57 var namespace = name.split( "." )[ 0 ];
58 name = name.split( "." )[ 1 ];
59 if ( name === "__proto__" || name === "constructor" ) {
60 return $.error( "Invalid widget name: " + name );
61 }
62 var fullName = namespace + "-" + name;
63
64 if ( !prototype ) {
65 prototype = base;
66 base = $.Widget;
67 }
68
69 if ( Array.isArray( prototype ) ) {
70 prototype = $.extend.apply( null, [ {} ].concat( prototype ) );
71 }
72
73 // Create selector for plugin
74 $.expr.pseudos[ fullName.toLowerCase() ] = function( elem ) {
75 return !!$.data( elem, fullName );
76 };
77
78 $[ namespace ] = $[ namespace ] || {};
79 existingConstructor = $[ namespace ][ name ];
80 constructor = $[ namespace ][ name ] = function( options, element ) {
81
82 // Allow instantiation without "new" keyword
83 if ( !this || !this._createWidget ) {
84 return new constructor( options, element );
85 }
86
87 // Allow instantiation without initializing for simple inheritance
88 // must use "new" keyword (the code above always passes args)
89 if ( arguments.length ) {
90 this._createWidget( options, element );
91 }
92 };
93
94 // Extend with the existing constructor to carry over any static properties
95 $.extend( constructor, existingConstructor, {
96 version: prototype.version,
97
98 // Copy the object used to create the prototype in case we need to
99 // redefine the widget later
100 _proto: $.extend( {}, prototype ),
101
102 // Track widgets that inherit from this widget in case this widget is
103 // redefined after a widget inherits from it
104 _childConstructors: []
105 } );
106
107 basePrototype = new base();
108
109 // We need to make the options hash a property directly on the new instance
110 // otherwise we'll modify the options hash on the prototype that we're
111 // inheriting from
112 basePrototype.options = $.widget.extend( {}, basePrototype.options );
113 $.each( prototype, function( prop, value ) {
114 if ( typeof value !== "function" ) {
115 proxiedPrototype[ prop ] = value;
116 return;
117 }
118 proxiedPrototype[ prop ] = ( function() {
119 function _super() {
120 return base.prototype[ prop ].apply( this, arguments );
121 }
122
123 function _superApply( args ) {
124 return base.prototype[ prop ].apply( this, args );
125 }
126
127 return function() {
128 var __super = this._super;
129 var __superApply = this._superApply;
130 var returnValue;
131
132 this._super = _super;
133 this._superApply = _superApply;
134
135 returnValue = value.apply( this, arguments );
136
137 this._super = __super;
138 this._superApply = __superApply;
139
140 return returnValue;
141 };
142 } )();
143 } );
144 constructor.prototype = $.widget.extend( basePrototype, {
145
146 // TODO: remove support for widgetEventPrefix
147 // always use the name + a colon as the prefix, e.g., draggable:start
148 // don't prefix for widgets that aren't DOM-based
149 widgetEventPrefix: existingConstructor ? ( basePrototype.widgetEventPrefix || name ) : name
150 }, proxiedPrototype, {
151 constructor: constructor,
152 namespace: namespace,
153 widgetName: name,
154 widgetFullName: fullName
155 } );
156
157 // If this widget is being redefined then we need to find all widgets that
158 // are inheriting from it and redefine all of them so that they inherit from
159 // the new version of this widget. We're essentially trying to replace one
160 // level in the prototype chain.
161 if ( existingConstructor ) {
162 $.each( existingConstructor._childConstructors, function( i, child ) {
163 var childPrototype = child.prototype;
164
165 // Redefine the child widget using the same prototype that was
166 // originally used, but inherit from the new version of the base
167 $.widget( childPrototype.namespace + "." + childPrototype.widgetName, constructor,
168 child._proto );
169 } );
170
171 // Remove the list of existing child constructors from the old constructor
172 // so the old child constructors can be garbage collected
173 delete existingConstructor._childConstructors;
174 } else {
175 base._childConstructors.push( constructor );
176 }
177
178 $.widget.bridge( name, constructor );
179
180 return constructor;
181 };
182
183 $.widget.extend = function( target ) {
184 var input = widgetSlice.call( arguments, 1 );
185 var inputIndex = 0;
186 var inputLength = input.length;
187 var key;
188 var value;
189
190 for ( ; inputIndex < inputLength; inputIndex++ ) {
191 for ( key in input[ inputIndex ] ) {
192 value = input[ inputIndex ][ key ];
193 if ( widgetHasOwnProperty.call( input[ inputIndex ], key ) && value !== undefined ) {
194
195 // Clone objects
196 if ( $.isPlainObject( value ) ) {
197 target[ key ] = $.isPlainObject( target[ key ] ) ?
198 $.widget.extend( {}, target[ key ], value ) :
199
200 // Don't extend strings, arrays, etc. with objects
201 $.widget.extend( {}, value );
202
203 // Copy everything else by reference
204 } else {
205 target[ key ] = value;
206 }
207 }
208 }
209 }
210 return target;
211 };
212
213 $.widget.bridge = function( name, object ) {
214 var fullName = object.prototype.widgetFullName || name;
215 $.fn[ name ] = function( options ) {
216 var isMethodCall = typeof options === "string";
217 var args = widgetSlice.call( arguments, 1 );
218 var returnValue = this;
219
220 if ( isMethodCall ) {
221
222 // If this is an empty collection, we need to have the instance method
223 // return undefined instead of the jQuery instance
224 if ( !this.length && options === "instance" ) {
225 returnValue = undefined;
226 } else {
227 this.each( function() {
228 var methodValue;
229 var instance = $.data( this, fullName );
230
231 if ( options === "instance" ) {
232 returnValue = instance;
233 return false;
234 }
235
236 if ( !instance ) {
237 return $.error( "cannot call methods on " + name +
238 " prior to initialization; " +
239 "attempted to call method '" + options + "'" );
240 }
241
242 if ( typeof instance[ options ] !== "function" ||
243 options.charAt( 0 ) === "_" ) {
244 return $.error( "no such method '" + options + "' for " + name +
245 " widget instance" );
246 }
247
248 methodValue = instance[ options ].apply( instance, args );
249
250 if ( methodValue !== instance && methodValue !== undefined ) {
251 returnValue = methodValue && methodValue.jquery ?
252 returnValue.pushStack( methodValue.get() ) :
253 methodValue;
254 return false;
255 }
256 } );
257 }
258 } else {
259
260 // Allow multiple hashes to be passed on init
261 if ( args.length ) {
262 options = $.widget.extend.apply( null, [ options ].concat( args ) );
263 }
264
265 this.each( function() {
266 var instance = $.data( this, fullName );
267 if ( instance ) {
268 instance.option( options || {} );
269 if ( instance._init ) {
270 instance._init();
271 }
272 } else {
273 $.data( this, fullName, new object( options, this ) );
274 }
275 } );
276 }
277
278 return returnValue;
279 };
280 };
281
282 $.Widget = function( /* options, element */ ) {};
283 $.Widget._childConstructors = [];
284
285 $.Widget.prototype = {
286 widgetName: "widget",
287 widgetEventPrefix: "",
288 defaultElement: "<div>",
289
290 options: {
291 classes: {},
292 disabled: false,
293
294 // Callbacks
295 create: null
296 },
297
298 _createWidget: function( options, element ) {
299 element = $( element || this.defaultElement || this )[ 0 ];
300 this.element = $( element );
301 this.uuid = widgetUuid++;
302 this.eventNamespace = "." + this.widgetName + this.uuid;
303
304 this.bindings = $();
305 this.hoverable = $();
306 this.focusable = $();
307 this.classesElementLookup = {};
308
309 if ( element !== this ) {
310 $.data( element, this.widgetFullName, this );
311 this._on( true, this.element, {
312 remove: function( event ) {
313 if ( event.target === element ) {
314 this.destroy();
315 }
316 }
317 } );
318 this.document = $( element.style ?
319
320 // Element within the document
321 element.ownerDocument :
322
323 // Element is window or document
324 element.document || element );
325 this.window = $( this.document[ 0 ].defaultView || this.document[ 0 ].parentWindow );
326 }
327
328 this.options = $.widget.extend( {},
329 this.options,
330 this._getCreateOptions(),
331 options );
332
333 this._create();
334
335 if ( this.options.disabled ) {
336 this._setOptionDisabled( this.options.disabled );
337 }
338
339 this._trigger( "create", null, this._getCreateEventData() );
340 this._init();
341 },
342
343 _getCreateOptions: function() {
344 return {};
345 },
346
347 _getCreateEventData: $.noop,
348
349 _create: $.noop,
350
351 _init: $.noop,
352
353 destroy: function() {
354 var that = this;
355
356 this._destroy();
357 $.each( this.classesElementLookup, function( key, value ) {
358 that._removeClass( value, key );
359 } );
360
361 // We can probably remove the unbind calls in 2.0
362 // all event bindings should go through this._on()
363 this.element
364 .off( this.eventNamespace )
365 .removeData( this.widgetFullName );
366 this.widget()
367 .off( this.eventNamespace )
368 .removeAttr( "aria-disabled" );
369
370 // Clean up events and states
371 this.bindings.off( this.eventNamespace );
372 },
373
374 _destroy: $.noop,
375
376 widget: function() {
377 return this.element;
378 },
379
380 option: function( key, value ) {
381 var options = key;
382 var parts;
383 var curOption;
384 var i;
385
386 if ( arguments.length === 0 ) {
387
388 // Don't return a reference to the internal hash
389 return $.widget.extend( {}, this.options );
390 }
391
392 if ( typeof key === "string" ) {
393
394 // Handle nested keys, e.g., "foo.bar" => { foo: { bar: ___ } }
395 options = {};
396 parts = key.split( "." );
397 key = parts.shift();
398 if ( parts.length ) {
399 curOption = options[ key ] = $.widget.extend( {}, this.options[ key ] );
400 for ( i = 0; i < parts.length - 1; i++ ) {
401 curOption[ parts[ i ] ] = curOption[ parts[ i ] ] || {};
402 curOption = curOption[ parts[ i ] ];
403 }
404 key = parts.pop();
405 if ( arguments.length === 1 ) {
406 return curOption[ key ] === undefined ? null : curOption[ key ];
407 }
408 curOption[ key ] = value;
409 } else {
410 if ( arguments.length === 1 ) {
411 return this.options[ key ] === undefined ? null : this.options[ key ];
412 }
413 options[ key ] = value;
414 }
415 }
416
417 this._setOptions( options );
418
419 return this;
420 },
421
422 _setOptions: function( options ) {
423 var key;
424
425 for ( key in options ) {
426 this._setOption( key, options[ key ] );
427 }
428
429 return this;
430 },
431
432 _setOption: function( key, value ) {
433 if ( key === "classes" ) {
434 this._setOptionClasses( value );
435 }
436
437 this.options[ key ] = value;
438
439 if ( key === "disabled" ) {
440 this._setOptionDisabled( value );
441 }
442
443 return this;
444 },
445
446 _setOptionClasses: function( value ) {
447 var classKey, elements, currentElements;
448
449 for ( classKey in value ) {
450 currentElements = this.classesElementLookup[ classKey ];
451 if ( value[ classKey ] === this.options.classes[ classKey ] ||
452 !currentElements ||
453 !currentElements.length ) {
454 continue;
455 }
456
457 // We are doing this to create a new jQuery object because the _removeClass() call
458 // on the next line is going to destroy the reference to the current elements being
459 // tracked. We need to save a copy of this collection so that we can add the new classes
460 // below.
461 elements = $( currentElements.get() );
462 this._removeClass( currentElements, classKey );
463
464 // We don't use _addClass() here, because that uses this.options.classes
465 // for generating the string of classes. We want to use the value passed in from
466 // _setOption(), this is the new value of the classes option which was passed to
467 // _setOption(). We pass this value directly to _classes().
468 elements.addClass( this._classes( {
469 element: elements,
470 keys: classKey,
471 classes: value,
472 add: true
473 } ) );
474 }
475 },
476
477 _setOptionDisabled: function( value ) {
478 this._toggleClass( this.widget(), this.widgetFullName + "-disabled", null, !!value );
479
480 // If the widget is becoming disabled, then nothing is interactive
481 if ( value ) {
482 this._removeClass( this.hoverable, null, "ui-state-hover" );
483 this._removeClass( this.focusable, null, "ui-state-focus" );
484 }
485 },
486
487 enable: function() {
488 return this._setOptions( { disabled: false } );
489 },
490
491 disable: function() {
492 return this._setOptions( { disabled: true } );
493 },
494
495 _classes: function( options ) {
496 var full = [];
497 var that = this;
498
499 options = $.extend( {
500 element: this.element,
501 classes: this.options.classes || {}
502 }, options );
503
504 function bindRemoveEvent() {
505 var nodesToBind = [];
506
507 options.element.each( function( _, element ) {
508 var isTracked = $.map( that.classesElementLookup, function( elements ) {
509 return elements;
510 } )
511 .some( function( elements ) {
512 return elements.is( element );
513 } );
514
515 if ( !isTracked ) {
516 nodesToBind.push( element );
517 }
518 } );
519
520 that._on( $( nodesToBind ), {
521 remove: "_untrackClassesElement"
522 } );
523 }
524
525 function processClassString( classes, checkOption ) {
526 var current, i;
527 for ( i = 0; i < classes.length; i++ ) {
528 current = that.classesElementLookup[ classes[ i ] ] || $();
529 if ( options.add ) {
530 bindRemoveEvent();
531 current = $( $.uniqueSort( current.get().concat( options.element.get() ) ) );
532 } else {
533 current = $( current.not( options.element ).get() );
534 }
535 that.classesElementLookup[ classes[ i ] ] = current;
536 full.push( classes[ i ] );
537 if ( checkOption && options.classes[ classes[ i ] ] ) {
538 full.push( options.classes[ classes[ i ] ] );
539 }
540 }
541 }
542
543 if ( options.keys ) {
544 processClassString( options.keys.match( /\S+/g ) || [], true );
545 }
546 if ( options.extra ) {
547 processClassString( options.extra.match( /\S+/g ) || [] );
548 }
549
550 return full.join( " " );
551 },
552
553 _untrackClassesElement: function( event ) {
554 var that = this;
555 $.each( that.classesElementLookup, function( key, value ) {
556 if ( $.inArray( event.target, value ) !== -1 ) {
557 that.classesElementLookup[ key ] = $( value.not( event.target ).get() );
558 }
559 } );
560
561 this._off( $( event.target ) );
562 },
563
564 _removeClass: function( element, keys, extra ) {
565 return this._toggleClass( element, keys, extra, false );
566 },
567
568 _addClass: function( element, keys, extra ) {
569 return this._toggleClass( element, keys, extra, true );
570 },
571
572 _toggleClass: function( element, keys, extra, add ) {
573 add = ( typeof add === "boolean" ) ? add : extra;
574 var shift = ( typeof element === "string" || element === null ),
575 options = {
576 extra: shift ? keys : extra,
577 keys: shift ? element : keys,
578 element: shift ? this.element : element,
579 add: add
580 };
581 options.element.toggleClass( this._classes( options ), add );
582 return this;
583 },
584
585 _on: function( suppressDisabledCheck, element, handlers ) {
586 var delegateElement;
587 var instance = this;
588
589 // No suppressDisabledCheck flag, shuffle arguments
590 if ( typeof suppressDisabledCheck !== "boolean" ) {
591 handlers = element;
592 element = suppressDisabledCheck;
593 suppressDisabledCheck = false;
594 }
595
596 // No element argument, shuffle and use this.element
597 if ( !handlers ) {
598 handlers = element;
599 element = this.element;
600 delegateElement = this.widget();
601 } else {
602 element = delegateElement = $( element );
603 this.bindings = this.bindings.add( element );
604 }
605
606 $.each( handlers, function( event, handler ) {
607 function handlerProxy() {
608
609 // Allow widgets to customize the disabled handling
610 // - disabled as an array instead of boolean
611 // - disabled class as method for disabling individual parts
612 if ( !suppressDisabledCheck &&
613 ( instance.options.disabled === true ||
614 $( this ).hasClass( "ui-state-disabled" ) ) ) {
615 return;
616 }
617 return ( typeof handler === "string" ? instance[ handler ] : handler )
618 .apply( instance, arguments );
619 }
620
621 // Copy the guid so direct unbinding works
622 if ( typeof handler !== "string" ) {
623 handlerProxy.guid = handler.guid =
624 handler.guid || handlerProxy.guid || $.guid++;
625 }
626
627 var match = event.match( /^([\w:-]*)\s*(.*)$/ );
628 var eventName = match[ 1 ] + instance.eventNamespace;
629 var selector = match[ 2 ];
630
631 if ( selector ) {
632 delegateElement.on( eventName, selector, handlerProxy );
633 } else {
634 element.on( eventName, handlerProxy );
635 }
636 } );
637 },
638
639 _off: function( element, eventName ) {
640 eventName = ( eventName || "" ).split( " " ).join( this.eventNamespace + " " ) +
641 this.eventNamespace;
642 element.off( eventName );
643
644 // Clear the stack to avoid memory leaks (#10056)
645 this.bindings = $( this.bindings.not( element ).get() );
646 this.focusable = $( this.focusable.not( element ).get() );
647 this.hoverable = $( this.hoverable.not( element ).get() );
648 },
649
650 _delay: function( handler, delay ) {
651 function handlerProxy() {
652 return ( typeof handler === "string" ? instance[ handler ] : handler )
653 .apply( instance, arguments );
654 }
655 var instance = this;
656 return setTimeout( handlerProxy, delay || 0 );
657 },
658
659 _hoverable: function( element ) {
660 this.hoverable = this.hoverable.add( element );
661 this._on( element, {
662 mouseenter: function( event ) {
663 this._addClass( $( event.currentTarget ), null, "ui-state-hover" );
664 },
665 mouseleave: function( event ) {
666 this._removeClass( $( event.currentTarget ), null, "ui-state-hover" );
667 }
668 } );
669 },
670
671 _focusable: function( element ) {
672 this.focusable = this.focusable.add( element );
673 this._on( element, {
674 focusin: function( event ) {
675 this._addClass( $( event.currentTarget ), null, "ui-state-focus" );
676 },
677 focusout: function( event ) {
678 this._removeClass( $( event.currentTarget ), null, "ui-state-focus" );
679 }
680 } );
681 },
682
683 _trigger: function( type, event, data ) {
684 var prop, orig;
685 var callback = this.options[ type ];
686
687 data = data || {};
688 event = $.Event( event );
689 event.type = ( type === this.widgetEventPrefix ?
690 type :
691 this.widgetEventPrefix + type ).toLowerCase();
692
693 // The original event may come from any element
694 // so we need to reset the target on the new event
695 event.target = this.element[ 0 ];
696
697 // Copy original event properties over to the new event
698 orig = event.originalEvent;
699 if ( orig ) {
700 for ( prop in orig ) {
701 if ( !( prop in event ) ) {
702 event[ prop ] = orig[ prop ];
703 }
704 }
705 }
706
707 this.element.trigger( event, data );
708 return !( typeof callback === "function" &&
709 callback.apply( this.element[ 0 ], [ event ].concat( data ) ) === false ||
710 event.isDefaultPrevented() );
711 }
712 };
713
714 $.each( { show: "fadeIn", hide: "fadeOut" }, function( method, defaultEffect ) {
715 $.Widget.prototype[ "_" + method ] = function( element, options, callback ) {
716 if ( typeof options === "string" ) {
717 options = { effect: options };
718 }
719
720 var hasOptions;
721 var effectName = !options ?
722 method :
723 options === true || typeof options === "number" ?
724 defaultEffect :
725 options.effect || defaultEffect;
726
727 options = options || {};
728 if ( typeof options === "number" ) {
729 options = { duration: options };
730 } else if ( options === true ) {
731 options = {};
732 }
733
734 hasOptions = !$.isEmptyObject( options );
735 options.complete = callback;
736
737 if ( options.delay ) {
738 element.delay( options.delay );
739 }
740
741 if ( hasOptions && $.effects && $.effects.effect[ effectName ] ) {
742 element[ method ]( options );
743 } else if ( effectName !== method && element[ effectName ] ) {
744 element[ effectName ]( options.duration, options.easing, callback );
745 } else {
746 element.queue( function( next ) {
747 $( this )[ method ]();
748 if ( callback ) {
749 callback.call( element[ 0 ] );
750 }
751 next();
752 } );
753 }
754 };
755 } );
756
757 return $.widget;
758
759 } );