2 * jQuery UI Widget 1.8.7
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
10 (function( $, undefined ) {
14 var _cleanData
= $.cleanData
;
15 $.cleanData = function( elems
) {
16 for ( var i
= 0, elem
; (elem
= elems
[i
]) != null; i
++ ) {
17 $( elem
).triggerHandler( "remove" );
22 var _remove
= $.fn
.remove
;
23 $.fn
.remove = function( selector
, keepData
) {
24 return this.each(function() {
26 if ( !selector
|| $.filter( selector
, [ this ] ).length
) {
27 $( "*", this ).add( [ this ] ).each(function() {
28 $( this ).triggerHandler( "remove" );
32 return _remove
.call( $(this), selector
, keepData
);
37 $.widget = function( name
, base
, prototype ) {
38 var namespace = name
.split( "." )[ 0 ],
40 name
= name
.split( "." )[ 1 ];
41 fullName
= namespace + "-" + name
;
48 // create selector for plugin
49 $.expr
[ ":" ][ fullName
] = function( elem
) {
50 return !!$.data( elem
, name
);
53 $[ namespace ] = $[ namespace ] || {};
54 $[ namespace ][ name
] = function( options
, element
) {
55 // allow instantiation without initializing for simple inheritance
56 if ( arguments
.length
) {
57 this._createWidget( options
, element
);
61 var basePrototype
= new base();
62 // we need to make the options hash a property directly on the new instance
63 // otherwise we'll modify the options hash on the prototype that we're
65 // $.each( basePrototype, function( key, val ) {
66 // if ( $.isPlainObject(val) ) {
67 // basePrototype[ key ] = $.extend( {}, val );
70 basePrototype
.options
= $.extend( true, {}, basePrototype
.options
);
71 $[ namespace ][ name
].prototype = $.extend( true, basePrototype
, {
74 widgetEventPrefix
: $[ namespace ][ name
].prototype.widgetEventPrefix
|| name
,
75 widgetBaseClass
: fullName
78 $.widget
.bridge( name
, $[ namespace ][ name
] );
81 $.widget
.bridge = function( name
, object
) {
82 $.fn
[ name
] = function( options
) {
83 var isMethodCall
= typeof options
=== "string",
84 args
= Array
.prototype.slice
.call( arguments
, 1 ),
87 // allow multiple hashes to be passed on init
88 options
= !isMethodCall
&& args
.length
?
89 $.extend
.apply( null, [ true, options
].concat(args
) ) :
92 // prevent calls to internal methods
93 if ( isMethodCall
&& options
.charAt( 0 ) === "_" ) {
98 this.each(function() {
99 var instance
= $.data( this, name
),
100 methodValue
= instance
&& $.isFunction( instance
[options
] ) ?
101 instance
[ options
].apply( instance
, args
) :
103 // TODO: add this back in 1.9 and use $.error() (see #5972)
104 // if ( !instance ) {
105 // throw "cannot call methods on " + name + " prior to initialization; " +
106 // "attempted to call method '" + options + "'";
108 // if ( !$.isFunction( instance[options] ) ) {
109 // throw "no such method '" + options + "' for " + name + " widget instance";
111 // var methodValue = instance[ options ].apply( instance, args );
112 if ( methodValue
!== instance
&& methodValue
!== undefined ) {
113 returnValue
= methodValue
;
118 this.each(function() {
119 var instance
= $.data( this, name
);
121 instance
.option( options
|| {} )._init();
123 $.data( this, name
, new object( options
, this ) );
132 $.Widget = function( options
, element
) {
133 // allow instantiation without initializing for simple inheritance
134 if ( arguments
.length
) {
135 this._createWidget( options
, element
);
139 $.Widget
.prototype = {
140 widgetName
: "widget",
141 widgetEventPrefix
: "",
145 _createWidget: function( options
, element
) {
146 // $.widget.bridge stores the plugin instance, but we do it anyway
147 // so that it's stored even before the _create function runs
148 $.data( element
, this.widgetName
, this );
149 this.element
= $( element
);
150 this.options
= $.extend( true, {},
152 this._getCreateOptions(),
156 this.element
.bind( "remove." + this.widgetName
, function() {
161 this._trigger( "create" );
164 _getCreateOptions: function() {
165 return $.metadata
&& $.metadata
.get( this.element
[0] )[ this.widgetName
];
167 _create: function() {},
168 _init: function() {},
170 destroy: function() {
172 .unbind( "." + this.widgetName
)
173 .removeData( this.widgetName
);
175 .unbind( "." + this.widgetName
)
176 .removeAttr( "aria-disabled" )
178 this.widgetBaseClass
+ "-disabled " +
179 "ui-state-disabled" );
186 option: function( key
, value
) {
189 if ( arguments
.length
=== 0 ) {
190 // don't return a reference to the internal hash
191 return $.extend( {}, this.options
);
194 if (typeof key
=== "string" ) {
195 if ( value
=== undefined ) {
196 return this.options
[ key
];
199 options
[ key
] = value
;
202 this._setOptions( options
);
206 _setOptions: function( options
) {
208 $.each( options
, function( key
, value
) {
209 self
._setOption( key
, value
);
214 _setOption: function( key
, value
) {
215 this.options
[ key
] = value
;
217 if ( key
=== "disabled" ) {
219 [ value
? "addClass" : "removeClass"](
220 this.widgetBaseClass
+ "-disabled" + " " +
221 "ui-state-disabled" )
222 .attr( "aria-disabled", value
);
229 return this._setOption( "disabled", false );
231 disable: function() {
232 return this._setOption( "disabled", true );
235 _trigger: function( type
, event
, data
) {
236 var callback
= this.options
[ type
];
238 event
= $.Event( event
);
239 event
.type
= ( type
=== this.widgetEventPrefix
?
241 this.widgetEventPrefix
+ type
).toLowerCase();
244 // copy original event properties over to the new event
245 // this would happen if we could call $.event.fix instead of $.Event
246 // but we don't have a way to force an event to be fixed multiple times
247 if ( event
.originalEvent
) {
248 for ( var i
= $.event
.props
.length
, prop
; i
; ) {
249 prop
= $.event
.props
[ --i
];
250 event
[ prop
] = event
.originalEvent
[ prop
];
254 this.element
.trigger( event
, data
);
256 return !( $.isFunction(callback
) &&
257 callback
.call( this.element
[0], event
, data
) === false ||
258 event
.isDefaultPrevented() );