Nav apraksta

checkboxradio.js 7.3KB

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