jquery-mobile/js/jquery.mobile.buttonMarkup.js

220 lines
6.3 KiB
JavaScript
Raw Permalink Normal View History

//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
2012-01-07 00:28:54 +00:00
//>>description: For making button-like links.
//>>label: Buttons
define( [ "jquery", "./jquery.mobile.core", "./jquery.mobile.vmouse" ], function( $ ) {
//>>excludeEnd("jqmBuildExclude");
( function( $, undefined ) {
2010-09-29 18:56:41 +00:00
$.fn.buttonMarkup = function( options ) {
options = options || {};
for ( var i = 0; i < this.length; i++ ) {
var el = this.eq( i ),
e = el[ 0 ],
o = $.extend( {}, $.fn.buttonMarkup.defaults, {
icon: options.icon !== undefined ? options.icon : el.jqmData( "icon" ),
iconpos: options.iconpos !== undefined ? options.iconpos : el.jqmData( "iconpos" ),
theme: options.theme !== undefined ? options.theme : el.jqmData( "theme" ),
inline: options.inline !== undefined ? options.inline : el.jqmData( "inline" ),
shadow: options.shadow !== undefined ? options.shadow : el.jqmData( "shadow" ),
corners: options.corners !== undefined ? options.corners : el.jqmData( "corners" ),
iconshadow: options.iconshadow !== undefined ? options.iconshadow : el.jqmData( "iconshadow" )
}, options ),
2010-10-22 15:39:26 +00:00
2010-09-29 18:56:41 +00:00
// Classes Defined
innerClass = "ui-btn-inner",
textClass = "ui-btn-text",
buttonClass, iconClass,
// Button inner markup
buttonInner = document.createElement( o.wrapperEls ),
buttonText = document.createElement( o.wrapperEls ),
buttonIcon = o.icon ? document.createElement( "span" ) : null;
// if so, prevent double enhancement, and continue with rest of the elements.
if( e.tagName === "INPUT" && el.jqmData('role') === "button" ) continue;
// if this is a button, check if it's been enhanced and, if not, use the right function
if( e.tagName === "BUTTON" ) {
if ( !$( e.parentNode ).hasClass( "ui-btn" ) ) $( e ).button();
continue;
}
if ( attachEvents ) {
attachEvents();
}
// if not, try to find closest theme container
if ( !o.theme ) {
o.theme = $.mobile.getInheritedTheme( el, "c" );
}
2010-10-22 15:39:26 +00:00
buttonClass = "ui-btn ui-btn-up-" + o.theme;
2011-01-30 08:19:15 +00:00
if ( o.inline ) {
buttonClass += " ui-btn-inline";
}
2011-01-30 08:19:15 +00:00
if ( o.icon ) {
o.icon = "ui-icon-" + o.icon;
o.iconpos = o.iconpos || "left";
2010-09-29 18:56:41 +00:00
iconClass = "ui-icon " + o.icon;
2011-07-06 20:22:53 +00:00
if ( o.iconshadow ) {
2011-01-30 08:19:15 +00:00
iconClass += " ui-icon-shadow";
}
2010-09-29 18:56:41 +00:00
}
2010-10-22 15:39:26 +00:00
if ( o.iconpos ) {
2010-10-05 14:58:58 +00:00
buttonClass += " ui-btn-icon-" + o.iconpos;
2011-01-30 08:19:15 +00:00
if ( o.iconpos == "notext" && !el.attr( "title" ) ) {
el.attr( "title", el.getEncodedText() );
}
}
2011-01-30 08:19:15 +00:00
if ( o.corners ) {
2010-09-29 18:56:41 +00:00
buttonClass += " ui-btn-corner-all";
innerClass += " ui-btn-corner-all";
}
2011-01-30 08:19:15 +00:00
if ( o.shadow ) {
2010-09-29 18:56:41 +00:00
buttonClass += " ui-shadow";
2010-09-10 22:23:13 +00:00
}
2011-01-30 08:19:15 +00:00
e.setAttribute( "data-" + $.mobile.ns + "theme", o.theme );
2011-12-02 01:15:52 +00:00
el.removeClass( "ui-link" ).addClass( buttonClass );
2010-10-22 15:39:26 +00:00
buttonInner.className = innerClass;
buttonText.className = textClass;
buttonInner.appendChild( buttonText );
if ( buttonIcon ) {
buttonIcon.className = iconClass;
buttonInner.appendChild( buttonIcon );
}
while ( e.firstChild ) {
buttonText.appendChild( e.firstChild );
}
2010-10-22 15:39:26 +00:00
e.appendChild( buttonInner );
// TODO obviously it would be nice to pull this element out instead of
// retrieving it from the DOM again, but this change is much less obtrusive
// and 1.0 draws nigh
$.data( e, 'textWrapper', $( buttonText ) );
}
return this;
2010-09-29 18:56:41 +00:00
};
2010-09-10 22:23:13 +00:00
$.fn.buttonMarkup.defaults = {
2010-09-29 18:56:41 +00:00
corners: true,
shadow: true,
iconshadow: true,
inline: false,
wrapperEls: "span"
2010-09-10 22:23:13 +00:00
};
2010-09-29 18:56:41 +00:00
2011-06-22 16:31:49 +00:00
function closestEnabledButton( element ) {
2011-10-21 23:08:28 +00:00
var cname;
2011-10-21 23:08:28 +00:00
while ( element ) {
// Note that we check for typeof className below because the element we
// handed could be in an SVG DOM where className on SVG elements is defined to
// be of a different type (SVGAnimatedString). We only operate on HTML DOM
// elements, so we look for plain "string".
cname = ( typeof element.className === 'string' ) && (element.className + ' ');
if ( cname && cname.indexOf("ui-btn ") > -1 && cname.indexOf("ui-disabled ") < 0 ) {
2011-10-21 23:08:28 +00:00
break;
}
2011-10-21 23:08:28 +00:00
element = element.parentNode;
}
2011-10-21 23:08:28 +00:00
return element;
}
var attachEvents = function() {
var hoverDelay = 200,
hov, foc;
$( document ).bind( {
"vmousedown": function( event ) {
var btn = closestEnabledButton( event.target ),
$btn, theme;
if ( btn ) {
$btn = $( btn );
theme = $btn.attr( "data-" + $.mobile.ns + "theme" );
if( $.support.touch ) {
hov = setTimeout(function() {
$btn.removeClass( "ui-btn-up-" + theme ).addClass( "ui-btn-down-" + theme );
}, hoverDelay );
} else {
$btn.removeClass( "ui-btn-up-" + theme ).addClass( "ui-btn-down-" + theme );
}
}
},
"vmousecancel vmouseup": function( event ) {
var btn = closestEnabledButton( event.target ),
$btn, theme;
if ( btn ) {
$btn = $( btn );
theme = $btn.attr( "data-" + $.mobile.ns + "theme" );
$btn.removeClass( "ui-btn-down-" + theme ).addClass( "ui-btn-up-" + theme );
}
},
"vmouseover focus": function( event ) {
var btn = closestEnabledButton( event.target ),
$btn, theme;
if ( btn ) {
$btn = $( btn );
theme = $btn.attr( "data-" + $.mobile.ns + "theme" );
if( $.support.touch ) {
foc = setTimeout(function() {
$btn.removeClass( "ui-btn-up-" + theme ).addClass( "ui-btn-hover-" + theme );
}, hoverDelay );
} else {
$btn.removeClass( "ui-btn-up-" + theme ).addClass( "ui-btn-hover-" + theme );
}
}
},
"vmouseout blur scrollstart": function( event ) {
var btn = closestEnabledButton( event.target ),
$btn, theme;
if ( btn ) {
$btn = $( btn );
theme = $btn.attr( "data-" + $.mobile.ns + "theme" );
$btn.removeClass( "ui-btn-hover-" + theme + " ui-btn-down-" + theme ).addClass( "ui-btn-up-" + theme );
hov && clearTimeout( hov );
foc && clearTimeout( foc );
}
}
});
attachEvents = null;
2010-10-22 15:39:26 +00:00
};
2010-09-10 22:23:13 +00:00
//links in bars, or those with data-role become buttons
//auto self-init widgets
$( document ).bind( "pagecreate create", function( e ){
$( ":jqmData(role='button'), .ui-bar > a, .ui-header > a, .ui-footer > a, .ui-bar > :jqmData(role='controlgroup') > a", e.target )
.not( ".ui-btn, :jqmData(role='none'), :jqmData(role='nojs')" )
.buttonMarkup();
});
})( jQuery );
//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
2011-10-31 04:59:56 +00:00
});
//>>excludeEnd("jqmBuildExclude");