| 1 |
jQuery(document).ready(function($) { |
| 2 |
function disableBtn(el, exceptEl = '', isTrue = false) { |
| 3 |
if (exceptEl !== "") { |
| 4 |
$(el).not(exceptEl).each((index, item) => { |
| 5 |
$(item).prop('disabled', isTrue); |
| 6 |
}); |
| 7 |
} else { |
| 8 |
$(el).each((index, item) => { |
| 9 |
$(item).prop('disabled', isTrue); |
| 10 |
}); |
| 11 |
} |
| 12 |
} |
| 13 |
|
| 14 |
$('.emailkit-metform-edit-button').on('click', function(e) { |
| 15 |
e.preventDefault(); |
| 16 |
let _self = this; |
| 17 |
|
| 18 |
disableBtn('.emailkit-metform-btn', _self, true); |
| 19 |
|
| 20 |
var builderUrl = $(_self).attr('href'); |
| 21 |
var editorTemplateUrl = $(this).data('editor-template-url'); |
| 22 |
var emailType = $(this).data('emailkit-email-type'); |
| 23 |
var templateType = $(this).data('emailkit-template-type'); |
| 24 |
var templateTitle = $(this).data('emailkit-template-title'); |
| 25 |
var templateStatus = 'active'; |
| 26 |
let formId = $(this).attr('data-emailkit-form'); |
| 27 |
let formTitle = $(this).attr('data-emailkit-form-title'); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
// If builder URL exists, open it directly |
| 32 |
if (builderUrl && builderUrl.trim() !== '') { |
| 33 |
window.open(builderUrl, '_blank'); |
| 34 |
disableBtn('.emailkit-metform-btn', "", false); |
| 35 |
return false; |
| 36 |
} |
| 37 |
|
| 38 |
// $(_self).addClass('emailkit-slider-loader'); |
| 39 |
|
| 40 |
// Check if template exists for this form |
| 41 |
$.ajax({ |
| 42 |
url: metform.rest_url + 'check-template', |
| 43 |
method: 'GET', |
| 44 |
headers: { |
| 45 |
'X-WP-Nonce': metform.rest_nonce, |
| 46 |
}, |
| 47 |
data: { |
| 48 |
'form_id': formId |
| 49 |
}, |
| 50 |
success: function(response) { |
| 51 |
if (response.success && response.data.exists) { |
| 52 |
// Open existing template |
| 53 |
window.open(response.data.builder_url, '_blank'); |
| 54 |
} else { |
| 55 |
// Create new template |
| 56 |
createNewTemplate(); |
| 57 |
} |
| 58 |
}, |
| 59 |
error: function(error) { |
| 60 |
console.error('Template check error:', error); |
| 61 |
createNewTemplate(); // Fallback to create new template |
| 62 |
} |
| 63 |
}); |
| 64 |
|
| 65 |
function createNewTemplate() { |
| 66 |
$.ajax({ |
| 67 |
url: metform.rest_url + 'create-template', |
| 68 |
method: 'POST', |
| 69 |
headers: { |
| 70 |
'X-WP-Nonce': metform.rest_nonce, |
| 71 |
}, |
| 72 |
data: { |
| 73 |
'template_title': 'MetForm - ' + formTitle, |
| 74 |
'form_id': formId, |
| 75 |
'emailkit-editor-template': editorTemplateUrl, |
| 76 |
'emailkit_email_type': emailType, |
| 77 |
'emailkit_template_type': templateType, |
| 78 |
'emailkit_template_title': templateTitle, |
| 79 |
'emailkit_template_status': templateStatus |
| 80 |
}, |
| 81 |
success: function(response) { |
| 82 |
if (response.success && response.data.builder_url) { |
| 83 |
window.open(response.data.builder_url, '_blank'); |
| 84 |
disableBtn('.emailkit-woocom-btn', "", false); |
| 85 |
// jQuery(_self).removeClass('emailkit-slider-loader');; |
| 86 |
} |
| 87 |
}, |
| 88 |
error: function(error) { |
| 89 |
console.error('Template creation error:', error); |
| 90 |
}, |
| 91 |
}); |
| 92 |
} |
| 93 |
}); |
| 94 |
}); |