jquery-labelauty.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 text label ?
  25. // If false, then only an icon represents the input
  26. label: true,
  27. // Separator between labels' messages
  28. // If you use this separator for anything, choose a new one
  29. separator: "|",
  30. // Default Checked Message
  31. // This message will be visible when input is checked
  32. checked_label: "Checked",
  33. // Default UnChecked Message
  34. // This message will be visible when input is unchecked
  35. unchecked_label: "Unchecked",
  36. // Minimum Label Width
  37. // This value will be used to apply a minimum width to the text labels
  38. minimum_width: false,
  39. // Use the greatest width between two text labels ?
  40. // If this has a true value, then label width will be the greatest between labels
  41. same_width: true
  42. }, options);
  43. /*
  44. * Let's create the core function
  45. * It will try to cover all settings and mistakes of using
  46. */
  47. return this.each(function()
  48. {
  49. var $object = $( this );
  50. var use_labels = true;
  51. var labels;
  52. var labels_object;
  53. var input_id;
  54. // Test if object is a check input
  55. // Don't mess me up, come on
  56. if( $object.is( ":checkbox" ) === false && $object.is( ":radio" ) === false )
  57. return this;
  58. // Add "labelauty" class to all checkboxes
  59. // So you can apply some custom styles
  60. $object.addClass( settings.class );
  61. // Get the value of "data-labelauty" attribute
  62. // Then, we have the labels for each case (or not, as we will see)
  63. labels = $object.attr( "data-labelauty" );
  64. use_labels = settings.label;
  65. // It's time to check if it's going to the right way
  66. // Null values, more labels than expected or no labels will be handled here
  67. if( use_labels === true )
  68. {
  69. if( labels == null || labels.length === 0 )
  70. {
  71. // If attribute has no label and we want to use, then use the default labels
  72. labels_object = new Array();
  73. labels_object[0] = settings.unchecked_label;
  74. labels_object[1] = settings.checked_label;
  75. }
  76. else
  77. {
  78. // Ok, ok, it's time to split Checked and Unchecked labels
  79. // We split, by the "settings.separator" option
  80. labels_object = labels.split( settings.separator );
  81. // Now, let's check if exist _only_ two labels
  82. // If there's more than two, then we do not use labels :(
  83. // Else, do some additional tests
  84. if( labels_object.length > 2 )
  85. {
  86. use_labels = false;
  87. debug( settings.development, "There's more than two labels. LABELAUTY will not use labels." );
  88. }
  89. else
  90. {
  91. // If there's just one label (no split by "settings.separator"), it will be used for both cases
  92. // Here, we have the possibility of use the same label for both cases
  93. if( labels_object.length === 1 )
  94. debug( settings.development, "There's just one label. LABELAUTY will use this one for both cases." );
  95. }
  96. }
  97. }
  98. /*
  99. * Let's begin the beauty
  100. */
  101. // Start hiding ugly checkboxes
  102. // Obviously, we don't need native checkboxes :O
  103. $object.css({ display : "none" });
  104. // We don't need more data-labelauty attributes!
  105. // Ok, ok, it's just for beauty improvement
  106. $object.removeAttr( "data-labelauty" );
  107. // Now, grab checkbox ID Attribute for "label" tag use
  108. // If there's no ID Attribute, then generate a new one
  109. input_id = $object.attr( "id" );
  110. if( input_id == null )
  111. {
  112. var input_id_number = 1 + Math.floor( Math.random() * 1024000 );
  113. input_id = "labelauty-" + input_id_number;
  114. // Is there any element with this random ID ?
  115. // If exists, then increment until get an unused ID
  116. while( $( input_id ).length !== 0 )
  117. {
  118. input_id_number++;
  119. input_id = "labelauty-" + input_id_number;
  120. debug( settings.development, "Holy crap, between 1024 thousand numbers, one raised a conflict. Trying again." );
  121. }
  122. $object.attr( "id", input_id );
  123. }
  124. // Now, add necessary tags to make this work
  125. // Here, we're going to test some control variables and act properly
  126. $object.after( create( input_id, labels_object, use_labels ) );
  127. // Now, add "min-width" to label
  128. // Let's say the truth, a fixed width is more beautiful than a variable width
  129. if( settings.minimum_width !== false )
  130. $object.next( "label[for=" + input_id + "]" ).css({ "min-width": settings.minimum_width });
  131. // Now, add "min-width" to label
  132. // Let's say the truth, a fixed width is more beautiful than a variable width
  133. if( settings.same_width != false && settings.label == true )
  134. {
  135. var label_object = $object.next( "label[for=" + input_id + "]" );
  136. var unchecked_width = getRealWidth(label_object.find( "span.labelauty-unchecked" ));
  137. var checked_width = getRealWidth(label_object.find( "span.labelauty-checked" ));
  138. if( unchecked_width > checked_width )
  139. label_object.find( "span.labelauty-checked" ).width( unchecked_width );
  140. else
  141. label_object.find( "span.labelauty-unchecked" ).width( checked_width );
  142. }
  143. });
  144. };
  145. /*
  146. * Tricky code to work with hidden elements, like tabs.
  147. * Note: This code is based on jquery.actual plugin.
  148. * https://github.com/dreamerslab/jquery.actual
  149. */
  150. function getRealWidth( element )
  151. {
  152. var width = 0;
  153. var $target = element;
  154. var style = 'position: absolute !important; top: -1000 !important; ';
  155. $target = $target.clone().attr('style', style).appendTo('body');
  156. width = $target.width(true);
  157. $target.remove();
  158. return width;
  159. }
  160. function debug( debug, message )
  161. {
  162. if( debug && window.console && window.console.log )
  163. window.console.log( "jQuery-LABELAUTY: " + message );
  164. };
  165. function create( input_id, messages_object, label )
  166. {
  167. var block;
  168. var unchecked_message;
  169. var checked_message;
  170. if( messages_object == null )
  171. unchecked_message = checked_message = "";
  172. else
  173. {
  174. unchecked_message = messages_object[0];
  175. // If checked message is null, then put the same text of unchecked message
  176. if( messages_object[1] == null )
  177. checked_message = unchecked_message;
  178. else
  179. checked_message = messages_object[1];
  180. }
  181. if( label == true )
  182. {
  183. block = '<label for="' + input_id + '">' +
  184. '<span class="labelauty-unchecked-image"></span>' +
  185. '<span class="labelauty-unchecked">' + unchecked_message + '</span>' +
  186. '<span class="labelauty-checked-image"></span>' +
  187. '<span class="labelauty-checked">' + checked_message + '</span>' +
  188. '</label>';
  189. }
  190. else
  191. {
  192. block = '<label for="' + input_id + '">' +
  193. '<span class="labelauty-unchecked-image"></span>' +
  194. '<span class="labelauty-checked-image"></span>' +
  195. '</label>';
  196. }
  197. return block;
  198. };
  199. }( jQuery ));