1/* ------------------------------------------------------------------------ 2 * LC Switch 3 * superlight jQuery plugin improving forms look and functionality 4 * 5 * @version: 1.1 6 * @requires: jQuery v1.7 or later 7 * @author: Luca Montanari (LCweb) 8 * @website: https://lcweb.it 9 10 * Licensed under the MIT license 11------------------------------------------------------------------------- */ 12 13(function($){ 14 "use strict"; 15 if(typeof($.fn.lc_switch) != 'undefined') {return false;} // prevent multiple script inits 16 17 18 19 $.fn.lc_switch = function(on_text, off_text) { 20 21 // destruct 22 $.fn.lcs_destroy = function() { 23 24 $(this).each(function() { 25 var $wrap = $(this).parents('.lcs_wrap'); 26 27 $wrap.children().not('input').remove(); 28 $(this).unwrap(); 29 }); 30 31 return true; 32 }; 33 34 35 36 // set to ON 37 $.fn.lcs_on = function() { 38 $(this).each(function(i, v) { 39 var $wrap = $(this).parents('.lcs_wrap'), 40 $input = $wrap.find('input'), 41 $switch = $wrap.find('.lcs_switch'); 42 43 // if is already on - skip 44 if($wrap.find('.lcs_on').length) { 45 return true; 46 } 47 48 (typeof($.fn.prop) == 'function') ? $input.prop('checked', true) : $input.attr('checked', true); 49 50 $input.trigger('lcs-on'); 51 $input.trigger('lcs-statuschange'); 52 $switch.removeClass('lcs_off').addClass('lcs_on'); 53 54 // WCAG AA: Update aria-checked and aria-label for accessibility 55 $switch.attr('aria-checked', 'true'); 56 57 if (!$switch.attr('aria-labelledby')) { 58 var labelText = $input.attr('data-label') || $input.attr('aria-label') || 'Switch'; 59 $switch.attr('aria-label', labelText + ' - ' + (window.i18n ? (window.i18n.activated || 'activated') : 'activated')); 60 } 61 62 // if radio - disable other ones 63 if( $switch.hasClass('lcs_radio_switch') ) { 64 65 var f_name = $input.attr('name'); 66 $wrap.parents('form').find('input[name='+f_name+']').not($input).lcs_off(); 67 } 68 }); 69 70 return true; 71 }; 72 73 74 75 // set to OFF 76 $.fn.lcs_off = function() { 77 78 $(this).each(function() { 79 var $wrap = $(this).parents('.lcs_wrap'), 80 $input = $wrap.find('input'), 81 $switch = $wrap.find('.lcs_switch'); 82 83 // if is already off - skip 84 if(!$wrap.find('.lcs_on').length) { 85 return true; 86 } 87 88 // uncheck 89 (typeof($.fn.prop) == 'function') ? $input.prop('checked', false) : $input.attr('checked', false); 90 91 $input.trigger('lcs-off'); 92 $input.trigger('lcs-statuschange'); 93 $switch.removeClass('lcs_on').addClass('lcs_off'); 94 95 // WCAG AA: Update aria-checked and aria-label for accessibility 96 $switch.attr('aria-checked', 'false'); 97 98 if (!$switch.attr('aria-labelledby')) { 99 var labelText = $input.attr('data-label') || $input.attr('aria-label') || 'Switch'; 100 $switch.attr('aria-label', labelText + ' - ' + (window.i18n ? (window.i18n.deactivated || 'deactivated') : 'deactivated')); 101 } 102 }); 103 104 return true; 105 }; 106 107 108 109 // toggle status 110 $.fn.lcs_toggle = function() { 111 $(this).each(function() { 112 113 // not for radios 114 if( $(this).hasClass('lcs_radio_switch')) { 115 return true; 116 } 117 118 ($(this).is(':checked')) ? $(this).lcs_off() : $(this).lcs_on(); 119 }); 120 121 return true; 122 }; 123 124 125 126 // construct 127 return this.each(function(){ 128 129 // check against double init 130 if( !$(this).parent().hasClass('lcs_wrap') ) { 131 132 // default texts 133 var ckd_on_txt = (typeof(on_text) == 'undefined') ? 'ON' : on_text, 134 ckd_off_txt = (typeof(off_text) == 'undefined') ? 'OFF' : off_text; 135 136 // labels structure 137 var on_label = (ckd_on_txt) ? '<div class="lcs_label lcs_label_on">'+ ckd_on_txt +'</div>' : '', 138 off_label = (ckd_off_txt) ? '<div class="lcs_label lcs_label_off">'+ ckd_off_txt +'</div>' : ''; 139 140 141 // default states 142 var disabled = ($(this).is(':disabled')) ? true : false, 143 active = ($(this).is(':checked')) ? true : false; 144 145 var status_classes = ''; 146 status_classes += (active) ? ' lcs_on' : ' lcs_off'; 147 if(disabled) { 148 status_classes += ' lcs_disabled'; 149 } 150 151 152 // WCAG AA: Get proper label text for accessibility 153 var label = 'aria-label="' + ($(this).attr('data-label') || $(this).attr('aria-label') || $(this).attr('name') || 'Switch') + ' - ' + (active ? 154 (window.i18n ? (window.i18n.activated || 'activated') : 'activated') : 155 (window.i18n ? (window.i18n.deactivated || 'deactivated') : 'deactivated')) + '"'; 156 157 if ($(this).attr('aria-labelledby')) { // Overwrite aria-label 158 label = 'aria-labelledby="' + $(this).attr('aria-labelledby') + '"'; 159 } 160 if ($(this).attr('aria-describedby')) { 161 label += ' aria-describedby="' + $(this).attr('aria-describedby') + '"'; 162 } 163 164 // wrap and append 165 var structure = 166 '<div class="lcs_switch '+status_classes+'" role="switch" aria-checked="' + (active ? 'true' : 'false') + '" ' + label + ' tabindex="0">' + 167 '<div class="lcs_cursor"></div>' + 168 on_label + off_label + 169 '</div>'; 170 171 if( $(this).is(':input') && ($(this).attr('type') == 'checkbox' || $(this).attr('type') == 'radio') ) { 172 173 $(this).wrap('<div class="lcs_wrap"></div>'); 174 $(this).parent().append(structure); 175 176 $(this).parent().find('.lcs_switch').addClass('lcs_'+ $(this).attr('type') +'_switch'); 177 } 178 } 179 }); 180 }; 181 182 183 184 // handlers - only register once 185 if (!window.lcSwitchHandlersRegistered) { 186 window.lcSwitchHandlersRegistered = true; 187 188 $(function() { 189 // on click - WCAG AA compliant 190 $(document).on('click tap', '.lcs_switch:not(.lcs_disabled)', function(e) { 191 e.preventDefault(); 192 e.stopPropagation(); 193 194 var $input = $(this).parent().find('input'); 195 196 if( $(this).hasClass('lcs_on') ) { 197 if( !$(this).hasClass('lcs_radio_switch') ) { // not for radio 198 $input.lcs_off(); 199 } 200 } else { 201 $input.lcs_on(); 202 } 203 }); 204 205 // WCAG AA: Keyboard support for switches 206 $(document).on('keydown', '.lcs_switch:not(.lcs_disabled)', function(e) { 207 // Space or Enter key 208 if (e.keyCode === 32 || e.keyCode === 13) { 209 e.preventDefault(); 210 e.stopPropagation(); 211 212 // Handle the toggle directly instead of triggering click 213 var $input = $(this).parent().find('input'); 214 215 if( $(this).hasClass('lcs_on') ) { 216 if( !$(this).hasClass('lcs_radio_switch') ) { // not for radio 217 $input.lcs_off(); 218 } 219 } else { 220 $input.lcs_on(); 221 } 222 } 223 }); 224 225 226 // on checkbox status change 227 $(document).on('change', '.lcs_wrap input', function() { 228 ( $(this).is(':checked') ) ? $(this).lcs_on() : $(this).lcs_off(); 229 }); 230 231 }); 232 } 233 234})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.