2 * jQuery UI Widget 1.8.2
4 * Copyright (c) 2010 AUTHORS.txt (http://jqueryui.com/about)
5 * Dual licensed under the MIT (MIT-LICENSE.txt)
6 * and GPL (GPL-LICENSE.txt) licenses.
8 * http://docs.jquery.com/UI/Widget
12 var _remove = $.fn.remove;
14 $.fn.remove = function( selector, keepData ) {
15 return this.each(function() {
17 if ( !selector || $.filter( selector, [ this ] ).length ) {
18 $( "*", this ).add( this ).each(function() {
19 $( this ).triggerHandler( "remove" );
23 return _remove.call( $(this), selector, keepData );
27 $.widget = function( name, base, prototype ) {
28 var namespace = name.split( "." )[ 0 ],
30 name = name.split( "." )[ 1 ];
31 fullName = namespace + "-" + name;
38 // create selector for plugin
39 $.expr[ ":" ][ fullName ] = function( elem ) {
40 return !!$.data( elem, name );
43 $[ namespace ] = $[ namespace ] || {};
44 $[ namespace ][ name ] = function( options, element ) {
45 // allow instantiation without initializing for simple inheritance
46 if ( arguments.length ) {
47 this._createWidget( options, element );
51 var basePrototype = new base();
52 // we need to make the options hash a property directly on the new instance
53 // otherwise we'll modify the options hash on the prototype that we're
55 // $.each( basePrototype, function( key, val ) {
56 // if ( $.isPlainObject(val) ) {
57 // basePrototype[ key ] = $.extend( {}, val );
60 basePrototype.options = $.extend( {}, basePrototype.options );
61 $[ namespace ][ name ].prototype = $.extend( true, basePrototype, {
64 widgetEventPrefix: $[ namespace ][ name ].prototype.widgetEventPrefix || name,
65 widgetBaseClass: fullName
68 $.widget.bridge( name, $[ namespace ][ name ] );
71 $.widget.bridge = function( name, object ) {
72 $.fn[ name ] = function( options ) {
73 var isMethodCall = typeof options === "string",
74 args = Array.prototype.slice.call( arguments, 1 ),
77 // allow multiple hashes to be passed on init
78 options = !isMethodCall && args.length ?
79 $.extend.apply( null, [ true, options ].concat(args) ) :
82 // prevent calls to internal methods
83 if ( isMethodCall && options.substring( 0, 1 ) === "_" ) {
88 this.each(function() {
89 var instance = $.data( this, name ),
90 methodValue = instance && $.isFunction( instance[options] ) ?
91 instance[ options ].apply( instance, args ) :
93 if ( methodValue !== instance && methodValue !== undefined ) {
94 returnValue = methodValue;
99 this.each(function() {
100 var instance = $.data( this, name );
103 instance.option( options );
107 $.data( this, name, new object( options, this ) );
116 $.Widget = function( options, element ) {
117 // allow instantiation without initializing for simple inheritance
118 if ( arguments.length ) {
119 this._createWidget( options, element );
123 $.Widget.prototype = {
124 widgetName: "widget",
125 widgetEventPrefix: "",
129 _createWidget: function( options, element ) {
130 // $.widget.bridge stores the plugin instance, but we do it anyway
131 // so that it's stored even before the _create function runs
132 this.element = $( element ).data( this.widgetName, this );
133 this.options = $.extend( true, {},
135 $.metadata && $.metadata.get( element )[ this.widgetName ],
139 this.element.bind( "remove." + this.widgetName, function() {
146 _create: function() {},
147 _init: function() {},
149 destroy: function() {
151 .unbind( "." + this.widgetName )
152 .removeData( this.widgetName );
154 .unbind( "." + this.widgetName )
155 .removeAttr( "aria-disabled" )
157 this.widgetBaseClass + "-disabled " +
158 "ui-state-disabled" );
165 option: function( key, value ) {
169 if ( arguments.length === 0 ) {
170 // don't return a reference to the internal hash
171 return $.extend( {}, self.options );
174 if (typeof key === "string" ) {
175 if ( value === undefined ) {
176 return this.options[ key ];
179 options[ key ] = value;
182 $.each( options, function( key, value ) {
183 self._setOption( key, value );
188 _setOption: function( key, value ) {
189 this.options[ key ] = value;
191 if ( key === "disabled" ) {
193 [ value ? "addClass" : "removeClass"](
194 this.widgetBaseClass + "-disabled" + " " +
195 "ui-state-disabled" )
196 .attr( "aria-disabled", value );
203 return this._setOption( "disabled", false );
205 disable: function() {
206 return this._setOption( "disabled", true );
209 _trigger: function( type, event, data ) {
210 var callback = this.options[ type ];
212 event = $.Event( event );
213 event.type = ( type === this.widgetEventPrefix ?
215 this.widgetEventPrefix + type ).toLowerCase();
218 // copy original event properties over to the new event
219 // this would happen if we could call $.event.fix instead of $.Event
220 // but we don't have a way to force an event to be fixed multiple times
221 if ( event.originalEvent ) {
222 for ( var i = $.event.props.length, prop; i; ) {
223 prop = $.event.props[ --i ];
224 event[ prop ] = event.originalEvent[ prop ];
228 this.element.trigger( event, data );
230 return !( $.isFunction(callback) &&
231 callback.call( this.element[0], event, data ) === false ||
232 event.isDefaultPrevented() );