2 * jQuery UI Button 1.8.24
4 * Copyright 2012, AUTHORS.txt (http://jqueryui.com/about)
5 * Dual licensed under the MIT or GPL Version 2 licenses.
6 * http://jquery.org/license
8 * http://docs.jquery.com/UI/Button
14 (function( $, undefined ) {
16 var lastActive, startXPos, startYPos, clickDragged,
17 baseClasses = "ui-button ui-widget ui-state-default ui-corner-all",
18 stateClasses = "ui-state-hover ui-state-active ",
19 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",
20 formResetHandler = function() {
21 var buttons = $( this ).find( ":ui-button" );
22 setTimeout(function() {
23 buttons.button( "refresh" );
26 radioGroup = function( radio ) {
27 var name = radio.name,
32 radios = $( form ).find( "[name='" + name + "']" );
34 radios = $( "[name='" + name + "']", radio.ownerDocument )
43 $.widget( "ui.button", {
54 this.element.closest( "form" )
55 .unbind( "reset.button" )
56 .bind( "reset.button", formResetHandler );
58 if ( typeof this.options.disabled !== "boolean" ) {
59 this.options.disabled = !!this.element.propAttr( "disabled" );
61 this.element.propAttr( "disabled", this.options.disabled );
64 this._determineButtonType();
65 this.hasTitle = !!this.buttonElement.attr( "title" );
68 options = this.options,
69 toggleButton = this.type === "checkbox" || this.type === "radio",
70 hoverClass = "ui-state-hover" + ( !toggleButton ? " ui-state-active" : "" ),
71 focusClass = "ui-state-focus";
73 if ( options.label === null ) {
74 options.label = this.buttonElement.html();
78 .addClass( baseClasses )
79 .attr( "role", "button" )
80 .bind( "mouseenter.button", function() {
81 if ( options.disabled ) {
84 $( this ).addClass( "ui-state-hover" );
85 if ( this === lastActive ) {
86 $( this ).addClass( "ui-state-active" );
89 .bind( "mouseleave.button", function() {
90 if ( options.disabled ) {
93 $( this ).removeClass( hoverClass );
95 .bind( "click.button", function( event ) {
96 if ( options.disabled ) {
97 event.preventDefault();
98 event.stopImmediatePropagation();
103 .bind( "focus.button", function() {
104 // no need to check disabled, focus won't be triggered anyway
105 self.buttonElement.addClass( focusClass );
107 .bind( "blur.button", function() {
108 self.buttonElement.removeClass( focusClass );
111 if ( toggleButton ) {
112 this.element.bind( "change.button", function() {
113 if ( clickDragged ) {
118 // if mouse moves between mousedown and mouseup (drag) set clickDragged flag
119 // prevents issue where button state changes but checkbox/radio checked state
120 // does not in Firefox (see ticket #6970)
122 .bind( "mousedown.button", function( event ) {
123 if ( options.disabled ) {
126 clickDragged = false;
127 startXPos = event.pageX;
128 startYPos = event.pageY;
130 .bind( "mouseup.button", function( event ) {
131 if ( options.disabled ) {
134 if ( startXPos !== event.pageX || startYPos !== event.pageY ) {
140 if ( this.type === "checkbox" ) {
141 this.buttonElement.bind( "click.button", function() {
142 if ( options.disabled || clickDragged ) {
145 $( this ).toggleClass( "ui-state-active" );
146 self.buttonElement.attr( "aria-pressed", self.element[0].checked );
148 } else if ( this.type === "radio" ) {
149 this.buttonElement.bind( "click.button", function() {
150 if ( options.disabled || clickDragged ) {
153 $( this ).addClass( "ui-state-active" );
154 self.buttonElement.attr( "aria-pressed", "true" );
156 var radio = self.element[ 0 ];
160 return $( this ).button( "widget" )[ 0 ];
162 .removeClass( "ui-state-active" )
163 .attr( "aria-pressed", "false" );
167 .bind( "mousedown.button", function() {
168 if ( options.disabled ) {
171 $( this ).addClass( "ui-state-active" );
173 $( document ).one( "mouseup", function() {
177 .bind( "mouseup.button", function() {
178 if ( options.disabled ) {
181 $( this ).removeClass( "ui-state-active" );
183 .bind( "keydown.button", function(event) {
184 if ( options.disabled ) {
187 if ( event.keyCode == $.ui.keyCode.SPACE || event.keyCode == $.ui.keyCode.ENTER ) {
188 $( this ).addClass( "ui-state-active" );
191 .bind( "keyup.button", function() {
192 $( this ).removeClass( "ui-state-active" );
195 if ( this.buttonElement.is("a") ) {
196 this.buttonElement.keyup(function(event) {
197 if ( event.keyCode === $.ui.keyCode.SPACE ) {
198 // TODO pass through original event correctly (just as 2nd argument doesn't work)
205 // TODO: pull out $.Widget's handling for the disabled option into
206 // $.Widget.prototype._setOptionDisabled so it's easy to proxy and can
207 // be overridden by individual plugins
208 this._setOption( "disabled", options.disabled );
212 _determineButtonType: function() {
214 if ( this.element.is(":checkbox") ) {
215 this.type = "checkbox";
216 } else if ( this.element.is(":radio") ) {
218 } else if ( this.element.is("input") ) {
221 this.type = "button";
224 if ( this.type === "checkbox" || this.type === "radio" ) {
225 // we don't search against the document in case the element
226 // is disconnected from the DOM
227 var ancestor = this.element.parents().filter(":last"),
228 labelSelector = "label[for='" + this.element.attr("id") + "']";
229 this.buttonElement = ancestor.find( labelSelector );
230 if ( !this.buttonElement.length ) {
231 ancestor = ancestor.length ? ancestor.siblings() : this.element.siblings();
232 this.buttonElement = ancestor.filter( labelSelector );
233 if ( !this.buttonElement.length ) {
234 this.buttonElement = ancestor.find( labelSelector );
237 this.element.addClass( "ui-helper-hidden-accessible" );
239 var checked = this.element.is( ":checked" );
241 this.buttonElement.addClass( "ui-state-active" );
243 this.buttonElement.attr( "aria-pressed", checked );
245 this.buttonElement = this.element;
250 return this.buttonElement;
253 destroy: function() {
255 .removeClass( "ui-helper-hidden-accessible" );
257 .removeClass( baseClasses + " " + stateClasses + " " + typeClasses )
258 .removeAttr( "role" )
259 .removeAttr( "aria-pressed" )
260 .html( this.buttonElement.find(".ui-button-text").html() );
262 if ( !this.hasTitle ) {
263 this.buttonElement.removeAttr( "title" );
266 $.Widget.prototype.destroy.call( this );
269 _setOption: function( key, value ) {
270 $.Widget.prototype._setOption.apply( this, arguments );
271 if ( key === "disabled" ) {
273 this.element.propAttr( "disabled", true );
275 this.element.propAttr( "disabled", false );
282 refresh: function() {
283 var isDisabled = this.element.is( ":disabled" );
284 if ( isDisabled !== this.options.disabled ) {
285 this._setOption( "disabled", isDisabled );
287 if ( this.type === "radio" ) {
288 radioGroup( this.element[0] ).each(function() {
289 if ( $( this ).is( ":checked" ) ) {
290 $( this ).button( "widget" )
291 .addClass( "ui-state-active" )
292 .attr( "aria-pressed", "true" );
294 $( this ).button( "widget" )
295 .removeClass( "ui-state-active" )
296 .attr( "aria-pressed", "false" );
299 } else if ( this.type === "checkbox" ) {
300 if ( this.element.is( ":checked" ) ) {
302 .addClass( "ui-state-active" )
303 .attr( "aria-pressed", "true" );
306 .removeClass( "ui-state-active" )
307 .attr( "aria-pressed", "false" );
312 _resetButton: function() {
313 if ( this.type === "input" ) {
314 if ( this.options.label ) {
315 this.element.val( this.options.label );
319 var buttonElement = this.buttonElement.removeClass( typeClasses ),
320 buttonText = $( "<span></span>", this.element[0].ownerDocument )
321 .addClass( "ui-button-text" )
322 .html( this.options.label )
323 .appendTo( buttonElement.empty() )
325 icons = this.options.icons,
326 multipleIcons = icons.primary && icons.secondary,
329 if ( icons.primary || icons.secondary ) {
330 if ( this.options.text ) {
331 buttonClasses.push( "ui-button-text-icon" + ( multipleIcons ? "s" : ( icons.primary ? "-primary" : "-secondary" ) ) );
334 if ( icons.primary ) {
335 buttonElement.prepend( "<span class='ui-button-icon-primary ui-icon " + icons.primary + "'></span>" );
338 if ( icons.secondary ) {
339 buttonElement.append( "<span class='ui-button-icon-secondary ui-icon " + icons.secondary + "'></span>" );
342 if ( !this.options.text ) {
343 buttonClasses.push( multipleIcons ? "ui-button-icons-only" : "ui-button-icon-only" );
345 if ( !this.hasTitle ) {
346 buttonElement.attr( "title", buttonText );
350 buttonClasses.push( "ui-button-text-only" );
352 buttonElement.addClass( buttonClasses.join( " " ) );
356 $.widget( "ui.buttonset", {
358 items: ":button, :submit, :reset, :checkbox, :radio, a, :data(button)"
361 _create: function() {
362 this.element.addClass( "ui-buttonset" );
369 _setOption: function( key, value ) {
370 if ( key === "disabled" ) {
371 this.buttons.button( "option", key, value );
374 $.Widget.prototype._setOption.apply( this, arguments );
377 refresh: function() {
378 var rtl = this.element.css( "direction" ) === "rtl";
380 this.buttons = this.element.find( this.options.items )
381 .filter( ":ui-button" )
388 return $( this ).button( "widget" )[ 0 ];
390 .removeClass( "ui-corner-all ui-corner-left ui-corner-right" )
392 .addClass( rtl ? "ui-corner-right" : "ui-corner-left" )
395 .addClass( rtl ? "ui-corner-left" : "ui-corner-right" )
400 destroy: function() {
401 this.element.removeClass( "ui-buttonset" );
404 return $( this ).button( "widget" )[ 0 ];
406 .removeClass( "ui-corner-left ui-corner-right" )
408 .button( "destroy" );
410 $.Widget.prototype.destroy.call( this );