1 | /*!
|
---|
2 | * jQuery UI Widget 1.8rc1
|
---|
3 | *
|
---|
4 | * Copyright (c) 2010 AUTHORS.txt (http://jqueryui.com/about)
|
---|
5 | * Dual licensed under the MIT (MIT-LICENSE.txt)
|
---|
6 | * and GPL (GPL-LICENSE.txt) licenses.
|
---|
7 | *
|
---|
8 | * http://docs.jquery.com/UI/Widget
|
---|
9 | */
|
---|
10 | (function( $ ) {
|
---|
11 |
|
---|
12 | var _remove = $.fn.remove;
|
---|
13 |
|
---|
14 | $.fn.remove = function( selector, keepData ) {
|
---|
15 | if ( !keepData ) {
|
---|
16 | $( "*", this ).add( this ).each(function() {
|
---|
17 | $( this ).triggerHandler( "remove" );
|
---|
18 | });
|
---|
19 | }
|
---|
20 | return _remove.apply( this, arguments );
|
---|
21 | };
|
---|
22 |
|
---|
23 | $.widget = function( name, base, prototype ) {
|
---|
24 | var namespace = name.split( "." )[ 0 ],
|
---|
25 | fullName;
|
---|
26 | name = name.split( "." )[ 1 ];
|
---|
27 | fullName = namespace + "-" + name;
|
---|
28 |
|
---|
29 | if ( !prototype ) {
|
---|
30 | prototype = base;
|
---|
31 | base = $.Widget;
|
---|
32 | }
|
---|
33 |
|
---|
34 | // create selector for plugin
|
---|
35 | $.expr[ ":" ][ fullName ] = function( elem ) {
|
---|
36 | return !!$.data( elem, name );
|
---|
37 | };
|
---|
38 |
|
---|
39 | $[ namespace ] = $[ namespace ] || {};
|
---|
40 | $[ namespace ][ name ] = function( options, element ) {
|
---|
41 | // allow instantiation without initializing for simple inheritance
|
---|
42 | if ( arguments.length ) {
|
---|
43 | this._createWidget( options, element );
|
---|
44 | }
|
---|
45 | };
|
---|
46 |
|
---|
47 | var basePrototype = new base();
|
---|
48 | // we need to make the options hash a property directly on the new instance
|
---|
49 | // otherwise we'll modify the options hash on the prototype that we're
|
---|
50 | // inheriting from
|
---|
51 | // $.each( basePrototype, function( key, val ) {
|
---|
52 | // if ( $.isPlainObject(val) ) {
|
---|
53 | // basePrototype[ key ] = $.extend( {}, val );
|
---|
54 | // }
|
---|
55 | // });
|
---|
56 | basePrototype.options = $.extend( {}, basePrototype.options );
|
---|
57 | $[ namespace ][ name ].prototype = $.extend( true, basePrototype, {
|
---|
58 | namespace: namespace,
|
---|
59 | widgetName: name,
|
---|
60 | widgetEventPrefix: $[ namespace ][ name ].prototype.widgetEventPrefix || name,
|
---|
61 | widgetBaseClass: fullName
|
---|
62 | }, prototype );
|
---|
63 |
|
---|
64 | $.widget.bridge( name, $[ namespace ][ name ] );
|
---|
65 | };
|
---|
66 |
|
---|
67 | $.widget.bridge = function( name, object ) {
|
---|
68 | $.fn[ name ] = function( options ) {
|
---|
69 | var isMethodCall = typeof options === "string",
|
---|
70 | args = Array.prototype.slice.call( arguments, 1 ),
|
---|
71 | returnValue = this;
|
---|
72 |
|
---|
73 | // allow multiple hashes to be passed on init
|
---|
74 | options = !isMethodCall && args.length ?
|
---|
75 | $.extend.apply( null, [ true, options ].concat(args) ) :
|
---|
76 | options;
|
---|
77 |
|
---|
78 | // prevent calls to internal methods
|
---|
79 | if ( isMethodCall && options.substring( 0, 1 ) === "_" ) {
|
---|
80 | return returnValue;
|
---|
81 | }
|
---|
82 |
|
---|
83 | if ( isMethodCall ) {
|
---|
84 | this.each(function() {
|
---|
85 | var instance = $.data( this, name ),
|
---|
86 | methodValue = instance && $.isFunction( instance[options] ) ?
|
---|
87 | instance[ options ].apply( instance, args ) :
|
---|
88 | instance;
|
---|
89 | if ( methodValue !== instance && methodValue !== undefined ) {
|
---|
90 | returnValue = methodValue;
|
---|
91 | return false;
|
---|
92 | }
|
---|
93 | });
|
---|
94 | } else {
|
---|
95 | this.each(function() {
|
---|
96 | var instance = $.data( this, name );
|
---|
97 | if ( instance ) {
|
---|
98 | if ( options ) {
|
---|
99 | instance.option( options );
|
---|
100 | }
|
---|
101 | instance._init();
|
---|
102 | } else {
|
---|
103 | $.data( this, name, new object( options, this ) );
|
---|
104 | }
|
---|
105 | });
|
---|
106 | }
|
---|
107 |
|
---|
108 | return returnValue;
|
---|
109 | };
|
---|
110 | };
|
---|
111 |
|
---|
112 | $.Widget = function( options, element ) {
|
---|
113 | // allow instantiation without initializing for simple inheritance
|
---|
114 | if ( arguments.length ) {
|
---|
115 | this._createWidget( options, element );
|
---|
116 | }
|
---|
117 | };
|
---|
118 |
|
---|
119 | $.Widget.prototype = {
|
---|
120 | widgetName: "widget",
|
---|
121 | widgetEventPrefix: "",
|
---|
122 | options: {
|
---|
123 | disabled: false
|
---|
124 | },
|
---|
125 | _createWidget: function( options, element ) {
|
---|
126 | // $.widget.bridge stores the plugin instance, but we do it anyway
|
---|
127 | // so that it's stored even before the _create function runs
|
---|
128 | this.element = $( element ).data( this.widgetName, this );
|
---|
129 | this.options = $.extend( true, {},
|
---|
130 | this.options,
|
---|
131 | $.metadata && $.metadata.get( element )[ this.widgetName ],
|
---|
132 | options );
|
---|
133 |
|
---|
134 | var self = this;
|
---|
135 | this.element.bind( "remove." + this.widgetName, function() {
|
---|
136 | self.destroy();
|
---|
137 | });
|
---|
138 |
|
---|
139 | this._create();
|
---|
140 | this._init();
|
---|
141 | },
|
---|
142 | _create: function() {},
|
---|
143 | _init: function() {},
|
---|
144 |
|
---|
145 | destroy: function() {
|
---|
146 | this.element
|
---|
147 | .unbind( "." + this.widgetName )
|
---|
148 | .removeData( this.widgetName );
|
---|
149 | this.widget()
|
---|
150 | .unbind( "." + this.widgetName )
|
---|
151 | .removeAttr( "aria-disabled" )
|
---|
152 | .removeClass(
|
---|
153 | this.widgetBaseClass + "-disabled " +
|
---|
154 | this.namespace + "-state-disabled" );
|
---|
155 | },
|
---|
156 |
|
---|
157 | widget: function() {
|
---|
158 | return this.element;
|
---|
159 | },
|
---|
160 |
|
---|
161 | option: function( key, value ) {
|
---|
162 | var options = key,
|
---|
163 | self = this;
|
---|
164 |
|
---|
165 | if ( arguments.length === 0 ) {
|
---|
166 | // don't return a reference to the internal hash
|
---|
167 | return $.extend( {}, self.options );
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (typeof key === "string" ) {
|
---|
171 | if ( value === undefined ) {
|
---|
172 | return this.options[ key ];
|
---|
173 | }
|
---|
174 | options = {};
|
---|
175 | options[ key ] = value;
|
---|
176 | }
|
---|
177 |
|
---|
178 | $.each( options, function( key, value ) {
|
---|
179 | self._setOption( key, value );
|
---|
180 | });
|
---|
181 |
|
---|
182 | return self;
|
---|
183 | },
|
---|
184 | _setOption: function( key, value ) {
|
---|
185 | this.options[ key ] = value;
|
---|
186 |
|
---|
187 | if ( key === "disabled" ) {
|
---|
188 | this.widget()
|
---|
189 | [ value ? "addClass" : "removeClass"](
|
---|
190 | this.widgetBaseClass + "-disabled" + " " +
|
---|
191 | this.namespace + "-state-disabled" )
|
---|
192 | .attr( "aria-disabled", value );
|
---|
193 | }
|
---|
194 |
|
---|
195 | return this;
|
---|
196 | },
|
---|
197 |
|
---|
198 | enable: function() {
|
---|
199 | return this._setOption( "disabled", false );
|
---|
200 | },
|
---|
201 | disable: function() {
|
---|
202 | return this._setOption( "disabled", true );
|
---|
203 | },
|
---|
204 |
|
---|
205 | _trigger: function( type, event, data ) {
|
---|
206 | var callback = this.options[ type ];
|
---|
207 |
|
---|
208 | event = $.Event( event );
|
---|
209 | event.type = ( type === this.widgetEventPrefix ?
|
---|
210 | type :
|
---|
211 | this.widgetEventPrefix + type ).toLowerCase();
|
---|
212 | data = data || {};
|
---|
213 |
|
---|
214 | // copy original event properties over to the new event
|
---|
215 | // this would happen if we could call $.event.fix instead of $.Event
|
---|
216 | // but we don't have a way to force an event to be fixed multiple times
|
---|
217 | if ( event.originalEvent ) {
|
---|
218 | for ( var i = $.event.props.length, prop; i; ) {
|
---|
219 | prop = $.event.props[ --i ];
|
---|
220 | event[ prop ] = event.originalEvent[ prop ];
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | this.element.trigger( event, data );
|
---|
225 |
|
---|
226 | return !( $.isFunction(callback) &&
|
---|
227 | callback.call( this.element[0], event, data ) === false ||
|
---|
228 | event.isDefaultPrevented() );
|
---|
229 | }
|
---|
230 | };
|
---|
231 |
|
---|
232 | })( jQuery );
|
---|