| 1 |
var tfa_query_leaving = false; |
| 2 |
|
| 3 |
// Prevent accidental leaving if there are unsaved settings |
| 4 |
window.onbeforeunload = function(e) { |
| 5 |
if (tfa_query_leaving) { |
| 6 |
e.returnValue = simba_tfa_frontend.ask; |
| 7 |
return simba_tfa_frontend.ask; |
| 8 |
} |
| 9 |
} |
| 10 |
|
| 11 |
jQuery(function($) { |
| 12 |
|
| 13 |
$(".tfa_settings_form input, .tfa_settings_form textarea, .tfa_settings_form select" ).on('change', function() { |
| 14 |
tfa_query_leaving = true; |
| 15 |
}); |
| 16 |
|
| 17 |
$(".tfa_settings_form input[name='simbatfa_delivery_type']").on('change', function() { |
| 18 |
$(".tfa_third_party_holder").slideToggle(); |
| 19 |
}); |
| 20 |
|
| 21 |
$(".simbatfa_settings_save").on('click', function() { |
| 22 |
|
| 23 |
$.blockUI({ message: '<div style="margin: 8px;font-size:150%;">'+simba_tfa_frontend.saving+'</div>' }); |
| 24 |
|
| 25 |
// https://stackoverflow.com/questions/10147149/how-can-i-override-jquerys-serialize-to-include-unchecked-checkboxes |
| 26 |
var form_data = $('.tfa_settings_form input, .tfa_settings_form textarea, .tfa_settings_form select').serialize(); |
| 27 |
|
| 28 |
// Include unchecked checkboxes. Use filter to only include unchecked boxes. |
| 29 |
$.each($('.tfa_settings_form input[type=checkbox]') |
| 30 |
.filter(function(idx) { |
| 31 |
return $(this).prop('checked') === false |
| 32 |
}), |
| 33 |
function(idx, el){ |
| 34 |
// attach matched element names to the form_data with a chosen value. |
| 35 |
var emptyVal = '0'; |
| 36 |
form_data += '&' + $(el).attr('name') + '=' + emptyVal; |
| 37 |
} |
| 38 |
); |
| 39 |
|
| 40 |
$.post(simba_tfa_frontend.ajax_url, { |
| 41 |
action: 'tfa_frontend', |
| 42 |
subaction: 'savesettings', |
| 43 |
settings: form_data, |
| 44 |
nonce: simba_tfa_frontend.nonce |
| 45 |
}, function(response) { |
| 46 |
var settings_saved = false; |
| 47 |
try { |
| 48 |
var resp = JSON.parse(response); |
| 49 |
if (resp.hasOwnProperty('result')) { |
| 50 |
settings_saved = true; |
| 51 |
tfa_query_leaving = false; |
| 52 |
// Allow user code to respond |
| 53 |
$(document).trigger('tfa_settings_saved', resp); |
| 54 |
} |
| 55 |
|
| 56 |
if (resp.hasOwnProperty('message')) { |
| 57 |
alert(resp.message); |
| 58 |
} |
| 59 |
|
| 60 |
if (resp.hasOwnProperty('qr')) { |
| 61 |
$('.simbaotp_qr_container').data('qrcode', resp['qr']).empty().qrcode({ |
| 62 |
"render": "image", |
| 63 |
"text": resp['qr'], |
| 64 |
}); |
| 65 |
} |
| 66 |
|
| 67 |
if (resp.hasOwnProperty('al_type_disp')) { |
| 68 |
$("#al_type_name").html(resp['al_type_disp']['disp']); |
| 69 |
$("#al_type_desc").html(resp['al_type_disp']['desc']); |
| 70 |
} |
| 71 |
|
| 72 |
} catch(err) { |
| 73 |
console.log(err); |
| 74 |
console.log(response); |
| 75 |
if ('' === simba_tfa_frontend.also_try) { |
| 76 |
alert(simba_tfa_frontend.response+response); |
| 77 |
} |
| 78 |
} |
| 79 |
if ('' != simba_tfa_frontend.also_try) { |
| 80 |
if (!settings_saved) { |
| 81 |
$.post(simba_tfa_frontend.also_try, { |
| 82 |
action: 'tfa_frontend', |
| 83 |
subaction: 'savesettings', |
| 84 |
settings: form_data, |
| 85 |
nonce: simba_tfa_frontend.nonce |
| 86 |
}, function(response) { |
| 87 |
|
| 88 |
try { |
| 89 |
var resp = JSON.parse(response); |
| 90 |
if (resp.hasOwnProperty('result')) { |
| 91 |
settings_saved = true; |
| 92 |
tfa_query_leaving = false; |
| 93 |
// Allow user code to respond |
| 94 |
$(document).trigger('tfa_settings_saved', resp); |
| 95 |
} |
| 96 |
if (resp.hasOwnProperty('message')) { |
| 97 |
alert(resp.message); |
| 98 |
} |
| 99 |
if (resp.hasOwnProperty('qr')) { |
| 100 |
$('.simbaotp_qr_container').data('qrcode', resp['qr']).empty().qrcode({ |
| 101 |
"render": "image", |
| 102 |
"text": resp['qr'], |
| 103 |
}); |
| 104 |
} |
| 105 |
if (resp.hasOwnProperty('al_type_disp')) { |
| 106 |
$("#al_type_name").html(resp['al_type_disp']['disp']); |
| 107 |
$("#al_type_desc").html(resp['al_type_disp']['desc']); |
| 108 |
} |
| 109 |
|
| 110 |
} catch(err) { |
| 111 |
console.log(err); |
| 112 |
console.log(response); |
| 113 |
alert(simba_tfa_frontend.response+response); |
| 114 |
} |
| 115 |
$.unblockUI(); |
| 116 |
}); |
| 117 |
} else { |
| 118 |
$.unblockUI(); |
| 119 |
} |
| 120 |
} else { |
| 121 |
$.unblockUI(); |
| 122 |
} |
| 123 |
}); |
| 124 |
|
| 125 |
}); |
| 126 |
}); |
| 127 |
|