Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

286 lignes
7.3 KiB

  1. /*!
  2. * jQuery UI Checkboxradio 1.13.1
  3. * http://jqueryui.com
  4. *
  5. * Copyright jQuery Foundation and other contributors
  6. * Released under the MIT license.
  7. * http://jquery.org/license
  8. */
  9. //>>label: Checkboxradio
  10. //>>group: Widgets
  11. //>>description: Enhances a form with multiple themeable checkboxes or radio buttons.
  12. //>>docs: http://api.jqueryui.com/checkboxradio/
  13. //>>demos: http://jqueryui.com/checkboxradio/
  14. //>>css.structure: ../../themes/base/core.css
  15. //>>css.structure: ../../themes/base/button.css
  16. //>>css.structure: ../../themes/base/checkboxradio.css
  17. //>>css.theme: ../../themes/base/theme.css
  18. ( function( factory ) {
  19. "use strict";
  20. if ( typeof define === "function" && define.amd ) {
  21. // AMD. Register as an anonymous module.
  22. define( [
  23. "jquery",
  24. "../form-reset-mixin",
  25. "../labels",
  26. "../widget"
  27. ], factory );
  28. } else {
  29. // Browser globals
  30. factory( jQuery );
  31. }
  32. } )( function( $ ) {
  33. "use strict";
  34. $.widget( "ui.checkboxradio", [ $.ui.formResetMixin, {
  35. version: "1.13.1",
  36. options: {
  37. disabled: null,
  38. label: null,
  39. icon: true,
  40. classes: {
  41. "ui-checkboxradio-label": "ui-corner-all",
  42. "ui-checkboxradio-icon": "ui-corner-all"
  43. }
  44. },
  45. _getCreateOptions: function() {
  46. var disabled, labels;
  47. var that = this;
  48. var options = this._super() || {};
  49. // We read the type here, because it makes more sense to throw a element type error first,
  50. // rather then the error for lack of a label. Often if its the wrong type, it
  51. // won't have a label (e.g. calling on a div, btn, etc)
  52. this._readType();
  53. labels = this.element.labels();
  54. // If there are multiple labels, use the last one
  55. this.label = $( labels[ labels.length - 1 ] );
  56. if ( !this.label.length ) {
  57. $.error( "No label found for checkboxradio widget" );
  58. }
  59. this.originalLabel = "";
  60. // We need to get the label text but this may also need to make sure it does not contain the
  61. // input itself.
  62. this.label.contents().not( this.element[ 0 ] ).each( function() {
  63. // The label contents could be text, html, or a mix. We concat each element to get a
  64. // string representation of the label, without the input as part of it.
  65. that.originalLabel += this.nodeType === 3 ? $( this ).text() : this.outerHTML;
  66. } );
  67. // Set the label option if we found label text
  68. if ( this.originalLabel ) {
  69. options.label = this.originalLabel;
  70. }
  71. disabled = this.element[ 0 ].disabled;
  72. if ( disabled != null ) {
  73. options.disabled = disabled;
  74. }
  75. return options;
  76. },
  77. _create: function() {
  78. var checked = this.element[ 0 ].checked;
  79. this._bindFormResetHandler();
  80. if ( this.options.disabled == null ) {
  81. this.options.disabled = this.element[ 0 ].disabled;
  82. }
  83. this._setOption( "disabled", this.options.disabled );
  84. this._addClass( "ui-checkboxradio", "ui-helper-hidden-accessible" );
  85. this._addClass( this.label, "ui-checkboxradio-label", "ui-button ui-widget" );
  86. if ( this.type === "radio" ) {
  87. this._addClass( this.label, "ui-checkboxradio-radio-label" );
  88. }
  89. if ( this.options.label && this.options.label !== this.originalLabel ) {
  90. this._updateLabel();
  91. } else if ( this.originalLabel ) {
  92. this.options.label = this.originalLabel;
  93. }
  94. this._enhance();
  95. if ( checked ) {
  96. this._addClass( this.label, "ui-checkboxradio-checked", "ui-state-active" );
  97. }
  98. this._on( {
  99. change: "_toggleClasses",
  100. focus: function() {
  101. this._addClass( this.label, null, "ui-state-focus ui-visual-focus" );
  102. },
  103. blur: function() {
  104. this._removeClass( this.label, null, "ui-state-focus ui-visual-focus" );
  105. }
  106. } );
  107. },
  108. _readType: function() {
  109. var nodeName = this.element[ 0 ].nodeName.toLowerCase();
  110. this.type = this.element[ 0 ].type;
  111. if ( nodeName !== "input" || !/radio|checkbox/.test( this.type ) ) {
  112. $.error( "Can't create checkboxradio on element.nodeName=" + nodeName +
  113. " and element.type=" + this.type );
  114. }
  115. },
  116. // Support jQuery Mobile enhanced option
  117. _enhance: function() {
  118. this._updateIcon( this.element[ 0 ].checked );
  119. },
  120. widget: function() {
  121. return this.label;
  122. },
  123. _getRadioGroup: function() {
  124. var group;
  125. var name = this.element[ 0 ].name;
  126. var nameSelector = "input[name='" + $.escapeSelector( name ) + "']";
  127. if ( !name ) {
  128. return $( [] );
  129. }
  130. if ( this.form.length ) {
  131. group = $( this.form[ 0 ].elements ).filter( nameSelector );
  132. } else {
  133. // Not inside a form, check all inputs that also are not inside a form
  134. group = $( nameSelector ).filter( function() {
  135. return $( this )._form().length === 0;
  136. } );
  137. }
  138. return group.not( this.element );
  139. },
  140. _toggleClasses: function() {
  141. var checked = this.element[ 0 ].checked;
  142. this._toggleClass( this.label, "ui-checkboxradio-checked", "ui-state-active", checked );
  143. if ( this.options.icon && this.type === "checkbox" ) {
  144. this._toggleClass( this.icon, null, "ui-icon-check ui-state-checked", checked )
  145. ._toggleClass( this.icon, null, "ui-icon-blank", !checked );
  146. }
  147. if ( this.type === "radio" ) {
  148. this._getRadioGroup()
  149. .each( function() {
  150. var instance = $( this ).checkboxradio( "instance" );
  151. if ( instance ) {
  152. instance._removeClass( instance.label,
  153. "ui-checkboxradio-checked", "ui-state-active" );
  154. }
  155. } );
  156. }
  157. },
  158. _destroy: function() {
  159. this._unbindFormResetHandler();
  160. if ( this.icon ) {
  161. this.icon.remove();
  162. this.iconSpace.remove();
  163. }
  164. },
  165. _setOption: function( key, value ) {
  166. // We don't allow the value to be set to nothing
  167. if ( key === "label" && !value ) {
  168. return;
  169. }
  170. this._super( key, value );
  171. if ( key === "disabled" ) {
  172. this._toggleClass( this.label, null, "ui-state-disabled", value );
  173. this.element[ 0 ].disabled = value;
  174. // Don't refresh when setting disabled
  175. return;
  176. }
  177. this.refresh();
  178. },
  179. _updateIcon: function( checked ) {
  180. var toAdd = "ui-icon ui-icon-background ";
  181. if ( this.options.icon ) {
  182. if ( !this.icon ) {
  183. this.icon = $( "<span>" );
  184. this.iconSpace = $( "<span> </span>" );
  185. this._addClass( this.iconSpace, "ui-checkboxradio-icon-space" );
  186. }
  187. if ( this.type === "checkbox" ) {
  188. toAdd += checked ? "ui-icon-check ui-state-checked" : "ui-icon-blank";
  189. this._removeClass( this.icon, null, checked ? "ui-icon-blank" : "ui-icon-check" );
  190. } else {
  191. toAdd += "ui-icon-blank";
  192. }
  193. this._addClass( this.icon, "ui-checkboxradio-icon", toAdd );
  194. if ( !checked ) {
  195. this._removeClass( this.icon, null, "ui-icon-check ui-state-checked" );
  196. }
  197. this.icon.prependTo( this.label ).after( this.iconSpace );
  198. } else if ( this.icon !== undefined ) {
  199. this.icon.remove();
  200. this.iconSpace.remove();
  201. delete this.icon;
  202. }
  203. },
  204. _updateLabel: function() {
  205. // Remove the contents of the label ( minus the icon, icon space, and input )
  206. var contents = this.label.contents().not( this.element[ 0 ] );
  207. if ( this.icon ) {
  208. contents = contents.not( this.icon[ 0 ] );
  209. }
  210. if ( this.iconSpace ) {
  211. contents = contents.not( this.iconSpace[ 0 ] );
  212. }
  213. contents.remove();
  214. this.label.append( this.options.label );
  215. },
  216. refresh: function() {
  217. var checked = this.element[ 0 ].checked,
  218. isDisabled = this.element[ 0 ].disabled;
  219. this._updateIcon( checked );
  220. this._toggleClass( this.label, "ui-checkboxradio-checked", "ui-state-active", checked );
  221. if ( this.options.label !== null ) {
  222. this._updateLabel();
  223. }
  224. if ( isDisabled !== this.options.disabled ) {
  225. this._setOptions( { "disabled": isDisabled } );
  226. }
  227. }
  228. } ] );
  229. return $.ui.checkboxradio;
  230. } );