| 1 |
jQuery(document).ready(function ($) { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
//Getting the localize script data |
| 5 |
var {emailkit_pro_status} = emailkit; |
| 6 |
|
| 7 |
// Frequently used elements |
| 8 |
var emailkitTemplateLists = $('.emailkit-template-item'); |
| 9 |
var emailTypeDropdown = $('#emailkit-add-new-form-model__form-type'); |
| 10 |
var templateTypeDropdown = $('#emailkit-add-new-email__template-type'); |
| 11 |
let emailkitWrapper = jQuery('.emailkit-template-wrapper'); |
| 12 |
var form = $('#emailkit-form'); |
| 13 |
var notice = $('#emailkit-notice'); |
| 14 |
var newFormModal = $('#emailkit_new_form_modal'); |
| 15 |
let responseData = ''; |
| 16 |
emailkitWrapper.hide(); |
| 17 |
// Flag to control form reset |
| 18 |
var resetFormFlag = true; |
| 19 |
// Open Template Create Modalg |
| 20 |
$('.page-title-action').on('click', function (e) { |
| 21 |
e.preventDefault(); |
| 22 |
newFormModal.modal('show'); |
| 23 |
$('#emailkit-add-new-form-model__form-type').val('wordpress'); |
| 24 |
$('#emailkit-add-new-form-model__form-type').trigger('change'); |
| 25 |
}); |
| 26 |
|
| 27 |
// Modify URL to go to the emailkit builder |
| 28 |
$('.row-title').each(function (_, item) { |
| 29 |
item.href = item.href.replace(/(action=)[^&]+/g, '$1' + 'emailkit-builder'); |
| 30 |
}); |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
// Initialize the Select2 dropdown |
| 36 |
templateTypeDropdown.select2({ |
| 37 |
dropdownParent: newFormModal, |
| 38 |
width: '95%', |
| 39 |
placeholder: "Select email type", |
| 40 |
containerCssClass: 'custom-dropdown' |
| 41 |
}); |
| 42 |
|
| 43 |
|
| 44 |
templateTypeDropdown.on('select2:open', function (e) { |
| 45 |
$('input.select2-search__field').prop('placeholder', 'Search...'); |
| 46 |
$('.select2-container').addClass('emailkit-template-type-dropdown'); |
| 47 |
}); |
| 48 |
|
| 49 |
// Remove the custom class when the dropdown is closed |
| 50 |
templateTypeDropdown.on('select2:close', function (e) { |
| 51 |
$('.select2-container').removeClass('emailkit-template-type-dropdown'); |
| 52 |
}); |
| 53 |
|
| 54 |
|
| 55 |
emailTypeDropdown.select2({ |
| 56 |
dropdownParent: newFormModal, |
| 57 |
width: '95%', |
| 58 |
placeholder: "Select email type", |
| 59 |
containerCssClass: 'custom-dropdown' |
| 60 |
}); |
| 61 |
|
| 62 |
|
| 63 |
emailTypeDropdown.on('select2:open', function (e) { |
| 64 |
$('input.select2-search__field').prop('placeholder', 'Search...'); |
| 65 |
$('.select2-container').addClass('emailkit-email-type-dropdown'); |
| 66 |
}); |
| 67 |
|
| 68 |
// Remove the custom class when the dropdown is closed |
| 69 |
emailTypeDropdown.on('select2:close', function (e) { |
| 70 |
$('.select2-container').removeClass('emailkit-email-type-dropdown'); |
| 71 |
}); |
| 72 |
|
| 73 |
|
| 74 |
// Validate if the user selects dropdown items |
| 75 |
$('.emailkit-open-new-form-editor-modal').on('click', function (e) { |
| 76 |
|
| 77 |
let _self = this; |
| 78 |
|
| 79 |
var selectedEmailType = emailTypeDropdown.val(); |
| 80 |
var selectedTemplateType = templateTypeDropdown.val(); |
| 81 |
|
| 82 |
if (!selectedEmailType) { |
| 83 |
handleValidationFailure('Please select email type'); |
| 84 |
e.preventDefault(); |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
if (!selectedTemplateType || selectedTemplateType === 'Select Template') { |
| 89 |
handleValidationFailure('Please select template type'); |
| 90 |
e.preventDefault(); |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
e.preventDefault(); |
| 95 |
|
| 96 |
// create-from-saved-template |
| 97 |
var url = $(this).attr('resturl'); |
| 98 |
var editAction = $('#emailkit-add-new-form-model__form-type').val().trim(); |
| 99 |
url += editAction !== 'saved-templates'? "template-data/" : "create-from-saved-template/"; |
| 100 |
|
| 101 |
var emailkitProStatus = checkEmailKitProStatus(); |
| 102 |
|
| 103 |
// show popup for pro |
| 104 |
if( ('saved-templates' == editAction) && !emailkit_pro_status){ |
| 105 |
return false; |
| 106 |
} |
| 107 |
jQuery(this).addClass('emailkit-slider-loader'); |
| 108 |
|
| 109 |
$.ajax({ |
| 110 |
url: url, |
| 111 |
type: 'post', |
| 112 |
data: form.serialize(), |
| 113 |
headers: { |
| 114 |
'X-WP-Nonce': emailTypeDropdown.data('nonce'), |
| 115 |
}, |
| 116 |
dataType: 'json', |
| 117 |
success: function (output) { |
| 118 |
handleAjaxSuccess(output); |
| 119 |
}, |
| 120 |
|
| 121 |
beforeSend: function () { |
| 122 |
// $('.emailkit-open-new-form-editor-modal').attr('disabled',true); |
| 123 |
}, |
| 124 |
complete: function (jqXHR, textStatus) { |
| 125 |
// $('.emailkit-open-new-form-editor-modal').attr('disabled',false); |
| 126 |
jQuery(_self).remove('emailkit-slider-loader'); |
| 127 |
|
| 128 |
|
| 129 |
if (jqXHR.status === 404) { |
| 130 |
// $('#emailkitproOverlay').fadeIn(); |
| 131 |
// $('#emailkitproOverlay .content').html('Active EmailKit Pro'); |
| 132 |
jQuery('.emailkit-pro-alert-msg-wrapper').show(); |
| 133 |
jQuery('.emailkit-saved-template-alert-msg').hide(); |
| 134 |
} |
| 135 |
}, |
| 136 |
error: function (jqXHR, textStatus, errorThrown) { |
| 137 |
jQuery(_self).remove('emailkit-slider-loader'); |
| 138 |
console.log('Status Text: ' + textStatus); |
| 139 |
console.log('Error Thrown: ' + errorThrown); |
| 140 |
// $('.emailkit-open-new-form-editor-modal').attr('disabled',false); |
| 141 |
$('#emailkitproOverlay .content').html('Something went wrong'); |
| 142 |
} |
| 143 |
}); |
| 144 |
|
| 145 |
}); |
| 146 |
|
| 147 |
/** |
| 148 |
* Checking EmailKit Pro Status |
| 149 |
*/ |
| 150 |
function checkEmailKitProStatus(){ |
| 151 |
return window.emailkit.emailkit_pro_status.trim() === 'active' ? true : false; |
| 152 |
} |
| 153 |
|
| 154 |
// Handle the "Buy Pro" action |
| 155 |
$('#buyProBtn').on('click', function() { |
| 156 |
closeProNotice(); // close popup and go to buy |
| 157 |
window.open("https://wpmet.com/") |
| 158 |
return false; |
| 159 |
}); |
| 160 |
|
| 161 |
// Handle the "Close" button click |
| 162 |
$('#closeProBtn').on('click', function() { |
| 163 |
closeProNotice(); |
| 164 |
}); |
| 165 |
|
| 166 |
// Close the pro notice on focusout |
| 167 |
$(document).on('click', function(event) { |
| 168 |
if (!$(event.target).closest('.pro-notice').length) { |
| 169 |
closeProNotice(); |
| 170 |
} |
| 171 |
}); |
| 172 |
|
| 173 |
function closeProNotice() { |
| 174 |
$('#emailkitproOverlay').fadeOut(); |
| 175 |
} |
| 176 |
|
| 177 |
// Close popup on click close |
| 178 |
$('.emailkit-add-new-form-modal-close-btn').on('click', function () { |
| 179 |
deleteTemplatePopup.hide(); |
| 180 |
resetFormAndSelect2(); |
| 181 |
$('.emailkit-template-item').css('display', 'block'); // visible all templates items |
| 182 |
}); |
| 183 |
|
| 184 |
|
| 185 |
// Get email template type on change |
| 186 |
emailTypeDropdown.on('change', function () { |
| 187 |
var emailType = emailTypeDropdown.val(); |
| 188 |
templateTypeDropdown.empty(); |
| 189 |
updateTemplateDropdown(); |
| 190 |
emailkitEmailFilter(emailType); |
| 191 |
}); |
| 192 |
|
| 193 |
// Email template type change |
| 194 |
templateTypeDropdown.on('change', function () { |
| 195 |
resetFormFlag = false; // Set the flag to false to prevent form reset |
| 196 |
var templateType = templateTypeDropdown.val(); |
| 197 |
var emailType = emailTypeDropdown.val(); |
| 198 |
if(emailType == 'saved-templates'){ |
| 199 |
let selectedTemplateType = jQuery(this).find('option:selected').text(); |
| 200 |
jQuery('.emailkit-saved-template-name').text(selectedTemplateType); |
| 201 |
jQuery('.emailkit-template-delete-btn-confirm').attr('postId', templateType); |
| 202 |
} |
| 203 |
|
| 204 |
emailkitTemplateFilter(templateType, emailType); |
| 205 |
resetFormFlag = true; // Set the flag back to true after handling the change event |
| 206 |
}); |
| 207 |
|
| 208 |
|
| 209 |
/** |
| 210 |
* @description - show delete saved templates popup |
| 211 |
*/ |
| 212 |
|
| 213 |
let deleteSavedTemplateBtn = jQuery('.emailkit-saved-template-delete'); |
| 214 |
let deleteTemplatePopup = jQuery('.emailkit-template-delete-popover'); |
| 215 |
let closeDeleteTemplatePopup = jQuery('.emailkit-template-delete-btn-cancel'); |
| 216 |
|
| 217 |
|
| 218 |
//Show popup when delete button is clicked ref- https://prnt.sc/RSZCXUn1kTPl |
| 219 |
if(deleteSavedTemplateBtn.length > 0){ |
| 220 |
deleteSavedTemplateBtn.on('click', function(e){ |
| 221 |
e.preventDefault(); |
| 222 |
deleteTemplatePopup.show(); |
| 223 |
}) |
| 224 |
} |
| 225 |
|
| 226 |
if(closeDeleteTemplatePopup.length > 0){ |
| 227 |
closeDeleteTemplatePopup.on('click', function(e){ |
| 228 |
e.preventDefault(); |
| 229 |
deleteTemplatePopup.hide(); |
| 230 |
}) |
| 231 |
} |
| 232 |
|
| 233 |
//Close the popover when clicked outside |
| 234 |
jQuery(document).on('mouseup', function(e){ |
| 235 |
if(!deleteTemplatePopup.is(e.target) && deleteTemplatePopup.has(e.target).length === 0){ |
| 236 |
deleteTemplatePopup.hide(); |
| 237 |
} |
| 238 |
}) |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
/** |
| 243 |
* @description - handling Saved Template Delete feature. |
| 244 |
*/ |
| 245 |
|
| 246 |
if(jQuery('.emailkit-template-delete-btn-confirm').length > 0){ |
| 247 |
jQuery('.emailkit-template-delete-btn-confirm').on('click', function(e){ |
| 248 |
e.preventDefault(); |
| 249 |
let postId = jQuery(this).attr('postid'); |
| 250 |
let postText = jQuery(this).attr('posttext') |
| 251 |
|
| 252 |
|
| 253 |
$.ajax( { |
| 254 |
url: emailkit?.rest_url + `delete-saved-template/${postId}`, |
| 255 |
type: 'delete', |
| 256 |
headers: { |
| 257 |
'X-WP-Nonce': emailkit.rest_nonce, |
| 258 |
}, |
| 259 |
dataType: 'json', |
| 260 |
success: function( response ) { |
| 261 |
emailTypeDropdown.val('saved-templates').trigger('change'); |
| 262 |
deleteTemplatePopup.hide(); |
| 263 |
jQuery('.emailkit-template-delete-success-notification').show(); |
| 264 |
setTimeout(() => { |
| 265 |
jQuery('.emailkit-template-delete-success-notification').hide(); |
| 266 |
}, 2000); |
| 267 |
}, |
| 268 |
error: function(error){ |
| 269 |
console.log(error); |
| 270 |
deleteTemplatePopup.hide(); |
| 271 |
} |
| 272 |
} ); |
| 273 |
|
| 274 |
}); |
| 275 |
} |
| 276 |
// Function to handle validation failure |
| 277 |
function handleValidationFailure(message) { |
| 278 |
showNotice(message); |
| 279 |
} |
| 280 |
|
| 281 |
// Function to handle AJAX success |
| 282 |
function handleAjaxSuccess(output) { |
| 283 |
if (localStorage.getItem('editorState')) { |
| 284 |
localStorage.removeItem('editorState'); |
| 285 |
} |
| 286 |
window.location.href = output.data.builder_url; |
| 287 |
$('#message').css('display', 'block'); |
| 288 |
} |
| 289 |
|
| 290 |
// Function to show notice |
| 291 |
function showNotice(message) { |
| 292 |
notice.text(message); |
| 293 |
notice.css('background', '#d63638'); |
| 294 |
notice.fadeIn().delay(1000).slideUp(); |
| 295 |
} |
| 296 |
|
| 297 |
// Function to reset form |
| 298 |
function resetFormAndSelect2() { |
| 299 |
form[0].reset(); |
| 300 |
templateTypeDropdown.empty().select2({ |
| 301 |
dropdownParent: newFormModal, |
| 302 |
width: '95%', |
| 303 |
placeholder: "Select template", |
| 304 |
data: [{ id: 'Select Template', text: 'Select Template' }], |
| 305 |
}); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Function to filter emails based on the dropdownEmailType. |
| 310 |
* |
| 311 |
* @param {type} dropdownEmailType - the type of email to filter |
| 312 |
* @return {type} - no explicit return value |
| 313 |
*/ |
| 314 |
function emailkitEmailFilter(dropdownEmailType) { |
| 315 |
|
| 316 |
|
| 317 |
if(dropdownEmailType == 'saved-templates'){ |
| 318 |
|
| 319 |
if(!emailkit_pro_status){ |
| 320 |
jQuery('.emailkit-pro-alert-msg-wrapper').show(); |
| 321 |
jQuery('.emailkit-blank-template').hide(); |
| 322 |
|
| 323 |
//disabling the edit with emailkit btn if pro is not active |
| 324 |
$('#editWithEmailkit').prop('disabled', true); |
| 325 |
jQuery('.emailkit-edit-template-btn').addClass('emailkit-pro-inactive'); |
| 326 |
|
| 327 |
|
| 328 |
} else if(emailkit_pro_status){ |
| 329 |
|
| 330 |
jQuery('.emailkit-edit-template-btn').prop('disabled',false); |
| 331 |
jQuery('.emailkit-edit-template-btn').removeClass('emailkit-pro-inactive'); |
| 332 |
|
| 333 |
jQuery('.emailkit-saved-template-alert-msg').show(); |
| 334 |
jQuery('.emailkit-blank-template').hide(); |
| 335 |
} |
| 336 |
jQuery('.emailkit-select-template-type-msg').hide(); |
| 337 |
|
| 338 |
} else { |
| 339 |
jQuery('.emailkit-edit-template-btn').prop('disabled',false); |
| 340 |
jQuery('.emailkit-edit-template-btn').removeClass('emailkit-pro-inactive'); |
| 341 |
jQuery('.emailkit-pro-alert-msg-wrapper').hide(); |
| 342 |
jQuery('.emailkit-saved-template-alert-msg').hide(); |
| 343 |
jQuery('.emailkit-blank-template').show(); |
| 344 |
emailkitTemplateLists.each(function (_, value) { |
| 345 |
var currentTemplate = $(value); |
| 346 |
currentTemplate.css('display', dropdownEmailType !== currentTemplate.data('mail-type') ? 'none' : 'block'); |
| 347 |
}); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* A function to filter emailkit templates based on dropdown selection. |
| 353 |
* |
| 354 |
* @param {type} dropdownTemplateType - description of parameter |
| 355 |
* @param {type} emailType - description of parameter |
| 356 |
* @return {void} |
| 357 |
*/ |
| 358 |
function emailkitTemplateFilter(dropdownTemplateType, emailType) { |
| 359 |
jQuery('.emailkit-template-loader-wrapper').show(); |
| 360 |
jQuery('.emailkit-add-new-form-model-contents').css('overflow-y', 'hidden'); |
| 361 |
|
| 362 |
if('saved-templates' == emailType){ |
| 363 |
$('.emailkit-templates-list').hide() |
| 364 |
filterSaveAsTemplate(dropdownTemplateType, emailType); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
var emailkitTemplateLists = $('.emailkit-template-item'); |
| 369 |
|
| 370 |
if (dropdownTemplateType.trim() === 'Select Template') { |
| 371 |
jQuery('.emailkit-template-loader-wrapper').hide(); |
| 372 |
jQuery('.emailkit-add-new-form-model-contents').css('overflow-y', 'scroll'); |
| 373 |
jQuery('.emailkit-select-template-type-msg').show(); |
| 374 |
jQuery('.emailkit-templates-list').hide(); |
| 375 |
emailkitTemplateLists.css('display', 'block'); |
| 376 |
return; |
| 377 |
} else { |
| 378 |
jQuery('.emailkit-select-template-type-msg').hide(); |
| 379 |
jQuery('.emailkit-templates-list').show(); |
| 380 |
} |
| 381 |
|
| 382 |
emailkitTemplateLists.each(function (_, value) { |
| 383 |
var currentTemplate = $(value); |
| 384 |
|
| 385 |
currentTemplate.css('display', dropdownTemplateType.trim() === currentTemplate.data('template-type').trim() ? 'block' : 'none'); |
| 386 |
}); |
| 387 |
jQuery('.emailkit-add-new-form-model-contents').css('overflow-y', 'scroll'); |
| 388 |
jQuery('.emailkit-template-loader-wrapper').hide(); |
| 389 |
} |
| 390 |
|
| 391 |
function disableEnableEmailkitBtn(el, value) { |
| 392 |
|
| 393 |
if(emailkit_pro_status){ |
| 394 |
jQuery(el).prop('disabled', value); |
| 395 |
} else if(!emailkit_pro_status){ |
| 396 |
jQuery(el).prop('disabled', value); |
| 397 |
} |
| 398 |
|
| 399 |
} |
| 400 |
|
| 401 |
/** |
| 402 |
* Function for filtering saved templates. |
| 403 |
* |
| 404 |
* @param {string} template_id - the ID of the template |
| 405 |
* @param {string} emailType - the type of email |
| 406 |
* @return {json} the response from the AJAX call |
| 407 |
*/ |
| 408 |
function filterSaveAsTemplate(template_id, emailType){ |
| 409 |
jQuery('.emailkit-template-loader-wrapper').show(); |
| 410 |
jQuery('.emailkit-add-new-form-model-contents').css('overflow-y', 'hidden'); |
| 411 |
$.ajax( { |
| 412 |
url: window.ajaxurl, |
| 413 |
type: 'post', |
| 414 |
data: {template_id:template_id,email_type:emailType, action: 'emailkit_filter_save_as_template', nonce: emailkit.nonce}, |
| 415 |
dataType: 'json', |
| 416 |
success: function( response ) { |
| 417 |
jQuery('.emailkit-template-loader-wrapper').hide(); |
| 418 |
jQuery('.emailkit-add-new-form-model-contents').css('overflow-y', 'scroll'); |
| 419 |
if(response.data.dependency_status === false){ |
| 420 |
$('.emailkit-open-new-form-editor-modal').attr('disabled',true); |
| 421 |
$('.emailkit-open-new-form-editor-modal').addClass('emailkit-pro-inactive'); |
| 422 |
// $('.emailkit-open-new-form-editor-modal').css('opacity','.5'); |
| 423 |
$('.emailkit-templates-list').hide() |
| 424 |
emailkitWrapper.show(); |
| 425 |
emailkitWrapper.html("<div><br>" + |
| 426 |
'<p>Looks like you\'ve forgot to <br><a class="emailkit_missing_plugin" target="_blank" href="'+response.data.need_plugin.url+'">' |
| 427 |
|
| 428 |
+response.data.need_plugin.label+` <span class="dashicons dashicons-admin-plugins"></span> |
| 429 |
`+"</a>"+ |
| 430 |
"</p></div>") |
| 431 |
}else{ |
| 432 |
if(emailkit_pro_status){ |
| 433 |
$('.emailkit-open-new-form-editor-modal').attr('disabled',false); |
| 434 |
$('.emailkit-open-new-form-editor-modal').removeClass('emailkit-pro-inactive'); |
| 435 |
} else if(!emailkit_pro_status){ |
| 436 |
$('.emailkit-open-new-form-editor-modal').attr('disabled',true); |
| 437 |
$('.emailkit-open-new-form-editor-modal').addClass('emailkit-pro-inactive'); |
| 438 |
} |
| 439 |
$('.emailkit-open-new-form-editor-modal').css('opacity','1'); |
| 440 |
$('.emailkit-templates-list').show() |
| 441 |
emailkitWrapper.hide(); |
| 442 |
$('.emailkit-templates-list .emailkit-template-item').remove() |
| 443 |
} |
| 444 |
|
| 445 |
}, |
| 446 |
error: function(error){ |
| 447 |
jQuery('.emailkit-template-loader-wrapper').hide(); |
| 448 |
jQuery('.emailkit-add-new-form-model-contents').css('overflow-y', 'scroll'); |
| 449 |
console.log(error); |
| 450 |
} |
| 451 |
} ); |
| 452 |
} |
| 453 |
|
| 454 |
/** |
| 455 |
* Updates the template dropdown based on the selected email type. |
| 456 |
* |
| 457 |
* @param None |
| 458 |
* @return None |
| 459 |
*/ |
| 460 |
function updateTemplateDropdown() { |
| 461 |
jQuery('.emailkit-template-loader-wrapper').show(); |
| 462 |
|
| 463 |
if(emailTypeDropdown.val().trim() == 'saved-templates'){ |
| 464 |
jQuery('.emailkit-templates-list').css('min-height', '250px'); |
| 465 |
} |
| 466 |
|
| 467 |
jQuery('.emailkit-add-new-form-model-contents').css('overflow-y', 'hidden'); |
| 468 |
$.ajax({ |
| 469 |
url: emailkit.ajaxurl, |
| 470 |
type: 'post', |
| 471 |
data: { |
| 472 |
action: 'emailkit_get_email_template_type', |
| 473 |
nonce: emailkit.nonce, |
| 474 |
data: emailTypeDropdown.val(), |
| 475 |
}, |
| 476 |
dataType: 'json', |
| 477 |
success: function (response) { |
| 478 |
|
| 479 |
jQuery('.emailkit-add-new-form-model-contents').css('overflow-y', 'scroll'); |
| 480 |
if(response.data.dependency_status === false){ |
| 481 |
|
| 482 |
$('.emailkit-open-new-form-editor-modal').attr('disabled',true); |
| 483 |
$('.emailkit-open-new-form-editor-modal').addClass('emailkit-pro-inactive'); |
| 484 |
// $('.emailkit-open-new-form-editor-modal').css('opacity','.5'); |
| 485 |
$('.emailkit-templates-list').hide() |
| 486 |
emailkitWrapper.show(); |
| 487 |
emailkitWrapper.html("<div><br>" + |
| 488 |
'<p>Looks like you\'ve forgot to <br><a class="emailkit_missing_plugin" target="_blank" href="'+response.data.need_plugin.url+'">' |
| 489 |
|
| 490 |
+response.data.need_plugin.label+` <span class="dashicons dashicons-admin-plugins"></span> |
| 491 |
`+"</a>"+ |
| 492 |
"</p></div>") |
| 493 |
// $('.emailkit-template-wrapper').text( response.data.btn ) |
| 494 |
}else{ |
| 495 |
$('.emailkit-open-new-form-editor-modal').attr('disabled',false); |
| 496 |
$('.emailkit-open-new-form-editor-modal').css('opacity','1'); |
| 497 |
$('.emailkit-templates-list').show() |
| 498 |
emailkitWrapper.hide(); |
| 499 |
var templates = response.data.templates; |
| 500 |
var data = response.data.template_name; |
| 501 |
templateTypeDropdown.select2({ |
| 502 |
data: data, |
| 503 |
width: '95%', |
| 504 |
dropdownParent: newFormModal, |
| 505 |
}); |
| 506 |
|
| 507 |
$('.emailkit-templates-list .emailkit-template-item').remove() |
| 508 |
$('.emailkit-templates-list').append(...templates); |
| 509 |
if(response && response?.data?.template_name?.length > 0){ |
| 510 |
templateTypeDropdown.val(response?.data?.template_name[0].id).trigger('change'); |
| 511 |
jQuery('.emailkit-saved-template-name').text(data[0]?.text) |
| 512 |
jQuery('.emailkit-template-delete-btn-confirm').attr('postId',data[0]?.id); |
| 513 |
jQuery('.emailkit-template-delete-btn-confirm').attr('postText',data[0]?.text); |
| 514 |
jQuery('.emailkit-template-not-available').hide(); |
| 515 |
jQuery('.emailkit-edit-template-btn').prop('disabled',false); |
| 516 |
} else { |
| 517 |
jQuery('.emailkit-saved-template-info').hide(); |
| 518 |
jQuery('.emailkit-template-not-available').show(); |
| 519 |
jQuery('.emailkit-edit-template-btn').prop('disabled',true); |
| 520 |
} |
| 521 |
} |
| 522 |
jQuery('.emailkit-template-loader-wrapper').hide(); |
| 523 |
if(emailTypeDropdown.val().trim() == 'saved-templates'){ |
| 524 |
jQuery('.emailkit-templates-list').css('min-height', '110px'); |
| 525 |
} |
| 526 |
}, |
| 527 |
|
| 528 |
error: function (data) { |
| 529 |
console.log('Error: ' + data); |
| 530 |
} |
| 531 |
}); |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
//Edit popup |
| 536 |
|
| 537 |
$( '.row-actions .edit a, .emailkit-form-edit-btn, body.post-type-emailkit-form a.row-title' ).on( 'click', function( e ) { |
| 538 |
e.preventDefault(); |
| 539 |
var id = 0; |
| 540 |
var modal = $( '#emailkit_form_modal' ); |
| 541 |
$('.emailkit_update_inputs .update_template_name').val('') |
| 542 |
|
| 543 |
id = (new URL($(this).attr('href')).searchParams.get('post')); |
| 544 |
|
| 545 |
modal.modal( 'show' ); |
| 546 |
|
| 547 |
$('.emailkit_update_inputs #template_id').val(id) |
| 548 |
|
| 549 |
$.ajax( { |
| 550 |
url: window.ajaxurl, |
| 551 |
type: 'post', |
| 552 |
data: {ID:id, action: 'emailkit_template_data', nonce: emailkit.nonce}, |
| 553 |
dataType: 'json', |
| 554 |
success: function( response ) { |
| 555 |
|
| 556 |
$('.emailkit_update_inputs .update_template_name').val(response.data) |
| 557 |
|
| 558 |
}, |
| 559 |
error: function(error){ |
| 560 |
console.log(error); |
| 561 |
} |
| 562 |
} ); |
| 563 |
|
| 564 |
} ); |
| 565 |
|
| 566 |
$( '.emailkit_update_template' ).on( 'click', function( e ) { |
| 567 |
e.preventDefault(); |
| 568 |
var id = null; |
| 569 |
var title = null; |
| 570 |
var actionType = null; |
| 571 |
var modal = $( '#emailkit_form_modal' ); |
| 572 |
actionType = $(e.target).attr('data-action-type') |
| 573 |
id = $('.emailkit_update_inputs #template_id').val(); |
| 574 |
title = $('.emailkit_update_inputs .update_template_name').val(); |
| 575 |
|
| 576 |
if(title.trim() == ''){ |
| 577 |
alert('Please Give a title') |
| 578 |
return false; |
| 579 |
} |
| 580 |
|
| 581 |
$.ajax( { |
| 582 |
url: window.ajaxurl, |
| 583 |
type: 'post', |
| 584 |
data: {id:id, title:title, action_type:actionType, action:'emailkit_update_template_data', nonce: emailkit.nonce}, |
| 585 |
dataType: 'json', |
| 586 |
success: function( response ) { |
| 587 |
|
| 588 |
if(response.data.builder_url){ |
| 589 |
location.href = response.data.builder_url; |
| 590 |
}else{ |
| 591 |
$('.update_close_icon').click() |
| 592 |
window.location.reload(); |
| 593 |
} |
| 594 |
}, |
| 595 |
error: function(error){ |
| 596 |
console.log(error); |
| 597 |
} |
| 598 |
} ); |
| 599 |
|
| 600 |
} ); |
| 601 |
|
| 602 |
}); |