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