scripts_admin.js
139 lines
| 1 | /** |
| 2 | * Created by jules on 7/17/2015. |
| 3 | */ |
| 4 | |
| 5 | var old_compose = _wpcf7.taggen.compose; |
| 6 | |
| 7 | (function($) { |
| 8 | |
| 9 | // ...before overwriting the jQuery extension point |
| 10 | _wpcf7.taggen.compose = function(tagType, $form) |
| 11 | { |
| 12 | |
| 13 | $('#tag-generator-panel-group-style-hidden').val($('#tag-generator-panel-group-style').val()); |
| 14 | |
| 15 | // original behavior - use function.apply to preserve context |
| 16 | var ret = old_compose.apply(this, arguments); |
| 17 | //tagType = arguments[0]; |
| 18 | //$form = arguments[1]; |
| 19 | |
| 20 | // START: code here will be executed after the _wpcf7.taggen.update function |
| 21 | if (tagType== 'group') ret += "[/group]"; |
| 22 | // END |
| 23 | |
| 24 | return ret; |
| 25 | }; |
| 26 | |
| 27 | var index = $('#wpcf7cf-entries .entry').length; |
| 28 | |
| 29 | $('.delete-button').click(function(){ |
| 30 | |
| 31 | //if (confirm('You sure?')===false) return false; |
| 32 | $(this).parent().remove(); |
| 33 | return false; |
| 34 | |
| 35 | }); |
| 36 | |
| 37 | $('#wpcf7cf-add-button').click(function(){ |
| 38 | |
| 39 | var id = add_condition_fields(); |
| 40 | |
| 41 | return false; |
| 42 | |
| 43 | }); |
| 44 | |
| 45 | function clear_all_condition_fields() { |
| 46 | $('.entry').remove(); |
| 47 | } |
| 48 | |
| 49 | function add_condition_fields() { |
| 50 | var $delete_button = $('#wpcf7cf-delete-button').clone().removeAttr('id'); |
| 51 | $('<div class="entry" id="entry-'+index+'">'+($('#wpcf7cf-new-entry').html().replace(/{id}/g, index))+'</div>').prependTo('#wpcf7cf-entries').append($delete_button); |
| 52 | $delete_button.click(function(){ |
| 53 | $(this).parent().remove(); |
| 54 | return false; |
| 55 | }); |
| 56 | index++; |
| 57 | |
| 58 | return (index-1); |
| 59 | } |
| 60 | |
| 61 | function import_condition_fields() { |
| 62 | var lines = $('#wpcf7cf-settings-text').val().split(/\r?\n/); |
| 63 | for (var i = lines.length+1; i>-1; i--) { |
| 64 | |
| 65 | var str = lines[i]; |
| 66 | |
| 67 | var match = regex.exec(str); |
| 68 | |
| 69 | if (match == null) continue; |
| 70 | |
| 71 | var id = add_condition_fields(); |
| 72 | |
| 73 | $('#entry-'+id+' .if-field-select').val(match[1]); |
| 74 | $('#entry-'+id+' .operator').val(match[2]); |
| 75 | $('#entry-'+id+' .if-value').val(match[3]); |
| 76 | $('#entry-'+id+' .then-field-select').val(match[4]); |
| 77 | |
| 78 | regex.lastIndex = 0; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // export/import settings |
| 83 | |
| 84 | $('#wpcf7cf-settings-text-wrap').hide(); |
| 85 | |
| 86 | $('#wpcf7cf-settings-to-text').click(function() { |
| 87 | $('#wpcf7cf-settings-text-wrap').show(); |
| 88 | |
| 89 | $('#wpcf7cf-settings-text').val(''); |
| 90 | $('#wpcf7cf-entries .entry').each(function() { |
| 91 | var $entry = $(this); |
| 92 | var line = 'if [' + $entry.find('.if-field-select').val() + ']' |
| 93 | + ' ' + $entry.find('.operator').val() |
| 94 | + ' "' + $entry.find('.if-value').val() + '" then show' |
| 95 | + ' [' + $entry.find('.then-field-select').val() + ']'; |
| 96 | $('#wpcf7cf-settings-text').val($('#wpcf7cf-settings-text').val() + line + "\n" ).select(); |
| 97 | }); |
| 98 | return false; |
| 99 | }); |
| 100 | |
| 101 | var regex = /if \[(.*)] (equals|not equals|equals \(regex\)|not equals \(regex\)|>|>=|<=|<|is empty|not empty) "(.*)" then show \[(.*)]/g; |
| 102 | |
| 103 | $('#add-fields').click(function() { |
| 104 | import_condition_fields(); |
| 105 | update_entries(); |
| 106 | return false; |
| 107 | }); |
| 108 | |
| 109 | $('#overwrite-fields').click(function() { |
| 110 | clear_all_condition_fields(); |
| 111 | import_condition_fields(); |
| 112 | update_entries(); |
| 113 | return false; |
| 114 | }); |
| 115 | |
| 116 | $('#wpcf7cf-settings-text-clear').click(function() { |
| 117 | $('#wpcf7cf-settings-text-wrap').hide(); |
| 118 | $('#wpcf7cf-settings-text').val(''); |
| 119 | return false; |
| 120 | }); |
| 121 | |
| 122 | function update_entries() { |
| 123 | $('.if-value').css({'visibility':'visible'}); |
| 124 | $('.entry').each(function() { |
| 125 | var $entry = $(this); |
| 126 | if ($entry.find('.operator').eq(0).val() == 'is empty' || $entry.find('.operator').eq(0).val() == 'not empty') { |
| 127 | $entry.find('.if-value').eq(0).css({'visibility':'hidden'}); |
| 128 | } |
| 129 | }); |
| 130 | } |
| 131 | |
| 132 | update_entries(); |
| 133 | $('.operator').change(function() { |
| 134 | update_entries(); |
| 135 | }); |
| 136 | |
| 137 | |
| 138 | })( jQuery ); |
| 139 |