2010-01-07 03:19:50 +00:00
|
|
|
/*
|
|
|
|
* jQuery UI Button @VERSION
|
|
|
|
*
|
2010-07-14 19:45:45 +00:00
|
|
|
* Copyright 2010, AUTHORS.txt (http://jqueryui.com/about)
|
2010-07-09 13:01:04 +00:00
|
|
|
* Dual licensed under the MIT or GPL Version 2 licenses.
|
|
|
|
* http://jquery.org/license
|
2010-01-07 03:19:50 +00:00
|
|
|
*
|
|
|
|
* http://docs.jquery.com/UI/Button
|
|
|
|
*
|
|
|
|
* Depends:
|
|
|
|
* jquery.ui.core.js
|
|
|
|
* jquery.ui.widget.js
|
|
|
|
*/
|
2010-07-13 13:57:58 +00:00
|
|
|
(function( $, undefined ) {
|
2010-01-07 03:19:50 +00:00
|
|
|
|
|
|
|
var lastActive,
|
|
|
|
baseClasses = "ui-button ui-widget ui-state-default ui-corner-all",
|
2010-04-30 01:38:23 +00:00
|
|
|
stateClasses = "ui-state-hover ui-state-active ",
|
2010-05-26 19:08:37 +00:00
|
|
|
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",
|
2010-03-11 20:10:43 +00:00
|
|
|
formResetHandler = function( event ) {
|
|
|
|
$( ":ui-button", event.target.form ).each(function() {
|
|
|
|
var inst = $( this ).data( "button" );
|
2010-03-11 06:31:16 +00:00
|
|
|
setTimeout(function() {
|
2010-03-11 20:10:43 +00:00
|
|
|
inst.refresh();
|
|
|
|
}, 1 );
|
2010-03-11 06:31:16 +00:00
|
|
|
});
|
|
|
|
},
|
2010-03-11 20:10:43 +00:00
|
|
|
radioGroup = function( radio ) {
|
2010-03-11 06:31:16 +00:00
|
|
|
var name = radio.name,
|
|
|
|
form = radio.form,
|
2010-03-11 20:10:43 +00:00
|
|
|
radios = $( [] );
|
2010-03-11 06:31:16 +00:00
|
|
|
if ( name ) {
|
|
|
|
if ( form ) {
|
|
|
|
radios = $( form ).find( "[name='" + name + "']" );
|
|
|
|
} else {
|
|
|
|
radios = $( "[name='" + name + "']", radio.ownerDocument )
|
|
|
|
.filter(function() {
|
|
|
|
return !this.form;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return radios;
|
|
|
|
};
|
2010-01-07 03:19:50 +00:00
|
|
|
|
2010-01-18 01:58:15 +00:00
|
|
|
$.widget( "ui.button", {
|
2010-01-07 03:19:50 +00:00
|
|
|
options: {
|
2010-08-18 18:51:30 +00:00
|
|
|
disabled: null,
|
2010-01-07 03:19:50 +00:00
|
|
|
text: true,
|
|
|
|
label: null,
|
|
|
|
icons: {
|
|
|
|
primary: null,
|
|
|
|
secondary: null
|
|
|
|
}
|
|
|
|
},
|
2010-01-15 18:58:20 +00:00
|
|
|
_create: function() {
|
2010-03-11 20:10:43 +00:00
|
|
|
this.element.closest( "form" )
|
|
|
|
.unbind( "reset.button" )
|
|
|
|
.bind( "reset.button", formResetHandler );
|
|
|
|
|
2010-08-18 18:51:30 +00:00
|
|
|
if ( typeof this.options.disabled !== "boolean" ) {
|
|
|
|
this.options.disabled = this.element.attr( "disabled" );
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2010-03-16 04:07:23 +00:00
|
|
|
if ( this.element.is( ":disabled" ) ) {
|
2010-03-15 14:00:53 +00:00
|
|
|
options.disabled = true;
|
|
|
|
}
|
|
|
|
|
2010-01-07 03:19:50 +00:00
|
|
|
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-03-11 20:10:43 +00:00
|
|
|
|
2010-03-11 06:31:16 +00:00
|
|
|
if ( toggleButton ) {
|
2010-03-11 20:10:43 +00:00
|
|
|
this.element.bind( "change.button", function() {
|
2010-03-11 06:31:16 +00:00
|
|
|
self.refresh();
|
|
|
|
});
|
|
|
|
}
|
2010-01-07 03:19:50 +00:00
|
|
|
|
2010-03-14 17:53:18 +00:00
|
|
|
if ( this.type === "checkbox" ) {
|
2010-01-18 01:58:15 +00:00
|
|
|
this.buttonElement.bind( "click.button", function() {
|
|
|
|
if ( options.disabled ) {
|
2010-03-15 14:00:53 +00:00
|
|
|
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-03-14 17:53:18 +00:00
|
|
|
} else if ( this.type === "radio" ) {
|
2010-01-18 01:58:15 +00:00
|
|
|
this.buttonElement.bind( "click.button", function() {
|
|
|
|
if ( options.disabled ) {
|
2010-03-15 14:00:53 +00:00
|
|
|
return false;
|
2010-01-18 01:58:15 +00:00
|
|
|
}
|
|
|
|
$( this ).addClass( "ui-state-active" );
|
|
|
|
self.buttonElement.attr( "aria-pressed", true );
|
|
|
|
|
2010-03-11 06:31:16 +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 ) {
|
2010-03-15 14:00:53 +00:00
|
|
|
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-07 03:19:50 +00:00
|
|
|
});
|
2010-01-18 01:58:15 +00:00
|
|
|
})
|
|
|
|
.bind( "mouseup.button", function() {
|
|
|
|
if ( options.disabled ) {
|
2010-03-15 14:00:53 +00:00
|
|
|
return false;
|
2010-01-18 01:58:15 +00:00
|
|
|
}
|
|
|
|
$( this ).removeClass( "ui-state-active" );
|
2010-01-22 22:46:32 +00:00
|
|
|
})
|
|
|
|
.bind( "keydown.button", function(event) {
|
2010-03-14 17:53:18 +00:00
|
|
|
if ( options.disabled ) {
|
2010-03-15 14:00:53 +00:00
|
|
|
return false;
|
2010-03-14 17:53:18 +00:00
|
|
|
}
|
|
|
|
if ( event.keyCode == $.ui.keyCode.SPACE || event.keyCode == $.ui.keyCode.ENTER ) {
|
2010-01-22 22:46:32 +00:00
|
|
|
$( 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
|
|
|
}
|
2010-01-07 03:19:50 +00:00
|
|
|
}
|
|
|
|
|
2010-03-02 05:29:35 +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
|
2010-03-14 17:53:18 +00:00
|
|
|
this._setOption( "disabled", options.disabled );
|
2010-01-07 03:19:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_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 {
|
2010-03-17 12:56:15 +00:00
|
|
|
if ( this.element.is("input") ) {
|
2010-03-11 21:17:28 +00:00
|
|
|
this.type = "input";
|
|
|
|
} else {
|
|
|
|
this.type = "button";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-18 01:58:15 +00:00
|
|
|
if ( this.type === "checkbox" || this.type === "radio" ) {
|
2010-03-11 03:50:20 +00:00
|
|
|
// we don't search against the document in case the element
|
|
|
|
// is disconnected from the DOM
|
|
|
|
this.buttonElement = this.element.parents().last()
|
2010-07-16 15:48:40 +00:00
|
|
|
.find( "label[for=" + this.element.attr("id") + "]" );
|
2010-03-17 12:56:15 +00:00
|
|
|
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" );
|
2010-01-07 03:19:50 +00:00
|
|
|
}
|
2010-02-18 01:45:23 +00:00
|
|
|
this.buttonElement.attr( "aria-pressed", checked );
|
2010-01-07 03:19:50 +00:00
|
|
|
} else {
|
|
|
|
this.buttonElement = this.element;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
widget: function() {
|
2010-07-30 16:38:18 +00:00
|
|
|
return this.buttonElement;
|
2010-01-07 03:19:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function() {
|
2010-03-17 12:49:27 +00:00
|
|
|
this.element
|
|
|
|
.removeClass( "ui-helper-hidden-accessible" );
|
2010-01-07 03:19:50 +00:00
|
|
|
this.buttonElement
|
2010-04-30 01:38:23 +00:00
|
|
|
.removeClass( baseClasses + " " + stateClasses + " " + typeClasses )
|
2010-01-18 01:58:15 +00:00
|
|
|
.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
|
|
|
$.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-03-15 14:00:53 +00:00
|
|
|
if ( key === "disabled" ) {
|
2010-03-15 14:07:00 +00:00
|
|
|
if ( value ) {
|
|
|
|
this.element.attr( "disabled", true );
|
|
|
|
} else {
|
|
|
|
this.element.removeAttr( "disabled" );
|
|
|
|
}
|
2010-03-15 14:00:53 +00:00
|
|
|
}
|
2010-01-07 03:19:50 +00:00
|
|
|
this._resetButton();
|
|
|
|
},
|
2010-03-11 20:10:43 +00:00
|
|
|
|
2010-03-11 06:31:16 +00:00
|
|
|
refresh: function() {
|
2010-03-16 04:07:23 +00:00
|
|
|
var isDisabled = this.element.is( ":disabled" );
|
2010-03-15 14:13:17 +00:00
|
|
|
if ( isDisabled !== this.options.disabled ) {
|
|
|
|
this._setOption( "disabled", isDisabled );
|
|
|
|
}
|
2010-03-11 06:31:16 +00:00
|
|
|
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 );
|
2010-03-11 06:31:16 +00:00
|
|
|
} else {
|
2010-03-11 20:10:43 +00:00
|
|
|
$( this ).button( "widget" )
|
|
|
|
.removeClass( "ui-state-active" )
|
|
|
|
.attr( "aria-pressed", false );
|
2010-03-11 06:31:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if ( this.type === "checkbox" ) {
|
2010-03-11 20:10:43 +00:00
|
|
|
if ( this.element.is( ":checked" ) ) {
|
2010-03-11 06:31:16 +00:00
|
|
|
this.buttonElement
|
2010-03-11 20:10:43 +00:00
|
|
|
.addClass( "ui-state-active" )
|
|
|
|
.attr( "aria-pressed", true );
|
2010-03-11 06:31:16 +00:00
|
|
|
} else {
|
|
|
|
this.buttonElement
|
2010-03-11 20:10:43 +00:00
|
|
|
.removeClass( "ui-state-active" )
|
|
|
|
.attr( "aria-pressed", false );
|
2010-03-11 06:31:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2010-01-07 03:19:50 +00:00
|
|
|
|
|
|
|
_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;
|
|
|
|
}
|
2010-04-30 01:38:23 +00:00
|
|
|
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,
|
2010-01-07 03:19:50 +00:00
|
|
|
multipleIcons = icons.primary && icons.secondary;
|
2010-01-18 01:58:15 +00:00
|
|
|
if ( icons.primary || icons.secondary ) {
|
|
|
|
buttonElement.addClass( "ui-button-text-icon" +
|
2010-05-26 19:08:37 +00:00
|
|
|
( 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-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" )
|
2010-05-26 19:08:37 +00:00
|
|
|
.removeClass( "ui-button-text-icons ui-button-text-icon-primary ui-button-text-icon-secondary" );
|
2010-01-18 01:58:15 +00:00
|
|
|
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-03-17 13:16:40 +00:00
|
|
|
this.element.addClass( "ui-buttonset" );
|
2010-03-17 13:07:46 +00:00
|
|
|
this._init();
|
|
|
|
},
|
|
|
|
|
|
|
|
_init: function() {
|
|
|
|
this.refresh();
|
|
|
|
},
|
|
|
|
|
|
|
|
_setOption: function( key, value ) {
|
|
|
|
if ( key === "disabled" ) {
|
|
|
|
this.buttons.button( "option", key, value );
|
|
|
|
}
|
|
|
|
|
|
|
|
$.Widget.prototype._setOption.apply( this, arguments );
|
|
|
|
},
|
|
|
|
|
|
|
|
refresh: function() {
|
2010-03-02 02:58:52 +00:00
|
|
|
this.buttons = this.element.find( ":button, :submit, :reset, :checkbox, :radio, a, :data(button)" )
|
2010-03-17 13:07:46 +00:00
|
|
|
.filter( ":ui-button" )
|
|
|
|
.button( "refresh" )
|
|
|
|
.end()
|
|
|
|
.not( ":ui-button" )
|
|
|
|
.button()
|
|
|
|
.end()
|
2010-01-07 03:19:50 +00:00
|
|
|
.map(function() {
|
2010-01-18 01:58:15 +00:00
|
|
|
return $( this ).button( "widget" )[ 0 ];
|
2010-01-07 03:19:50 +00:00
|
|
|
})
|
2010-03-17 13:07:46 +00:00
|
|
|
.removeClass( "ui-corner-all ui-corner-left ui-corner-right" )
|
2010-08-18 14:13:57 +00:00
|
|
|
.filter( ":visible" )
|
|
|
|
.filter( ":first" )
|
|
|
|
.addClass( "ui-corner-left" )
|
|
|
|
.end()
|
|
|
|
.filter( ":last" )
|
|
|
|
.addClass( "ui-corner-right" )
|
|
|
|
.end()
|
2010-01-07 03:19:50 +00:00
|
|
|
.end()
|
|
|
|
.end();
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function() {
|
2010-03-17 13:16:40 +00:00
|
|
|
this.element.removeClass( "ui-buttonset" );
|
2010-01-07 03:19:50 +00:00
|
|
|
this.buttons
|
2010-03-17 13:03:23 +00:00
|
|
|
.map(function() {
|
|
|
|
return $( this ).button( "widget" )[ 0 ];
|
|
|
|
})
|
|
|
|
.removeClass( "ui-corner-left ui-corner-right" )
|
|
|
|
.end()
|
2010-04-23 16:08:55 +00:00
|
|
|
.button( "destroy" );
|
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-03-14 17:53:18 +00:00
|
|
|
}( jQuery ) );
|