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
|
|
|
*/
|
2011-06-29 00:35:40 +00:00
|
|
|
|
|
|
|
|
(function( $, undefined ) {
|
|
|
|
|
|
2010-10-28 02:38:02 +00:00
|
|
|
$.widget( "mobile.checkboxradio", $.mobile.widget, {
|
|
|
|
|
options: {
|
2011-07-26 18:22:08 +00:00
|
|
|
theme: null,
|
2011-07-27 22:42:16 +00:00
|
|
|
initSelector: "input[type='checkbox'],input[type='radio']"
|
2010-10-28 02:38:02 +00:00
|
|
|
},
|
2011-06-29 00:35:40 +00:00
|
|
|
_create: function() {
|
2011-01-21 22:45:22 +00:00
|
|
|
var self = this,
|
|
|
|
|
input = this.element,
|
2011-06-29 00:35:40 +00:00
|
|
|
// NOTE: Windows Phone could not find the label through a selector
|
|
|
|
|
// filter works though.
|
|
|
|
|
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" ),
|
2011-06-24 00:03:22 +00:00
|
|
|
checkedState = inputtype + "-on",
|
|
|
|
|
uncheckedState = inputtype + "-off",
|
2011-06-24 15:00:51 +00:00
|
|
|
icon = input.parents( ":jqmData(type='horizontal')" ).length ? undefined : uncheckedState,
|
|
|
|
|
activeBtn = icon ? "" : " " + $.mobile.activeBtnClass,
|
2011-06-29 00:35:40 +00:00
|
|
|
checkedClass = "ui-" + checkedState + activeBtn,
|
|
|
|
|
uncheckedClass = "ui-" + uncheckedState,
|
2011-06-24 00:03:22 +00:00
|
|
|
checkedicon = "ui-icon-" + checkedState,
|
|
|
|
|
uncheckedicon = "ui-icon-" + uncheckedState;
|
2010-10-22 16:42:24 +00:00
|
|
|
|
2011-06-29 00:35:40 +00:00
|
|
|
if ( inputtype !== "checkbox" && inputtype !== "radio" ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2010-12-18 13:20:15 +00:00
|
|
|
|
2011-06-29 00:35:40 +00:00
|
|
|
// Expose for other methods
|
|
|
|
|
$.extend( this, {
|
|
|
|
|
label: label,
|
|
|
|
|
inputtype: inputtype,
|
|
|
|
|
checkedClass: checkedClass,
|
|
|
|
|
uncheckedClass: uncheckedClass,
|
|
|
|
|
checkedicon: checkedicon,
|
|
|
|
|
uncheckedicon: uncheckedicon
|
2011-03-13 16:08:27 +00:00
|
|
|
});
|
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
|
|
|
|
2011-06-29 00:35:40 +00:00
|
|
|
label.buttonMarkup({
|
|
|
|
|
theme: this.options.theme,
|
|
|
|
|
icon: icon,
|
|
|
|
|
shadow: false
|
|
|
|
|
});
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2011-06-29 00:35:40 +00:00
|
|
|
// Wrap the input + label in a div
|
|
|
|
|
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() {
|
2011-06-29 00:35:40 +00:00
|
|
|
if ( $( this ).parent().is( ".ui-disabled" ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2010-10-28 03:11:36 +00:00
|
|
|
},
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2011-06-29 00:35:40 +00:00
|
|
|
vclick: function( event ) {
|
|
|
|
|
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-06-29 00:35:40 +00:00
|
|
|
input.prop( "checked", inputtype === "radio" && true || !input.prop( "checked" ) );
|
|
|
|
|
|
|
|
|
|
// Input set for common radio buttons will contain all the radio
|
2011-05-02 07:53:51 +00:00
|
|
|
// buttons, but will not for checkboxes. clearing the checked status
|
|
|
|
|
// of other radios ensures the active button state is applied properly
|
2011-06-29 00:35:40 +00:00
|
|
|
self._getInputSet().not( input ).prop( "checked", false );
|
2011-05-02 07:53:51 +00:00
|
|
|
|
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-06-29 00:35:40 +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-06-29 00:35:40 +00:00
|
|
|
vclick: function() {
|
|
|
|
|
|
|
|
|
|
var $this = $(this);
|
|
|
|
|
|
|
|
|
|
// Adds checked attribute to checked input when keyboard is used
|
|
|
|
|
if ( $this.is( ":checked" ) ) {
|
|
|
|
|
|
|
|
|
|
$this.prop( "checked", true);
|
|
|
|
|
self._getInputSet().not($this).prop( "checked", false );
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
$this.prop( "checked", false );
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2011-06-29 00:35:40 +00:00
|
|
|
_cacheVals: function() {
|
|
|
|
|
this._getInputSet().each(function() {
|
|
|
|
|
var $this = $(this);
|
|
|
|
|
|
|
|
|
|
$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-06-23 11:31:05 +00:00
|
|
|
if(this.inputtype == "checkbox") {
|
|
|
|
|
return this.element;
|
|
|
|
|
}
|
|
|
|
|
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-06-29 00:35:40 +00:00
|
|
|
_updateAll: function() {
|
2011-04-06 05:12:09 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
2011-06-29 00:35:40 +00:00
|
|
|
this._getInputSet().each(function() {
|
|
|
|
|
var $this = $(this);
|
|
|
|
|
|
|
|
|
|
if ( $this.is( ":checked" ) || self.inputtype === "checkbox" ) {
|
|
|
|
|
$this.trigger( "change" );
|
2011-01-24 18:04:41 +00:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.checkboxradio( "refresh" );
|
|
|
|
|
},
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2011-06-29 00:35:40 +00:00
|
|
|
refresh: function() {
|
2010-10-28 02:38:02 +00:00
|
|
|
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'
|
2011-06-29 00:35:40 +00:00
|
|
|
if ( $( input[ 0 ] ).prop( "checked" ) ) {
|
|
|
|
|
|
2011-06-23 23:57:32 +00:00
|
|
|
label.addClass( this.checkedClass ).removeClass( this.uncheckedClass );
|
2011-03-13 16:08:27 +00:00
|
|
|
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-06-29 00:35:40 +00:00
|
|
|
|
2011-06-23 23:57:32 +00:00
|
|
|
label.removeClass( this.checkedClass ).addClass( this.uncheckedClass );
|
2011-03-13 16:08:27 +00:00
|
|
|
icon.removeClass( this.checkedicon ).addClass( this.uncheckedicon );
|
2010-09-10 22:23:13 +00:00
|
|
|
}
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2011-06-29 00:35:40 +00:00
|
|
|
if ( input.is( ":disabled" ) ) {
|
2010-10-28 03:11:36 +00:00
|
|
|
this.disable();
|
2011-06-29 00:35:40 +00:00
|
|
|
} else {
|
2010-10-28 03:11:36 +00:00
|
|
|
this.enable();
|
|
|
|
|
}
|
|
|
|
|
},
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2011-06-29 00:35:40 +00:00
|
|
|
disable: function() {
|
|
|
|
|
this.element.prop( "disabled", true ).parent().addClass( "ui-disabled" );
|
2010-10-28 03:11:36 +00:00
|
|
|
},
|
2011-02-27 07:59:17 +00:00
|
|
|
|
2011-06-29 00:35:40 +00:00
|
|
|
enable: function() {
|
|
|
|
|
this.element.prop( "disabled", false ).parent().removeClass( "ui-disabled" );
|
2010-10-28 02:38:02 +00:00
|
|
|
}
|
|
|
|
|
});
|
2011-07-27 22:42:16 +00:00
|
|
|
|
|
|
|
|
//auto self-init widgets
|
|
|
|
|
$( document ).bind( "pagecreate create", function( e ){
|
2011-10-10 20:49:55 +00:00
|
|
|
$.mobile.checkboxradio.prototype.enhanceWithin( e.target );
|
2011-07-27 22:42:16 +00:00
|
|
|
});
|
|
|
|
|
|
2010-10-28 02:38:02 +00:00
|
|
|
})( jQuery );
|