Project

General

Profile

1 5669 leinfelder
/*!
2
 * jQuery UI Widget 1.8.6
3
 *
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
7
 *
8
 * http://docs.jquery.com/UI/Widget
9
 */
10
(function( $, undefined ) {
11
12
// jQuery 1.4+
13
if ( $.cleanData ) {
14
	var _cleanData = $.cleanData;
15
	$.cleanData = function( elems ) {
16
		for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
17
			$( elem ).triggerHandler( "remove" );
18
		}
19
		_cleanData( elems );
20
	};
21
} else {
22
	var _remove = $.fn.remove;
23
	$.fn.remove = function( selector, keepData ) {
24
		return this.each(function() {
25
			if ( !keepData ) {
26
				if ( !selector || $.filter( selector, [ this ] ).length ) {
27
					$( "*", this ).add( [ this ] ).each(function() {
28
						$( this ).triggerHandler( "remove" );
29
					});
30
				}
31
			}
32
			return _remove.call( $(this), selector, keepData );
33
		});
34
	};
35
}
36
37
$.widget = function( name, base, prototype ) {
38
	var namespace = name.split( "." )[ 0 ],
39
		fullName;
40
	name = name.split( "." )[ 1 ];
41
	fullName = namespace + "-" + name;
42
43
	if ( !prototype ) {
44
		prototype = base;
45
		base = $.Widget;
46
	}
47
48
	// create selector for plugin
49
	$.expr[ ":" ][ fullName ] = function( elem ) {
50
		return !!$.data( elem, name );
51
	};
52
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 );
58
		}
59
	};
60
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
64
	// inheriting from
65
//	$.each( basePrototype, function( key, val ) {
66
//		if ( $.isPlainObject(val) ) {
67
//			basePrototype[ key ] = $.extend( {}, val );
68
//		}
69
//	});
70
	basePrototype.options = $.extend( true, {}, basePrototype.options );
71
	$[ namespace ][ name ].prototype = $.extend( true, basePrototype, {
72
		namespace: namespace,
73
		widgetName: name,
74
		widgetEventPrefix: $[ namespace ][ name ].prototype.widgetEventPrefix || name,
75
		widgetBaseClass: fullName
76
	}, prototype );
77
78
	$.widget.bridge( name, $[ namespace ][ name ] );
79
};
80
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 ),
85
			returnValue = this;
86
87
		// allow multiple hashes to be passed on init
88
		options = !isMethodCall && args.length ?
89
			$.extend.apply( null, [ true, options ].concat(args) ) :
90
			options;
91
92
		// prevent calls to internal methods
93
		if ( isMethodCall && options.charAt( 0 ) === "_" ) {
94
			return returnValue;
95
		}
96
97
		if ( isMethodCall ) {
98
			this.each(function() {
99
				var instance = $.data( this, name ),
100
					methodValue = instance && $.isFunction( instance[options] ) ?
101
						instance[ options ].apply( instance, args ) :
102
						instance;
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 + "'";
107
//				}
108
//				if ( !$.isFunction( instance[options] ) ) {
109
//					throw "no such method '" + options + "' for " + name + " widget instance";
110
//				}
111
//				var methodValue = instance[ options ].apply( instance, args );
112
				if ( methodValue !== instance && methodValue !== undefined ) {
113
					returnValue = methodValue;
114
					return false;
115
				}
116
			});
117
		} else {
118
			this.each(function() {
119
				var instance = $.data( this, name );
120
				if ( instance ) {
121
					instance.option( options || {} )._init();
122
				} else {
123
					$.data( this, name, new object( options, this ) );
124
				}
125
			});
126
		}
127
128
		return returnValue;
129
	};
130
};
131
132
$.Widget = function( options, element ) {
133
	// allow instantiation without initializing for simple inheritance
134
	if ( arguments.length ) {
135
		this._createWidget( options, element );
136
	}
137
};
138
139
$.Widget.prototype = {
140
	widgetName: "widget",
141
	widgetEventPrefix: "",
142
	options: {
143
		disabled: false
144
	},
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, {},
151
			this.options,
152
			this._getCreateOptions(),
153
			options );
154
155
		var self = this;
156
		this.element.bind( "remove." + this.widgetName, function() {
157
			self.destroy();
158
		});
159
160
		this._create();
161
		this._trigger( "create" );
162
		this._init();
163
	},
164
	_getCreateOptions: function() {
165
		return $.metadata && $.metadata.get( this.element[0] )[ this.widgetName ];
166
	},
167
	_create: function() {},
168
	_init: function() {},
169
170
	destroy: function() {
171
		this.element
172
			.unbind( "." + this.widgetName )
173
			.removeData( this.widgetName );
174
		this.widget()
175
			.unbind( "." + this.widgetName )
176
			.removeAttr( "aria-disabled" )
177
			.removeClass(
178
				this.widgetBaseClass + "-disabled " +
179
				"ui-state-disabled" );
180
	},
181
182
	widget: function() {
183
		return this.element;
184
	},
185
186
	option: function( key, value ) {
187
		var options = key;
188
189
		if ( arguments.length === 0 ) {
190
			// don't return a reference to the internal hash
191
			return $.extend( {}, this.options );
192
		}
193
194
		if  (typeof key === "string" ) {
195
			if ( value === undefined ) {
196
				return this.options[ key ];
197
			}
198
			options = {};
199
			options[ key ] = value;
200
		}
201
202
		this._setOptions( options );
203
204
		return this;
205
	},
206
	_setOptions: function( options ) {
207
		var self = this;
208
		$.each( options, function( key, value ) {
209
			self._setOption( key, value );
210
		});
211
212
		return this;
213
	},
214
	_setOption: function( key, value ) {
215
		this.options[ key ] = value;
216
217
		if ( key === "disabled" ) {
218
			this.widget()
219
				[ value ? "addClass" : "removeClass"](
220
					this.widgetBaseClass + "-disabled" + " " +
221
					"ui-state-disabled" )
222
				.attr( "aria-disabled", value );
223
		}
224
225
		return this;
226
	},
227
228
	enable: function() {
229
		return this._setOption( "disabled", false );
230
	},
231
	disable: function() {
232
		return this._setOption( "disabled", true );
233
	},
234
235
	_trigger: function( type, event, data ) {
236
		var callback = this.options[ type ];
237
238
		event = $.Event( event );
239
		event.type = ( type === this.widgetEventPrefix ?
240
			type :
241
			this.widgetEventPrefix + type ).toLowerCase();
242
		data = data || {};
243
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 ];
251
			}
252
		}
253
254
		this.element.trigger( event, data );
255
256
		return !( $.isFunction(callback) &&
257
			callback.call( this.element[0], event, data ) === false ||
258
			event.isDefaultPrevented() );
259
	}
260
};
261
262
})( jQuery );