jquery-ui/ui/jquery.ui.button.js

394 lines
11 KiB
JavaScript
Raw Normal View History

/*!
* jQuery UI Button @VERSION
2012-07-04 13:08:08 +00:00
* http://jqueryui.com
*
2013-01-10 13:52:20 +00:00
* Copyright 2013 jQuery Foundation and other contributors
2012-08-09 14:13:24 +00:00
* Released under the MIT license.
* http://jquery.org/license
*
2012-09-26 23:06:20 +00:00
* http://api.jqueryui.com/button/
*
* Depends:
* jquery.ui.core.js
* jquery.ui.widget.js
*/
(function( $, undefined ) {
var lastActive,
baseClasses = "ui-button ui-widget ui-state-default ui-corner-all",
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",
formResetHandler = function() {
var form = $( this );
setTimeout(function() {
form.find( ":ui-button" ).button( "refresh" );
}, 1 );
},
2010-03-11 20:10:43 +00:00
radioGroup = function( radio ) {
var name = radio.name,
form = radio.form,
2010-03-11 20:10:43 +00:00
radios = $( [] );
if ( name ) {
name = name.replace( /'/g, "\\'" );
if ( form ) {
radios = $( form ).find( "[name='" + name + "']" );
} else {
radios = $( "[name='" + name + "']", radio.ownerDocument )
.filter(function() {
return !this.form;
});
}
}
return radios;
};
2010-01-18 01:58:15 +00:00
$.widget( "ui.button", {
version: "@VERSION",
defaultElement: "<button>",
options: {
disabled: null,
text: true,
label: null,
icons: {
primary: null,
secondary: null
}
},
_create: function() {
2010-03-11 20:10:43 +00:00
this.element.closest( "form" )
.unbind( "reset" + this.eventNamespace )
.bind( "reset" + this.eventNamespace, formResetHandler );
2010-03-11 20:10:43 +00:00
if ( typeof this.options.disabled !== "boolean" ) {
this.options.disabled = !!this.element.prop( "disabled" );
} else {
this.element.prop( "disabled", this.options.disabled );
}
this._determineButtonType();
2010-01-18 01:58:15 +00:00
this.hasTitle = !!this.buttonElement.attr( "title" );
var that = this,
options = this.options,
2010-01-18 01:58:15 +00:00
toggleButton = this.type === "checkbox" || this.type === "radio",
activeClass = !toggleButton ? "ui-state-active" : "";
2010-01-18 01:58:15 +00:00
if ( options.label === null ) {
options.label = (this.type === "input" ? this.buttonElement.val() : this.buttonElement.html());
}
this._hoverable( this.buttonElement );
this.buttonElement
2010-01-18 01:58:15 +00:00
.addClass( baseClasses )
.attr( "role", "button" )
.bind( "mouseenter" + this.eventNamespace, function() {
2010-01-18 01:58:15 +00:00
if ( options.disabled ) {
return;
}
if ( this === lastActive ) {
$( this ).addClass( "ui-state-active" );
}
})
.bind( "mouseleave" + this.eventNamespace, function() {
2010-01-18 01:58:15 +00:00
if ( options.disabled ) {
return;
}
$( this ).removeClass( activeClass );
})
.bind( "click" + this.eventNamespace, function( event ) {
if ( options.disabled ) {
event.preventDefault();
event.stopImmediatePropagation();
}
});
2010-03-11 20:10:43 +00:00
// Can't use _focusable() because the element that receives focus
// and the element that gets the ui-state-focus class are different
this._on({
focus: function() {
this.buttonElement.addClass( "ui-state-focus" );
},
blur: function() {
this.buttonElement.removeClass( "ui-state-focus" );
}
});
if ( toggleButton ) {
this.element.bind( "change" + this.eventNamespace, function() {
that.refresh();
});
}
if ( this.type === "checkbox" ) {
this.buttonElement.bind( "click" + this.eventNamespace, function() {
if ( options.disabled ) {
return false;
2010-01-18 01:58:15 +00:00
}
});
} else if ( this.type === "radio" ) {
this.buttonElement.bind( "click" + this.eventNamespace, function() {
if ( options.disabled ) {
return false;
2010-01-18 01:58:15 +00:00
}
$( this ).addClass( "ui-state-active" );
that.buttonElement.attr( "aria-pressed", "true" );
2010-01-18 01:58:15 +00:00
var radio = that.element[ 0 ];
radioGroup( radio )
.not( radio )
.map(function() {
return $( this ).button( "widget" )[ 0 ];
})
.removeClass( "ui-state-active" )
.attr( "aria-pressed", "false" );
2010-01-18 01:58:15 +00:00
});
} else {
this.buttonElement
.bind( "mousedown" + this.eventNamespace, function() {
2010-01-18 01:58:15 +00:00
if ( options.disabled ) {
return false;
2010-01-18 01:58:15 +00:00
}
$( this ).addClass( "ui-state-active" );
lastActive = this;
that.document.one( "mouseup", function() {
2010-01-18 01:58:15 +00:00
lastActive = null;
});
2010-01-18 01:58:15 +00:00
})
.bind( "mouseup" + this.eventNamespace, function() {
2010-01-18 01:58:15 +00:00
if ( options.disabled ) {
return false;
2010-01-18 01:58:15 +00:00
}
$( this ).removeClass( "ui-state-active" );
})
.bind( "keydown" + this.eventNamespace, function(event) {
if ( options.disabled ) {
return false;
}
2012-04-02 23:12:21 +00:00
if ( event.keyCode === $.ui.keyCode.SPACE || event.keyCode === $.ui.keyCode.ENTER ) {
$( this ).addClass( "ui-state-active" );
}
})
// see #8559, we bind to blur here in case the button element loses
// focus between keydown and keyup, it would be left in an "active" state
.bind( "keyup" + this.eventNamespace + " blur" + this.eventNamespace, function() {
$( this ).removeClass( "ui-state-active" );
2010-01-18 01:58:15 +00:00
});
2010-03-11 20:10:43 +00:00
if ( this.buttonElement.is("a") ) {
2010-01-22 22:52:31 +00:00
this.buttonElement.keyup(function(event) {
2010-03-11 20:10:43 +00:00
if ( event.keyCode === $.ui.keyCode.SPACE ) {
2010-01-22 22:52:31 +00:00
// TODO pass through original event correctly (just as 2nd argument doesn't work)
2010-03-11 20:10:43 +00:00
$( this ).click();
2010-01-22 22:52:31 +00:00
}
2010-02-18 01:45:23 +00:00
});
2010-01-22 22:52:31 +00:00
}
}
this._setOption( "disabled", options.disabled );
this._resetButton();
},
_determineButtonType: function() {
2012-04-02 23:12:21 +00:00
var ancestor, labelSelector, checked;
2012-05-21 16:47:15 +00:00
if ( this.element.is("[type=checkbox]") ) {
2010-03-11 21:17:28 +00:00
this.type = "checkbox";
2012-05-21 16:02:28 +00:00
} else if ( this.element.is("[type=radio]") ) {
this.type = "radio";
} else if ( this.element.is("input") ) {
this.type = "input";
2010-03-11 21:17:28 +00:00
} else {
this.type = "button";
2010-03-11 21:17:28 +00:00
}
2010-01-18 01:58:15 +00:00
if ( this.type === "checkbox" || this.type === "radio" ) {
// we don't search against the document in case the element
// is disconnected from the DOM
2012-04-02 23:12:21 +00:00
ancestor = this.element.parents().last();
labelSelector = "label[for='" + this.element.attr("id") + "']";
this.buttonElement = ancestor.find( labelSelector );
if ( !this.buttonElement.length ) {
ancestor = ancestor.length ? ancestor.siblings() : this.element.siblings();
this.buttonElement = ancestor.filter( labelSelector );
if ( !this.buttonElement.length ) {
this.buttonElement = ancestor.find( labelSelector );
}
}
this.element.addClass( "ui-helper-hidden-accessible" );
2010-01-18 01:58:15 +00:00
2012-04-02 23:12:21 +00:00
checked = this.element.is( ":checked" );
2010-01-18 01:58:15 +00:00
if ( checked ) {
this.buttonElement.addClass( "ui-state-active" );
}
2011-06-08 21:02:57 +00:00
this.buttonElement.prop( "aria-pressed", checked );
} else {
this.buttonElement = this.element;
}
},
widget: function() {
return this.buttonElement;
},
_destroy: function() {
this.element
.removeClass( "ui-helper-hidden-accessible" );
this.buttonElement
.removeClass( baseClasses + " ui-state-active " + typeClasses )
2010-01-18 01:58:15 +00:00
.removeAttr( "role" )
.removeAttr( "aria-pressed" )
2010-01-18 01:58:15 +00:00
.html( this.buttonElement.find(".ui-button-text").html() );
2010-01-18 01:58:15 +00:00
if ( !this.hasTitle ) {
this.buttonElement.removeAttr( "title" );
}
},
2010-01-18 01:58:15 +00:00
_setOption: function( key, value ) {
this._super( key, value );
if ( key === "disabled" ) {
this.widget().toggleClass( "ui-state-disabled", !!value );
this.element.prop( "disabled", !!value );
if ( value ) {
this.buttonElement.removeClass( "ui-state-focus" );
}
return;
}
this._resetButton();
},
2010-03-11 20:10:43 +00:00
refresh: function() {
//See #8237 & #8828
var isDisabled = this.element.is( "input, button" ) ? this.element.is( ":disabled" ) : this.element.hasClass( "ui-button-disabled" );
if ( isDisabled !== this.options.disabled ) {
this._setOption( "disabled", isDisabled );
}
if ( this.type === "radio" ) {
radioGroup( this.element[0] ).each(function() {
2010-03-11 20:10:43 +00:00
if ( $( this ).is( ":checked" ) ) {
$( this ).button( "widget" )
.addClass( "ui-state-active" )
.attr( "aria-pressed", "true" );
} else {
2010-03-11 20:10:43 +00:00
$( this ).button( "widget" )
.removeClass( "ui-state-active" )
.attr( "aria-pressed", "false" );
}
});
} else if ( this.type === "checkbox" ) {
2010-03-11 20:10:43 +00:00
if ( this.element.is( ":checked" ) ) {
this.buttonElement
2010-03-11 20:10:43 +00:00
.addClass( "ui-state-active" )
.attr( "aria-pressed", "true" );
} else {
this.buttonElement
2010-03-11 20:10:43 +00:00
.removeClass( "ui-state-active" )
.attr( "aria-pressed", "false" );
}
}
},
_resetButton: function() {
2010-01-18 01:58:15 +00:00
if ( this.type === "input" ) {
if ( this.options.label ) {
this.element.val( this.options.label );
}
return;
}
var buttonElement = this.buttonElement.removeClass( typeClasses ),
buttonText = $( "<span></span>", this.document[0] )
2010-01-18 01:58:15 +00:00
.addClass( "ui-button-text" )
.html( this.options.label )
.appendTo( buttonElement.empty() )
2010-03-11 21:17:28 +00:00
.text(),
icons = this.options.icons,
multipleIcons = icons.primary && icons.secondary,
buttonClasses = [];
2010-01-18 01:58:15 +00:00
if ( icons.primary || icons.secondary ) {
if ( this.options.text ) {
buttonClasses.push( "ui-button-text-icon" + ( multipleIcons ? "s" : ( icons.primary ? "-primary" : "-secondary" ) ) );
}
2010-01-18 01:58:15 +00:00
if ( icons.primary ) {
buttonElement.prepend( "<span class='ui-button-icon-primary ui-icon " + icons.primary + "'></span>" );
}
2010-01-18 01:58:15 +00:00
if ( icons.secondary ) {
buttonElement.append( "<span class='ui-button-icon-secondary ui-icon " + icons.secondary + "'></span>" );
}
2010-01-18 01:58:15 +00:00
if ( !this.options.text ) {
buttonClasses.push( multipleIcons ? "ui-button-icons-only" : "ui-button-icon-only" );
2010-01-18 01:58:15 +00:00
if ( !this.hasTitle ) {
buttonElement.attr( "title", $.trim( buttonText ) );
}
}
} else {
buttonClasses.push( "ui-button-text-only" );
}
buttonElement.addClass( buttonClasses.join( " " ) );
}
});
2010-01-18 01:58:15 +00:00
$.widget( "ui.buttonset", {
2012-05-21 17:51:49 +00:00
version: "@VERSION",
options: {
items: "button, input[type=button], input[type=submit], input[type=reset], input[type=checkbox], input[type=radio], a, :data(ui-button)"
},
_create: function() {
this.element.addClass( "ui-buttonset" );
},
_init: function() {
this.refresh();
},
_setOption: function( key, value ) {
if ( key === "disabled" ) {
this.buttons.button( "option", key, value );
}
this._super( key, value );
},
refresh: function() {
var rtl = this.element.css( "direction" ) === "rtl";
this.buttons = this.element.find( this.options.items )
.filter( ":ui-button" )
.button( "refresh" )
.end()
.not( ":ui-button" )
.button()
.end()
.map(function() {
2010-01-18 01:58:15 +00:00
return $( this ).button( "widget" )[ 0 ];
})
.removeClass( "ui-corner-all ui-corner-left ui-corner-right" )
.filter( ":first" )
.addClass( rtl ? "ui-corner-right" : "ui-corner-left" )
.end()
.filter( ":last" )
.addClass( rtl ? "ui-corner-left" : "ui-corner-right" )
.end()
.end();
},
_destroy: function() {
this.element.removeClass( "ui-buttonset" );
this.buttons
.map(function() {
return $( this ).button( "widget" )[ 0 ];
})
.removeClass( "ui-corner-left ui-corner-right" )
.end()
.button( "destroy" );
}
});
}( jQuery ) );