/* * jQuery Mobile Framework : "checkboxradio" plugin * Copyright (c) jQuery Project * Dual licensed under the MIT or GPL Version 2 licenses. * http://jquery.org/license */ (function($, undefined ) { $.widget( "mobile.checkboxradio", $.mobile.widget, { options: { theme: null }, _create: function(){ var self = this, input = this.element, label = input.closest("form,fieldset,[data-role='page']").find("label[for='" + input.attr( "id" ) + "']"), inputtype = input.attr( "type" ), checkedicon = "ui-icon-" + inputtype + "-on", uncheckedicon = "ui-icon-" + inputtype + "-off"; if ( inputtype != "checkbox" && inputtype != "radio" ) { return; } // If there's no selected theme... if( !this.options.theme ) { this.options.theme = this.element.data( "theme" ); } label .buttonMarkup({ theme: this.options.theme, icon: this.element.parents( "[data-type='horizontal']" ).length ? undefined : uncheckedicon, shadow: false }); // wrap the input + label in a div input .add( label ) .wrapAll( "
" ); label.bind({ mouseover: function() { if( $(this).parent().is('.ui-disabled') ){ return false; } }, "touchend mouseup": function( event ){ //prevent both events from firing, keep the first if( $(this).parent().is('.ui-disabled') || $(this).data("prevEvent") && $(this).data("prevEvent") !== event.type ){ return false; } $(this).data("prevEvent", event.type); setTimeout(function(){ label.removeData("prevEvent"); }, 1000); input.attr( "checked", inputtype === "radio" && true || !input.is( ":checked" ) ); input.trigger( "updateAll" ); event.preventDefault(); }, click: false }); input .bind({ updateAll: function() { $( "input[name='" + input.attr( "name" ) + "'][type='" + inputtype + "']" ).checkboxradio( "refresh" ); }, click: function(){ $( this ).trigger( "updateAll" ); }, focus: function() { label.addClass( "ui-focus" ); }, blur: function() { label.removeClass( "ui-focus" ); } }); this.refresh(); }, refresh: function( ){ var input = this.element, label = input.closest("form,fieldset,[data-role='page']").find("label[for='" + input.attr( "id" ) + "']"), inputtype = input.attr( "type" ), icon = label.find( ".ui-icon" ), checkedicon = "ui-icon-" + inputtype + "-on", uncheckedicon = "ui-icon-" + inputtype + "-off"; if ( input[0].checked ) { label.addClass( "ui-btn-active" ); icon.addClass( checkedicon ); icon.removeClass( uncheckedicon ); } else { label.removeClass( "ui-btn-active" ); icon.removeClass( checkedicon ); icon.addClass( uncheckedicon ); } if( input.is( ":disabled" ) ){ this.disable(); } else { this.enable(); } }, disable: function(){ this.element.attr("disabled",true).parent().addClass("ui-disabled"); }, enable: function(){ this.element.attr("disabled",false).parent().removeClass("ui-disabled"); } }); })( jQuery );