JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
add needed jquery and jquery-ui files
[peach-cgt.git] / external / jquery.ui.widget.js
1 /*!
2  * jQuery UI Widget 1.8.17
3  *
4  * Copyright 2011, 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 ) {
11
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         };
41 }
42
43 $.widget = function( name, base, prototype ) {
44         var namespace = name.split( "." )[ 0 ],
45                 fullName;
46         name = name.split( "." )[ 1 ];
47         fullName = namespace + "-" + name;
48
49         if ( !prototype ) {
50                 prototype = base;
51                 base = $.Widget;
52         }
53
54         // create selector for plugin
55         $.expr[ ":" ][ fullName ] = function( elem ) {
56                 return !!$.data( elem, name );
57         };
58
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         };
66
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 );
83
84         $.widget.bridge( name, $[ namespace ][ name ] );
85 };
86
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;
92
93                 // allow multiple hashes to be passed on init
94                 options = !isMethodCall && args.length ?
95                         $.extend.apply( null, [ true, options ].concat(args) ) :
96                         options;
97
98                 // prevent calls to internal methods
99                 if ( isMethodCall && options.charAt( 0 ) === "_" ) {
100                         return returnValue;
101                 }
102
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                 }
133
134                 return returnValue;
135         };
136 };
137
138 $.Widget = function( options, element ) {
139         // allow instantiation without initializing for simple inheritance
140         if ( arguments.length ) {
141                 this._createWidget( options, element );
142         }
143 };
144
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 );
160
161                 var self = this;
162                 this.element.bind( "remove." + this.widgetName, function() {
163                         self.destroy();
164                 });
165
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() {},
175
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         },
187
188         widget: function() {
189                 return this.element;
190         },
191
192         option: function( key, value ) {
193                 var options = key;
194
195                 if ( arguments.length === 0 ) {
196                         // don't return a reference to the internal hash
197                         return $.extend( {}, this.options );
198                 }
199
200                 if  (typeof key === "string" ) {
201                         if ( value === undefined ) {
202                                 return this.options[ key ];
203                         }
204                         options = {};
205                         options[ key ] = value;
206                 }
207
208                 this._setOptions( options );
209
210                 return this;
211         },
212         _setOptions: function( options ) {
213                 var self = this;
214                 $.each( options, function( key, value ) {
215                         self._setOption( key, value );
216                 });
217
218                 return this;
219         },
220         _setOption: function( key, value ) {
221                 this.options[ key ] = value;
222
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                 }
230
231                 return this;
232         },
233
234         enable: function() {
235                 return this._setOption( "disabled", false );
236         },
237         disable: function() {
238                 return this._setOption( "disabled", true );
239         },
240
241         _trigger: function( type, event, data ) {
242                 var prop, orig,
243                         callback = this.options[ type ];
244
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 ];
253
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                 }
263
264                 this.element.trigger( event, data );
265
266                 return !( $.isFunction(callback) &&
267                         callback.call( this.element[0], event, data ) === false ||
268                         event.isDefaultPrevented() );
269         }
270 };
271
272 })( jQuery );