jquery-labelauty.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*!
  2. * LABELAUTY jQuery Plugin
  3. *
  4. * @file: jquery-labelauty.js
  5. * @author: Francisco Neves (@fntneves)
  6. * @site: www.francisconeves.com
  7. * @license: MIT License
  8. */
  9. (function( $ ){
  10. $.fn.labelauty = function( options )
  11. {
  12. /*
  13. * Our default settings
  14. * Hope you don't need to change anything, with these settings
  15. */
  16. var settings = $.extend(
  17. {
  18. // Development Mode
  19. // This will activate console debug messages
  20. "development": false,
  21. // Trigger Class
  22. // This class will be used to apply styles
  23. "class": "labelauty",
  24. // Use icon?
  25. // If false, then only a text label represents the input
  26. "icon": true,
  27. // Use text label ?
  28. // If false, then only an icon represents the input
  29. "label": true,
  30. // Separator between labels' messages
  31. // If you use this separator for anything, choose a new one
  32. "separator": "|",
  33. // Default Checked Message
  34. // This message will be visible when input is checked
  35. "checked_label": "Checked",
  36. // Default UnChecked Message
  37. // This message will be visible when input is unchecked
  38. "unchecked_label": "Unchecked",
  39. // Force random ID's
  40. // Replace original ID's with random ID's,
  41. "force_random_id": false,
  42. // Minimum Label Width
  43. // This value will be used to apply a minimum width to the text labels
  44. "minimum_width": false,
  45. // Use the greatest width between two text labels ?
  46. // If this has a true value, then label width will be the greatest between labels
  47. "same_width": true
  48. }, options);
  49. /*
  50. * Let's create the core function
  51. * It will try to cover all settings and mistakes of using
  52. */
  53. return this.each(function()
  54. {
  55. var $object = $( this );
  56. var selected = $object.is(':checked');
  57. var type = $object.attr('type');
  58. var use_icons = true;
  59. var use_labels = true;
  60. var labels;
  61. var labels_object;
  62. var input_id;
  63. //Get the aria label from the input element
  64. var aria_label = $object.attr( "aria-label" );
  65. // Hide the object form screen readers
  66. $object.attr( "aria-hidden", true );
  67. // Test if object is a check input
  68. // Don't mess me up, come on
  69. if( $object.is( ":checkbox" ) === false && $object.is( ":radio" ) === false )
  70. return this;
  71. // Add "labelauty" class to all checkboxes
  72. // So you can apply some custom styles
  73. $object.addClass( settings["class"] );
  74. // Get the value of "data-labelauty" attribute
  75. // Then, we have the labels for each case (or not, as we will see)
  76. labels = $object.attr( "data-labelauty" );
  77. use_labels = settings.label;
  78. use_icons = settings.icon;
  79. // It's time to check if it's going to the right way
  80. // Null values, more labels than expected or no labels will be handled here
  81. if( use_labels === true )
  82. {
  83. if( labels == null || labels.length === 0 )
  84. {
  85. // If attribute has no label and we want to use, then use the default labels
  86. labels_object = [settings.unchecked_label, settings.checked_label]
  87. }
  88. else
  89. {
  90. // Ok, ok, it's time to split Checked and Unchecked labels
  91. // We split, by the "settings.separator" option
  92. labels_object = labels.split( settings.separator );
  93. // Now, let's check if exist _only_ two labels
  94. // If there's more than two, then we do not use labels :(
  95. // Else, do some additional tests
  96. if( labels_object.length > 2 )
  97. {
  98. use_labels = false;
  99. debug( settings.development, "There's more than two labels. LABELAUTY will not use labels." );
  100. }
  101. else
  102. {
  103. // If there's just one label (no split by "settings.separator"), it will be used for both cases
  104. // Here, we have the possibility of use the same label for both cases
  105. if( labels_object.length === 1 )
  106. debug( settings.development, "There's just one label. LABELAUTY will use this one for both cases." );
  107. }
  108. }
  109. }
  110. /*
  111. * Let's begin the beauty
  112. */
  113. // Start hiding ugly checkboxes
  114. // Obviously, we don't need native checkboxes :O
  115. $object.css({ display : "none" });
  116. // We don't need more data-labelauty attributes!
  117. // Ok, ok, it's just for beauty improvement
  118. $object.removeAttr( "data-labelauty" );
  119. // Now, grab checkbox ID Attribute for "label" tag use
  120. // If there's no ID Attribute, then generate a new one
  121. input_id = $object.attr( "id" );
  122. if( settings.force_random_id || input_id == null || input_id.trim() === "")
  123. {
  124. var input_id_number = 1 + Math.floor( Math.random() * 1024000 );
  125. input_id = "labelauty-" + input_id_number;
  126. // Is there any element with this random ID ?
  127. // If exists, then increment until get an unused ID
  128. while( $( input_id ).length !== 0 )
  129. {
  130. input_id_number++;
  131. input_id = "labelauty-" + input_id_number;
  132. debug( settings.development, "Holy crap, between 1024 thousand numbers, one raised a conflict. Trying again." );
  133. }
  134. $object.attr( "id", input_id );
  135. }
  136. // Now, add necessary tags to make this work
  137. // Here, we're going to test some control variables and act properly
  138. var element = jQuery(create( input_id, aria_label, selected, type, labels_object, use_labels, use_icons ));
  139. element.click(function(){
  140. if($object.is(':checked')){
  141. $(element).attr('aria-checked', false);
  142. }else{
  143. $(element).attr('aria-checked', true);
  144. }
  145. });
  146. element.keypress(function(event){
  147. event.preventDefault();
  148. if(event.keyCode === 32 || event.keyCode === 13){
  149. if($object.is(':checked')){
  150. $object.prop('checked', false);
  151. $(element).attr('aria-checked',false);
  152. }else{
  153. $object.prop('checked', true);
  154. $(element).attr('aria-checked', true);
  155. }
  156. }
  157. });
  158. $object.after(element);
  159. // Now, add "min-width" to label
  160. // Let's say the truth, a fixed width is more beautiful than a variable width
  161. if( settings.minimum_width !== false )
  162. $object.next( "label[for=" + input_id + "]" ).css({ "min-width": settings.minimum_width });
  163. // Now, add "min-width" to label
  164. // Let's say the truth, a fixed width is more beautiful than a variable width
  165. if( settings.same_width != false && settings.label == true )
  166. {
  167. var label_object = $object.next( "label[for=" + input_id + "]" );
  168. var unchecked_width = getRealWidth(label_object.find( "span.labelauty-unchecked" ));
  169. var checked_width = getRealWidth(label_object.find( "span.labelauty-checked" ));
  170. if( unchecked_width > checked_width )
  171. label_object.find( "span.labelauty-checked" ).width( unchecked_width );
  172. else
  173. label_object.find( "span.labelauty-unchecked" ).width( checked_width );
  174. }
  175. });
  176. };
  177. /*
  178. * Tricky code to work with hidden elements, like tabs.
  179. * Note: This code is based on jquery.actual plugin.
  180. * https://github.com/dreamerslab/jquery.actual
  181. */
  182. function getRealWidth( element )
  183. {
  184. var width = 0;
  185. var $target = element;
  186. var css_class = 'hidden_element';
  187. $target = $target.clone().attr('class', css_class).appendTo('body');
  188. width = $target.width(true);
  189. $target.remove();
  190. return width;
  191. }
  192. function debug( debug, message )
  193. {
  194. if( debug && window.console && window.console.log )
  195. window.console.log( "jQuery-LABELAUTY: " + message );
  196. }
  197. function create( input_id, aria_label, selected, type, messages_object, label, icon )
  198. {
  199. var block;
  200. var unchecked_message;
  201. var checked_message;
  202. var aria = "";
  203. if( messages_object == null )
  204. unchecked_message = checked_message = "";
  205. else
  206. {
  207. unchecked_message = messages_object[0];
  208. // If checked message is null, then put the same text of unchecked message
  209. if( messages_object[1] == null )
  210. checked_message = unchecked_message;
  211. else
  212. checked_message = messages_object[1];
  213. }
  214. if(aria_label == null)
  215. aria = "";
  216. else
  217. aria = 'tabindex="0" role="' + type + '" aria-checked="' + selected + '" aria-label="' + aria_label + '"';
  218. if( label == true && icon == true)
  219. {
  220. block = '<label for="' + input_id + '" ' + aria + '>' +
  221. '<span class="labelauty-unchecked-image"></span>' +
  222. '<span class="labelauty-unchecked">' + unchecked_message + '</span>' +
  223. '<span class="labelauty-checked-image"></span>' +
  224. '<span class="labelauty-checked">' + checked_message + '</span>' +
  225. '</label>';
  226. }
  227. else if( label == true )
  228. {
  229. block = '<label for="' + input_id + '" ' + aria + '>' +
  230. '<span class="labelauty-unchecked">' + unchecked_message + '</span>' +
  231. '<span class="labelauty-checked">' + checked_message + '</span>' +
  232. '</label>';
  233. }
  234. else
  235. {
  236. block = '<label for="' + input_id + '" ' + aria + '>' +
  237. '<span class="labelauty-unchecked-image"></span>' +
  238. '<span class="labelauty-checked-image"></span>' +
  239. '</label>';
  240. }
  241. return block;
  242. }
  243. }( jQuery ));