2 * jQuery UI Button 1.9.2
5 * Copyright 2012 jQuery Foundation and other contributors
6 * Released under the MIT license.
7 * http://jquery.org/license
9 * http://api.jqueryui.com/button/
15 (function( $, undefined ) {
17 var lastActive
, startXPos
, startYPos
, clickDragged
,
18 baseClasses
= "ui-button ui-widget ui-state-default ui-corner-all",
19 stateClasses
= "ui-state-hover ui-state-active ",
20 typeClasses
= "ui-button-icons-only ui-button-icon-only ui-button-text-icons ui-button-text-icon-primary ui-button-text-icon-secondary ui-button-text-only",
21 formResetHandler = function() {
22 var buttons
= $( this ).find( ":ui-button" );
23 setTimeout(function() {
24 buttons
.button( "refresh" );
27 radioGroup = function( radio
) {
28 var name
= radio
.name
,
33 radios
= $( form
).find( "[name='" + name
+ "']" );
35 radios
= $( "[name='" + name
+ "']", radio
.ownerDocument
)
44 $.widget( "ui.button", {
46 defaultElement
: "<button>",
57 this.element
.closest( "form" )
58 .unbind( "reset" + this.eventNamespace
)
59 .bind( "reset" + this.eventNamespace
, formResetHandler
);
61 if ( typeof this.options
.disabled
!== "boolean" ) {
62 this.options
.disabled
= !!this.element
.prop( "disabled" );
64 this.element
.prop( "disabled", this.options
.disabled
);
67 this._determineButtonType();
68 this.hasTitle
= !!this.buttonElement
.attr( "title" );
71 options
= this.options
,
72 toggleButton
= this.type
=== "checkbox" || this.type
=== "radio",
73 activeClass
= !toggleButton
? "ui-state-active" : "",
74 focusClass
= "ui-state-focus";
76 if ( options
.label
=== null ) {
77 options
.label
= (this.type
=== "input" ? this.buttonElement
.val() : this.buttonElement
.html());
80 this._hoverable( this.buttonElement
);
83 .addClass( baseClasses
)
84 .attr( "role", "button" )
85 .bind( "mouseenter" + this.eventNamespace
, function() {
86 if ( options
.disabled
) {
89 if ( this === lastActive
) {
90 $( this ).addClass( "ui-state-active" );
93 .bind( "mouseleave" + this.eventNamespace
, function() {
94 if ( options
.disabled
) {
97 $( this ).removeClass( activeClass
);
99 .bind( "click" + this.eventNamespace
, function( event
) {
100 if ( options
.disabled
) {
101 event
.preventDefault();
102 event
.stopImmediatePropagation();
107 .bind( "focus" + this.eventNamespace
, function() {
108 // no need to check disabled, focus won't be triggered anyway
109 that
.buttonElement
.addClass( focusClass
);
111 .bind( "blur" + this.eventNamespace
, function() {
112 that
.buttonElement
.removeClass( focusClass
);
115 if ( toggleButton
) {
116 this.element
.bind( "change" + this.eventNamespace
, function() {
117 if ( clickDragged
) {
122 // if mouse moves between mousedown and mouseup (drag) set clickDragged flag
123 // prevents issue where button state changes but checkbox/radio checked state
124 // does not in Firefox (see ticket #6970)
126 .bind( "mousedown" + this.eventNamespace
, function( event
) {
127 if ( options
.disabled
) {
130 clickDragged
= false;
131 startXPos
= event
.pageX
;
132 startYPos
= event
.pageY
;
134 .bind( "mouseup" + this.eventNamespace
, function( event
) {
135 if ( options
.disabled
) {
138 if ( startXPos
!== event
.pageX
|| startYPos
!== event
.pageY
) {
144 if ( this.type
=== "checkbox" ) {
145 this.buttonElement
.bind( "click" + this.eventNamespace
, function() {
146 if ( options
.disabled
|| clickDragged
) {
149 $( this ).toggleClass( "ui-state-active" );
150 that
.buttonElement
.attr( "aria-pressed", that
.element
[0].checked
);
152 } else if ( this.type
=== "radio" ) {
153 this.buttonElement
.bind( "click" + this.eventNamespace
, function() {
154 if ( options
.disabled
|| clickDragged
) {
157 $( this ).addClass( "ui-state-active" );
158 that
.buttonElement
.attr( "aria-pressed", "true" );
160 var radio
= that
.element
[ 0 ];
164 return $( this ).button( "widget" )[ 0 ];
166 .removeClass( "ui-state-active" )
167 .attr( "aria-pressed", "false" );
171 .bind( "mousedown" + this.eventNamespace
, function() {
172 if ( options
.disabled
) {
175 $( this ).addClass( "ui-state-active" );
177 that
.document
.one( "mouseup", function() {
181 .bind( "mouseup" + this.eventNamespace
, function() {
182 if ( options
.disabled
) {
185 $( this ).removeClass( "ui-state-active" );
187 .bind( "keydown" + this.eventNamespace
, function(event
) {
188 if ( options
.disabled
) {
191 if ( event
.keyCode
=== $.ui
.keyCode
.SPACE
|| event
.keyCode
=== $.ui
.keyCode
.ENTER
) {
192 $( this ).addClass( "ui-state-active" );
195 .bind( "keyup" + this.eventNamespace
, function() {
196 $( this ).removeClass( "ui-state-active" );
199 if ( this.buttonElement
.is("a") ) {
200 this.buttonElement
.keyup(function(event
) {
201 if ( event
.keyCode
=== $.ui
.keyCode
.SPACE
) {
202 // TODO pass through original event correctly (just as 2nd argument doesn't work)
209 // TODO: pull out $.Widget's handling for the disabled option into
210 // $.Widget.prototype._setOptionDisabled so it's easy to proxy and can
211 // be overridden by individual plugins
212 this._setOption( "disabled", options
.disabled
);
216 _determineButtonType: function() {
217 var ancestor
, labelSelector
, checked
;
219 if ( this.element
.is("[type=checkbox]") ) {
220 this.type
= "checkbox";
221 } else if ( this.element
.is("[type=radio]") ) {
223 } else if ( this.element
.is("input") ) {
226 this.type
= "button";
229 if ( this.type
=== "checkbox" || this.type
=== "radio" ) {
230 // we don't search against the document in case the element
231 // is disconnected from the DOM
232 ancestor
= this.element
.parents().last();
233 labelSelector
= "label[for='" + this.element
.attr("id") + "']";
234 this.buttonElement
= ancestor
.find( labelSelector
);
235 if ( !this.buttonElement
.length
) {
236 ancestor
= ancestor
.length
? ancestor
.siblings() : this.element
.siblings();
237 this.buttonElement
= ancestor
.filter( labelSelector
);
238 if ( !this.buttonElement
.length
) {
239 this.buttonElement
= ancestor
.find( labelSelector
);
242 this.element
.addClass( "ui-helper-hidden-accessible" );
244 checked
= this.element
.is( ":checked" );
246 this.buttonElement
.addClass( "ui-state-active" );
248 this.buttonElement
.prop( "aria-pressed", checked
);
250 this.buttonElement
= this.element
;
255 return this.buttonElement
;
258 _destroy: function() {
260 .removeClass( "ui-helper-hidden-accessible" );
262 .removeClass( baseClasses
+ " " + stateClasses
+ " " + typeClasses
)
263 .removeAttr( "role" )
264 .removeAttr( "aria-pressed" )
265 .html( this.buttonElement
.find(".ui-button-text").html() );
267 if ( !this.hasTitle
) {
268 this.buttonElement
.removeAttr( "title" );
272 _setOption: function( key
, value
) {
273 this._super( key
, value
);
274 if ( key
=== "disabled" ) {
276 this.element
.prop( "disabled", true );
278 this.element
.prop( "disabled", false );
285 refresh: function() {
287 var isDisabled
= this.element
.is( "input, button" ) ? this.element
.is( ":disabled" ) : this.element
.hasClass( "ui-button-disabled" );
289 if ( isDisabled
!== this.options
.disabled
) {
290 this._setOption( "disabled", isDisabled
);
292 if ( this.type
=== "radio" ) {
293 radioGroup( this.element
[0] ).each(function() {
294 if ( $( this ).is( ":checked" ) ) {
295 $( this ).button( "widget" )
296 .addClass( "ui-state-active" )
297 .attr( "aria-pressed", "true" );
299 $( this ).button( "widget" )
300 .removeClass( "ui-state-active" )
301 .attr( "aria-pressed", "false" );
304 } else if ( this.type
=== "checkbox" ) {
305 if ( this.element
.is( ":checked" ) ) {
307 .addClass( "ui-state-active" )
308 .attr( "aria-pressed", "true" );
311 .removeClass( "ui-state-active" )
312 .attr( "aria-pressed", "false" );
317 _resetButton: function() {
318 if ( this.type
=== "input" ) {
319 if ( this.options
.label
) {
320 this.element
.val( this.options
.label
);
324 var buttonElement
= this.buttonElement
.removeClass( typeClasses
),
325 buttonText
= $( "<span></span>", this.document
[0] )
326 .addClass( "ui-button-text" )
327 .html( this.options
.label
)
328 .appendTo( buttonElement
.empty() )
330 icons
= this.options
.icons
,
331 multipleIcons
= icons
.primary
&& icons
.secondary
,
334 if ( icons
.primary
|| icons
.secondary
) {
335 if ( this.options
.text
) {
336 buttonClasses
.push( "ui-button-text-icon" + ( multipleIcons
? "s" : ( icons
.primary
? "-primary" : "-secondary" ) ) );
339 if ( icons
.primary
) {
340 buttonElement
.prepend( "<span class='ui-button-icon-primary ui-icon " + icons
.primary
+ "'></span>" );
343 if ( icons
.secondary
) {
344 buttonElement
.append( "<span class='ui-button-icon-secondary ui-icon " + icons
.secondary
+ "'></span>" );
347 if ( !this.options
.text
) {
348 buttonClasses
.push( multipleIcons
? "ui-button-icons-only" : "ui-button-icon-only" );
350 if ( !this.hasTitle
) {
351 buttonElement
.attr( "title", $.trim( buttonText
) );
355 buttonClasses
.push( "ui-button-text-only" );
357 buttonElement
.addClass( buttonClasses
.join( " " ) );
361 $.widget( "ui.buttonset", {
364 items
: "button, input[type=button], input[type=submit], input[type=reset], input[type=checkbox], input[type=radio], a, :data(button)"
367 _create: function() {
368 this.element
.addClass( "ui-buttonset" );
375 _setOption: function( key
, value
) {
376 if ( key
=== "disabled" ) {
377 this.buttons
.button( "option", key
, value
);
380 this._super( key
, value
);
383 refresh: function() {
384 var rtl
= this.element
.css( "direction" ) === "rtl";
386 this.buttons
= this.element
.find( this.options
.items
)
387 .filter( ":ui-button" )
394 return $( this ).button( "widget" )[ 0 ];
396 .removeClass( "ui-corner-all ui-corner-left ui-corner-right" )
398 .addClass( rtl
? "ui-corner-right" : "ui-corner-left" )
401 .addClass( rtl
? "ui-corner-left" : "ui-corner-right" )
406 _destroy: function() {
407 this.element
.removeClass( "ui-buttonset" );
410 return $( this ).button( "widget" )[ 0 ];
412 .removeClass( "ui-corner-left ui-corner-right" )
414 .button( "destroy" );