2010-01-07 03:19:50 +00:00
|
|
|
/*
|
|
|
|
* jQuery UI Button @VERSION
|
|
|
|
*
|
2010-01-20 14:04:26 +00:00
|
|
|
* Copyright (c) 2010 AUTHORS.txt (http://jqueryui.com/about)
|
2010-01-07 03:19:50 +00:00
|
|
|
* Dual licensed under the MIT (MIT-LICENSE.txt)
|
|
|
|
* and GPL (GPL-LICENSE.txt) licenses.
|
|
|
|
*
|
|
|
|
* http://docs.jquery.com/UI/Button
|
|
|
|
*
|
|
|
|
* Depends:
|
|
|
|
* jquery.ui.core.js
|
|
|
|
* jquery.ui.widget.js
|
|
|
|
*/
|
2010-01-18 01:58:15 +00:00
|
|
|
(function( $ ) {
|
2010-01-07 03:19:50 +00:00
|
|
|
|
|
|
|
var lastActive,
|
|
|
|
baseClasses = "ui-button ui-widget ui-state-default ui-corner-all",
|
|
|
|
otherClasses = "ui-state-hover ui-state-active " +
|
|
|
|
"ui-button-icons-only ui-button-icon-only ui-button-text-icons ui-button-text-icon ui-button-text-only";
|
|
|
|
|
2010-01-18 01:58:15 +00:00
|
|
|
$.widget( "ui.button", {
|
2010-01-07 03:19:50 +00:00
|
|
|
options: {
|
|
|
|
text: true,
|
|
|
|
label: null,
|
|
|
|
icons: {
|
|
|
|
primary: null,
|
|
|
|
secondary: null
|
|
|
|
}
|
|
|
|
},
|
2010-01-15 18:58:20 +00:00
|
|
|
_create: function() {
|
2010-01-07 03:19:50 +00:00
|
|
|
this._determineButtonType();
|
2010-01-18 01:58:15 +00:00
|
|
|
this.hasTitle = !!this.buttonElement.attr( "title" );
|
2010-01-07 03:19:50 +00:00
|
|
|
|
|
|
|
var self = this,
|
|
|
|
options = this.options,
|
2010-01-18 01:58:15 +00:00
|
|
|
toggleButton = this.type === "checkbox" || this.type === "radio",
|
2010-01-20 17:44:54 +00:00
|
|
|
hoverClass = "ui-state-hover" + ( !toggleButton ? " ui-state-active" : "" ),
|
|
|
|
focusClass = "ui-state-focus";
|
2010-01-07 03:19:50 +00:00
|
|
|
|
2010-01-18 01:58:15 +00:00
|
|
|
if ( options.label === null ) {
|
2010-01-07 03:19:50 +00:00
|
|
|
options.label = this.buttonElement.html();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.buttonElement
|
2010-01-18 01:58:15 +00:00
|
|
|
.addClass( baseClasses )
|
|
|
|
.attr( "role", "button" )
|
|
|
|
.bind( "mouseenter.button", function() {
|
|
|
|
if ( options.disabled ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$( this ).addClass( "ui-state-hover" );
|
|
|
|
if ( this === lastActive ) {
|
|
|
|
$( this ).addClass( "ui-state-active" );
|
2010-01-07 03:19:50 +00:00
|
|
|
}
|
|
|
|
})
|
2010-01-18 01:58:15 +00:00
|
|
|
.bind( "mouseleave.button", function() {
|
|
|
|
if ( options.disabled ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$( this ).removeClass( hoverClass );
|
2010-01-20 17:44:54 +00:00
|
|
|
})
|
|
|
|
.bind( "focus.button", function() {
|
|
|
|
// no need to check disabled, focus won't be triggered anyway
|
|
|
|
$( this ).addClass( focusClass );
|
|
|
|
})
|
|
|
|
.bind( "blur.button", function() {
|
|
|
|
$( this ).removeClass( focusClass );
|
2010-01-07 03:19:50 +00:00
|
|
|
});
|
|
|
|
|
2010-01-18 01:58:15 +00:00
|
|
|
if ( this.type === "checkbox") {
|
|
|
|
this.buttonElement.bind( "click.button", function() {
|
|
|
|
if ( options.disabled ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$( this ).toggleClass( "ui-state-active" );
|
|
|
|
self.element
|
|
|
|
.attr( "checked", !self.element[0].checked )
|
|
|
|
.click();
|
|
|
|
self.buttonElement.attr( "aria-pressed", self.element[0].checked );
|
|
|
|
});
|
|
|
|
} else if ( this.type === "radio") {
|
|
|
|
this.buttonElement.bind( "click.button", function() {
|
|
|
|
if ( options.disabled ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$( this ).addClass( "ui-state-active" );
|
|
|
|
self.element
|
|
|
|
.attr( "checked", true )
|
|
|
|
.click();
|
|
|
|
self.buttonElement.attr( "aria-pressed", true );
|
|
|
|
|
|
|
|
var radio = self.element[ 0 ],
|
|
|
|
name = radio.name,
|
|
|
|
form = radio.form,
|
|
|
|
radios;
|
|
|
|
if ( name ) {
|
|
|
|
if ( form ) {
|
|
|
|
radios = $( form ).find( "[name=" + name + "]" );
|
|
|
|
} else {
|
|
|
|
radios = $( "[name=" + name + "]", radio.ownerDocument )
|
|
|
|
.filter(function() {
|
|
|
|
return !this.form;
|
|
|
|
});
|
2010-01-07 03:19:50 +00:00
|
|
|
}
|
2010-01-18 01:58:15 +00:00
|
|
|
radios
|
|
|
|
.not( radio )
|
|
|
|
.map(function() {
|
|
|
|
return $( this ).button( "widget" )[ 0 ];
|
|
|
|
})
|
|
|
|
.removeClass( "ui-state-active" )
|
|
|
|
.attr( "aria-pressed", false );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.buttonElement
|
|
|
|
.bind( "mousedown.button", function() {
|
|
|
|
if ( options.disabled ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$( this ).addClass( "ui-state-active" );
|
|
|
|
lastActive = this;
|
|
|
|
$( document ).one( "mouseup", function() {
|
|
|
|
lastActive = null;
|
2010-01-07 03:19:50 +00:00
|
|
|
});
|
2010-01-18 01:58:15 +00:00
|
|
|
})
|
|
|
|
.bind( "mouseup.button", function() {
|
|
|
|
if ( options.disabled ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$( this ).removeClass( "ui-state-active" );
|
|
|
|
});
|
2010-01-07 03:19:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this._resetButton();
|
|
|
|
},
|
|
|
|
|
|
|
|
_determineButtonType: function() {
|
2010-01-18 01:58:15 +00:00
|
|
|
this.type = this.element.is( ":checkbox" )
|
|
|
|
? "checkbox"
|
|
|
|
: this.element.is( ":radio" )
|
|
|
|
? "radio"
|
|
|
|
: this.element.is( "input" )
|
|
|
|
? "input"
|
|
|
|
: "button";
|
|
|
|
|
|
|
|
if ( this.type === "checkbox" || this.type === "radio" ) {
|
|
|
|
this.buttonElement = $( "[for=" + this.element.attr("id") + "]" );
|
2010-01-07 03:19:50 +00:00
|
|
|
this.element.hide();
|
2010-01-18 01:58:15 +00:00
|
|
|
|
|
|
|
var checked = this.element.is( ":checked" );
|
|
|
|
if ( checked ) {
|
|
|
|
this.buttonElement.addClass( "ui-state-active" );
|
2010-01-07 03:19:50 +00:00
|
|
|
}
|
2010-01-18 01:58:15 +00:00
|
|
|
this.buttonElement.attr( "aria-pressed", checked )
|
2010-01-07 03:19:50 +00:00
|
|
|
} else {
|
|
|
|
this.buttonElement = this.element;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
widget: function() {
|
|
|
|
return this.buttonElement;
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function() {
|
|
|
|
this.buttonElement
|
2010-01-18 01:58:15 +00:00
|
|
|
.removeClass( baseClasses + " " + otherClasses )
|
|
|
|
.removeAttr( "role" )
|
|
|
|
.removeAttr( "aria-pressed" )
|
|
|
|
.html( this.buttonElement.find(".ui-button-text").html() );
|
2010-01-07 03:19:50 +00:00
|
|
|
|
2010-01-18 01:58:15 +00:00
|
|
|
if ( !this.hasTitle ) {
|
|
|
|
this.buttonElement.removeAttr( "title" );
|
2010-01-07 03:19:50 +00:00
|
|
|
}
|
|
|
|
|
2010-01-18 01:58:15 +00:00
|
|
|
if ( this.type === "checkbox" || this.type === "radio" ) {
|
2010-01-07 03:19:50 +00:00
|
|
|
this.element.show();
|
|
|
|
}
|
|
|
|
|
2010-01-18 01:58:15 +00:00
|
|
|
$.Widget.prototype.destroy.call( this );
|
2010-01-07 03:19:50 +00:00
|
|
|
},
|
|
|
|
|
2010-01-18 01:58:15 +00:00
|
|
|
_setOption: function( key, value ) {
|
|
|
|
$.Widget.prototype._setOption.apply( this, arguments );
|
2010-01-07 03:19:50 +00:00
|
|
|
this._resetButton();
|
|
|
|
},
|
|
|
|
|
|
|
|
_resetButton: function() {
|
2010-01-18 01:58:15 +00:00
|
|
|
if ( this.type === "input" ) {
|
|
|
|
if ( this.options.label ) {
|
|
|
|
this.element.val( this.options.label );
|
2010-01-07 03:19:50 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var buttonElement = this.buttonElement,
|
2010-01-18 01:58:15 +00:00
|
|
|
buttonText = $( "<span></span>" )
|
|
|
|
.addClass( "ui-button-text" )
|
|
|
|
.html( this.options.label )
|
|
|
|
.appendTo( buttonElement.empty() )
|
2010-01-07 03:19:50 +00:00
|
|
|
.text();
|
|
|
|
|
|
|
|
var icons = this.options.icons,
|
|
|
|
multipleIcons = icons.primary && icons.secondary;
|
2010-01-18 01:58:15 +00:00
|
|
|
if ( icons.primary || icons.secondary ) {
|
|
|
|
buttonElement.addClass( "ui-button-text-icon" +
|
|
|
|
( multipleIcons ? "s" : "" ) );
|
|
|
|
if ( icons.primary ) {
|
|
|
|
buttonElement.prepend( "<span class='ui-button-icon-primary ui-icon " + icons.primary + "'></span>" );
|
2010-01-07 03:19:50 +00:00
|
|
|
}
|
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-07 03:19:50 +00:00
|
|
|
}
|
2010-01-18 01:58:15 +00:00
|
|
|
if ( !this.options.text ) {
|
2010-01-07 03:19:50 +00:00
|
|
|
buttonElement
|
2010-01-18 01:58:15 +00:00
|
|
|
.addClass( multipleIcons ? "ui-button-icons-only" : "ui-button-icon-only" )
|
|
|
|
.removeClass( "ui-button-text-icons ui-button-text-icon" );
|
|
|
|
if ( !this.hasTitle ) {
|
|
|
|
buttonElement.attr( "title", buttonText );
|
2010-01-07 03:19:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2010-01-18 01:58:15 +00:00
|
|
|
buttonElement.addClass( "ui-button-text-only" );
|
2010-01-07 03:19:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-01-18 01:58:15 +00:00
|
|
|
$.widget( "ui.buttonset", {
|
2010-01-15 18:58:20 +00:00
|
|
|
_create: function() {
|
2010-01-18 01:58:15 +00:00
|
|
|
this.element.addClass( "ui-button-set" );
|
|
|
|
this.buttons = this.element.find( ":button, :submit, :reset, :checkbox, :radio, a, .ui-button" )
|
2010-01-07 03:19:50 +00:00
|
|
|
.button()
|
|
|
|
.map(function() {
|
2010-01-18 01:58:15 +00:00
|
|
|
return $( this ).button( "widget" )[ 0 ];
|
2010-01-07 03:19:50 +00:00
|
|
|
})
|
2010-01-18 01:58:15 +00:00
|
|
|
.removeClass( "ui-corner-all" )
|
|
|
|
.filter( ":first" )
|
|
|
|
.addClass( "ui-corner-left" )
|
2010-01-07 03:19:50 +00:00
|
|
|
.end()
|
2010-01-18 01:58:15 +00:00
|
|
|
.filter( ":last" )
|
|
|
|
.addClass( "ui-corner-right" )
|
2010-01-07 03:19:50 +00:00
|
|
|
.end()
|
|
|
|
.end();
|
|
|
|
},
|
|
|
|
|
2010-01-18 01:58:15 +00:00
|
|
|
_setOption: function( key, value ) {
|
|
|
|
if ( key === "disabled" ) {
|
|
|
|
this.buttons.button( "option", key, value );
|
2010-01-07 03:19:50 +00:00
|
|
|
}
|
|
|
|
|
2010-01-18 01:58:15 +00:00
|
|
|
$.Widget.prototype._setOption.apply( this, arguments );
|
2010-01-07 03:19:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function() {
|
2010-01-18 01:58:15 +00:00
|
|
|
this.element.removeClass( "ui-button-set" );
|
2010-01-07 03:19:50 +00:00
|
|
|
this.buttons
|
2010-01-18 01:58:15 +00:00
|
|
|
.button( "destroy" )
|
|
|
|
.removeClass( "ui-corner-left ui-corner-right" );
|
2010-01-07 03:19:50 +00:00
|
|
|
|
2010-01-18 01:58:15 +00:00
|
|
|
$.Widget.prototype.destroy.call( this );
|
2010-01-07 03:19:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2010-01-18 01:58:15 +00:00
|
|
|
})( jQuery );
|