jquery-ui/ui/jquery.ui.widget.js

237 lines
5.9 KiB
JavaScript
Raw Normal View History

/*!
* jQuery UI Widget @VERSION
*
* Copyright 2010, AUTHORS.txt (http://jqueryui.com/about)
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* http://docs.jquery.com/UI/Widget
*/
(function( $, undefined ) {
var _remove = $.fn.remove;
$.fn.remove = function( selector, keepData ) {
return this.each(function() {
if ( !keepData ) {
if ( !selector || $.filter( selector, [ this ] ).length ) {
$( "*", this ).add( [ this ] ).each(function() {
$( this ).triggerHandler( "remove" );
});
}
}
return _remove.call( $(this), selector, keepData );
});
};
2010-01-15 20:47:41 +00:00
$.widget = function( name, base, prototype ) {
var namespace = name.split( "." )[ 0 ],
fullName;
2010-01-15 20:47:41 +00:00
name = name.split( "." )[ 1 ];
fullName = namespace + "-" + name;
2010-01-15 20:47:41 +00:00
if ( !prototype ) {
prototype = base;
base = $.Widget;
}
// create selector for plugin
2010-01-15 20:47:41 +00:00
$.expr[ ":" ][ fullName ] = function( elem ) {
return !!$.data( elem, name );
};
2010-01-15 20:47:41 +00:00
$[ namespace ] = $[ namespace ] || {};
$[ namespace ][ name ] = function( options, element ) {
// allow instantiation without initializing for simple inheritance
2010-01-15 20:47:41 +00:00
if ( arguments.length ) {
this._createWidget( options, element );
}
};
var basePrototype = new base();
// we need to make the options hash a property directly on the new instance
// otherwise we'll modify the options hash on the prototype that we're
// inheriting from
2010-01-15 20:47:41 +00:00
// $.each( basePrototype, function( key, val ) {
// if ( $.isPlainObject(val) ) {
// basePrototype[ key ] = $.extend( {}, val );
// }
// });
basePrototype.options = $.extend( true, {}, basePrototype.options );
2010-01-15 20:47:41 +00:00
$[ namespace ][ name ].prototype = $.extend( true, basePrototype, {
namespace: namespace,
widgetName: name,
2010-01-15 20:47:41 +00:00
widgetEventPrefix: $[ namespace ][ name ].prototype.widgetEventPrefix || name,
widgetBaseClass: fullName
2010-01-15 20:47:41 +00:00
}, prototype );
2010-01-15 20:47:41 +00:00
$.widget.bridge( name, $[ namespace ][ name ] );
};
2010-01-15 20:47:41 +00:00
$.widget.bridge = function( name, object ) {
2010-01-15 22:15:18 +00:00
$.fn[ name ] = function( options ) {
2010-01-15 20:47:41 +00:00
var isMethodCall = typeof options === "string",
args = Array.prototype.slice.call( arguments, 1 ),
returnValue = this;
2010-01-15 20:47:41 +00:00
// allow multiple hashes to be passed on init
2010-01-15 22:15:18 +00:00
options = !isMethodCall && args.length ?
$.extend.apply( null, [ true, options ].concat(args) ) :
options;
// prevent calls to internal methods
2010-01-15 22:15:18 +00:00
if ( isMethodCall && options.substring( 0, 1 ) === "_" ) {
return returnValue;
}
2010-01-15 20:47:41 +00:00
if ( isMethodCall ) {
this.each(function() {
var instance = $.data( this, name ),
methodValue = instance && $.isFunction( instance[options] ) ?
2010-01-15 22:15:18 +00:00
instance[ options ].apply( instance, args ) :
2010-01-15 20:47:41 +00:00
instance;
if ( methodValue !== instance && methodValue !== undefined ) {
returnValue = methodValue;
return false;
}
2010-01-15 20:47:41 +00:00
});
} else {
this.each(function() {
var instance = $.data( this, name );
if ( instance ) {
if ( options ) {
instance.option( options );
}
instance._init();
} else {
2010-01-15 20:47:41 +00:00
$.data( this, name, new object( options, this ) );
}
});
}
return returnValue;
};
};
2010-01-15 20:47:41 +00:00
$.Widget = function( options, element ) {
// allow instantiation without initializing for simple inheritance
2010-01-15 20:47:41 +00:00
if ( arguments.length ) {
this._createWidget( options, element );
}
};
$.Widget.prototype = {
2010-01-15 20:47:41 +00:00
widgetName: "widget",
widgetEventPrefix: "",
options: {
disabled: false
},
2010-01-15 20:47:41 +00:00
_createWidget: function( options, element ) {
// $.widget.bridge stores the plugin instance, but we do it anyway
// so that it's stored even before the _create function runs
2010-01-15 20:47:41 +00:00
this.element = $( element ).data( this.widgetName, this );
this.options = $.extend( true, {},
this.options,
2010-01-15 20:47:41 +00:00
$.metadata && $.metadata.get( element )[ this.widgetName ],
options );
var self = this;
2010-01-19 13:53:32 +00:00
this.element.bind( "remove." + this.widgetName, function() {
self.destroy();
});
this._create();
this._init();
},
_create: function() {},
_init: function() {},
destroy: function() {
this.element
2010-01-15 20:47:41 +00:00
.unbind( "." + this.widgetName )
.removeData( this.widgetName );
this.widget()
2010-01-15 20:47:41 +00:00
.unbind( "." + this.widgetName )
.removeAttr( "aria-disabled" )
.removeClass(
2010-01-15 20:47:41 +00:00
this.widgetBaseClass + "-disabled " +
"ui-state-disabled" );
},
widget: function() {
return this.element;
},
2010-01-15 20:47:41 +00:00
option: function( key, value ) {
var options = key,
self = this;
2010-01-15 20:47:41 +00:00
if ( arguments.length === 0 ) {
// don't return a reference to the internal hash
2010-01-15 20:47:41 +00:00
return $.extend( {}, self.options );
}
2010-01-15 20:47:41 +00:00
if (typeof key === "string" ) {
if ( value === undefined ) {
return this.options[ key ];
}
options = {};
2010-01-15 20:47:41 +00:00
options[ key ] = value;
}
2010-01-15 20:47:41 +00:00
$.each( options, function( key, value ) {
self._setOption( key, value );
});
return self;
},
2010-01-15 20:47:41 +00:00
_setOption: function( key, value ) {
this.options[ key ] = value;
2010-01-15 20:47:41 +00:00
if ( key === "disabled" ) {
this.widget()
2010-01-15 20:47:41 +00:00
[ value ? "addClass" : "removeClass"](
this.widgetBaseClass + "-disabled" + " " +
"ui-state-disabled" )
2010-01-15 20:47:41 +00:00
.attr( "aria-disabled", value );
}
return this;
},
enable: function() {
2010-01-15 20:47:41 +00:00
return this._setOption( "disabled", false );
},
disable: function() {
2010-01-15 20:47:41 +00:00
return this._setOption( "disabled", true );
},
2010-01-15 20:47:41 +00:00
_trigger: function( type, event, data ) {
var callback = this.options[ type ];
2010-01-15 20:47:41 +00:00
event = $.Event( event );
event.type = ( type === this.widgetEventPrefix ?
2010-01-15 22:15:18 +00:00
type :
this.widgetEventPrefix + type ).toLowerCase();
data = data || {};
// copy original event properties over to the new event
// this would happen if we could call $.event.fix instead of $.Event
// but we don't have a way to force an event to be fixed multiple times
2010-01-15 20:47:41 +00:00
if ( event.originalEvent ) {
for ( var i = $.event.props.length, prop; i; ) {
prop = $.event.props[ --i ];
event[ prop ] = event.originalEvent[ prop ];
}
}
2010-01-15 20:47:41 +00:00
this.element.trigger( event, data );
2010-01-15 22:15:18 +00:00
return !( $.isFunction(callback) &&
callback.call( this.element[0], event, data ) === false ||
event.isDefaultPrevented() );
}
};
2010-01-15 20:47:41 +00:00
})( jQuery );