2 * jQuery UI Widget 1.8.24
4 * Copyright 2012, 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++ ) {
18 $( elem ).triggerHandler( "remove" );
19 // http://bugs.jquery.com/ticket/8235
25 var _remove = $.fn.remove;
26 $.fn.remove = function( selector, keepData ) {
27 return this.each(function() {
29 if ( !selector || $.filter( selector, [ this ] ).length ) {
30 $( "*", this ).add( [ this ] ).each(function() {
32 $( this ).triggerHandler( "remove" );
33 // http://bugs.jquery.com/ticket/8235
38 return _remove.call( $(this), selector, keepData );
43 $.widget = function( name, base, prototype ) {
44 var namespace = name.split( "." )[ 0 ],
46 name = name.split( "." )[ 1 ];
47 fullName = namespace + "-" + name;
54 // create selector for plugin
55 $.expr[ ":" ][ fullName ] = function( elem ) {
56 return !!$.data( elem, name );
59 $[ namespace ] = $[ namespace ] || {};
60 $[ namespace ][ name ] = function( options, element ) {
61 // allow instantiation without initializing for simple inheritance
62 if ( arguments.length ) {
63 this._createWidget( options, element );
67 var basePrototype = new base();
68 // we need to make the options hash a property directly on the new instance
69 // otherwise we'll modify the options hash on the prototype that we're
71 // $.each( basePrototype, function( key, val ) {
72 // if ( $.isPlainObject(val) ) {
73 // basePrototype[ key ] = $.extend( {}, val );
76 basePrototype.options = $.extend( true, {}, basePrototype.options );
77 $[ namespace ][ name ].prototype = $.extend( true, basePrototype, {
80 widgetEventPrefix: $[ namespace ][ name ].prototype.widgetEventPrefix || name,
81 widgetBaseClass: fullName
84 $.widget.bridge( name, $[ namespace ][ name ] );
87 $.widget.bridge = function( name, object ) {
88 $.fn[ name ] = function( options ) {
89 var isMethodCall = typeof options === "string",
90 args = Array.prototype.slice.call( arguments, 1 ),
93 // allow multiple hashes to be passed on init
94 options = !isMethodCall && args.length ?
95 $.extend.apply( null, [ true, options ].concat(args) ) :
98 // prevent calls to internal methods
99 if ( isMethodCall && options.charAt( 0 ) === "_" ) {
103 if ( isMethodCall ) {
104 this.each(function() {
105 var instance = $.data( this, name ),
106 methodValue = instance && $.isFunction( instance[options] ) ?
107 instance[ options ].apply( instance, args ) :
109 // TODO: add this back in 1.9 and use $.error() (see #5972)
110 // if ( !instance ) {
111 // throw "cannot call methods on " + name + " prior to initialization; " +
112 // "attempted to call method '" + options + "'";
114 // if ( !$.isFunction( instance[options] ) ) {
115 // throw "no such method '" + options + "' for " + name + " widget instance";
117 // var methodValue = instance[ options ].apply( instance, args );
118 if ( methodValue !== instance && methodValue !== undefined ) {
119 returnValue = methodValue;
124 this.each(function() {
125 var instance = $.data( this, name );
127 instance.option( options || {} )._init();
129 $.data( this, name, new object( options, this ) );
138 $.Widget = function( options, element ) {
139 // allow instantiation without initializing for simple inheritance
140 if ( arguments.length ) {
141 this._createWidget( options, element );
145 $.Widget.prototype = {
146 widgetName: "widget",
147 widgetEventPrefix: "",
151 _createWidget: function( options, element ) {
152 // $.widget.bridge stores the plugin instance, but we do it anyway
153 // so that it's stored even before the _create function runs
154 $.data( element, this.widgetName, this );
155 this.element = $( element );
156 this.options = $.extend( true, {},
158 this._getCreateOptions(),
162 this.element.bind( "remove." + this.widgetName, function() {
167 this._trigger( "create" );
170 _getCreateOptions: function() {
171 return $.metadata && $.metadata.get( this.element[0] )[ this.widgetName ];
173 _create: function() {},
174 _init: function() {},
176 destroy: function() {
178 .unbind( "." + this.widgetName )
179 .removeData( this.widgetName );
181 .unbind( "." + this.widgetName )
182 .removeAttr( "aria-disabled" )
184 this.widgetBaseClass + "-disabled " +
185 "ui-state-disabled" );
192 option: function( key, value ) {
195 if ( arguments.length === 0 ) {
196 // don't return a reference to the internal hash
197 return $.extend( {}, this.options );
200 if (typeof key === "string" ) {
201 if ( value === undefined ) {
202 return this.options[ key ];
205 options[ key ] = value;
208 this._setOptions( options );
212 _setOptions: function( options ) {
214 $.each( options, function( key, value ) {
215 self._setOption( key, value );
220 _setOption: function( key, value ) {
221 this.options[ key ] = value;
223 if ( key === "disabled" ) {
225 [ value ? "addClass" : "removeClass"](
226 this.widgetBaseClass + "-disabled" + " " +
227 "ui-state-disabled" )
228 .attr( "aria-disabled", value );
235 return this._setOption( "disabled", false );
237 disable: function() {
238 return this._setOption( "disabled", true );
241 _trigger: function( type, event, data ) {
243 callback = this.options[ type ];
246 event = $.Event( event );
247 event.type = ( type === this.widgetEventPrefix ?
249 this.widgetEventPrefix + type ).toLowerCase();
250 // the original event may come from any element
251 // so we need to reset the target on the new event
252 event.target = this.element[ 0 ];
254 // copy original event properties over to the new event
255 orig = event.originalEvent;
257 for ( prop in orig ) {
258 if ( !( prop in event ) ) {
259 event[ prop ] = orig[ prop ];
264 this.element.trigger( event, data );
266 return !( $.isFunction(callback) &&
267 callback.call( this.element[0], event, data ) === false ||
268 event.isDefaultPrevented() );