Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / resources / jquery.ui / jquery.ui.widget.js
blob66ef0133ef3276f4fb06de3356566618c24b4264
1 /*!
2  * jQuery UI Widget 1.8.24
3  *
4  * Copyright 2012, AUTHORS.txt (http://jqueryui.com/about)
5  * Dual licensed under the MIT or GPL Version 2 licenses.
6  * http://jquery.org/license
7  *
8  * http://docs.jquery.com/UI/Widget
9  */
10 (function( $, undefined ) {
12 // jQuery 1.4+
13 if ( $.cleanData ) {
14         var _cleanData = $.cleanData;
15         $.cleanData = function( elems ) {
16                 for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
17                         try {
18                                 $( elem ).triggerHandler( "remove" );
19                         // http://bugs.jquery.com/ticket/8235
20                         } catch( e ) {}
21                 }
22                 _cleanData( elems );
23         };
24 } else {
25         var _remove = $.fn.remove;
26         $.fn.remove = function( selector, keepData ) {
27                 return this.each(function() {
28                         if ( !keepData ) {
29                                 if ( !selector || $.filter( selector, [ this ] ).length ) {
30                                         $( "*", this ).add( [ this ] ).each(function() {
31                                                 try {
32                                                         $( this ).triggerHandler( "remove" );
33                                                 // http://bugs.jquery.com/ticket/8235
34                                                 } catch( e ) {}
35                                         });
36                                 }
37                         }
38                         return _remove.call( $(this), selector, keepData );
39                 });
40         };
43 $.widget = function( name, base, prototype ) {
44         var namespace = name.split( "." )[ 0 ],
45                 fullName;
46         name = name.split( "." )[ 1 ];
47         fullName = namespace + "-" + name;
49         if ( !prototype ) {
50                 prototype = base;
51                 base = $.Widget;
52         }
54         // create selector for plugin
55         $.expr[ ":" ][ fullName ] = function( elem ) {
56                 return !!$.data( elem, name );
57         };
59         $[ namespace ] = $[ namespace ] || {};
60         $[ namespace ][ name ] = function( options, element ) {
61                 // allow instantiation without initializing for simple inheritance
62                 if ( arguments.length ) {
63                         this._createWidget( options, element );
64                 }
65         };
67         var basePrototype = new base();
68         // we need to make the options hash a property directly on the new instance
69         // otherwise we'll modify the options hash on the prototype that we're
70         // inheriting from
71 //      $.each( basePrototype, function( key, val ) {
72 //              if ( $.isPlainObject(val) ) {
73 //                      basePrototype[ key ] = $.extend( {}, val );
74 //              }
75 //      });
76         basePrototype.options = $.extend( true, {}, basePrototype.options );
77         $[ namespace ][ name ].prototype = $.extend( true, basePrototype, {
78                 namespace: namespace,
79                 widgetName: name,
80                 widgetEventPrefix: $[ namespace ][ name ].prototype.widgetEventPrefix || name,
81                 widgetBaseClass: fullName
82         }, prototype );
84         $.widget.bridge( name, $[ namespace ][ name ] );
87 $.widget.bridge = function( name, object ) {
88         $.fn[ name ] = function( options ) {
89                 var isMethodCall = typeof options === "string",
90                         args = Array.prototype.slice.call( arguments, 1 ),
91                         returnValue = this;
93                 // allow multiple hashes to be passed on init
94                 options = !isMethodCall && args.length ?
95                         $.extend.apply( null, [ true, options ].concat(args) ) :
96                         options;
98                 // prevent calls to internal methods
99                 if ( isMethodCall && options.charAt( 0 ) === "_" ) {
100                         return returnValue;
101                 }
103                 if ( isMethodCall ) {
104                         this.each(function() {
105                                 var instance = $.data( this, name ),
106                                         methodValue = instance && $.isFunction( instance[options] ) ?
107                                                 instance[ options ].apply( instance, args ) :
108                                                 instance;
109                                 // TODO: add this back in 1.9 and use $.error() (see #5972)
110 //                              if ( !instance ) {
111 //                                      throw "cannot call methods on " + name + " prior to initialization; " +
112 //                                              "attempted to call method '" + options + "'";
113 //                              }
114 //                              if ( !$.isFunction( instance[options] ) ) {
115 //                                      throw "no such method '" + options + "' for " + name + " widget instance";
116 //                              }
117 //                              var methodValue = instance[ options ].apply( instance, args );
118                                 if ( methodValue !== instance && methodValue !== undefined ) {
119                                         returnValue = methodValue;
120                                         return false;
121                                 }
122                         });
123                 } else {
124                         this.each(function() {
125                                 var instance = $.data( this, name );
126                                 if ( instance ) {
127                                         instance.option( options || {} )._init();
128                                 } else {
129                                         $.data( this, name, new object( options, this ) );
130                                 }
131                         });
132                 }
134                 return returnValue;
135         };
138 $.Widget = function( options, element ) {
139         // allow instantiation without initializing for simple inheritance
140         if ( arguments.length ) {
141                 this._createWidget( options, element );
142         }
145 $.Widget.prototype = {
146         widgetName: "widget",
147         widgetEventPrefix: "",
148         options: {
149                 disabled: false
150         },
151         _createWidget: function( options, element ) {
152                 // $.widget.bridge stores the plugin instance, but we do it anyway
153                 // so that it's stored even before the _create function runs
154                 $.data( element, this.widgetName, this );
155                 this.element = $( element );
156                 this.options = $.extend( true, {},
157                         this.options,
158                         this._getCreateOptions(),
159                         options );
161                 var self = this;
162                 this.element.bind( "remove." + this.widgetName, function() {
163                         self.destroy();
164                 });
166                 this._create();
167                 this._trigger( "create" );
168                 this._init();
169         },
170         _getCreateOptions: function() {
171                 return $.metadata && $.metadata.get( this.element[0] )[ this.widgetName ];
172         },
173         _create: function() {},
174         _init: function() {},
176         destroy: function() {
177                 this.element
178                         .unbind( "." + this.widgetName )
179                         .removeData( this.widgetName );
180                 this.widget()
181                         .unbind( "." + this.widgetName )
182                         .removeAttr( "aria-disabled" )
183                         .removeClass(
184                                 this.widgetBaseClass + "-disabled " +
185                                 "ui-state-disabled" );
186         },
188         widget: function() {
189                 return this.element;
190         },
192         option: function( key, value ) {
193                 var options = key;
195                 if ( arguments.length === 0 ) {
196                         // don't return a reference to the internal hash
197                         return $.extend( {}, this.options );
198                 }
200                 if  (typeof key === "string" ) {
201                         if ( value === undefined ) {
202                                 return this.options[ key ];
203                         }
204                         options = {};
205                         options[ key ] = value;
206                 }
208                 this._setOptions( options );
210                 return this;
211         },
212         _setOptions: function( options ) {
213                 var self = this;
214                 $.each( options, function( key, value ) {
215                         self._setOption( key, value );
216                 });
218                 return this;
219         },
220         _setOption: function( key, value ) {
221                 this.options[ key ] = value;
223                 if ( key === "disabled" ) {
224                         this.widget()
225                                 [ value ? "addClass" : "removeClass"](
226                                         this.widgetBaseClass + "-disabled" + " " +
227                                         "ui-state-disabled" )
228                                 .attr( "aria-disabled", value );
229                 }
231                 return this;
232         },
234         enable: function() {
235                 return this._setOption( "disabled", false );
236         },
237         disable: function() {
238                 return this._setOption( "disabled", true );
239         },
241         _trigger: function( type, event, data ) {
242                 var prop, orig,
243                         callback = this.options[ type ];
245                 data = data || {};
246                 event = $.Event( event );
247                 event.type = ( type === this.widgetEventPrefix ?
248                         type :
249                         this.widgetEventPrefix + type ).toLowerCase();
250                 // the original event may come from any element
251                 // so we need to reset the target on the new event
252                 event.target = this.element[ 0 ];
254                 // copy original event properties over to the new event
255                 orig = event.originalEvent;
256                 if ( orig ) {
257                         for ( prop in orig ) {
258                                 if ( !( prop in event ) ) {
259                                         event[ prop ] = orig[ prop ];
260                                 }
261                         }
262                 }
264                 this.element.trigger( event, data );
266                 return !( $.isFunction(callback) &&
267                         callback.call( this.element[0], event, data ) === false ||
268                         event.isDefaultPrevented() );
269         }
272 })( jQuery );