scripts_admin.js
371 lines
| 1 | /** |
| 2 | * Created by jules on 7/17/2015. |
| 3 | */ |
| 4 | var $wpcf7cf_new_entry = jQuery('#wpcf7cf-new-entry').eq(0); |
| 5 | |
| 6 | if ($wpcf7cf_new_entry.length > 0) { |
| 7 | |
| 8 | var wpcf7cf_new_and_rule_html = $wpcf7cf_new_entry.find('.wpcf7cf-and-rule')[0].outerHTML; |
| 9 | var wpcf7cf_new_entry_html = $wpcf7cf_new_entry.html(); |
| 10 | |
| 11 | var regex = /show \[(.*)\] if \[(.*)\] (equals|not equals|equals \(regex\)|not equals \(regex\)|>|>=|<=|<|is empty|not empty) "(.*)"/g; |
| 12 | var regex_and = /and if \[(.*)\] (equals|not equals|equals \(regex\)|not equals \(regex\)|>|>=|<=|<|is empty|not empty) "(.*)"/g; |
| 13 | |
| 14 | |
| 15 | if (_wpcf7 == null) { var _wpcf7 = wpcf7}; // wpcf7 4.8 fix |
| 16 | |
| 17 | var old_compose = _wpcf7.taggen.compose; |
| 18 | |
| 19 | var regexes = [ |
| 20 | { label: wpcf7cf_options_0.regex_email_label, desc: wpcf7cf_options_0.regex_email }, |
| 21 | { label: wpcf7cf_options_0.regex_numeric_label, desc: wpcf7cf_options_0.regex_numeric }, |
| 22 | { label: wpcf7cf_options_0.regex_alphanumeric_label, desc: wpcf7cf_options_0.regex_alphanumeric }, |
| 23 | { label: wpcf7cf_options_0.regex_alphabetic_label, desc: wpcf7cf_options_0.regex_alphabetic }, |
| 24 | { label: wpcf7cf_options_0.regex_date_label, desc: wpcf7cf_options_0.regex_date }, |
| 25 | { label: wpcf7cf_options_0.regex_custom_1_label, desc: wpcf7cf_options_0.regex_custom_1 }, |
| 26 | { label: wpcf7cf_options_0.regex_custom_2_label, desc: wpcf7cf_options_0.regex_custom_2 }, |
| 27 | { label: wpcf7cf_options_0.regex_custom_3_label, desc: wpcf7cf_options_0.regex_custom_3 }, |
| 28 | { label: wpcf7cf_options_0.regex_custom_4_label, desc: wpcf7cf_options_0.regex_custom_4 }, |
| 29 | { label: wpcf7cf_options_0.regex_custom_5_label, desc: wpcf7cf_options_0.regex_custom_5 }, |
| 30 | ]; |
| 31 | |
| 32 | var i = regexes.length; |
| 33 | while (i--) { |
| 34 | if (null == regexes[i].label || null == regexes[i].desc || regexes[i].label == '' || regexes[i].desc == '') { |
| 35 | regexes.splice(i,1); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | var termTemplate = "<span class='ui-autocomplete-term'>%s</span>"; |
| 40 | |
| 41 | (function($) { |
| 42 | |
| 43 | $('#wpcf7cf-entries').sortable(); |
| 44 | $(('.wpcf7cf-and-rules')).sortable(); |
| 45 | |
| 46 | |
| 47 | // ...before overwriting the jQuery extension point |
| 48 | _wpcf7.taggen.compose = function(tagType, $form) |
| 49 | { |
| 50 | |
| 51 | $('#tag-generator-panel-group-style-hidden').val($('#tag-generator-panel-group-style').val()); |
| 52 | |
| 53 | // original behavior - use function.apply to preserve context |
| 54 | var ret = old_compose.apply(this, arguments); |
| 55 | //tagType = arguments[0]; |
| 56 | //$form = arguments[1]; |
| 57 | |
| 58 | // START: code here will be executed after the _wpcf7.taggen.update function |
| 59 | if (tagType== 'group') ret += "[/group]"; |
| 60 | if (tagType== 'repeater') ret += "[/repeater]"; |
| 61 | |
| 62 | // END |
| 63 | |
| 64 | if (tagType== 'togglebutton') { |
| 65 | $val1 = $('#tag-generator-panel-togglebutton-value-1'); |
| 66 | $val2 = $('#tag-generator-panel-togglebutton-value-2'); |
| 67 | var val1 = $val1.val(); |
| 68 | var val2 = $val2.val(); |
| 69 | |
| 70 | if (val1 == "") val1 = $val1.data('default'); |
| 71 | if (val2 == "") val2 = $val2.data('default'); |
| 72 | |
| 73 | str_val = ' "'+val1+'" "'+val2+'"'; |
| 74 | |
| 75 | ret = ret.replace(']', str_val+']'); |
| 76 | } |
| 77 | |
| 78 | return ret; |
| 79 | }; |
| 80 | |
| 81 | var index = $('#wpcf7cf-entries .entry').length; |
| 82 | var index_and = 0; |
| 83 | |
| 84 | $('#wpcf7cf-add-button').click(function(){ |
| 85 | |
| 86 | var id = add_condition_fields(); |
| 87 | |
| 88 | return false; |
| 89 | |
| 90 | }); |
| 91 | |
| 92 | function clear_all_condition_fields() { |
| 93 | $('.entry').remove(); |
| 94 | } |
| 95 | |
| 96 | function add_condition_fields() { |
| 97 | $('<div class="entry" id="entry-'+index+'">'+(wpcf7cf_new_entry_html.replace(/{id}/g, index))+'</div>').appendTo('#wpcf7cf-entries'); |
| 98 | index++; |
| 99 | update_entries(); |
| 100 | update_settings_textarea(); |
| 101 | return (index-1); |
| 102 | } |
| 103 | |
| 104 | function add_and_condition_fields(id) { |
| 105 | // $('#entry-'+id+' .wpcf7cf-and-rules').eq(0).append($wpcf7cf_new_and_rule.clone()); |
| 106 | $('#entry-'+id+' .wpcf7cf-and-rules').eq(0).append(wpcf7cf_new_and_rule_html.replace(/{id}/g, index-1).replace(/\[and_rules\]\[0\]/g, '[and_rules]['+index_and+']')); |
| 107 | index_and++; |
| 108 | return (index_and-1); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Copy the textarea field to the entries. |
| 113 | */ |
| 114 | function import_condition_fields() { |
| 115 | |
| 116 | $if_values = $('.if-value'); |
| 117 | |
| 118 | var lines = $('#wpcf7cf-settings-text').val().split(/\r?\n/); |
| 119 | |
| 120 | var id = -1; |
| 121 | |
| 122 | for (var i = 0; i<lines.length; i++) { |
| 123 | |
| 124 | var str = lines[i]; |
| 125 | |
| 126 | var match = regex.exec(str); |
| 127 | |
| 128 | if (match != null) { |
| 129 | |
| 130 | index_and = 0; // reset this for each first condition (This one has and_index [0]). |
| 131 | |
| 132 | id = add_condition_fields(); |
| 133 | |
| 134 | $('#entry-'+id+' .then-field-select').val(match[1]); |
| 135 | $('#entry-'+id+' .if-field-select').val(match[2]); |
| 136 | $('#entry-'+id+' .operator').val(match[3]); |
| 137 | $('#entry-'+id+' .if-value').val(match[4]); |
| 138 | |
| 139 | index_and = 1; // the next and condition will have and_index [1]; |
| 140 | |
| 141 | regex.lastIndex = 0; |
| 142 | |
| 143 | } |
| 144 | |
| 145 | match = regex_and.exec(str); |
| 146 | |
| 147 | if (match != null && id != -1) { |
| 148 | |
| 149 | var and_id = add_and_condition_fields(id); |
| 150 | |
| 151 | $('#entry-'+id+' .wpcf7cf-and-rule:last-child .if-field-select').val(match[1]); |
| 152 | $('#entry-'+id+' .wpcf7cf-and-rule:last-child .operator').val(match[2]); |
| 153 | $('#entry-'+id+' .wpcf7cf-and-rule:last-child .if-value').val(match[3]); |
| 154 | |
| 155 | regex_and.lastIndex = 0; |
| 156 | |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | update_entries(); |
| 161 | |
| 162 | } |
| 163 | |
| 164 | $('#wpcf7-admin-form-element').on('submit.wpcf7cf', function() { |
| 165 | update_settings_textarea(); |
| 166 | return true; |
| 167 | }); |
| 168 | |
| 169 | // export/import settings |
| 170 | |
| 171 | $('#wpcf7cf-settings-text-wrap').hide(); |
| 172 | |
| 173 | $('#wpcf7cf-settings-to-text').click(function() { |
| 174 | $('#wpcf7cf-settings-text-wrap').show(); |
| 175 | update_settings_textarea(); |
| 176 | return false; |
| 177 | }); |
| 178 | |
| 179 | /** |
| 180 | * Copy the entries to the textarea field. |
| 181 | */ |
| 182 | function update_settings_textarea() { |
| 183 | $('#wpcf7cf-settings-text').val(''); |
| 184 | $('#wpcf7cf-entries .entry').each(function() { |
| 185 | var $entry = $(this); |
| 186 | var line = 'show [' + $entry.find('.then-field-select').val() + ']'; |
| 187 | var text_indent = line.length-3; |
| 188 | $entry.find('.wpcf7cf-and-rule').each(function(i) { |
| 189 | $and_rule = $(this); |
| 190 | if (i>0) { |
| 191 | |
| 192 | line += '\n'+' '.repeat(text_indent)+'and'; |
| 193 | |
| 194 | } |
| 195 | line += ' if [' + $and_rule.find('.if-field-select').val() + ']' |
| 196 | + ' ' + $and_rule.find('.operator').val() |
| 197 | + ' "' + $and_rule.find('.if-value').val() + '"'; |
| 198 | }); |
| 199 | $('#wpcf7cf-settings-text').val($('#wpcf7cf-settings-text').val() + line + "\n" ); |
| 200 | }); |
| 201 | } |
| 202 | |
| 203 | $if_values = $('.if-value'); |
| 204 | |
| 205 | $('#add-fields').click(function() { |
| 206 | import_condition_fields(); |
| 207 | return false; |
| 208 | }); |
| 209 | |
| 210 | $('#overwrite-fields').click(function() { |
| 211 | clear_all_condition_fields(); |
| 212 | import_condition_fields(); |
| 213 | return false; |
| 214 | }); |
| 215 | |
| 216 | $('#wpcf7cf-settings-text').on('change.wpcf7cf', function(){ |
| 217 | clear_all_condition_fields(); |
| 218 | import_condition_fields(); |
| 219 | return true; |
| 220 | }); |
| 221 | |
| 222 | |
| 223 | |
| 224 | $('#wpcf7cf-settings-text-clear').click(function() { |
| 225 | $('#wpcf7cf-settings-text-wrap').hide(); |
| 226 | $('#wpcf7cf-settings-text').val(''); |
| 227 | return false; |
| 228 | }); |
| 229 | |
| 230 | function update_entries() { |
| 231 | $if_values = $('.if-value'); |
| 232 | init_autocomplete(); |
| 233 | $if_values.css({'visibility':'visible'}); |
| 234 | $if_values.autocomplete( "disable" ); |
| 235 | |
| 236 | $('#wpcf7cf-entries .wpcf7cf-and-rule').each(function() { |
| 237 | var $and_rule = $(this); |
| 238 | if ($and_rule.find('.operator').eq(0).val() === 'is empty' || $and_rule.find('.operator').eq(0).val() === 'not empty') { |
| 239 | $and_rule.find('.if-value').eq(0).css({'visibility':'hidden'}); |
| 240 | } else if ($and_rule.find('.operator').eq(0).val().endsWith('(regex)')) { |
| 241 | $and_rule.find('.if-value').eq(0).autocomplete( "enable" ); |
| 242 | } |
| 243 | }); |
| 244 | |
| 245 | scale_and_button(); |
| 246 | |
| 247 | set_events(); |
| 248 | } |
| 249 | |
| 250 | function init_autocomplete() { |
| 251 | |
| 252 | $if_values.autocomplete({ |
| 253 | disabled: true, |
| 254 | source: function(request, response) { |
| 255 | var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i"); |
| 256 | response($.grep(regexes, function(value) { |
| 257 | return matcher.test(value.label || value.value || value) || matcher.test(value.desc); |
| 258 | })); |
| 259 | }, |
| 260 | focus: function( event, ui ) { |
| 261 | $( event.target ).val( ui.item.desc ); |
| 262 | return false; |
| 263 | }, |
| 264 | select: function( event, ui ) { |
| 265 | $( event.target ).val( ui.item.desc ); |
| 266 | return false; |
| 267 | }, |
| 268 | open: function(e,ui) { |
| 269 | $el = $(e.target); |
| 270 | var styledTerm = termTemplate.replace('%s', $el.val()); |
| 271 | |
| 272 | $('.ui-autocomplete').find('em').each(function() { |
| 273 | var me = $(this); |
| 274 | me.html( me.text().replace($el.val(), styledTerm) ); |
| 275 | }); |
| 276 | }, |
| 277 | minLength: 0 |
| 278 | }).each(function() { |
| 279 | $(this).autocomplete( "instance" )._renderItem = function( ul, item ) { |
| 280 | return $("<li>") |
| 281 | .append("<div><em>" + item.label + "</em><br><em>" + item.desc + "</em></div>") |
| 282 | .appendTo(ul); |
| 283 | } |
| 284 | }); |
| 285 | $if_values.on('focus', function() { |
| 286 | $(this).autocomplete("search"); |
| 287 | }); |
| 288 | } |
| 289 | |
| 290 | update_entries(); |
| 291 | |
| 292 | function set_events() { // called at the end of update_entries |
| 293 | |
| 294 | $('.wpcf7cf-and-rules').sortable(); |
| 295 | |
| 296 | $('.and-button').off('click').click(function() { |
| 297 | $this = $(this); |
| 298 | $andblock = $this.closest('.wpcf7cf-and-rule'); |
| 299 | $andblocks_container = $this.closest('.wpcf7cf-and-rules'); |
| 300 | next_index = $andblocks_container.data('next-index'); |
| 301 | $andblocks_container.data('next-index',next_index+1); |
| 302 | var and_i = next_index; |
| 303 | clone_html = $andblock.get(0).outerHTML.replace(/wpcf7cf_options\[([0-9]*)\]\[and_rules\]\[([0-9]*)\]/g, 'wpcf7cf_options[$1][and_rules]['+and_i+']'); |
| 304 | $andblock.after(clone_html); |
| 305 | update_settings_textarea(); |
| 306 | update_entries(); |
| 307 | return false; |
| 308 | }); |
| 309 | |
| 310 | $('.delete-button').off('click').click(function(){ |
| 311 | $and_rule = $(this).closest('.wpcf7cf-and-rule'); |
| 312 | if ($and_rule.siblings().length > 0) { |
| 313 | $and_rule.remove(); |
| 314 | } else { |
| 315 | $and_rule[0].closest('.entry').remove(); |
| 316 | } |
| 317 | |
| 318 | update_settings_textarea(); |
| 319 | update_entries(); |
| 320 | |
| 321 | return false; |
| 322 | }); |
| 323 | |
| 324 | $('.operator').off('change').change(function() { |
| 325 | update_entries(); |
| 326 | return false; |
| 327 | }); |
| 328 | |
| 329 | $('input,select', '#wpcf7cf-entries').off('change.wpcf7cf_sync').on('change.wpcf7cf_sync', function() { |
| 330 | update_settings_textarea(); |
| 331 | }); |
| 332 | } |
| 333 | |
| 334 | function scale_and_button() { |
| 335 | $('.wpcf7cf-and-rule:first-child .and-button').each(function(){ |
| 336 | $and_button = $(this); |
| 337 | num_and_rules = $and_button.closest('.wpcf7cf-and-rule').siblings().length+1; |
| 338 | var height = (34*num_and_rules-12)+'px'; |
| 339 | $and_button.css({'height':height,'line-height':height}); |
| 340 | }); |
| 341 | } |
| 342 | |
| 343 | })( jQuery ); |
| 344 | |
| 345 | } |
| 346 | |
| 347 | (function($) { |
| 348 | // ------------------------------------ |
| 349 | // OPTIONS PAGE |
| 350 | // ------------------------------------ |
| 351 | |
| 352 | $(document).ready(function() { |
| 353 | |
| 354 | $('.wpcf7cf-options-notice .notice-dismiss-2').click(function () { |
| 355 | $('.wpcf7cf-options-notice .notice-dismiss').click(); |
| 356 | }); |
| 357 | $('.wpcf7cf-options-notice .notice-dismiss').click(function () { |
| 358 | wpcf7cf_dismiss_notice(); |
| 359 | }); |
| 360 | |
| 361 | function wpcf7cf_dismiss_notice() { |
| 362 | |
| 363 | $('input[name="wpcf7cf_options[notice_dismissed]"]').val('true'); |
| 364 | |
| 365 | $.post(ajaxurl, {action:'wpcf7cf_dismiss_notice'}, function(response) { |
| 366 | // nothing to do. dismiss_notice option should be set to TRUE server side by now. |
| 367 | }); |
| 368 | } |
| 369 | |
| 370 | }); |
| 371 | })( jQuery ); |