/*! |
/*! |
* jQuery UI Widget @VERSION |
* jQuery UI Widget @VERSION |
* |
* |
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about) |
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about) |
* Dual licensed under the MIT or GPL Version 2 licenses. |
* Dual licensed under the MIT or GPL Version 2 licenses. |
* http://jquery.org/license |
* http://jquery.org/license |
* |
* |
* http://docs.jquery.com/UI/Widget |
* http://docs.jquery.com/UI/Widget |
*/ |
*/ |
(function( $, undefined ) { |
(function( $, undefined ) { |
|
|
var slice = Array.prototype.slice; |
var slice = Array.prototype.slice; |
|
|
var _cleanData = $.cleanData; |
var _cleanData = $.cleanData; |
$.cleanData = function( elems ) { |
$.cleanData = function( elems ) { |
for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) { |
for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) { |
$( elem ).triggerHandler( "remove" ); |
$( elem ).triggerHandler( "remove" ); |
} |
} |
_cleanData( elems ); |
_cleanData( elems ); |
}; |
}; |
|
|
$.widget = function( name, base, prototype ) { |
$.widget = function( name, base, prototype ) { |
var namespace = name.split( "." )[ 0 ], |
var namespace = name.split( "." )[ 0 ], |
fullName; |
fullName; |
name = name.split( "." )[ 1 ]; |
name = name.split( "." )[ 1 ]; |
fullName = namespace + "-" + name; |
fullName = namespace + "-" + name; |
|
|
if ( !prototype ) { |
if ( !prototype ) { |
prototype = base; |
prototype = base; |
base = $.Widget; |
base = $.Widget; |
} |
} |
|
|
// create selector for plugin |
// create selector for plugin |
$.expr[ ":" ][ fullName ] = function( elem ) { |
$.expr[ ":" ][ fullName ] = function( elem ) { |
return !!$.data( elem, name ); |
return !!$.data( elem, name ); |
}; |
}; |
|
|
$[ namespace ] = $[ namespace ] || {}; |
$[ namespace ] = $[ namespace ] || {}; |
$[ namespace ][ name ] = $[ namespace ][ name ] || function( options, element ) { |
$[ namespace ][ name ] = $[ namespace ][ name ] || function( options, element ) { |
// allow instantiation without "new" keyword |
// allow instantiation without "new" keyword |
if ( !this._createWidget ) { |
if ( !this._createWidget ) { |
return new $[ namespace ][ name ]( options, element ); |
return new $[ namespace ][ name ]( options, element ); |
} |
} |
|
|
// allow instantiation without initializing for simple inheritance |
// allow instantiation without initializing for simple inheritance |
// must use "new" keyword (the code above always passes args) |
// must use "new" keyword (the code above always passes args) |
if ( arguments.length ) { |
if ( arguments.length ) { |
this._createWidget( options, element ); |
this._createWidget( options, element ); |
} |
} |
}; |
}; |
|
|
var basePrototype = new base(); |
var basePrototype = new base(); |
// we need to make the options hash a property directly on the new instance |
// 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 |
// otherwise we'll modify the options hash on the prototype that we're |
// inheriting from |
// inheriting from |
basePrototype.options = $.extend( true, {}, basePrototype.options ); |
basePrototype.options = $.extend( true, {}, basePrototype.options ); |
$[ namespace ][ name ].prototype = $.extend( true, basePrototype, { |
$[ namespace ][ name ].prototype = $.extend( true, basePrototype, { |
namespace: namespace, |
namespace: namespace, |
widgetName: name, |
widgetName: name, |
widgetEventPrefix: name, |
widgetEventPrefix: name, |
widgetBaseClass: fullName, |
widgetBaseClass: fullName, |
base: base.prototype |
base: base.prototype |
}, prototype ); |
}, prototype ); |
|
|
$.widget.bridge( name, $[ namespace ][ name ] ); |
$.widget.bridge( name, $[ namespace ][ name ] ); |
}; |
}; |
|
|
$.widget.bridge = function( name, object ) { |
$.widget.bridge = function( name, object ) { |
$.fn[ name ] = function( options ) { |
$.fn[ name ] = function( options ) { |
var isMethodCall = typeof options === "string", |
var isMethodCall = typeof options === "string", |
args = slice.call( arguments, 1 ), |
args = slice.call( arguments, 1 ), |
returnValue = this; |
returnValue = this; |
|
|
// allow multiple hashes to be passed on init |
// allow multiple hashes to be passed on init |
options = !isMethodCall && args.length ? |
options = !isMethodCall && args.length ? |
$.extend.apply( null, [ true, options ].concat(args) ) : |
$.extend.apply( null, [ true, options ].concat(args) ) : |
options; |
options; |
|
|
// prevent calls to internal methods |
// prevent calls to internal methods |
if ( isMethodCall && options.charAt( 0 ) === "_" ) { |
if ( isMethodCall && options.charAt( 0 ) === "_" ) { |
return returnValue; |
return returnValue; |
} |
} |
|
|
if ( isMethodCall ) { |
if ( isMethodCall ) { |
this.each(function() { |
this.each(function() { |
var instance = $.data( this, name ); |
var instance = $.data( this, name ); |
if ( !instance ) { |
if ( !instance ) { |
return $.error( "cannot call methods on " + name + " prior to initialization; " + |
return $.error( "cannot call methods on " + name + " prior to initialization; " + |
"attempted to call method '" + options + "'" ); |
"attempted to call method '" + options + "'" ); |
} |
} |
if ( !$.isFunction( instance[options] ) ) { |
if ( !$.isFunction( instance[options] ) ) { |
return $.error( "no such method '" + options + "' for " + name + " widget instance" ); |
return $.error( "no such method '" + options + "' for " + name + " widget instance" ); |
} |
} |
var methodValue = instance[ options ].apply( instance, args ); |
var methodValue = instance[ options ].apply( instance, args ); |
if ( methodValue !== instance && methodValue !== undefined ) { |
if ( methodValue !== instance && methodValue !== undefined ) { |
returnValue = methodValue; |
returnValue = methodValue; |
return false; |
return false; |
} |
} |
}); |
}); |
} else { |
} else { |
this.each(function() { |
this.each(function() { |
var instance = $.data( this, name ); |
var instance = $.data( this, name ); |
if ( instance ) { |
if ( instance ) { |
instance.option( options || {} )._init(); |
instance.option( options || {} )._init(); |
} else { |
} else { |
object( options, this ); |
object( options, this ); |
} |
} |
}); |
}); |
} |
} |
|
|
return returnValue; |
return returnValue; |
}; |
}; |
}; |
}; |
|
|
$.Widget = function( options, element ) { |
$.Widget = function( options, element ) { |
// allow instantiation without "new" keyword |
// allow instantiation without "new" keyword |
if ( !this._createWidget ) { |
if ( !this._createWidget ) { |
return new $[ namespace ][ name ]( options, element ); |
return new $[ namespace ][ name ]( options, element ); |
} |
} |
|
|
// allow instantiation without initializing for simple inheritance |
// allow instantiation without initializing for simple inheritance |
// must use "new" keyword (the code above always passes args) |
// must use "new" keyword (the code above always passes args) |
if ( arguments.length ) { |
if ( arguments.length ) { |
this._createWidget( options, element ); |
this._createWidget( options, element ); |
} |
} |
}; |
}; |
|
|
$.Widget.prototype = { |
$.Widget.prototype = { |
widgetName: "widget", |
widgetName: "widget", |
widgetEventPrefix: "", |
widgetEventPrefix: "", |
defaultElement: "<div>", |
defaultElement: "<div>", |
options: { |
options: { |
disabled: false |
disabled: false |
}, |
}, |
_createWidget: function( options, element ) { |
_createWidget: function( options, element ) { |
element = $( element || this.defaultElement || this )[ 0 ]; |
element = $( element || this.defaultElement || this )[ 0 ]; |
this.element = $( element ); |
this.element = $( element ); |
this.options = $.extend( true, {}, |
this.options = $.extend( true, {}, |
this.options, |
this.options, |
this._getCreateOptions(), |
this._getCreateOptions(), |
options ); |
options ); |
|
|
this.bindings = $(); |
this.bindings = $(); |
this.hoverable = $(); |
this.hoverable = $(); |
this.focusable = $(); |
this.focusable = $(); |
|
|
if ( element !== this ) { |
if ( element !== this ) { |
$.data( element, this.widgetName, this ); |
$.data( element, this.widgetName, this ); |
this._bind({ remove: "destroy" }); |
this._bind({ remove: "destroy" }); |
} |
} |
|
|
this._create(); |
this._create(); |
this._trigger( "create" ); |
this._trigger( "create" ); |
this._init(); |
this._init(); |
}, |
}, |
_getCreateOptions: function() { |
_getCreateOptions: function() { |
return $.metadata && $.metadata.get( this.element[0] )[ this.widgetName ]; |
return $.metadata && $.metadata.get( this.element[0] )[ this.widgetName ]; |
}, |
}, |
_create: $.noop, |
_create: $.noop, |
_init: $.noop, |
_init: $.noop, |
|
|
_super: function( method ) { |
_super: function( method ) { |
return this.base[ method ].apply( this, slice.call( arguments, 1 ) ); |
return this.base[ method ].apply( this, slice.call( arguments, 1 ) ); |
}, |
}, |
_superApply: function( method, args ) { |
_superApply: function( method, args ) { |
return this.base[ method ].apply( this, args ); |
return this.base[ method ].apply( this, args ); |
}, |
}, |
|
|
destroy: function() { |
destroy: function() { |
this._destroy(); |
this._destroy(); |
// we can probably remove the unbind calls in 2.0 |
// we can probably remove the unbind calls in 2.0 |
// all event bindings should go through this._bind() |
// all event bindings should go through this._bind() |
this.element |
this.element |
.unbind( "." + this.widgetName ) |
.unbind( "." + this.widgetName ) |
.removeData( this.widgetName ); |
.removeData( this.widgetName ); |
this.widget() |
this.widget() |
.unbind( "." + this.widgetName ) |
.unbind( "." + this.widgetName ) |
.removeAttr( "aria-disabled" ) |
.removeAttr( "aria-disabled" ) |
.removeClass( |
.removeClass( |
this.widgetBaseClass + "-disabled " + |
this.widgetBaseClass + "-disabled " + |
"ui-state-disabled" ); |
"ui-state-disabled" ); |
|
|
// clean up events and states |
// clean up events and states |
this.bindings.unbind( "." + this.widgetName ); |
this.bindings.unbind( "." + this.widgetName ); |
this.hoverable.removeClass( "ui-state-hover" ); |
this.hoverable.removeClass( "ui-state-hover" ); |
this.focusable.removeClass( "ui-state-focus" ); |
this.focusable.removeClass( "ui-state-focus" ); |
}, |
}, |
_destroy: $.noop, |
_destroy: $.noop, |
|
|
widget: function() { |
widget: function() { |
return this.element; |
return this.element; |
}, |
}, |
|
|
option: function( key, value ) { |
option: function( key, value ) { |
var options = key; |
var options = key; |
|
|
if ( arguments.length === 0 ) { |
if ( arguments.length === 0 ) { |
// don't return a reference to the internal hash |
// don't return a reference to the internal hash |
return $.extend( {}, this.options ); |
return $.extend( {}, this.options ); |
} |
} |
|
|
if (typeof key === "string" ) { |
if (typeof key === "string" ) { |
if ( value === undefined ) { |
if ( value === undefined ) { |
return this.options[ key ]; |
return this.options[ key ]; |
} |
} |
options = {}; |
options = {}; |
options[ key ] = value; |
options[ key ] = value; |
} |
} |
|
|
this._setOptions( options ); |
this._setOptions( options ); |
|
|
return this; |
return this; |
}, |
}, |
_setOptions: function( options ) { |
_setOptions: function( options ) { |
var self = this; |
var self = this; |
$.each( options, function( key, value ) { |
$.each( options, function( key, value ) { |
self._setOption( key, value ); |
self._setOption( key, value ); |
}); |
}); |
|
|
return this; |
return this; |
}, |
}, |
_setOption: function( key, value ) { |
_setOption: function( key, value ) { |
this.options[ key ] = value; |
this.options[ key ] = value; |
|
|
if ( key === "disabled" ) { |
if ( key === "disabled" ) { |
this.widget() |
this.widget() |
.toggleClass( this.widgetBaseClass + "-disabled ui-state-disabled", !!value ) |
.toggleClass( this.widgetBaseClass + "-disabled ui-state-disabled", !!value ) |
.attr( "aria-disabled", value ); |
.attr( "aria-disabled", value ); |
this.hoverable.removeClass( "ui-state-hover" ); |
this.hoverable.removeClass( "ui-state-hover" ); |
this.focusable.removeClass( "ui-state-focus" ); |
this.focusable.removeClass( "ui-state-focus" ); |
} |
} |
|
|
return this; |
return this; |
}, |
}, |
|
|
enable: function() { |
enable: function() { |
return this._setOption( "disabled", false ); |
return this._setOption( "disabled", false ); |
}, |
}, |
disable: function() { |
disable: function() { |
return this._setOption( "disabled", true ); |
return this._setOption( "disabled", true ); |
}, |
}, |
|
|
_bind: function( element, handlers ) { |
_bind: function( element, handlers ) { |
// no element argument, shuffle and use this.element |
// no element argument, shuffle and use this.element |
if ( !handlers ) { |
if ( !handlers ) { |
handlers = element; |
handlers = element; |
element = this.element; |
element = this.element; |
} else { |
} else { |
this.bindings = this.bindings.add( element ); |
this.bindings = this.bindings.add( element ); |
} |
} |
var instance = this; |
var instance = this; |
$.each( handlers, function( event, handler ) { |
$.each( handlers, function( event, handler ) { |
element.bind( event + "." + instance.widgetName, function() { |
element.bind( event + "." + instance.widgetName, function() { |
// allow widgets to customize the disabled handling |
// allow widgets to customize the disabled handling |
// - disabled as an array instead of boolean |
// - disabled as an array instead of boolean |
// - disabled class as method for disabling individual parts |
// - disabled class as method for disabling individual parts |
if ( instance.options.disabled === true || |
if ( instance.options.disabled === true || |
$( this ).hasClass( "ui-state-disabled" ) ) { |
$( this ).hasClass( "ui-state-disabled" ) ) { |
return; |
return; |
} |
} |
return ( typeof handler === "string" ? instance[ handler ] : handler ) |
return ( typeof handler === "string" ? instance[ handler ] : handler ) |
.apply( instance, arguments ); |
.apply( instance, arguments ); |
}); |
}); |
}); |
}); |
}, |
}, |
|
|
_hoverable: function( element ) { |
_hoverable: function( element ) { |
this.hoverable = this.hoverable.add( element ); |
this.hoverable = this.hoverable.add( element ); |
this._bind( element, { |
this._bind( element, { |
mouseenter: function( event ) { |
mouseenter: function( event ) { |
$( event.currentTarget ).addClass( "ui-state-hover" ); |
$( event.currentTarget ).addClass( "ui-state-hover" ); |
}, |
}, |
mouseleave: function( event ) { |
mouseleave: function( event ) { |
$( event.currentTarget ).removeClass( "ui-state-hover" ); |
$( event.currentTarget ).removeClass( "ui-state-hover" ); |
} |
} |
}); |
}); |
}, |
}, |
|
|
_focusable: function( element ) { |
_focusable: function( element ) { |
this.focusable = this.focusable.add( element ); |
this.focusable = this.focusable.add( element ); |
this._bind( element, { |
this._bind( element, { |
focusin: function( event ) { |
focusin: function( event ) { |
$( event.currentTarget ).addClass( "ui-state-focus" ); |
$( event.currentTarget ).addClass( "ui-state-focus" ); |
}, |
}, |
focusout: function( event ) { |
focusout: function( event ) { |
$( event.currentTarget ).removeClass( "ui-state-focus" ); |
$( event.currentTarget ).removeClass( "ui-state-focus" ); |
} |
} |
}); |
}); |
}, |
}, |
|
|
_trigger: function( type, event, data ) { |
_trigger: function( type, event, data ) { |
var callback = this.options[ type ], |
var callback = this.options[ type ], |
args; |
args; |
|
|
event = $.Event( event ); |
event = $.Event( event ); |
event.type = ( type === this.widgetEventPrefix ? |
event.type = ( type === this.widgetEventPrefix ? |
type : |
type : |
this.widgetEventPrefix + type ).toLowerCase(); |
this.widgetEventPrefix + type ).toLowerCase(); |
data = data || {}; |
data = data || {}; |
|
|
// copy original event properties over to the new event |
// copy original event properties over to the new event |
// this would happen if we could call $.event.fix instead of $.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 |
// but we don't have a way to force an event to be fixed multiple times |
if ( event.originalEvent ) { |
if ( event.originalEvent ) { |
for ( var i = $.event.props.length, prop; i; ) { |
for ( var i = $.event.props.length, prop; i; ) { |
prop = $.event.props[ --i ]; |
prop = $.event.props[ --i ]; |
event[ prop ] = event.originalEvent[ prop ]; |
event[ prop ] = event.originalEvent[ prop ]; |
} |
} |
} |
} |
|
|
this.element.trigger( event, data ); |
this.element.trigger( event, data ); |
|
|
args = $.isArray( data ) ? |
args = $.isArray( data ) ? |
[ event ].concat( data ) : |
[ event ].concat( data ) : |
[ event, data ]; |
[ event, data ]; |
|
|
return !( $.isFunction( callback ) && |
return !( $.isFunction( callback ) && |
callback.apply( this.element[0], args ) === false || |
callback.apply( this.element[0], args ) === false || |
event.isDefaultPrevented() ); |
event.isDefaultPrevented() ); |
} |
} |
}; |
}; |
|
|
})( jQuery ); |
})( jQuery ); |
|
|