jquery-mobile/js/jquery.mobile.forms.button.js

125 lines
3.2 KiB
JavaScript
Raw Normal View History

//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
2012-01-07 00:28:54 +00:00
//>>description: Form Buttons
//>>label: links that proxy to native input/buttons
define( [ "jquery", "./jquery.mobile.widget", "./jquery.mobile.buttonMarkup" ], function( $ ) {
//>>excludeEnd("jqmBuildExclude");
(function( $, undefined ) {
$.widget( "mobile.button", $.mobile.widget, {
2010-11-11 03:54:57 +00:00
options: {
theme: null,
2010-11-11 04:08:22 +00:00
icon: null,
iconpos: null,
inline: null,
corners: true,
shadow: true,
iconshadow: true,
initSelector: "button, [type='button'], [type='submit'], [type='reset'], [type='image']"
2010-11-11 03:54:57 +00:00
},
_create: function() {
var $el = this.element,
$button,
o = this.options,
type,
name,
$buttonPlaceholder;
// if this is a link, check if it's been enhanced and, if not, use the right function
if( $el[ 0 ].tagName === "A" ) {
if ( !$el.hasClass( "ui-btn" ) ) $el.buttonMarkup();
return;
}
// Add ARIA role
this.button = $( "<div></div>" )
.text( $el.text() || $el.val() )
.insertBefore( $el )
.buttonMarkup({
theme: o.theme,
2010-11-11 03:54:57 +00:00
icon: o.icon,
iconpos: o.iconpos,
inline: o.inline,
corners: o.corners,
shadow: o.shadow,
iconshadow: o.iconshadow
})
.append( $el.addClass( "ui-btn-hidden" ) );
$button = this.button;
type = $el.attr( "type" );
name = $el.attr( "name" );
// Add hidden input during submit if input type="submit" has a name.
if ( type !== "button" && type !== "reset" && name ) {
$el.bind( "vclick", function() {
// Add hidden input if it doesnt already exist.
if( $buttonPlaceholder === undefined ) {
$buttonPlaceholder = $( "<input>", {
type: "hidden",
name: $el.attr( "name" ),
value: $el.attr( "value" )
}).insertBefore( $el );
// Bind to doc to remove after submit handling
$( document ).one("submit", function(){
$buttonPlaceholder.remove();
// reset the local var so that the hidden input
// will be re-added on subsequent clicks
$buttonPlaceholder = undefined;
});
}
});
}
$el.bind({
focus: function() {
$button.addClass( $.mobile.focusClass );
},
blur: function() {
$button.removeClass( $.mobile.focusClass );
}
});
this.refresh();
},
enable: function() {
this.element.attr( "disabled", false );
this.button.removeClass( "ui-disabled" ).attr( "aria-disabled", false );
return this._setOption( "disabled", false );
},
disable: function() {
this.element.attr( "disabled", true );
this.button.addClass( "ui-disabled" ).attr( "aria-disabled", true );
return this._setOption( "disabled", true );
},
refresh: function() {
var $el = this.element;
2011-10-31 18:44:56 +00:00
if ( $el.prop("disabled") ) {
this.disable();
} else {
this.enable();
}
// the textWrapper is stored as a data element on the button object
// to prevent referencing by it's implementation details (eg 'class')
this.button.data( 'textWrapper' ).text( $el.text() || $el.val() );
}
});
//auto self-init widgets
$( document ).bind( "pagecreate create", function( e ){
2011-10-10 21:12:52 +00:00
$.mobile.button.prototype.enhanceWithin( e.target );
});
})( jQuery );
//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
2011-10-31 04:59:56 +00:00
});
//>>excludeEnd("jqmBuildExclude");