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

269 lines
6.9 KiB
JavaScript
Raw Normal View History

/*!
* jQuery UI Widget @VERSION
*
2011-01-17 14:36:00 +00:00
* Copyright 2011, 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 ) {
// jQuery 1.4+
if ( $.cleanData ) {
var _cleanData = $.cleanData;
$.cleanData = function( elems ) {
for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
try {
$( elem ).triggerHandler( "remove" );
// http://bugs.jquery.com/ticket/8235
} catch( e ) {}
}
_cleanData( elems );
};
} else {
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() {
try {
$( this ).triggerHandler( "remove" );
// http://bugs.jquery.com/ticket/8235
} catch( e ) {}
});
}
}
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
if ( isMethodCall && options.charAt( 0 ) === "_" ) {
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] ) ?
instance[ options ].apply( instance, args ) :
instance;
// TODO: add this back in 1.9 and use $.error() (see #5972)
// if ( !instance ) {
// throw "cannot call methods on " + name + " prior to initialization; " +
// "attempted to call method '" + options + "'";
// }
// if ( !$.isFunction( instance[options] ) ) {
// throw "no such method '" + options + "' for " + name + " widget instance";
// }
// var methodValue = instance[ options ].apply( instance, args );
2010-01-15 20:47:41 +00:00
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 ) {
instance.option( options || {} )._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
$.data( element, this.widgetName, this );
this.element = $( element );
2010-01-15 20:47:41 +00:00
this.options = $.extend( true, {},
this.options,
this._getCreateOptions(),
2010-01-15 20:47:41 +00:00
options );
var self = this;
2010-01-19 13:53:32 +00:00
this.element.bind( "remove." + this.widgetName, function() {
self.destroy();
});
this._create();
this._trigger( "create" );
this._init();
},
_getCreateOptions: function() {
return $.metadata && $.metadata.get( this.element[0] )[ this.widgetName ];
},
_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;
2010-01-15 20:47:41 +00:00
if ( arguments.length === 0 ) {
// don't return a reference to the internal hash
return $.extend( {}, this.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;
}
this._setOptions( options );
return this;
},
_setOptions: function( options ) {
var self = this;
2010-01-15 20:47:41 +00:00
$.each( options, function( key, value ) {
self._setOption( key, value );
});
return this;
},
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 );