2009-12-16 22:20:18 +00:00
|
|
|
/*!
|
2009-12-22 19:51:24 +00:00
|
|
|
* jQuery UI Widget @VERSION
|
2009-12-16 22:20:18 +00:00
|
|
|
*
|
2010-07-14 19:45:45 +00:00
|
|
|
* Copyright 2010, AUTHORS.txt (http://jqueryui.com/about)
|
2010-07-09 13:01:04 +00:00
|
|
|
* Dual licensed under the MIT or GPL Version 2 licenses.
|
|
|
|
* http://jquery.org/license
|
2009-12-16 22:20:18 +00:00
|
|
|
*
|
2009-12-22 19:51:24 +00:00
|
|
|
* http://docs.jquery.com/UI/Widget
|
2009-12-16 22:20:18 +00:00
|
|
|
*/
|
2010-07-13 13:57:58 +00:00
|
|
|
(function( $, undefined ) {
|
2009-12-22 19:51:24 +00:00
|
|
|
|
2010-09-03 13:49:29 +00:00
|
|
|
// jQuery 1.4+
|
|
|
|
if ( $.cleanData ) {
|
|
|
|
var _cleanData = $.cleanData;
|
|
|
|
$.cleanData = function( elems ) {
|
|
|
|
for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
|
|
|
|
$( elem ).triggerHandler( "remove" );
|
2010-02-06 02:12:41 +00:00
|
|
|
}
|
2010-09-03 13:49:29 +00:00
|
|
|
_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() {
|
|
|
|
$( this ).triggerHandler( "remove" );
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _remove.call( $(this), selector, keepData );
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2009-12-22 19:51:24 +00:00
|
|
|
|
2010-01-15 20:47:41 +00:00
|
|
|
$.widget = function( name, base, prototype ) {
|
|
|
|
var namespace = name.split( "." )[ 0 ],
|
2009-12-16 22:20:18 +00:00
|
|
|
fullName;
|
2010-01-15 20:47:41 +00:00
|
|
|
name = name.split( "." )[ 1 ];
|
|
|
|
fullName = namespace + "-" + name;
|
2009-12-16 22:20:18 +00:00
|
|
|
|
2010-01-15 20:47:41 +00:00
|
|
|
if ( !prototype ) {
|
2010-01-07 03:19:50 +00:00
|
|
|
prototype = base;
|
|
|
|
base = $.Widget;
|
|
|
|
}
|
|
|
|
|
2009-12-16 22:20:18 +00:00
|
|
|
// create selector for plugin
|
2010-01-15 20:47:41 +00:00
|
|
|
$.expr[ ":" ][ fullName ] = function( elem ) {
|
|
|
|
return !!$.data( elem, name );
|
2009-12-16 22:20:18 +00:00
|
|
|
};
|
2010-01-07 03:19:50 +00:00
|
|
|
|
2010-01-15 20:47:41 +00:00
|
|
|
$[ namespace ] = $[ namespace ] || {};
|
|
|
|
$[ namespace ][ name ] = function( options, element ) {
|
2010-01-07 03:19:50 +00:00
|
|
|
// allow instantiation without initializing for simple inheritance
|
2010-01-15 20:47:41 +00:00
|
|
|
if ( arguments.length ) {
|
|
|
|
this._createWidget( options, element );
|
|
|
|
}
|
2010-01-07 03:19:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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 );
|
2010-01-07 03:19:50 +00:00
|
|
|
// }
|
|
|
|
// });
|
2010-07-15 14:26:00 +00:00
|
|
|
basePrototype.options = $.extend( true, {}, basePrototype.options );
|
2010-01-15 20:47:41 +00:00
|
|
|
$[ namespace ][ name ].prototype = $.extend( true, basePrototype, {
|
2010-01-07 03:19:50 +00:00
|
|
|
namespace: namespace,
|
|
|
|
widgetName: name,
|
2010-01-15 20:47:41 +00:00
|
|
|
widgetEventPrefix: $[ namespace ][ name ].prototype.widgetEventPrefix || name,
|
2010-01-07 03:19:50 +00:00
|
|
|
widgetBaseClass: fullName
|
2010-01-15 20:47:41 +00:00
|
|
|
}, prototype );
|
2010-01-07 03:19:50 +00:00
|
|
|
|
2010-01-15 20:47:41 +00:00
|
|
|
$.widget.bridge( name, $[ namespace ][ name ] );
|
2010-01-07 03:19:50 +00:00
|
|
|
};
|
|
|
|
|
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 ),
|
2009-12-16 22:20:18 +00:00
|
|
|
returnValue = this;
|
2010-01-15 20:47:41 +00:00
|
|
|
|
2009-12-16 22:20:18 +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;
|
2009-12-16 22:20:18 +00:00
|
|
|
|
|
|
|
// prevent calls to internal methods
|
2010-09-20 19:03:42 +00:00
|
|
|
if ( isMethodCall && options.charAt( 0 ) === "_" ) {
|
2009-12-16 22:20:18 +00:00
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
2010-01-15 20:47:41 +00:00
|
|
|
if ( isMethodCall ) {
|
|
|
|
this.each(function() {
|
2010-08-27 18:48:17 +00:00
|
|
|
var instance = $.data( this, name );
|
|
|
|
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 ) {
|
2009-12-16 22:20:18 +00:00
|
|
|
returnValue = methodValue;
|
|
|
|
return false;
|
|
|
|
}
|
2010-01-15 20:47:41 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.each(function() {
|
2010-01-15 21:09:32 +00:00
|
|
|
var instance = $.data( this, name );
|
|
|
|
if ( instance ) {
|
2010-08-23 19:35:49 +00:00
|
|
|
instance.option( options || {} )._init();
|
2010-01-15 21:09:32 +00:00
|
|
|
} else {
|
2010-01-15 20:47:41 +00:00
|
|
|
$.data( this, name, new object( options, this ) );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2009-12-16 22:20:18 +00:00
|
|
|
|
|
|
|
return returnValue;
|
|
|
|
};
|
2010-01-07 03:19:50 +00:00
|
|
|
};
|
2009-12-16 22:20:18 +00:00
|
|
|
|
2010-01-15 20:47:41 +00:00
|
|
|
$.Widget = function( options, element ) {
|
2010-01-07 03:19:50 +00:00
|
|
|
// allow instantiation without initializing for simple inheritance
|
2010-01-15 20:47:41 +00:00
|
|
|
if ( arguments.length ) {
|
|
|
|
this._createWidget( options, element );
|
|
|
|
}
|
2010-01-07 03:19:50 +00:00
|
|
|
};
|
2009-12-16 22:20:18 +00:00
|
|
|
|
2010-01-07 03:19:50 +00:00
|
|
|
$.Widget.prototype = {
|
2010-01-15 20:47:41 +00:00
|
|
|
widgetName: "widget",
|
|
|
|
widgetEventPrefix: "",
|
2010-01-07 03:19:50 +00:00
|
|
|
options: {
|
|
|
|
disabled: false
|
|
|
|
},
|
2010-01-15 20:47:41 +00:00
|
|
|
_createWidget: function( options, element ) {
|
2010-01-07 03:19:50 +00:00
|
|
|
// $.widget.bridge stores the plugin instance, but we do it anyway
|
2010-01-15 18:58:20 +00:00
|
|
|
// so that it's stored even before the _create function runs
|
2010-07-30 12:04:11 +00:00
|
|
|
$.data( element, this.widgetName, this );
|
|
|
|
this.element = $( element );
|
2010-01-15 20:47:41 +00:00
|
|
|
this.options = $.extend( true, {},
|
2010-01-07 03:19:50 +00:00
|
|
|
this.options,
|
2010-01-15 20:47:41 +00:00
|
|
|
$.metadata && $.metadata.get( element )[ this.widgetName ],
|
|
|
|
options );
|
2009-12-16 22:20:18 +00:00
|
|
|
|
2010-01-07 03:19:50 +00:00
|
|
|
var self = this;
|
2010-01-19 13:53:32 +00:00
|
|
|
this.element.bind( "remove." + this.widgetName, function() {
|
2010-01-07 03:19:50 +00:00
|
|
|
self.destroy();
|
|
|
|
});
|
2009-12-16 22:20:18 +00:00
|
|
|
|
2010-01-17 17:36:56 +00:00
|
|
|
this._create();
|
2010-09-30 00:36:45 +00:00
|
|
|
this._trigger( "create" );
|
2010-01-15 21:09:32 +00:00
|
|
|
this._init();
|
2010-01-07 03:19:50 +00:00
|
|
|
},
|
2010-01-15 21:09:32 +00:00
|
|
|
_create: function() {},
|
|
|
|
_init: function() {},
|
2009-12-16 22:20:18 +00:00
|
|
|
|
|
|
|
destroy: function() {
|
2010-01-07 03:19:50 +00:00
|
|
|
this.element
|
2010-01-15 20:47:41 +00:00
|
|
|
.unbind( "." + this.widgetName )
|
|
|
|
.removeData( this.widgetName );
|
2010-01-07 03:19:50 +00:00
|
|
|
this.widget()
|
2010-01-15 20:47:41 +00:00
|
|
|
.unbind( "." + this.widgetName )
|
|
|
|
.removeAttr( "aria-disabled" )
|
2010-01-07 03:19:50 +00:00
|
|
|
.removeClass(
|
2010-01-15 20:47:41 +00:00
|
|
|
this.widgetBaseClass + "-disabled " +
|
2010-03-29 14:00:26 +00:00
|
|
|
"ui-state-disabled" );
|
2010-01-07 03:19:50 +00:00
|
|
|
},
|
2009-12-16 22:20:18 +00:00
|
|
|
|
2010-01-07 03:19:50 +00:00
|
|
|
widget: function() {
|
2010-07-30 16:38:18 +00:00
|
|
|
return this.element;
|
2009-12-16 22:20:18 +00:00
|
|
|
},
|
|
|
|
|
2010-01-15 20:47:41 +00:00
|
|
|
option: function( key, value ) {
|
2010-09-27 15:21:09 +00:00
|
|
|
var options = key;
|
2009-12-16 22:20:18 +00:00
|
|
|
|
2010-01-15 20:47:41 +00:00
|
|
|
if ( arguments.length === 0 ) {
|
2010-01-07 03:19:50 +00:00
|
|
|
// don't return a reference to the internal hash
|
2010-09-27 15:21:09 +00:00
|
|
|
return $.extend( {}, this.options );
|
2010-01-07 03:19:50 +00:00
|
|
|
}
|
|
|
|
|
2010-01-15 20:47:41 +00:00
|
|
|
if (typeof key === "string" ) {
|
|
|
|
if ( value === undefined ) {
|
|
|
|
return this.options[ key ];
|
2009-12-16 22:20:18 +00:00
|
|
|
}
|
|
|
|
options = {};
|
2010-01-15 20:47:41 +00:00
|
|
|
options[ key ] = value;
|
2009-12-16 22:20:18 +00:00
|
|
|
}
|
|
|
|
|
2010-09-27 15:21:09 +00:00
|
|
|
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 );
|
2009-12-16 22:20:18 +00:00
|
|
|
});
|
|
|
|
|
2010-09-27 15:21:09 +00:00
|
|
|
return this;
|
2009-12-16 22:20:18 +00:00
|
|
|
},
|
2010-01-15 20:47:41 +00:00
|
|
|
_setOption: function( key, value ) {
|
|
|
|
this.options[ key ] = value;
|
2009-12-16 22:20:18 +00:00
|
|
|
|
2010-01-15 20:47:41 +00:00
|
|
|
if ( key === "disabled" ) {
|
2010-01-07 03:19:50 +00:00
|
|
|
this.widget()
|
2010-01-15 20:47:41 +00:00
|
|
|
[ value ? "addClass" : "removeClass"](
|
|
|
|
this.widgetBaseClass + "-disabled" + " " +
|
2010-03-29 14:00:26 +00:00
|
|
|
"ui-state-disabled" )
|
2010-01-15 20:47:41 +00:00
|
|
|
.attr( "aria-disabled", value );
|
2009-12-16 22:20:18 +00:00
|
|
|
}
|
2010-01-07 03:19:50 +00:00
|
|
|
|
|
|
|
return this;
|
2009-12-16 22:20:18 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
enable: function() {
|
2010-01-15 20:47:41 +00:00
|
|
|
return this._setOption( "disabled", false );
|
2009-12-16 22:20:18 +00:00
|
|
|
},
|
|
|
|
disable: function() {
|
2010-01-15 20:47:41 +00:00
|
|
|
return this._setOption( "disabled", true );
|
2009-12-16 22:20:18 +00:00
|
|
|
},
|
|
|
|
|
2010-01-15 20:47:41 +00:00
|
|
|
_trigger: function( type, event, data ) {
|
|
|
|
var callback = this.options[ type ];
|
2009-12-16 22:20:18 +00:00
|
|
|
|
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();
|
2009-12-16 22:20:18 +00:00
|
|
|
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 ];
|
2009-12-16 22:20:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-15 20:47:41 +00:00
|
|
|
this.element.trigger( event, data );
|
2009-12-16 22:20:18 +00:00
|
|
|
|
2010-01-15 22:15:18 +00:00
|
|
|
return !( $.isFunction(callback) &&
|
|
|
|
callback.call( this.element[0], event, data ) === false ||
|
|
|
|
event.isDefaultPrevented() );
|
2009-12-16 22:20:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-01-15 20:47:41 +00:00
|
|
|
})( jQuery );
|