| 1 |
(function( $ ) { |
| 2 |
|
| 3 |
var prefix = 'automatorwp-aweber-'; |
| 4 |
var _prefix = 'automatorwp_aweber_'; |
| 5 |
|
| 6 |
// On click authorize button |
| 7 |
$('body').on('click', '.automatorwp_settings #' + _prefix + 'authorize', function(e) { |
| 8 |
e.preventDefault(); |
| 9 |
|
| 10 |
var button = $(this); |
| 11 |
var wrapper = button.parent(); |
| 12 |
|
| 13 |
var client_id = $('#' + _prefix + 'client_id').val(); |
| 14 |
var client_secret = $('#' + _prefix + 'client_secret').val(); |
| 15 |
|
| 16 |
// Check if response div exists |
| 17 |
var response_wrap = wrapper.find('#' + _prefix + 'response'); |
| 18 |
|
| 19 |
if( ! response_wrap.length ) { |
| 20 |
wrapper.append( '<div id="' + _prefix + 'response" style="display: none; margin-top: 10px;"></div>' ); |
| 21 |
response_wrap = wrapper.find('#' + _prefix + 'response'); |
| 22 |
} |
| 23 |
|
| 24 |
// Show error message if not correctly configured |
| 25 |
if( client_id.length === 0 || client_secret.length === 0 ) { |
| 26 |
response_wrap.addClass( 'automatorwp-notice-error' ); |
| 27 |
response_wrap.html( 'All fields are required to connect with AWeber' ); |
| 28 |
response_wrap.slideDown('fast'); |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
response_wrap.slideUp('fast'); |
| 33 |
response_wrap.attr('class', ''); |
| 34 |
|
| 35 |
// Show spinner |
| 36 |
wrapper.append('<span class="spinner is-active" style="float: none;"></span>'); |
| 37 |
|
| 38 |
// Disable button |
| 39 |
button.prop('disabled', true); |
| 40 |
|
| 41 |
$.post( |
| 42 |
ajaxurl, |
| 43 |
{ |
| 44 |
action: 'automatorwp_aweber_authorize', |
| 45 |
nonce: automatorwp_aweber.nonce, |
| 46 |
client_id: client_id, |
| 47 |
client_secret: client_secret, |
| 48 |
}, |
| 49 |
function( response ) { |
| 50 |
|
| 51 |
// Add class automatorwp-notice-success on successful unlock, if not will add the class automatorwp-notice-error |
| 52 |
response_wrap.addClass( 'automatorwp-notice-' + ( response.success === true ? 'success' : 'error' ) ); |
| 53 |
response_wrap.html( ( response.data.message !== undefined ? response.data.message : response.data ) ); |
| 54 |
response_wrap.slideDown('fast'); |
| 55 |
|
| 56 |
// Hide spinner |
| 57 |
wrapper.find('.spinner').remove(); |
| 58 |
|
| 59 |
// Redirect on success |
| 60 |
if( response.success === true && response.data.redirect_url !== undefined ) { |
| 61 |
window.location = response.data.redirect_url; |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
// Enable button |
| 66 |
button.prop('disabled', false); |
| 67 |
|
| 68 |
} |
| 69 |
); |
| 70 |
|
| 71 |
}); |
| 72 |
|
| 73 |
// On change account |
| 74 |
$('body').on('change', '.automatorwp-action-aweber-add-user-list .cmb2-id-account select, ' |
| 75 |
+ '.automatorwp-action-aweber-add-user-tag .cmb2-id-account select', function(e) { |
| 76 |
var account = $(this).closest('.cmb-row'); |
| 77 |
var list_row = account.next('.cmb2-id-lists'); |
| 78 |
|
| 79 |
var account_id = $(this).val(); |
| 80 |
var first_change = account.hasClass('is-option-change'); |
| 81 |
|
| 82 |
if( account_id === 'any' || account_id === '' || account_id === null ) { |
| 83 |
// Hide the term selector |
| 84 |
if( first_change ) { |
| 85 |
list_row.hide(); |
| 86 |
} else { |
| 87 |
list_row.slideUp('fast'); |
| 88 |
} |
| 89 |
} else { |
| 90 |
var list_selector = list_row.find('select.select2-hidden-accessible'); |
| 91 |
|
| 92 |
// Remove Select2 element |
| 93 |
list_selector.next('.select2').remove(); |
| 94 |
|
| 95 |
// Update the account (since we do not use the table attribute, lets to use it as account) |
| 96 |
list_selector.data( 'table', account_id ); |
| 97 |
|
| 98 |
// Reset the selector |
| 99 |
list_selector.removeAttr('data-select2-id'); |
| 100 |
|
| 101 |
// Init it again |
| 102 |
automatorwp_ajax_selector( list_selector ); |
| 103 |
|
| 104 |
// Show the term selector |
| 105 |
if( first_change ) { |
| 106 |
list_row.show(); |
| 107 |
} else { |
| 108 |
list_row.slideDown('fast'); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
account.removeClass('is-option-change'); |
| 113 |
}); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
// On click on an option, check if form contains the account selector |
| 118 |
$('body').on('click', '.automatorwp-automation-item-label > .automatorwp-option', function(e) { |
| 119 |
|
| 120 |
var item = $(this).closest('.automatorwp-automation-item'); |
| 121 |
var option = $(this).data('option'); |
| 122 |
var option_form = item.find('.automatorwp-option-form-container[data-option="' + option + '"]'); |
| 123 |
var account_selector = option_form.find('.cmb2-id-account'); |
| 124 |
|
| 125 |
if( account_selector !== undefined ) { |
| 126 |
account_selector.addClass('is-option-change'); |
| 127 |
account_selector.find('select.select2-hidden-accessible').trigger('change'); |
| 128 |
} |
| 129 |
|
| 130 |
}); |
| 131 |
|
| 132 |
})( jQuery ); |