2014-08-28 19:16:51 +00:00
|
|
|
/*!
|
|
|
|
* jQuery UI Checkboxradio @VERSION
|
|
|
|
* http://jqueryui.com
|
|
|
|
*
|
2015-08-09 15:47:06 +00:00
|
|
|
* Copyright jQuery Foundation and other contributors
|
2014-08-28 19:16:51 +00:00
|
|
|
* Released under the MIT license.
|
|
|
|
* http://jquery.org/license
|
|
|
|
*/
|
2015-08-09 15:47:06 +00:00
|
|
|
|
|
|
|
//>>label: Checkboxradio
|
|
|
|
//>>group: Widgets
|
|
|
|
//>>description: Enhances a form with multiple themeable checkboxes or radio buttons.
|
|
|
|
//>>docs: http://api.jqueryui.com/checkboxradio/
|
|
|
|
//>>demos: http://jqueryui.com/checkboxradio/
|
2015-10-21 23:08:44 +00:00
|
|
|
//>>css.structure: ../../themes/base/core.css
|
|
|
|
//>>css.structure: ../../themes/base/button.css
|
|
|
|
//>>css.structure: ../../themes/base/checkboxradio.css
|
|
|
|
//>>css.theme: ../../themes/base/theme.css
|
2015-08-09 15:47:06 +00:00
|
|
|
|
2014-08-28 19:16:51 +00:00
|
|
|
( function( factory ) {
|
|
|
|
if ( typeof define === "function" && define.amd ) {
|
|
|
|
|
|
|
|
// AMD. Register as an anonymous module.
|
|
|
|
define( [
|
|
|
|
"jquery",
|
2015-01-08 02:51:38 +00:00
|
|
|
"../escape-selector",
|
|
|
|
"../form-reset-mixin",
|
|
|
|
"../labels",
|
|
|
|
"../widget"
|
2014-08-28 19:16:51 +00:00
|
|
|
], factory );
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Browser globals
|
|
|
|
factory( jQuery );
|
|
|
|
}
|
|
|
|
}( function( $ ) {
|
|
|
|
|
2015-01-08 02:51:38 +00:00
|
|
|
$.widget( "ui.checkboxradio", [ $.ui.formResetMixin, {
|
2014-08-28 19:16:51 +00:00
|
|
|
version: "@VERSION",
|
|
|
|
options: {
|
|
|
|
disabled: null,
|
|
|
|
label: null,
|
|
|
|
icon: true,
|
|
|
|
classes: {
|
|
|
|
"ui-checkboxradio-label": "ui-corner-all",
|
|
|
|
"ui-checkboxradio-icon": "ui-corner-all"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_getCreateOptions: function() {
|
2015-09-30 15:47:06 +00:00
|
|
|
var disabled, labels;
|
|
|
|
var that = this;
|
|
|
|
var options = this._super() || {};
|
2014-08-28 19:16:51 +00:00
|
|
|
|
|
|
|
// We read the type here, because it makes more sense to throw a element type error first,
|
|
|
|
// rather then the error for lack of a label. Often if its the wrong type, it
|
|
|
|
// won't have a label (e.g. calling on a div, btn, etc)
|
|
|
|
this._readType();
|
|
|
|
|
|
|
|
labels = this.element.labels();
|
|
|
|
|
2015-10-06 13:42:24 +00:00
|
|
|
// If there are multiple labels, use the last one
|
2014-08-28 19:16:51 +00:00
|
|
|
this.label = $( labels[ labels.length - 1 ] );
|
|
|
|
if ( !this.label.length ) {
|
|
|
|
$.error( "No label found for checkboxradio widget" );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.originalLabel = "";
|
|
|
|
|
|
|
|
// We need to get the label text but this may also need to make sure it does not contain the
|
|
|
|
// input itself.
|
|
|
|
this.label.contents().not( this.element ).each( function() {
|
|
|
|
|
2015-09-16 05:48:23 +00:00
|
|
|
// The label contents could be text, html, or a mix. We concat each element to get a string
|
|
|
|
// representation of the label, without the input as part of it.
|
2014-08-28 19:16:51 +00:00
|
|
|
that.originalLabel += this.nodeType === 3 ? $( this ).text() : this.outerHTML;
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Set the label option if we found label text
|
|
|
|
if ( this.originalLabel ) {
|
|
|
|
options.label = this.originalLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
disabled = this.element[ 0 ].disabled;
|
|
|
|
if ( disabled != null ) {
|
|
|
|
options.disabled = disabled;
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
},
|
|
|
|
|
|
|
|
_create: function() {
|
2015-01-08 02:51:38 +00:00
|
|
|
var checked = this.element[ 0 ].checked;
|
2014-08-28 19:16:51 +00:00
|
|
|
|
2015-01-08 02:51:38 +00:00
|
|
|
this._bindFormResetHandler();
|
2014-08-28 19:16:51 +00:00
|
|
|
|
|
|
|
if ( this.options.disabled == null ) {
|
2015-09-16 05:48:23 +00:00
|
|
|
this.options.disabled = this.element[ 0 ].disabled;
|
2014-08-28 19:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this._setOption( "disabled", this.options.disabled );
|
|
|
|
this._addClass( "ui-checkboxradio", "ui-helper-hidden-accessible" );
|
|
|
|
this._addClass( this.label, "ui-checkboxradio-label", "ui-button ui-widget" );
|
|
|
|
|
|
|
|
if ( this.type === "radio" ) {
|
|
|
|
this._addClass( this.label, "ui-checkboxradio-radio-label" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.options.label && this.options.label !== this.originalLabel ) {
|
|
|
|
this._updateLabel();
|
|
|
|
} else if ( this.originalLabel ) {
|
|
|
|
this.options.label = this.originalLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._enhance();
|
|
|
|
|
|
|
|
if ( checked ) {
|
|
|
|
this._addClass( this.label, "ui-checkboxradio-checked", "ui-state-active" );
|
|
|
|
this._addClass( this.icon, null, "ui-state-hover" );
|
|
|
|
}
|
|
|
|
|
|
|
|
this._on( {
|
2015-09-16 05:48:23 +00:00
|
|
|
change: "_toggleClasses",
|
|
|
|
focus: function() {
|
2014-08-28 19:16:51 +00:00
|
|
|
this._addClass( this.label, null, "ui-state-focus ui-visual-focus" );
|
|
|
|
},
|
2015-09-16 05:48:23 +00:00
|
|
|
blur: function() {
|
2014-08-28 19:16:51 +00:00
|
|
|
this._removeClass( this.label, null, "ui-state-focus ui-visual-focus" );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
_readType: function() {
|
|
|
|
var nodeName = this.element[ 0 ].nodeName.toLowerCase();
|
|
|
|
this.type = this.element[ 0 ].type;
|
|
|
|
if ( nodeName !== "input" || !/radio|checkbox/.test( this.type ) ) {
|
|
|
|
$.error( "Can't create checkboxradio on element.nodeName=" + nodeName +
|
|
|
|
" and element.type=" + this.type );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Support jQuery Mobile enhanced option
|
|
|
|
_enhance: function() {
|
|
|
|
this._updateIcon( this.element[ 0 ].checked );
|
|
|
|
},
|
|
|
|
|
|
|
|
widget: function() {
|
|
|
|
return this.label;
|
|
|
|
},
|
|
|
|
|
|
|
|
_getRadioGroup: function() {
|
2015-10-27 18:35:27 +00:00
|
|
|
var group;
|
2015-09-16 05:48:23 +00:00
|
|
|
var name = this.element[ 0 ].name;
|
2015-10-27 18:35:27 +00:00
|
|
|
var nameSelector = "input[name='" + $.ui.escapeSelector( name ) + "']";
|
2014-08-28 19:16:51 +00:00
|
|
|
|
2015-09-30 15:47:06 +00:00
|
|
|
if ( !name ) {
|
|
|
|
return $( [] );
|
2015-10-06 13:42:24 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 18:35:27 +00:00
|
|
|
if ( this.form.length ) {
|
|
|
|
group = $( this.form[ 0 ].elements ).filter( nameSelector );
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Not inside a form, check all inputs that also are not inside a form
|
|
|
|
group = $( nameSelector ).filter( function() {
|
|
|
|
return $( this ).form().length === 0;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
return group.not( this.element );
|
2014-08-28 19:16:51 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_toggleClasses: function() {
|
|
|
|
var checked = this.element[ 0 ].checked;
|
|
|
|
this._toggleClass( this.label, "ui-checkboxradio-checked", "ui-state-active", checked );
|
|
|
|
|
|
|
|
if ( this.options.icon && this.type === "checkbox" ) {
|
|
|
|
|
|
|
|
// We add ui-state-highlight to change the icon color
|
|
|
|
this._toggleClass( this.icon, null, "ui-icon-check ui-state-highlight", checked )
|
|
|
|
._toggleClass( this.icon, null, "ui-icon-blank", !checked );
|
|
|
|
}
|
|
|
|
if ( this.type === "radio" ) {
|
|
|
|
this._getRadioGroup()
|
|
|
|
.each( function() {
|
|
|
|
var instance = $( this ).checkboxradio( "instance" );
|
|
|
|
|
|
|
|
if ( instance ) {
|
|
|
|
instance._removeClass( instance.label,
|
|
|
|
"ui-checkboxradio-checked", "ui-state-active" );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_destroy: function() {
|
2015-01-08 02:51:38 +00:00
|
|
|
this._unbindFormResetHandler();
|
2014-08-28 19:16:51 +00:00
|
|
|
|
|
|
|
if ( this.icon ) {
|
|
|
|
this.icon.remove();
|
|
|
|
this.iconSpace.remove();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_setOption: function( key, value ) {
|
|
|
|
|
2015-09-16 05:48:23 +00:00
|
|
|
// We don't allow the value to be set to nothing
|
2014-08-28 19:16:51 +00:00
|
|
|
if ( key === "label" && !value ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._super( key, value );
|
|
|
|
|
|
|
|
if ( key === "disabled" ) {
|
|
|
|
this._toggleClass( this.label, null, "ui-state-disabled", value );
|
|
|
|
this.element[ 0 ].disabled = value;
|
|
|
|
|
2015-09-30 15:47:06 +00:00
|
|
|
// Don't refresh when setting disabled
|
2014-08-28 19:16:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.refresh();
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateIcon: function( checked ) {
|
|
|
|
var toAdd = "ui-icon ui-icon-background ";
|
|
|
|
|
|
|
|
if ( this.options.icon ) {
|
|
|
|
if ( !this.icon ) {
|
|
|
|
this.icon = $( "<span>" );
|
|
|
|
this.iconSpace = $( "<span> </span>" );
|
|
|
|
this._addClass( this.iconSpace, "ui-checkboxradio-icon-space" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.type === "checkbox" ) {
|
2015-10-06 15:01:23 +00:00
|
|
|
toAdd += checked ? "ui-icon-check ui-state-highlight" : "ui-icon-blank";
|
2014-08-28 19:16:51 +00:00
|
|
|
this._removeClass( this.icon, null, checked ? "ui-icon-blank" : "ui-icon-check" );
|
|
|
|
} else {
|
|
|
|
toAdd += "ui-icon-blank";
|
|
|
|
}
|
|
|
|
this._addClass( this.icon, "ui-checkboxradio-icon", toAdd );
|
|
|
|
if ( !checked ) {
|
2015-10-06 15:01:23 +00:00
|
|
|
this._removeClass( this.icon, null, "ui-icon-check ui-state-highlight" );
|
2014-08-28 19:16:51 +00:00
|
|
|
}
|
|
|
|
this.icon.prependTo( this.label ).after( this.iconSpace );
|
|
|
|
} else if ( this.icon !== undefined ) {
|
|
|
|
this.icon.remove();
|
|
|
|
this.iconSpace.remove();
|
|
|
|
delete this.icon;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateLabel: function() {
|
|
|
|
|
|
|
|
// Remove the contents of the label ( minus the icon, icon space, and input )
|
|
|
|
this.label.contents().not( this.element.add( this.icon ).add( this.iconSpace ) ).remove();
|
|
|
|
this.label.append( this.options.label );
|
|
|
|
},
|
|
|
|
|
|
|
|
refresh: function() {
|
|
|
|
var checked = this.element[ 0 ].checked,
|
|
|
|
isDisabled = this.element[ 0 ].disabled;
|
|
|
|
|
|
|
|
this._updateIcon( checked );
|
|
|
|
this._toggleClass( this.label, "ui-checkboxradio-checked", "ui-state-active", checked );
|
|
|
|
if ( this.options.label !== null ) {
|
|
|
|
this._updateLabel();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isDisabled !== this.options.disabled ) {
|
|
|
|
this._setOptions( { "disabled": isDisabled } );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 02:51:38 +00:00
|
|
|
} ] );
|
2014-08-28 19:16:51 +00:00
|
|
|
|
|
|
|
return $.ui.checkboxradio;
|
|
|
|
|
|
|
|
} ) );
|