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

136 lines
3.2 KiB
JavaScript
Raw Normal View History

2010-09-10 22:23:13 +00:00
/*
2010-11-10 00:55:52 +00:00
* jQuery Mobile Framework : plugin for making button-like links
2010-09-10 22:23:13 +00:00
* Copyright (c) jQuery Project
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
2011-01-30 08:19:15 +00:00
*/
(function($, undefined ) {
2010-09-29 18:56:41 +00:00
$.fn.buttonMarkup = function( options ){
2010-09-29 18:56:41 +00:00
return this.each( function() {
var el = $( this ),
o = $.extend( {}, $.fn.buttonMarkup.defaults, el.jqmData(), options),
2010-10-22 15:39:26 +00:00
2010-09-29 18:56:41 +00:00
// Classes Defined
2010-10-22 15:39:26 +00:00
buttonClass,
2010-09-29 18:56:41 +00:00
innerClass = "ui-btn-inner",
iconClass;
if ( attachEvents ) {
attachEvents();
}
// if not, try to find closest theme container
if ( !o.theme ) {
2011-01-30 08:19:15 +00:00
var themedParent = el.closest("[class*='ui-bar-'],[class*='ui-body-']");
2010-10-22 15:39:26 +00:00
o.theme = themedParent.length ?
/ui-(bar|body)-([a-z])/.exec( themedParent.attr("class") )[2] :
"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;
if ( o.shadow ) {
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.text() );
}
}
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
2010-09-10 22:23:13 +00:00
el
.attr( "data-" + $.mobile.ns + "theme", o.theme )
.addClass( buttonClass );
2010-10-22 15:39:26 +00:00
var wrap = ("<D class='" + innerClass + "'><D class='ui-btn-text'></D>" +
( o.icon ? "<span class='" + iconClass + "'></span>" : "" ) +
"</D>").replace(/D/g, o.wrapperEls);
2010-10-22 15:39:26 +00:00
el.wrapInner( wrap );
2011-01-30 08:19:15 +00:00
});
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,
wrapperEls: "span"
2010-09-10 22:23:13 +00:00
};
2010-09-29 18:56:41 +00:00
function closestEnabledButton(element)
{
while (element){
var $ele = $(element);
if ($ele.hasClass("ui-btn") && !$ele.hasClass("ui-disabled")){
break;
}
element = element.parentNode;
}
return element;
}
var attachEvents = function() {
$(document).bind({
"vmousedown": function(event) {
var btn = closestEnabledButton(event.target);
if (btn){
var $btn = $(btn),
theme = $btn.attr( "data-" + $.mobile.ns + "theme" );
$btn.removeClass( "ui-btn-up-" + theme ).addClass( "ui-btn-down-" + theme );
}
},
"vmousecancel vmouseup": function(event) {
var btn = closestEnabledButton(event.target);
if (btn){
var $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);
if (btn){
var $btn = $(btn),
theme = $btn.attr( "data-" + $.mobile.ns + "theme" );
$btn.removeClass( "ui-btn-up-" + theme ).addClass( "ui-btn-hover-" + theme );
}
},
"vmouseout blur": function(event) {
var btn = closestEnabledButton(event.target);
if (btn){
var $btn = $(btn),
theme = $btn.attr( "data-" + $.mobile.ns + "theme" );
$btn.removeClass( "ui-btn-hover-" + theme ).addClass( "ui-btn-up-" + theme );
}
}
});
attachEvents = null;
2010-10-22 15:39:26 +00:00
};
2010-09-10 22:23:13 +00:00
})(jQuery);