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

417 lines
11 KiB
JavaScript
Raw Normal View History

/*
* jQuery UI Button @VERSION
*
2011-01-17 14:13:18 +00:00
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* http://docs.jquery.com/UI/Button
*
* Depends:
* jquery.ui.core.js
* jquery.ui.widget.js
*/
(function( $, undefined ) {
var lastActive, startXPos, startYPos, clickDragged,
baseClasses = "ui-button ui-widget ui-state-default ui-corner-all",
stateClasses = "ui-state-hover ui-state-active ",
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 buttons = $( this ).find( ":ui-button" );
setTimeout(function() {
buttons.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 ) {
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.button" )
.bind( "reset.button", formResetHandler );
if ( typeof this.options.disabled !== "boolean" ) {
2011-06-08 21:02:57 +00:00
this.options.disabled = this.element.prop( "disabled" );
}
this._determineButtonType();
2010-01-18 01:58:15 +00:00
this.hasTitle = !!this.buttonElement.attr( "title" );
var self = this,
options = this.options,
2010-01-18 01:58:15 +00:00
toggleButton = this.type === "checkbox" || this.type === "radio",
hoverClass = "ui-state-hover" + ( !toggleButton ? " ui-state-active" : "" ),
focusClass = "ui-state-focus";
2010-01-18 01:58:15 +00:00
if ( options.label === null ) {
options.label = this.buttonElement.html();
}
2010-03-16 04:07:23 +00:00
if ( this.element.is( ":disabled" ) ) {
options.disabled = true;
}
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-18 01:58:15 +00:00
.bind( "mouseleave.button", function() {
if ( options.disabled ) {
return;
}
$( this ).removeClass( hoverClass );
})
.bind( "click.button", function( event ) {
if ( options.disabled ) {
event.preventDefault();
event.stopImmediatePropagation();
}
});
2010-03-11 20:10:43 +00:00
this.element
.bind( "focus.button", function() {
// no need to check disabled, focus won't be triggered anyway
self.buttonElement.addClass( focusClass );
})
.bind( "blur.button", function() {
self.buttonElement.removeClass( focusClass );
});
if ( toggleButton ) {
2010-03-11 20:10:43 +00:00
this.element.bind( "change.button", function() {
if ( clickDragged ) {
return;
}
self.refresh();
});
// if mouse moves between mousedown and mouseup (drag) set clickDragged flag
// prevents issue where button state changes but checkbox/radio checked state
// does not in Firefox (see ticket #6970)
this.buttonElement
.bind( "mousedown.button", function( event ) {
if ( options.disabled ) {
return;
}
clickDragged = false;
startXPos = event.pageX;
startYPos = event.pageY;
})
.bind( "mouseup.button", function( event ) {
if ( options.disabled ) {
return;
}
if ( startXPos !== event.pageX || startYPos !== event.pageY ) {
clickDragged = true;
}
});
}
if ( this.type === "checkbox" ) {
2010-01-18 01:58:15 +00:00
this.buttonElement.bind( "click.button", function() {
if ( options.disabled || clickDragged ) {
return false;
2010-01-18 01:58:15 +00:00
}
$( this ).toggleClass( "ui-state-active" );
self.buttonElement.attr( "aria-pressed", self.element[0].checked );
2010-01-18 01:58:15 +00:00
});
} else if ( this.type === "radio" ) {
2010-01-18 01:58:15 +00:00
this.buttonElement.bind( "click.button", function() {
if ( options.disabled || clickDragged ) {
return false;
2010-01-18 01:58:15 +00:00
}
$( this ).addClass( "ui-state-active" );
self.buttonElement.attr( "aria-pressed", "true" );
2010-01-18 01:58:15 +00:00
var radio = self.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.button", function() {
if ( options.disabled ) {
return false;
2010-01-18 01:58:15 +00:00
}
$( this ).addClass( "ui-state-active" );
lastActive = this;
$( document ).one( "mouseup", function() {
lastActive = null;
});
2010-01-18 01:58:15 +00:00
})
.bind( "mouseup.button", function() {
if ( options.disabled ) {
return false;
2010-01-18 01:58:15 +00:00
}
$( this ).removeClass( "ui-state-active" );
})
.bind( "keydown.button", function(event) {
if ( options.disabled ) {
return false;
}
if ( event.keyCode == $.ui.keyCode.SPACE || event.keyCode == $.ui.keyCode.ENTER ) {
$( this ).addClass( "ui-state-active" );
}
})
.bind( "keyup.button", 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
}
}
// TODO: pull out $.Widget's handling for the disabled option into
// $.Widget.prototype._setOptionDisabled so it's easy to proxy and can
// be overridden by individual plugins
this._setOption( "disabled", options.disabled );
this._resetButton();
},
_determineButtonType: function() {
2010-03-11 21:17:28 +00:00
if ( this.element.is(":checkbox") ) {
this.type = "checkbox";
} else if ( this.element.is(":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
var 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
var checked = this.element.is( ":checked" );
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 + " " + stateClasses + " " + 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( "_setOption", key, value );
if ( key === "disabled" ) {
if ( value ) {
2011-06-08 21:02:57 +00:00
this.element.prop( "disabled", true );
} else {
2011-06-08 21:02:57 +00:00
this.element.prop( "disabled", false );
}
return;
}
this._resetButton();
},
2010-03-11 20:10:43 +00:00
refresh: function() {
2010-03-16 04:07:23 +00:00
var isDisabled = this.element.is( ":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 ),
2010-01-18 01:58:15 +00:00
buttonText = $( "<span></span>" )
.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", buttonText );
}
}
} else {
buttonClasses.push( "ui-button-text-only" );
}
buttonElement.addClass( buttonClasses.join( " " ) );
}
});
$.ui.button.version = "@VERSION";
2010-01-18 01:58:15 +00:00
$.widget( "ui.buttonset", {
options: {
items: ":button, :submit, :reset, :checkbox, :radio, a, :data(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( "_setOption", 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 ) );