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