1(function($) { 2 $.fn.conditionize = function(options) { 3 4 var settings = $.extend({ 5 hideJS: true, 6 inputParent: '.sv-form-group', 7 hiddenClass: 'sv-hidden', 8 }, options ); 9 10 $.fn.eval = function(valueIs, valueShould, operator) { 11 // manage wild card 12 if(valueShould.toString() === "*") { 13 return valueIs !== ""; 14 } 15 16 switch(operator) { 17 case '==': 18 // Support logical OR comparison 19 return valueShould.toString().split("|").indexOf(valueIs) > -1; 20 break; 21 case '!=': 22 return valueIs != valueShould; 23 break; 24 case '<=': 25 return valueIs <= valueShould; 26 break; 27 case '<': 28 return valueIs < valueShould; 29 break; 30 case '>=': 31 return valueIs >= valueShould; 32 break; 33 case '>': 34 return valueIs > valueShould; 35 break; 36 } 37 } 38 39 $.fn.showOrHide = function(listenTo, listenFor, operator, $section) { 40 41 if ($(listenTo).is('select, input[type=text]') && $.fn.eval($(listenTo).val(), listenFor, operator)) { 42 $section.removeClass( settings.hiddenClass ); 43 } 44 else if ($(listenTo + ":checked").filter(function(idx, elem){return $.fn.eval(elem.value, listenFor, operator);}).length > 0) { 45 $section.removeClass( settings.hiddenClass ); 46 } 47 else { 48 $.fn.clearInput($section); 49 $section.addClass( settings.hiddenClass ); 50 } 51 } 52 53 $.fn.showOrHideMulti = function(listenTo, listenFor, operator, $section) { 54 55 var answers = []; 56 57 $(listenTo).each(function() { 58 answers.push($(this).val()); 59 }); 60 61 if( answers.indexOf(listenFor.toString()) > -1 ) { 62 $section.removeClass( settings.hiddenClass ); 63 } 64 else { 65 $.fn.clearInput($section); 66 $section.addClass( settings.hiddenClass ); 67 68 } 69 } 70 71 $.fn.clearInput = function($section) { 72 $section.find('input:not([type="submit"]):not([type="button"]):not([type="radio"]):not([type="checkbox"]):not([type="hidden"]), select, textarea').val(''); 73 $section.find('input[type="radio"]').val(['']); 74 $section.find('input[type="radio"],input[type="checkbox"]').removeAttr('checked'); 75 $section.find('option').removeAttr('selected'); 76 $section.find('input[type="select"]').val(''); 77 $section.find('input:not([type="submit"]):not([type="button"]):visible, select:visible').change(); 78 } 79 80 return this.each( function() { 81 var $section = $(this), 82 listenTo, 83 method, 84 listenFor = $section.data('cond-value'), 85 operator = $section.data('cond-operator') ? $section.data('cond-operator') : '=='; 86 87 // setup listenTo selector and show/hide method 88 if( $section.data('cond-option') ) { 89 var selector = $section.data('cond-option').toString(); 90 91 // if first character of selector is a letter assume its a name 92 if( selector.substring(0,1).match(/[a-z]/gi) ) { 93 selector = selector.replace(/(:|\.|\[|\]|,)/g, "\\$1"); 94 selector = "[name=" + selector + "]"; 95 } 96 97 listenTo = selector; 98 method = 'showOrHide'; 99 } 100 else { 101 listenTo = $section.data('cond-options').toString(); 102 method = 'showOrHideMulti'; 103 } 104 105 // hide form group rather then inputs 106 if($section.is(':input')) { 107 $section = $section.parents( settings.inputParent ); 108 } 109 110 //If setting was chosen, hide everything first... 111 if (settings.hideJS) { 112 $section.addClass( settings.hiddenClass ); 113 } 114 115 //Set up event listener 116 $(listenTo).on('change', function() { 117 $.fn[method](listenTo, listenFor, operator, $section); 118 }); 119 120 //Show based on current value on page load 121 $.fn[method](listenTo, listenFor, operator, $section); 122 123 }); 124 } 125}(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.