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