2 * jQuery UI Widget @VERSION
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
8 * http://docs.jquery.com/UI/Widget
13 (function( $, undefined ) {
17 var _cleanData = $.cleanData;
18 $.cleanData = function( elems ) {
19 for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
21 $( elem ).triggerHandler( "remove" );
22 // http://bugs.jquery.com/ticket/8235
28 var _remove = $.fn.remove;
29 $.fn.remove = function( selector, keepData ) {
30 return this.each(function() {
32 if ( !selector || $.filter( selector, [ this ] ).length ) {
33 $( "*", this ).add( [ this ] ).each(function() {
35 $( this ).triggerHandler( "remove" );
36 // http://bugs.jquery.com/ticket/8235
41 return _remove.call( $(this), selector, keepData );
46 $.widget = function( name, base, prototype ) {
47 var namespace = name.split( "." )[ 0 ],
49 name = name.split( "." )[ 1 ];
50 fullName = namespace + "-" + name;
57 // create selector for plugin
58 $.expr[ ":" ][ fullName ] = function( elem ) {
59 return !!$.data( elem, name );
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 );
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
74 // $.each( basePrototype, function( key, val ) {
75 // if ( $.isPlainObject(val) ) {
76 // basePrototype[ key ] = $.extend( {}, val );
79 basePrototype.options = $.extend( true, {}, basePrototype.options );
80 $[ namespace ][ name ].prototype = $.extend( true, basePrototype, {
83 widgetEventPrefix: $[ namespace ][ name ].prototype.widgetEventPrefix || name,
84 widgetBaseClass: fullName
87 $.widget.bridge( name, $[ namespace ][ name ] );
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 ),
96 // allow multiple hashes to be passed on init
97 options = !isMethodCall && args.length ?
98 $.extend.apply( null, [ true, options ].concat(args) ) :
101 // prevent calls to internal methods
102 if ( isMethodCall && options.charAt( 0 ) === "_" ) {
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 ) :
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 + "'";
117 // if ( !$.isFunction( instance[options] ) ) {
118 // throw "no such method '" + options + "' for " + name + " widget instance";
120 // var methodValue = instance[ options ].apply( instance, args );
121 if ( methodValue !== instance && methodValue !== undefined ) {
122 returnValue = methodValue;
127 this.each(function() {
128 var instance = $.data( this, name );
130 instance.option( options || {} )._init();
132 $.data( this, name, new object( options, this ) );
141 $.Widget = function( options, element ) {
142 // allow instantiation without initializing for simple inheritance
143 if ( arguments.length ) {
144 this._createWidget( options, element );
148 $.Widget.prototype = {
149 widgetName: "widget",
150 widgetEventPrefix: "",
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, {},
161 this._getCreateOptions(),
165 this.element.bind( "remove." + this.widgetName, function() {
170 this._trigger( "create" );
173 _getCreateOptions: function() {
174 return $.metadata && $.metadata.get( this.element[0] )[ this.widgetName ];
176 _create: function() {},
177 _init: function() {},
179 destroy: function() {
181 .unbind( "." + this.widgetName )
182 .removeData( this.widgetName );
184 .unbind( "." + this.widgetName )
185 .removeAttr( "aria-disabled" )
187 this.widgetBaseClass + "-disabled " +
188 "ui-state-disabled" );
195 option: function( key, value ) {
198 if ( arguments.length === 0 ) {
199 // don't return a reference to the internal hash
200 return $.extend( {}, this.options );
203 if (typeof key === "string" ) {
204 if ( value === undefined ) {
205 return this.options[ key ];
208 options[ key ] = value;
211 this._setOptions( options );
215 _setOptions: function( options ) {
217 $.each( options, function( key, value ) {
218 self._setOption( key, value );
223 _setOption: function( key, value ) {
224 this.options[ key ] = value;
226 if ( key === "disabled" ) {
228 [ value ? "addClass" : "removeClass"](
229 this.widgetBaseClass + "-disabled" + " " +
230 "ui-state-disabled" )
231 .attr( "aria-disabled", value );
238 return this._setOption( "disabled", false );
240 disable: function() {
241 return this._setOption( "disabled", true );
244 _trigger: function( type, event, data ) {
246 callback = this.options[ type ];
249 event = $.Event( event );
250 event.type = ( type === this.widgetEventPrefix ?
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 ];
257 // copy original event properties over to the new event
258 orig = event.originalEvent;
260 for ( prop in orig ) {
261 if ( !( prop in event ) ) {
262 event[ prop ] = orig[ prop ];
267 this.element.trigger( event, data );
269 return !( $.isFunction(callback) &&
270 callback.call( this.element[0], event, data ) === false ||
271 event.isDefaultPrevented() );