8000 Widget: conform to coding standards. · ainformatico/jquery-ui@003cb9d · GitHub
Skip to content

Commit 003cb9d

Browse files
committed
Widget: conform to coding standards.
1 parent 7d96a0d commit 003cb9d

1 file changed

Lines changed: 100 additions & 87 deletions

File tree

ui/jquery.ui.widget.js

Lines changed: 100 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -7,213 +7,226 @@
77
*
88
* http://docs.jquery.com/UI/Widget
99
*/
10-
(function($) {
10+
(function( $ ) {
1111

1212
var _remove = $.fn.remove;
1313

1414
$.fn.remove = function() {
1515
// Safari has a native remove event which actually removes DOM elements,
1616
// 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" );
1919
});
20-
return _remove.apply(this, arguments);
20+
return _remove.apply( this, arguments );
2121
};
2222

23-
$.widget = function(name, base, prototype) {
24-
var namespace = name.split(".")[0],
23+
$.widget = function( name, base, prototype ) {
24+
var namespace = name.split( "." )[ 0 ],
2525
fullName;
26-
name = name.split(".")[1];
27-
fullName = namespace + '-' + name;
26+
name = name.split( "." )[ 1 ];
27+
fullName = namespace + "-" + name;
2828

29-
if (!prototype) {
29+
if ( !prototype ) {
3030
prototype = base;
3131
base = $.Widget;
3232
}
3333

3434
// create selector for plugin
35-
$.expr[':'][fullName] = function(elem) {
36-
return !!$.data(elem, name);
35+
$.expr[ ":" ][ fullName ] = function( elem ) {
36+
return !!$.data( elem, name );
3737
};
3838

39-
$[namespace] = $[namespace] || {};
40-
$[namespace][name] = function(options, element) {
39+
$[ namespace ] = $[ namespace ] || {};
40+
$[ namespace ][ name ] = function( options, element ) {
4141
// allow instantiation without initializing for simple inheritance
42-
(arguments.length && this._createWidget(options, element));
42+
if ( arguments.length ) {
43+
this._createWidget( options, element );
44+
}
4345
};
4446

4547
var basePrototype = new base();
4648
// we need to make the options hash a property directly on the new instance
4749
// otherwise we'll modify the options hash on the prototype that we're
4850
// 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 );
5254
// }
5355
// });
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, {
5658
namespace: namespace,
5759
widgetName: name,
58-
widgetEventPrefix: $[namespace][name].prototype.widgetEventPrefix || name,
60+
widgetEventPrefix: $[ namespace ][ name ].prototype.widgetEventPrefix || name,
5961
widgetBaseClass: fullName
60-
}, prototype);
62+
}, prototype );
6163

62-
$.widget.bridge(name, $[namespace][name]);
64+
$.widget.bridge( name, $[ namespace ][ name ] );
6365
};
6466

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 ),
6971
returnValue = this;
70-
72+
7173
// allow multiple hashes to be passed on init
7274
options = !isMethodCall && args.length
7375
? $.extend.apply(null, [true, options].concat(args))
7476
: options;
7577

7678
// prevent calls to internal methods
77-
if (isMethodCall && options.substring(0, 1) == '_') {
79+
if ( isMethodCall && options.substring(0, 1) === "_" ) {
7880
return returnValue;
7981
}
8082

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 ) {
8890
returnValue = methodValue;
8991
return false;
9092
}
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+
}
95101

96102
return returnValue;
97103
};
98104
};
99105

100-
$.Widget = function(options, element) {
106+
$.Widget = function( options, element ) {
101107
// allow instantiation without initializing for simple inheritance
102-
(arguments.length && this._createWidget(options, element));
108+
if ( arguments.length ) {
109+
this._createWidget( options, element );
110+
}
103111
};
104112

105113
$.Widget.prototype = {
106-
widgetName: 'widget',
107-
widgetEventPrefix: '',
114+
widgetName: "widget",
115+
widgetEventPrefix: "",
108116
options: {
109117
disabled: false
110118
},
111-
_createWidget: function(options, element) {
119+
_createWidget: function( options, element ) {
112120
// $.widget.bridge stores the plugin instance, but we do it anyway
113121
// 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, {},
116124
this.options,
117125
// DEPRECATED: move defaults to prototype.options
118126
$[this.namespace][this.widgetName].defaults,
119-
$.metadata && $.metadata.get(element)[this.widgetName],
120-
options);
127+
$.metadata && $.metadata.get( element )[ this.widgetName ],
128+
options );
121129

122130
var self = this;
123-
this.element.bind('remove.' + this.widgetName, function() {
131+
this.element.bind( "remove." + this.widgetName, function() {
124132
self.destroy();
125133
});
126134

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+
}
129141
},
130142

131143
destroy: function() {
132144
this.element
133-
.unbind('.' + this.widgetName)
134-
.removeData(this.widgetName);
145+
.unbind( "." + this.widgetName )
146+
.removeData( this.widgetName );
135147
this.widget()
136-
.unbind('.' + this.widgetName)
137-
.removeAttr('aria-disabled')
148+
.unbind( "." + this.widgetName )
149+
.removeAttr( "aria-disabled" )
138150
.removeClass(
139-
this.widgetBaseClass + '-disabled ' +
140-
this.namespace + '-state-disabled');
151+
this.widgetBaseClass + "-disabled " +
152+
this.namespace + "-state-disabled" );
141153
},
142154

143155
widget: function() {
144156
return this.element;
145157
},
146158

147-
option: function(key, value) {
159+
option: function( key, value ) {
148160
var options = key,
149161
self = this;
150162

151-
if (arguments.length === 0) {
163+
if ( arguments.length === 0 ) {
152164
// don't return a reference to the internal hash
153-
return $.extend({}, self.options);
165+
return $.extend( {}, self.options );
154166
}
155167

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 ];
159171
}
160172
options = {};
161-
options[key] = value;
173+
options[ key ] = value;
162174
}
163175

164-
$.each(options, function(key, value) {
165-
self._setOption(key, value);
176+
$.each( options, function( key, value ) {
177+
self._setOption( key, value );
166178
});
167179

168180
return self;
169181
},
170-
_setOption: function(key, value) {
171-
this.options[key] = value;
182+
_setOption: function( key, value ) {
183+
this.options[ key ] = value;
172184

173-
if (key == 'disabled') {
185+
if ( key === "disabled" ) {
174186
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 );
179191
}
180192

181193
return this;
182194
},
183195

184196
enable: function() {
185-
return this._setOption('disabled', false);
197+
return this._setOption( "disabled", false );
186198
},
187199
disable: function() {
188-
return this._setOption('disabled', true);
200+
return this._setOption( "disabled", true );
189201
},
190202

191-
_trigger: function(type, event, data) {
192-
var callback = this.options[type];
203+
_trigger: function( type, event, data ) {
204+
var callback = this.options[ type ];
193205

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();
197210
data = data || {};
198211

199212
// copy original event properties over to the new event
200213
// this would happen if we could call $.event.fix instead of $.Event
201214
// 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 ];
206219
}
207220
}
208221

209-
this.element.trigger(event, data);
222+
this.element.trigger( event, data );
210223

211-
return !($.isFunction(callback) && callback.call(this.element[0], event, data) === false
224+
return !($.isFunction(callback) && callback.call( this.element[0], event, data ) === false
212225
|| event.isDefaultPrevented());
213226
}
214227
};
215228

216229
// DEPRECATED: use the plugin's parent widget instead of $.widget
217230
$.widget.prototype = $.Widget.prototype;
218231

219-
})(jQuery);
232+
})( jQuery );

0 commit comments

Comments
 (0)