2010-09-10 22:23:13 +00:00
|
|
|
/*
|
2010-11-10 00:55:52 +00:00
|
|
|
* jQuery Mobile Framework : "checkboxradio" plugin
|
2010-09-10 22:23:13 +00:00
|
|
|
* Copyright (c) jQuery Project
|
2010-11-20 03:47:47 +00:00
|
|
|
* Dual licensed under the MIT or GPL Version 2 licenses.
|
|
|
|
|
* http://jquery.org/license
|
2011-02-27 07:59:17 +00:00
|
|
|
*/
|
2010-11-11 15:49:15 +00:00
|
|
|
(function($, undefined ) {
|
2010-10-28 02:38:02 +00:00
|
|
|
$.widget( "mobile.checkboxradio", $.mobile.widget, {
|
|
|
|
|
options: {
|
2010-11-11 04:19:41 +00:00
|
|
|
theme: null
|
2010-10-28 02:38:02 +00:00
|
|
|
},
|
|
|
|
|
_create: function(){
|
2011-01-21 22:45:22 +00:00
|
|
|
var self = this,
|
|
|
|
|
input = this.element,
|
2011-03-16 06:57:17 +00:00
|
|
|
//NOTE: Windows Phone could not find the label through a selector
|
|
|
|
|
//filter works though.
|
2011-05-02 08:00:36 +00:00
|
|
|
label = input.closest("form,fieldset,:jqmData(role='page')").find("label").filter('[for="' + input[0].id + '"]'),
|
2010-10-28 02:38:02 +00:00
|
|
|
inputtype = input.attr( "type" ),
|
|
|
|
|
checkedicon = "ui-icon-" + inputtype + "-on",
|
|
|
|
|
uncheckedicon = "ui-icon-" + inputtype + "-off";
|
2010-10-22 16:42:24 +00:00
|
|
|
|
2010-10-28 02:38:02 +00:00
|
|
|
if ( inputtype != "checkbox" && inputtype != "radio" ) { return; }
|
2010-12-18 13:20:15 +00:00
|
|
|
|
2011-03-13 16:08:27 +00:00
|
|
|
//expose for other methods
|
|
|
|
|
$.extend( this,{
|
|
|
|
|
label : label,
|
|
|
|
|
inputtype : inputtype,
|
|
|
|
|
checkedicon : checkedicon,
|
|
|
|
|
uncheckedicon : uncheckedicon
|
|
|
|
|
});
|
2010-12-18 13:20:15 +00:00
|
|
|
|
2010-12-22 15:53:07 +00:00
|
|
|
// If there's no selected theme...
|
2010-12-22 15:52:08 +00:00
|
|
|
if( !this.options.theme ) {
|
2011-03-25 21:50:40 +00:00
|
|
|
this.options.theme = this.element.jqmData( "theme" );
|
2010-12-22 15:52:08 +00:00
|
|
|
}
|
2010-12-22 15:53:07 +00:00
|
|
|
|
2010-10-28 02:38:02 +00:00
|
|
|
label
|
|
|
|
|
.buttonMarkup({
|
|
|
|
|
theme: this.options.theme,
|
2011-03-25 21:50:40 +00:00
|
|
|
icon: this.element.parents( ":jqmData(type='horizontal')" ).length ? undefined : uncheckedicon,
|
2010-10-28 02:38:02 +00:00
|
|
|
shadow: false
|
2010-10-22 16:42:24 +00:00
|
|
|
});
|
2011-02-27 07:59:17 +00:00
|
|
|
|
|
|
|
|
// wrap the input + label in a div
|
2010-10-28 02:38:02 +00:00
|
|
|
input
|
|
|
|
|
.add( label )
|
|
|
|
|
.wrapAll( "<div class='ui-" + inputtype +"'></div>" );
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2010-10-28 02:38:02 +00:00
|
|
|
label.bind({
|
2011-03-09 20:37:20 +00:00
|
|
|
vmouseover: function() {
|
2010-10-28 03:11:36 +00:00
|
|
|
if( $(this).parent().is('.ui-disabled') ){ return false; }
|
|
|
|
|
},
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2011-03-28 04:20:06 +00:00
|
|
|
vclick: function( event ){
|
2011-02-27 07:59:17 +00:00
|
|
|
if ( input.is( ":disabled" ) ){
|
2011-02-11 18:26:37 +00:00
|
|
|
event.preventDefault();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-03-09 20:37:20 +00:00
|
|
|
|
2011-01-24 18:04:41 +00:00
|
|
|
self._cacheVals();
|
2011-05-02 07:53:51 +00:00
|
|
|
|
2011-01-21 23:13:02 +00:00
|
|
|
input.attr( "checked", inputtype === "radio" && true || !input.is( ":checked" ) );
|
2011-05-02 07:53:51 +00:00
|
|
|
|
|
|
|
|
// input set for common radio buttons will contain all the radio
|
|
|
|
|
// buttons, but will not for checkboxes. clearing the checked status
|
|
|
|
|
// of other radios ensures the active button state is applied properly
|
|
|
|
|
self._getInputSet().not(input).removeAttr('checked');
|
|
|
|
|
|
2011-01-24 18:04:41 +00:00
|
|
|
self._updateAll();
|
2011-03-28 04:20:06 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2010-10-28 02:38:02 +00:00
|
|
|
});
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2010-10-28 02:38:02 +00:00
|
|
|
input
|
|
|
|
|
.bind({
|
2011-03-09 20:37:20 +00:00
|
|
|
vmousedown: function(){
|
2011-01-24 18:04:41 +00:00
|
|
|
this._cacheVals();
|
2010-10-28 02:38:02 +00:00
|
|
|
},
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2011-03-09 20:37:20 +00:00
|
|
|
vclick: function(){
|
2011-01-24 18:04:41 +00:00
|
|
|
self._updateAll();
|
2011-01-21 23:13:02 +00:00
|
|
|
},
|
2010-10-22 16:42:24 +00:00
|
|
|
|
2011-02-27 07:59:17 +00:00
|
|
|
focus: function() {
|
|
|
|
|
label.addClass( "ui-focus" );
|
2010-10-28 02:38:02 +00:00
|
|
|
},
|
2010-10-22 16:42:24 +00:00
|
|
|
|
2010-10-28 02:38:02 +00:00
|
|
|
blur: function() {
|
|
|
|
|
label.removeClass( "ui-focus" );
|
|
|
|
|
}
|
|
|
|
|
});
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2010-10-28 02:38:02 +00:00
|
|
|
this.refresh();
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2010-10-28 02:38:02 +00:00
|
|
|
},
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2011-01-24 18:04:41 +00:00
|
|
|
_cacheVals: function(){
|
|
|
|
|
this._getInputSet().each(function(){
|
2011-03-25 21:50:40 +00:00
|
|
|
$(this).jqmData("cacheVal", $(this).is(":checked") );
|
2011-02-27 07:59:17 +00:00
|
|
|
});
|
2011-01-24 18:04:41 +00:00
|
|
|
},
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2011-01-24 18:04:41 +00:00
|
|
|
//returns either a set of radios with the same name attribute, or a single checkbox
|
|
|
|
|
_getInputSet: function(){
|
2011-03-25 21:50:40 +00:00
|
|
|
return this.element.closest( "form,fieldset,:jqmData(role='page')" )
|
2011-03-13 16:08:27 +00:00
|
|
|
.find( "input[name='"+ this.element.attr( "name" ) +"'][type='"+ this.inputtype +"']" );
|
2011-01-24 18:04:41 +00:00
|
|
|
},
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2011-01-24 18:04:41 +00:00
|
|
|
_updateAll: function(){
|
2011-04-06 05:12:09 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
2011-01-24 18:04:41 +00:00
|
|
|
this._getInputSet().each(function(){
|
2011-04-06 05:12:09 +00:00
|
|
|
if( $(this).is(":checked") || self.inputtype === "checkbox" ){
|
2011-01-24 18:04:41 +00:00
|
|
|
$(this).trigger("change");
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.checkboxradio( "refresh" );
|
|
|
|
|
},
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2010-10-28 02:38:02 +00:00
|
|
|
refresh: function( ){
|
|
|
|
|
var input = this.element,
|
2011-03-13 16:08:27 +00:00
|
|
|
label = this.label,
|
|
|
|
|
icon = label.find( ".ui-icon" );
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2011-05-02 07:53:51 +00:00
|
|
|
// input[0].checked expando doesn't always report the proper value
|
|
|
|
|
// for checked='checked'
|
|
|
|
|
if ( $(input[0]).attr('checked') ) {
|
2011-03-13 16:08:27 +00:00
|
|
|
label.addClass( $.mobile.activeBtnClass );
|
|
|
|
|
icon.addClass( this.checkedicon ).removeClass( this.uncheckedicon );
|
2010-10-22 16:42:24 +00:00
|
|
|
|
2010-10-28 02:38:02 +00:00
|
|
|
} else {
|
2011-03-13 16:08:27 +00:00
|
|
|
label.removeClass( $.mobile.activeBtnClass );
|
|
|
|
|
icon.removeClass( this.checkedicon ).addClass( this.uncheckedicon );
|
2010-09-10 22:23:13 +00:00
|
|
|
}
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2010-10-28 03:11:36 +00:00
|
|
|
if( input.is( ":disabled" ) ){
|
|
|
|
|
this.disable();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
this.enable();
|
|
|
|
|
}
|
|
|
|
|
},
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2010-10-28 03:11:36 +00:00
|
|
|
disable: function(){
|
|
|
|
|
this.element.attr("disabled",true).parent().addClass("ui-disabled");
|
|
|
|
|
},
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2010-10-28 03:11:36 +00:00
|
|
|
enable: function(){
|
|
|
|
|
this.element.attr("disabled",false).parent().removeClass("ui-disabled");
|
2010-10-28 02:38:02 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
})( jQuery );
|