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

340 lines
8.9 KiB
JavaScript
Raw Normal View History

/*!
* jQuery UI Widget @VERSION
*
2011-01-17 14:13:18 +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 ) {
var slice = Array.prototype.slice;
var _cleanData = $.cleanData;
$.cleanData = function( elems ) {
for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
$( elem ).triggerHandler( "remove" );
}
_cleanData( elems );
};
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 ] || {};
// create the constructor using $.extend() so we can carry over any
// static properties stored on the existing constructor (if there is one)
$[ namespace ][ name ] = $.extend( function( options, element ) {
// allow instantiation without "new" keyword
if ( !this._createWidget ) {
return new $[ namespace ][ name ]( options, element );
}
// allow instantiation without initializing for simple inheritance
// must use "new" keyword (the code above always passes args)
2010-01-15 20:47:41 +00:00
if ( arguments.length ) {
this._createWidget( options, element );
}
}, $[ namespace ][ name ] );
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
basePrototype.options = $.extend( true, {}, basePrototype.options );
$.each( prototype, function( prop, value ) {
if ( $.isFunction( value ) ) {
prototype[ prop ] = (function() {
var _super = function( method ) {
return base.prototype[ method ].apply( this, slice.call( arguments, 1 ) );
};
var _superApply = function( method, args ) {
return base.prototype[ method ].apply( this, args );
};
return function() {
var __super = this._super,
__superApply = this._superApply,
returnValue;
this._super = _super;
this._superApply = _superApply;
returnValue = value.apply( this, arguments );
this._super = __super;
this._superApply = __superApply;
return returnValue;
};
}());
}
});
2010-01-15 20:47:41 +00:00
$[ namespace ][ name ].prototype = $.extend( true, basePrototype, {
namespace: namespace,
widgetName: name,
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 = 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;
2010-01-15 20:47:41 +00:00
if ( isMethodCall ) {
this.each(function() {
var instance = $.data( this, name );
if ( !instance ) {
return $.error( "cannot call methods on " + name + " prior to initialization; " +
"attempted to call method '" + options + "'" );
}
if ( !$.isFunction( instance[options] ) || options.charAt( 0 ) === "_" ) {
return $.error( "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 {
object( options, this );
2010-01-15 20:47:41 +00:00
}
});
}
return returnValue;
};
};
2010-01-15 20:47:41 +00:00
$.Widget = function( options, element ) {
// allow instantiation without "new" keyword
if ( !this._createWidget ) {
return new $[ namespace ][ name ]( options, element );
}
// allow instantiation without initializing for simple inheritance
// must use "new" keyword (the code above always passes args)
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: "",
defaultElement: "<div>",
options: {
disabled: false
},
2010-01-15 20:47:41 +00:00
_createWidget: function( options, element ) {
element = $( element || this.defaultElement || this )[ 0 ];
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 );
this.bindings = $();
this.hoverable = $();
this.focusable = $();
if ( element !== this ) {
$.data( element, this.widgetName, this );
this._bind({ remove: "destroy" });
}
this._create();
this._trigger( "create" );
this._init();
},
_getCreateOptions: function() {
return $.metadata && $.metadata.get( this.element[0] )[ this.widgetName ];
},
_create: $.noop,
_init: $.noop,
destroy: function() {
this._destroy();
// we can probably remove the unbind calls in 2.0
// all event bindings should go through this._bind()
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" );
// clean up events and states
this.bindings.unbind( "." + this.widgetName );
this.hoverable.removeClass( "ui-state-hover" );
this.focusable.removeClass( "ui-state-focus" );
},
_destroy: $.noop,
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()
.toggleClass( this.widgetBaseClass + "-disabled ui-state-disabled", !!value )
2010-01-15 20:47:41 +00:00
.attr( "aria-disabled", value );
this.hoverable.removeClass( "ui-state-hover" );
this.focusable.removeClass( "ui-state-focus" );
}
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 );
},
_bind: function( element, handlers ) {
// no element argument, shuffle and use this.element
if ( !handlers ) {
handlers = element;
element = this.element;
} else {
this.bindings = this.bindings.add( element );
}
var instance = this;
$.each( handlers, function( event, handler ) {
element.bind( event + "." + instance.widgetName, function() {
// allow widgets to customize the disabled handling
// - disabled as an array instead of boolean
// - disabled class as method for disabling individual parts
if ( instance.options.disabled === true ||
$( this ).hasClass( "ui-state-disabled" ) ) {
return;
}
return ( typeof handler === "string" ? instance[ handler ] : handler )
.apply( instance, arguments );
});
});
},
_hoverable: function( element ) {
this.hoverable = this.hoverable.add( element );
this._bind( element, {
mouseenter: function( event ) {
$( event.currentTarget ).addClass( "ui-state-hover" );
},
mouseleave: function( event ) {
$( event.currentTarget ).removeClass( "ui-state-hover" );
}
});
},
_focusable: function( element ) {
this.focusable = this.focusable.add( element );
this._bind( element, {
focusin: function( event ) {
$( event.currentTarget ).addClass( "ui-state-focus" );
},
focusout: function( event ) {
$( event.currentTarget ).removeClass( "ui-state-focus" );
}
});
},
2010-01-15 20:47:41 +00:00
_trigger: function( type, event, data ) {
var callback = this.options[ type ],
args;
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 );
args = $.isArray( data ) ?
[ event ].concat( data ) :
[ event, data ];
return !( $.isFunction( callback ) &&
callback.apply( this.element[0], args ) === false ||
2010-01-15 22:15:18 +00:00
event.isDefaultPrevented() );
}
};
2010-01-15 20:47:41 +00:00
})( jQuery );