scripts.js
294 lines
| 1 | var cf7signature_resized = 0; // for compatibility with contact-form-7-signature-addon |
| 2 | |
| 3 | var i=0; |
| 4 | var timeout; |
| 5 | |
| 6 | var show_animation = { "height": "show", "marginTop": "show", "marginBottom": "show", "paddingTop": "show", "paddingBottom": "show" }; |
| 7 | var hide_animation = { "height": "hide", "marginTop": "hide", "marginBottom": "hide", "paddingTop": "hide", "paddingBottom": "hide" }; |
| 8 | |
| 9 | var wpcf7cf = { |
| 10 | initForm : function($form) { |
| 11 | |
| 12 | var $ = jQuery; |
| 13 | |
| 14 | var options_element = $form.find('input[name="_wpcf7cf_options"]').eq(0); |
| 15 | if (options_element.length) { |
| 16 | var value = options_element.val(); |
| 17 | if (value) { |
| 18 | |
| 19 | form_options = JSON.parse(value); |
| 20 | form_options.unit_tag = $form.closest('.wpcf7').attr('id'); |
| 21 | |
| 22 | var unit_tag = form_options['unit_tag']; |
| 23 | var conditions = form_options['conditions']; |
| 24 | var settings = form_options['settings']; |
| 25 | |
| 26 | wpcf7cf.display_fields(unit_tag, conditions, settings); |
| 27 | |
| 28 | // monitor input changes, and call display_fields() is something has changed |
| 29 | $('#'+unit_tag+' input, #'+unit_tag+' select, #'+unit_tag+' textarea, #'+unit_tag+' button').on('input paste change click',{unit_tag:unit_tag, conditions:conditions, settings:settings}, function(e) { |
| 30 | clearTimeout(timeout); |
| 31 | timeout = setTimeout(wpcf7cf.display_fields, 100, e.data.unit_tag, e.data.conditions, e.data.settings); |
| 32 | }); |
| 33 | |
| 34 | // bring form in initial state if the reset event is fired on it. |
| 35 | $('#'+unit_tag+' form').on('reset', {unit_tag:unit_tag, conditions:conditions, settings:settings}, function(e) { |
| 36 | setTimeout(wpcf7cf.display_fields, 200, e.data.unit_tag, e.data.conditions, e.data.settings); |
| 37 | }); |
| 38 | |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | //removed pro functions |
| 43 | }, |
| 44 | display_fields : function(unit_tag, wpcf7cf_conditions, wpcf7cf_settings) { |
| 45 | |
| 46 | var $ = jQuery; |
| 47 | |
| 48 | $current_form = $('#'+unit_tag); |
| 49 | $groups = $("[data-class='wpcf7cf_group']",$current_form); |
| 50 | |
| 51 | //for compatibility with contact-form-7-signature-addon |
| 52 | if (cf7signature_resized == 0 && typeof signatures !== 'undefined' && signatures.constructor === Array && signatures.length > 0 ) { |
| 53 | for (var i = 0; i < signatures.length; i++) { |
| 54 | if (signatures[i].canvas.width == 0) { |
| 55 | |
| 56 | jQuery(".wpcf7-form-control-signature-body>canvas").eq(i).attr('width', jQuery(".wpcf7-form-control-signature-wrap").width()); |
| 57 | jQuery(".wpcf7-form-control-signature-body>canvas").eq(i).attr('height', jQuery(".wpcf7-form-control-signature-wrap").height()); |
| 58 | |
| 59 | cf7signature_resized = 1; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | $groups.addClass('wpcf7cf-hidden'); |
| 65 | |
| 66 | for (var i=0; i < wpcf7cf_conditions.length; i++) { |
| 67 | |
| 68 | var condition = wpcf7cf_conditions[i]; |
| 69 | |
| 70 | // compatibility with conditional forms created with older versions of the plugin ( < 1.4 ) |
| 71 | if (!('and_rules' in condition)) { |
| 72 | condition.and_rules = [{'if_field':condition.if_field,'if_value':condition.if_value,'operator':condition.operator}]; |
| 73 | } |
| 74 | |
| 75 | var show_group = wpcf7cf.should_group_be_shown(condition, $current_form); |
| 76 | |
| 77 | if (show_group) { |
| 78 | $('[data-id='+condition.then_field+']',$current_form).eq(0).removeClass('wpcf7cf-hidden'); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | var animation_intime = parseInt(wpcf7cf_settings.animation_intime); |
| 83 | var animation_outtime = parseInt(wpcf7cf_settings.animation_outtime); |
| 84 | |
| 85 | if (wpcf7cf_settings.animation === 'no') { |
| 86 | animation_intime = 0; |
| 87 | animation_outtime = 0; |
| 88 | } |
| 89 | |
| 90 | $groups.each(function (index) { |
| 91 | $group = $(this); |
| 92 | if ($group.is(':animated')) $group.finish(); // stop any current animations on the group |
| 93 | if ($group.css('display') === 'none' && !$group.hasClass('wpcf7cf-hidden')) { |
| 94 | if ($group.prop('tagName') === 'SPAN') { |
| 95 | $group.show().trigger('wpcf7cf_show_group'); |
| 96 | } else { |
| 97 | $group.animate(show_animation, animation_intime).trigger('wpcf7cf_show_group'); // show |
| 98 | } |
| 99 | } else if ($group.css('display') !== 'none' && $group.hasClass('wpcf7cf-hidden')) { |
| 100 | if ($group.prop('tagName') === 'SPAN') { |
| 101 | $group.hide().trigger('wpcf7cf_hide_group'); |
| 102 | } else { |
| 103 | $group.animate(hide_animation, animation_outtime).trigger('wpcf7cf_hide_group'); // hide |
| 104 | } |
| 105 | |
| 106 | if ($group.attr('data-clear_on_hide') !== undefined) { |
| 107 | $inputs = $(':input', $group).not(':button, :submit, :reset, :hidden'); |
| 108 | $inputs.prop('checked', false).prop('selected', false).prop('selectedIndex', 0); |
| 109 | $inputs.not('[type=checkbox],[type=radio],select').val(''); |
| 110 | $inputs.change(); |
| 111 | //display_fields(); |
| 112 | } |
| 113 | } |
| 114 | }); |
| 115 | |
| 116 | wpcf7cf.wpcf7cf_update_hidden_fields($current_form); |
| 117 | }, |
| 118 | wpcf7cf_update_hidden_fields : function($form) { |
| 119 | |
| 120 | var $ = jQuery; |
| 121 | |
| 122 | $hidden_group_fields = $form.find('[name="_wpcf7cf_hidden_group_fields"]'); |
| 123 | $hidden_groups = $form.find('[name="_wpcf7cf_hidden_groups"]'); |
| 124 | $visible_groups = $form.find('[name="_wpcf7cf_visible_groups"]'); |
| 125 | $repeaters = $form.find('[name="_wpcf7cf_repeaters"]'); |
| 126 | |
| 127 | var hidden_fields = []; |
| 128 | var hidden_groups = []; |
| 129 | var visible_groups = []; |
| 130 | |
| 131 | $form.find('[data-class="wpcf7cf_group"]').each(function () { |
| 132 | var $this = $(this); |
| 133 | if ($this.hasClass('wpcf7cf-hidden')) { |
| 134 | hidden_groups.push($this.data('id')); |
| 135 | $this.find('input,select,textarea').each(function () { |
| 136 | hidden_fields.push($(this).attr('name')); |
| 137 | }); |
| 138 | } else { |
| 139 | visible_groups.push($this.data('id')); |
| 140 | } |
| 141 | }); |
| 142 | |
| 143 | $hidden_group_fields.val(JSON.stringify(hidden_fields)); |
| 144 | $hidden_groups.val(JSON.stringify(hidden_groups)); |
| 145 | $visible_groups.val(JSON.stringify(visible_groups)); |
| 146 | |
| 147 | return true; |
| 148 | }, |
| 149 | should_group_be_shown : function(condition, $current_form) { |
| 150 | |
| 151 | var $ = jQuery; |
| 152 | |
| 153 | var show_group = true; |
| 154 | |
| 155 | for (var and_rule_i = 0; and_rule_i < condition.and_rules.length; and_rule_i++) { |
| 156 | |
| 157 | var condition_ok = false; |
| 158 | |
| 159 | var condition_and_rule = condition.and_rules[and_rule_i]; |
| 160 | |
| 161 | var regex_patt = new RegExp(condition_and_rule.if_value, 'i'); |
| 162 | |
| 163 | $field = $('[name="' + condition_and_rule.if_field + '"], [name="' + condition_and_rule.if_field + '[]"], [data-original-name="' + condition_and_rule.if_field + '"], [data-original-name="' + condition_and_rule.if_field + '[]"]',$current_form); //, [data-original-name="' + condition_and_rule.if_field + '"] |
| 164 | |
| 165 | //TODO: ignore everything outside the sub_repeater if field is inside sub_repeater |
| 166 | |
| 167 | if ($field.length == 1) { |
| 168 | |
| 169 | // single field (tested with text field, single checkbox, select with single value (dropdown), select with multiple values) |
| 170 | |
| 171 | if ($field.is('select')) { |
| 172 | |
| 173 | if (condition_and_rule.operator == 'not equals') { |
| 174 | condition_ok = true; |
| 175 | } |
| 176 | |
| 177 | $field.find('option:selected').each(function () { |
| 178 | var $option = $(this); |
| 179 | option_val = $option.val() |
| 180 | if ( |
| 181 | condition_and_rule.operator == 'equals' && option_val == condition_and_rule.if_value || |
| 182 | condition_and_rule.operator == 'equals (regex)' && regex_patt.test($option.val()) |
| 183 | ) { |
| 184 | condition_ok = true; |
| 185 | } else if ( |
| 186 | condition_and_rule.operator == 'not equals' && option_val == condition_and_rule.if_value || |
| 187 | condition_and_rule.operator == 'not equals (regex)' && !regex_patt.test($option.val()) |
| 188 | ) { |
| 189 | condition_ok = false; |
| 190 | return false; // break out of the loop |
| 191 | } |
| 192 | }); |
| 193 | |
| 194 | show_group = show_group && condition_ok; |
| 195 | } |
| 196 | |
| 197 | if ($field.attr('type') == 'checkbox') { |
| 198 | if ( |
| 199 | condition_and_rule.operator == 'equals' && $field.is(':checked') && $field.val() == condition_and_rule.if_value || |
| 200 | condition_and_rule.operator == 'not equals' && !$field.is(':checked') || |
| 201 | condition_and_rule.operator == 'is empty' && !$field.is(':checked') || |
| 202 | condition_and_rule.operator == 'not empty' && $field.is(':checked') || |
| 203 | condition_and_rule.operator == '>' && $field.is(':checked') && $field.val() > condition_and_rule.if_value || |
| 204 | condition_and_rule.operator == '<' && $field.is(':checked') && $field.val() < condition_and_rule.if_value || |
| 205 | condition_and_rule.operator == '>=' && $field.is(':checked') && $field.val() >= condition_and_rule.if_value || |
| 206 | condition_and_rule.operator == '<=' && $field.is(':checked') && $field.val() <= condition_and_rule.if_value || |
| 207 | condition_and_rule.operator == 'equals (regex)' && $field.is(':checked') && regex_patt.test($field.val()) || |
| 208 | condition_and_rule.operator == 'not equals (regex)' && !$field.is(':checked') |
| 209 | ) { |
| 210 | condition_ok = true; |
| 211 | } |
| 212 | } else if ( |
| 213 | ( condition_and_rule.operator == 'equals' && $field.val() == condition_and_rule.if_value ) || |
| 214 | ( condition_and_rule.operator == 'not equals' && $field.val() != condition_and_rule.if_value ) || |
| 215 | ( condition_and_rule.operator == 'equals (regex)' && regex_patt.test($field.val()) ) || |
| 216 | ( condition_and_rule.operator == 'not equals (regex)' && !regex_patt.test($field.val()) ) || |
| 217 | ( condition_and_rule.operator == '>' && $field.val() > condition_and_rule.if_value ) || |
| 218 | ( condition_and_rule.operator == '<' && $field.val() < condition_and_rule.if_value ) || |
| 219 | ( condition_and_rule.operator == '>=' && $field.val() >= condition_and_rule.if_value ) || |
| 220 | ( condition_and_rule.operator == '<=' && $field.val() <= condition_and_rule.if_value ) || |
| 221 | ( condition_and_rule.operator == 'is empty' && $field.val() == '' ) || |
| 222 | ( condition_and_rule.operator == 'not empty' && $field.val() != '' ) |
| 223 | ) { |
| 224 | condition_ok = true; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | } else if ($field.length > 1) { |
| 229 | |
| 230 | // multiple fields (tested with checkboxes, exclusive checkboxes, dropdown with multiple values) |
| 231 | |
| 232 | var all_values = []; |
| 233 | var checked_values = []; |
| 234 | $field.each(function () { |
| 235 | all_values.push($(this).val()); |
| 236 | if ($(this).is(':checked')) { |
| 237 | checked_values.push($(this).val()); |
| 238 | } |
| 239 | }); |
| 240 | |
| 241 | var checked_value_index = $.inArray(condition_and_rule.if_value, checked_values); |
| 242 | var value_index = $.inArray(condition_and_rule.if_value, all_values); |
| 243 | |
| 244 | if ( |
| 245 | ( condition_and_rule.operator == 'is empty' && checked_values.length == 0 ) || |
| 246 | ( condition_and_rule.operator == 'not empty' && checked_values.length > 0 ) |
| 247 | ) { |
| 248 | condition_ok = true; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | for (var ind = 0; ind < checked_values.length; ind++) { |
| 253 | if ( |
| 254 | ( condition_and_rule.operator == 'equals' && checked_values[ind] == condition_and_rule.if_value ) || |
| 255 | ( condition_and_rule.operator == 'not equals' && checked_values[ind] != condition_and_rule.if_value ) || |
| 256 | ( condition_and_rule.operator == 'equals (regex)' && regex_patt.test(checked_values[ind]) ) || |
| 257 | ( condition_and_rule.operator == 'not equals (regex)' && !regex_patt.test(checked_values[ind]) ) || |
| 258 | ( condition_and_rule.operator == '>' && checked_values[ind] > condition_and_rule.if_value ) || |
| 259 | ( condition_and_rule.operator == '<' && checked_values[ind] < condition_and_rule.if_value ) || |
| 260 | ( condition_and_rule.operator == '>=' && checked_values[ind] >= condition_and_rule.if_value ) || |
| 261 | ( condition_and_rule.operator == '<=' && checked_values[ind] <= condition_and_rule.if_value ) |
| 262 | ) { |
| 263 | condition_ok = true; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | show_group = show_group && condition_ok; |
| 269 | } |
| 270 | |
| 271 | return show_group; |
| 272 | |
| 273 | }, |
| 274 | //removed pro functions |
| 275 | }; |
| 276 | |
| 277 | |
| 278 | |
| 279 | (function($) { |
| 280 | |
| 281 | $('.wpcf7-form').each(function(){ |
| 282 | wpcf7cf.initForm($(this)); |
| 283 | }); |
| 284 | |
| 285 | // fix for exclusive checkboxes in IE (this will call the change-event again after all other checkboxes are unchecked, triggering the display_fields() function) |
| 286 | var old_wpcf7ExclusiveCheckbox = $.fn.wpcf7ExclusiveCheckbox; |
| 287 | $.fn.wpcf7ExclusiveCheckbox = function() { |
| 288 | return this.find('input:checkbox').click(function() { |
| 289 | var name = $(this).attr('name'); |
| 290 | $(this).closest('form').find('input:checkbox[name="' + name + '"]').not(this).prop('checked', false).eq(0).change(); |
| 291 | }); |
| 292 | }; |
| 293 | |
| 294 | })( jQuery ); |