2 * jQuery UI Widget @VERSION
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
8 * http://docs.jquery.com/UI/Widget
11 (function( $, undefined ) {
15 var _cleanData = $.cleanData;
16 $.cleanData = function( elems ) {
17 for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
18 $( elem ).triggerHandler( "remove" );
23 var _remove = $.fn.remove;
24 $.fn.remove = function( selector, keepData ) {
25 return this.each(function() {
27 if ( !selector || $.filter( selector, [ this ] ).length ) {
28 $( "*", this ).add( [ this ] ).each(function() {
29 $( this ).triggerHandler( "remove" );
33 return _remove.call( $(this), selector, keepData );
38 $.widget = function( name, base, prototype ) {
39 var namespace = name.split( "." )[ 0 ],
41 name = name.split( "." )[ 1 ];
42 fullName = namespace + "-" + name;
49 // create selector for plugin
50 $.expr[ ":" ][ fullName ] = function( elem ) {
51 return !!$.data( elem, name );
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 );
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
66 // $.each( basePrototype, function( key, val ) {
67 // if ( $.isPlainObject(val) ) {
68 // basePrototype[ key ] = $.extend( {}, val );
71 basePrototype.options = $.extend( true, {}, basePrototype.options );
72 $[ namespace ][ name ].prototype = $.extend( true, basePrototype, {
75 widgetEventPrefix: $[ namespace ][ name ].prototype.widgetEventPrefix || name,
76 widgetBaseClass: fullName
79 $.widget.bridge( name, $[ namespace ][ name ] );
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 ),
88 // allow multiple hashes to be passed on init
89 options = !isMethodCall && args.length ?
90 $.extend.apply( null, [ true, options ].concat(args) ) :
93 // prevent calls to internal methods
94 if ( isMethodCall && options.charAt( 0 ) === "_" ) {
99 this.each(function() {
100 var instance = $.data( this, name );
102 throw "cannot call methods on " + name + " prior to initialization; " +
103 "attempted to call method '" + options + "'";
105 if ( !$.isFunction( instance[options] ) ) {
106 throw "no such method '" + options + "' for " + name + " widget instance";
108 var methodValue = instance[ options ].apply( instance, args );
109 if ( methodValue !== instance && methodValue !== undefined ) {
110 returnValue = methodValue;
115 this.each(function() {
116 var instance = $.data( this, name );
118 instance.option( options || {} )._init();
120 $.data( this, name, new object( options, this ) );
129 $.Widget = function( options, element ) {
130 // allow instantiation without initializing for simple inheritance
131 if ( arguments.length ) {
132 this._createWidget( options, element );
136 $.Widget.prototype = {
137 widgetName: "widget",
138 widgetEventPrefix: "",
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, {},
149 this._getCreateOptions(),
153 this.element.bind( "remove." + this.widgetName, function() {
158 this._trigger( "create" );
161 _getCreateOptions: function() {
164 options = $.metadata.get( element )[ this.widgetName ];
168 _create: function() {},
169 _init: function() {},
171 destroy: function() {
173 .unbind( "." + this.widgetName )
174 .removeData( this.widgetName );
176 .unbind( "." + this.widgetName )
177 .removeAttr( "aria-disabled" )
179 this.widgetBaseClass + "-disabled " +
180 "ui-state-disabled" );
187 option: function( key, value ) {
190 if ( arguments.length === 0 ) {
191 // don't return a reference to the internal hash
192 return $.extend( {}, this.options );
195 if (typeof key === "string" ) {
196 if ( value === undefined ) {
197 return this.options[ key ];
200 options[ key ] = value;
203 this._setOptions( options );
207 _setOptions: function( options ) {
209 $.each( options, function( key, value ) {
210 self._setOption( key, value );
215 _setOption: function( key, value ) {
216 this.options[ key ] = value;
218 if ( key === "disabled" ) {
220 [ value ? "addClass" : "removeClass"](
221 this.widgetBaseClass + "-disabled" + " " +
222 "ui-state-disabled" )
223 .attr( "aria-disabled", value );
230 return this._setOption( "disabled", false );
232 disable: function() {
233 return this._setOption( "disabled", true );
236 _trigger: function( type, event, data ) {
237 var callback = this.options[ type ];
239 event = $.Event( event );
240 event.type = ( type === this.widgetEventPrefix ?
242 this.widgetEventPrefix + type ).toLowerCase();
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 ];
255 this.element.trigger( event, data );
257 return !( $.isFunction(callback) &&
258 callback.call( this.element[0], event, data ) === false ||
259 event.isDefaultPrevented() );