|
1
|
|
-import*as lpUtils from"./../utils.js";import SweetAlert from"sweetalert2";import*as lpToastify from"lpAssetsJsPath/lpToastify.js";let lp_structure_course,popupSweetAlert=null,lp_is_generating_course_data=!1;const lp_course_ai_setting=JSON.parse(localStorage.getItem("lp_course_ai_setting"))||{};let selectGutenberg,dispatchGutenberg,editorGutenberg,isLayoutGutenberg=!1;export class GenerateWithOpenai{constructor(t={}){this.options={autoInsertButtons:!0,isCourseBuilder:!1,titleInputSelector:"input[name=post_title]",courseBuilderTitleInputSelector:"input[name=course_title]",editorIdClassic:"content",editorIdCourseBuilder:"course_description_editor",...t},this.init()}static selectors={elBtnGenerateWithAi:".lp-btn-generate-with-ai",elGenerateDataAiWrap:".lp-generate-data-ai-wrap"};init(){lpData?.enable_open_ai&&(this.options.autoInsertButtons&&(lpUtils.lpOnElementReady("#titlewrap",t=>{t.insertAdjacentHTML("afterend",`<button type="button"\n\t\t\t\t\tstyle="margin-left: 5px"\n\t\t\t\t\tclass="lp-btn-generate-with-ai lp-btn-ai-style"\n\t\t\t\t\tdata-template="#lp-tmpl-edit-title-ai">\n\t\t\t\t\t<i class="lp-ico-ai"></i><span>${lpData.i18n.generate_with_ai}</span>\n\t\t\t\t</button>`)}),lpUtils.lpOnElementReady("#wp-content-media-buttons",t=>{t.insertAdjacentHTML("beforeend",'<button type="button"\n\t\t\t\t\tstyle="padding: 5px 10px; justify-content: center;"\n\t\t\t\t\tclass="lp-btn-generate-with-ai lp-btn-ai-style"\n\t\t\t\t\tdata-template="#lp-tmpl-edit-description-ai">\n\t\t\t\t\t<i class="lp-ico-ai"></i><span>Generate description with AI</span>\n\t\t\t\t</button>')}),lpUtils.lpOnElementReady("#postimagediv",t=>{t.querySelector(".postbox-header").insertAdjacentHTML("afterend",`<button type="button"\n\t\t\t\t\tstyle="padding: 5px 10px; justify-content: center;"\n\t\t\t\t\tclass="lp-btn-generate-with-ai lp-btn-ai-style"\n\t\t\t\t\tdata-template="#lp-tmpl-edit-image-ai">\n\t\t\t\t\t<i class="lp-ico-ai"></i><span>${lpData.i18n.generate_with_ai}</span>\n\t\t\t\t</button>`)}),wp.data&&wp.data.select("core/editor")&&(isLayoutGutenberg=!0,selectGutenberg=wp.data.select,dispatchGutenberg=wp.data.dispatch,editorGutenberg=selectGutenberg("core/editor"),lpUtils.lpOnElementReady(".editor-document-bar",t=>{t.insertAdjacentHTML("afterend",`<button type="button"\n\t\t\t\t\t\tstyle="margin-left: 5px"\n\t\t\t\t\t\tclass="lp-btn-generate-with-ai"\n\t\t\t\t\t\tdata-template="#lp-tmpl-edit-title-ai">\n\t\t\t\t\t\t<i class="lp-ico-ai"></i><span>${lpData.i18n.generate_with_ai}</span>\n\t\t\t\t\t</button>`)}),lpUtils.lpOnElementReady(".editor-post-featured-image",t=>{t.insertAdjacentHTML("beforebegin",'<button type="button"\n\t\t\t\t\t\tstyle="padding: 5px 10px; justify-content: center;"\n\t\t\t\t\t\tclass="lp-btn-generate-with-ai"\n\t\t\t\t\t\tdata-template="#lp-tmpl-edit-description-ai">\n\t\t\t\t\t\t<i class="lp-ico-ai"></i><span>Generate description with AI</span>\n\t\t\t\t\t</button>')}),lpUtils.lpOnElementReady(".editor-post-featured-image",t=>{t.insertAdjacentHTML("afterend",`<button type="button"\n\t\t\t\t\t\tstyle="padding: 5px 10px; justify-content: center;"\n\t\t\t\t\t\tclass="lp-btn-generate-with-ai"\n\t\t\t\t\t\tdata-template="#lp-tmpl-edit-image-ai">\n\t\t\t\t\t\t<i class="lp-ico-ai"></i><span>${lpData.i18n.generate_with_ai}</span>\n\t\t\t\t\t</button>`)}))),this.events())}getTitleInputSelector(){return this.options.isCourseBuilder?this.options.courseBuilderTitleInputSelector:this.options.titleInputSelector}getEditorId(){return this.options.isCourseBuilder?this.options.editorIdCourseBuilder:this.options.editorIdClassic}setCourseBuilderPostId(t){if(!this.options.isCourseBuilder||!t)return;const e=t.querySelector('[name="post-id"]');if(!e)return;const a=document.querySelector(".cb-section__course-edit[data-course-id]"),s=a?.dataset?.courseId||"";s&&(e.value=s)}extractImageSourceFromHtml(t){if(!t)return"";const e=document.createElement("div");return e.innerHTML=t,e.querySelector("img")?.getAttribute("src")||""}applyCourseBuilderFeaturedImage(t,e){return!(!t||!e)&&(document.dispatchEvent(new CustomEvent("lp-course-builder/ai-featured-image-applied",{detail:{attachmentId:t,imageSrc:e}})),!0)}events(){lpUtils.eventHandlers("click",[{selector:GenerateWithOpenai.selectors.elBtnGenerateWithAi,class:this,callBack:this.showPopup.name},{selector:".lp-btn-step",class:this,callBack:this.showStep.name},{selector:".lp-btn-generate-prompt",class:this,callBack:this.generatePrompt.name},{selector:".lp-btn-call-open-ai",class:this,callBack:this.generateData.name},{selector:".lp-btn-copy",class:this,callBack:this.copyGeneratedData.name},{selector:".lp-btn-apply",class:this,callBack:this.applyGeneratedData.name},{selector:".lp-btn-ai-apply-image",class:this,callBack:this.applyImageData.name},{selector:".lp-btn-close-ai-popup",callBack:t=>{const{e:e,target:a}=t,s=lpData.i18n.confirm_close_ai;lp_is_generating_course_data?confirm(s)&&SweetAlert.close():SweetAlert.close()}}])}showPopup(t){const{e:e,target:a}=t,s=a.dataset.template||a.closest(".lp-btn-generate-with-ai").dataset.template||a.closest(".lp-generate-data-ai-wrap").dataset.template||"",i=document.querySelector(s);i?SweetAlert.fire({html:i.innerHTML,width:"60%",showCloseButton:!1,showConfirmButton:!1,allowOutsideClick:!1,allowEscapeKey:!1,didOpen:()=>{popupSweetAlert=SweetAlert.getPopup(),popupSweetAlert.click();const t=document.querySelector(this.getTitleInputSelector());let e="";if(t)e=t.value;else if(isLayoutGutenberg){const t=document.querySelector(".editor-post-card-panel__title-name");t&&(e=t.textContent)}let a="";if(isLayoutGutenberg){a=editorGutenberg.getEditedPostContent().replace(/(<([^>]+)>)/gi,"")}else{const t=this.getEditorId();if(window.tinymce&&window.tinymce.get(t))a=window.tinymce.get(t).getContent({format:"text"});else{const e=document.querySelector(`#${t}`);a=e?e.value:""}}const s=popupSweetAlert.querySelector("form");this.setCourseBuilderPostId(s);const i=s.querySelector("[name=post-title]");if(i&&(i.value=e,!e)){const t=i.closest(".form-group").querySelector(".lp-ai-warning-refer");t&&lpUtils.lpShowHideEl(t,1)}const l=s.querySelector("[name=post-content]");if(l&&(l.value=a,a.length<2)){const t=l.closest(".form-group").querySelector(".lp-ai-warning-refer");t&&lpUtils.lpShowHideEl(t,1)}const n=popupSweetAlert.querySelector('select[name="target_audience"]');n&&(lp_course_ai_setting?.target_audience&&n.tomselect.setValue(lp_course_ai_setting.target_audience),n.addEventListener("change",t=>{lp_course_ai_setting.target_audience=n.tomselect.getValue(),localStorage.setItem("lp_course_ai_setting",JSON.stringify(lp_course_ai_setting))}));const o=popupSweetAlert.querySelector('select[name="tone"]');o&&(lp_course_ai_setting?.tone&&o.tomselect.setValue(lp_course_ai_setting.tone),o.addEventListener("change",t=>{lp_course_ai_setting.tone=o.tomselect.getValue(),localStorage.setItem("lp_course_ai_setting",JSON.stringify(lp_course_ai_setting))}));const r=popupSweetAlert.querySelector('select[name="language"]');r&&(lp_course_ai_setting?.language&&r.tomselect.setValue(lp_course_ai_setting.language),r.addEventListener("change",t=>{const e=r.tomselect.getValue();lp_course_ai_setting.language=e?[e]:[],localStorage.setItem("lp_course_ai_setting",JSON.stringify(lp_course_ai_setting))}))}}).then(t=>{t.isDismissed&&lp_is_generating_course_data&&(lp_is_generating_course_data=!1)}):console.error(`Template ${s} not found!`)}showStep(t){const{e:e,target:a}=t;e.preventDefault();const s=a.closest(".button-actions"),i=s.closest(GenerateWithOpenai.selectors.elGenerateDataAiWrap);let l=parseInt(s.dataset.step);const n=a.dataset.action;"next"===n?l++:"prev"===n&&l--,s.dataset.step=l;const o=a.closest("form"),r=o.querySelector(`.step-content[data-step="${l}"]`),p=i.querySelector(`.step-item[data-step="${l}"]`);o.querySelectorAll(".step-content").forEach(t=>t.classList.remove("active")),r.classList.add("active"),i.querySelectorAll(".step-item").forEach(t=>t.classList.remove("active")),p.classList.add("active");a.closest("form").querySelectorAll("button[data-step-show]").forEach(t=>{t.dataset.stepShow.split(",").map(t=>parseInt(t.trim())).includes(l)?lpUtils.lpShowHideEl(t,1):lpUtils.lpShowHideEl(t,0)})}generatePrompt(t){const{e:e,target:a}=t;e.preventDefault(),lpUtils.lpSetLoadingEl(a,!0);const s=a.closest("form");let i=JSON.parse(a.dataset.send);i=lpUtils.mergeDataWithDatForm(s,i);const l={success:t=>{const{message:e,status:a,data:i}=t;if(lpToastify.show(e,a),"success"===a){s.querySelector("textarea[name=lp-openai-prompt-generated-field]").value=i;s.querySelector(".lp-btn-step[data-action=next]").click()}},error:t=>{lpToastify.show(t,"error")},completed:()=>{lpUtils.lpSetLoadingEl(a,!1)}};window.lpAJAXG.fetchAJAX(i,l)}generateData(t){const{e:e,target:a}=t;e.preventDefault(),lpUtils.lpSetLoadingEl(a,!0);const s=a.closest("form");let i=JSON.parse(a.dataset.send);i=lpUtils.mergeDataWithDatForm(s,i);const l=s.querySelector(".lp-btn-step[data-action=prev]");lpUtils.lpShowHideEl(l,0);const n={success:t=>{const{message:e,status:a,data:i}=t;if(lp_is_generating_course_data&&lpToastify.show(e,a),"success"===a){lp_structure_course=i.lp_structure_course;s.querySelector(".lp-ai-generated-results").innerHTML=i.lp_html_preview;s.querySelector(".lp-btn-step[data-action=next]").click()}},error:t=>{lpToastify.show(t,"error")},completed:()=>{lpUtils.lpSetLoadingEl(a,!1),lpUtils.lpShowHideEl(l,1),lp_is_generating_course_data=!1}};lp_is_generating_course_data=!0,window.lpAJAXG.fetchAJAX(i,n)}applyGeneratedData(t){const{e:e,target:a}=t;e.preventDefault();const s=a.dataset.apply,i=a.dataset.target;if(i)if(isLayoutGutenberg)"set-wp-editor-content"===i?dispatchGutenberg("core/editor").editPost({content:s}):"set-wp-title"===i&&dispatchGutenberg("core/editor").editPost({title:s});else if("set-wp-editor-content"===i)this.setWPEditorContent(s);else if("set-wp-title"===i){const t=document.querySelector(this.getTitleInputSelector());t&&(t.value=s,t.dispatchEvent(new Event("input",{bubbles:!0})))}popupSweetAlert&&SweetAlert.close()}copyGeneratedData(t){const{e:e,target:a}=t;e.preventDefault();const s=a.dataset.copy;if(navigator.clipboard)navigator.clipboard.writeText(s).then(()=>{lpToastify.show("Copied to clipboard!","success")}).catch(t=>{lpToastify.show("Failed to copy text: "+t,"error")});else{const t=document.createElement("textarea");t.value=s,t.style.position="fixed",t.style.left="-9999px",document.body.appendChild(t),t.select();try{const t=document.execCommand("copy");lpToastify.show(t?"Copied to clipboard!":"Failed to copy text",t?"success":"error")}catch(t){lpToastify.show("Failed to copy text: "+t,"error")}document.body.removeChild(t)}}setWPEditorContent(t){const e=this.getEditorId(),a=window.tinymce?.get(e);if(a)return void a.setContent(t);const s=document.querySelector(`#${e}`);s&&(s.value=t,s.dispatchEvent(new Event("input",{bubbles:!0})))}applyImageData(t){const{e:e,target:a}=t;let s=JSON.parse(a.dataset.send);s=lpUtils.mergeDataWithDatForm(a.closest("form"),s),lpUtils.lpSetLoadingEl(a,!0);const i={success:t=>{const{message:e,status:a,data:s}=t;if(lpToastify.show(e,a),"success"===a){if(this.options.isCourseBuilder){const t=parseInt(s?.attachment_id||0,10),e=this.extractImageSourceFromHtml(s?.html_image||"");if(!this.applyCourseBuilderFeaturedImage(t,e))return void lpToastify.show("Failed to apply image to course featured image.","error")}else if(isLayoutGutenberg)dispatchGutenberg("core/editor").editPost({featured_media:s.attachment_id});else{document.querySelector("#postimagediv .inside").outerHTML=s.html_image}popupSweetAlert&&SweetAlert.close()}},error:t=>{lpToastify.show(t,"error")},completed:()=>{lpUtils.lpSetLoadingEl(a,!1)}};window.lpAJAXG.fetchAJAX(s,i)}} |
|
1
|
+import*as lpUtils from"./../utils.js";import SweetAlert from"sweetalert2";import*as lpToastify from"lpAssetsJsPath/lpToastify.js";let lp_structure_course,popupSweetAlert=null,lp_is_generating_course_data=!1;const lp_course_ai_setting=JSON.parse(localStorage.getItem("lp_course_ai_setting"))||{};let selectGutenberg,dispatchGutenberg,editorGutenberg,isLayoutGutenberg=!1;export class GenerateWithOpenai{constructor(t={}){this.options={autoInsertButtons:!0,isCourseBuilder:!1,titleInputSelector:"input[name=post_title]",courseBuilderTitleInputSelector:"input[name=course_title]",editorIdClassic:"content",editorIdCourseBuilder:"course_description_editor",...t},this.init()}static selectors={elBtnGenerateWithAi:".lp-btn-generate-with-ai",elGenerateDataAiWrap:".lp-generate-data-ai-wrap"};init(){lpData?.enable_open_ai&&(this.options.autoInsertButtons&&(lpUtils.lpOnElementReady("#titlewrap",t=>{t.insertAdjacentHTML("afterend",`<button type="button"\n\t\t\t\t\tstyle="margin-left: 5px"\n\t\t\t\t\tclass="lp-btn-generate-with-ai lp-btn-ai-style"\n\t\t\t\t\tdata-template="#lp-tmpl-edit-title-ai">\n\t\t\t\t\t<i class="lp-ico-ai"></i><span>${lpData.i18n.generate_with_ai}</span>\n\t\t\t\t</button>`)}),lpUtils.lpOnElementReady("#wp-content-media-buttons",t=>{t.insertAdjacentHTML("beforeend",'<button type="button"\n\t\t\t\t\tclass="lp-btn-generate-with-ai lp-btn-ai-style"\n\t\t\t\t\tdata-template="#lp-tmpl-edit-description-ai">\n\t\t\t\t\t<i class="lp-ico-ai"></i><span>Generate description with AI</span>\n\t\t\t\t</button>')}),lpUtils.lpOnElementReady("#postimagediv",t=>{t.querySelector(".postbox-header").insertAdjacentHTML("afterend",`<button type="button"\n\t\t\t\t\tclass="lp-btn-generate-with-ai lp-btn-ai-style"\n\t\t\t\t\tdata-template="#lp-tmpl-edit-image-ai">\n\t\t\t\t\t<i class="lp-ico-ai"></i><span>${lpData.i18n.generate_with_ai}</span>\n\t\t\t\t</button>`)}),wp.data&&wp.data.select("core/editor")&&(isLayoutGutenberg=!0,selectGutenberg=wp.data.select,dispatchGutenberg=wp.data.dispatch,editorGutenberg=selectGutenberg("core/editor"),lpUtils.lpOnElementReady(".editor-document-bar",t=>{t.insertAdjacentHTML("afterend",`<button type="button"\n\t\t\t\t\t\tstyle="margin-left: 5px"\n\t\t\t\t\t\tclass="lp-btn-generate-with-ai"\n\t\t\t\t\t\tdata-template="#lp-tmpl-edit-title-ai">\n\t\t\t\t\t\t<i class="lp-ico-ai"></i><span>${lpData.i18n.generate_with_ai}</span>\n\t\t\t\t\t</button>`)}),lpUtils.lpOnElementReady(".editor-post-featured-image",t=>{t.insertAdjacentHTML("beforebegin",'<button type="button"\n\t\t\t\t\t\tclass="lp-btn-generate-with-ai"\n\t\t\t\t\t\tdata-template="#lp-tmpl-edit-description-ai">\n\t\t\t\t\t\t<i class="lp-ico-ai"></i><span>Generate description with AI</span>\n\t\t\t\t\t</button>')}),lpUtils.lpOnElementReady(".editor-post-featured-image",t=>{t.insertAdjacentHTML("afterend",`<button type="button"\n\t\t\t\t\t\tclass="lp-btn-generate-with-ai"\n\t\t\t\t\t\tdata-template="#lp-tmpl-edit-image-ai">\n\t\t\t\t\t\t<i class="lp-ico-ai"></i><span>${lpData.i18n.generate_with_ai}</span>\n\t\t\t\t\t</button>`)}))),this.events())}getTitleInputSelector(){return this.options.isCourseBuilder?this.options.courseBuilderTitleInputSelector:this.options.titleInputSelector}getEditorId(){return this.options.isCourseBuilder?this.options.editorIdCourseBuilder:this.options.editorIdClassic}setCourseBuilderPostId(t){if(!this.options.isCourseBuilder||!t)return;const e=t.querySelector('[name="post-id"]');if(!e)return;const a=document.querySelector(".cb-section__course-edit[data-course-id]"),s=a?.dataset?.courseId||"";s&&(e.value=s)}extractImageSourceFromHtml(t){if(!t)return"";const e=document.createElement("div");return e.innerHTML=t,e.querySelector("img")?.getAttribute("src")||""}applyCourseBuilderFeaturedImage(t,e){return!(!t||!e)&&(document.dispatchEvent(new CustomEvent("lp-course-builder/ai-featured-image-applied",{detail:{attachmentId:t,imageSrc:e}})),!0)}events(){lpUtils.eventHandlers("click",[{selector:GenerateWithOpenai.selectors.elBtnGenerateWithAi,class:this,callBack:this.showPopup.name},{selector:".lp-btn-step",class:this,callBack:this.showStep.name},{selector:".lp-btn-generate-prompt",class:this,callBack:this.generatePrompt.name},{selector:".lp-btn-call-open-ai",class:this,callBack:this.generateData.name},{selector:".lp-btn-copy",class:this,callBack:this.copyGeneratedData.name},{selector:".lp-btn-apply",class:this,callBack:this.applyGeneratedData.name},{selector:".lp-btn-ai-apply-image",class:this,callBack:this.applyImageData.name},{selector:".lp-btn-close-ai-popup",callBack:t=>{const{e:e,target:a}=t,s=lpData.i18n.confirm_close_ai;lp_is_generating_course_data?confirm(s)&&SweetAlert.close():SweetAlert.close()}}])}showPopup(t){const{e:e,target:a}=t,s=a.dataset.template||a.closest(".lp-btn-generate-with-ai").dataset.template||a.closest(".lp-generate-data-ai-wrap").dataset.template||"",i=document.querySelector(s);i?SweetAlert.fire({html:i.innerHTML,width:"60%",showCloseButton:!1,showConfirmButton:!1,allowOutsideClick:!1,allowEscapeKey:!1,didOpen:()=>{popupSweetAlert=SweetAlert.getPopup(),popupSweetAlert.click();const t=document.querySelector(this.getTitleInputSelector());let e="";if(t)e=t.value;else if(isLayoutGutenberg){const t=document.querySelector(".editor-post-card-panel__title-name");t&&(e=t.textContent)}let a="";if(isLayoutGutenberg){a=editorGutenberg.getEditedPostContent().replace(/(<([^>]+)>)/gi,"")}else{const t=this.getEditorId();if(window.tinymce&&window.tinymce.get(t))a=window.tinymce.get(t).getContent({format:"text"});else{const e=document.querySelector(`#${t}`);a=e?e.value:""}}const s=popupSweetAlert.querySelector("form");this.setCourseBuilderPostId(s);const i=s.querySelector("[name=post-title]");if(i&&(i.value=e,!e)){const t=i.closest(".form-group").querySelector(".lp-ai-warning-refer");t&&lpUtils.lpShowHideEl(t,1)}const l=s.querySelector("[name=post-content]");if(l&&(l.value=a,a.length<2)){const t=l.closest(".form-group").querySelector(".lp-ai-warning-refer");t&&lpUtils.lpShowHideEl(t,1)}const o=popupSweetAlert.querySelector('select[name="target_audience"]');o&&(lp_course_ai_setting?.target_audience&&o.tomselect.setValue(lp_course_ai_setting.target_audience),o.addEventListener("change",t=>{lp_course_ai_setting.target_audience=o.tomselect.getValue(),localStorage.setItem("lp_course_ai_setting",JSON.stringify(lp_course_ai_setting))}));const n=popupSweetAlert.querySelector('select[name="tone"]');n&&(lp_course_ai_setting?.tone&&n.tomselect.setValue(lp_course_ai_setting.tone),n.addEventListener("change",t=>{lp_course_ai_setting.tone=n.tomselect.getValue(),localStorage.setItem("lp_course_ai_setting",JSON.stringify(lp_course_ai_setting))}));const r=popupSweetAlert.querySelector('select[name="language"]');r&&(lp_course_ai_setting?.language&&r.tomselect.setValue(lp_course_ai_setting.language),r.addEventListener("change",t=>{const e=r.tomselect.getValue();lp_course_ai_setting.language=e?[e]:[],localStorage.setItem("lp_course_ai_setting",JSON.stringify(lp_course_ai_setting))}))}}).then(t=>{t.isDismissed&&lp_is_generating_course_data&&(lp_is_generating_course_data=!1)}):console.error(`Template ${s} not found!`)}showStep(t){const{e:e,target:a}=t;e.preventDefault();const s=a.closest(".button-actions"),i=s.closest(GenerateWithOpenai.selectors.elGenerateDataAiWrap);let l=parseInt(s.dataset.step);const o=a.dataset.action;"next"===o?l++:"prev"===o&&l--,s.dataset.step=l;const n=a.closest("form"),r=n.querySelector(`.step-content[data-step="${l}"]`),p=i.querySelector(`.step-item[data-step="${l}"]`);n.querySelectorAll(".step-content").forEach(t=>t.classList.remove("active")),r.classList.add("active"),i.querySelectorAll(".step-item").forEach(t=>t.classList.remove("active")),p.classList.add("active");a.closest("form").querySelectorAll("button[data-step-show]").forEach(t=>{t.dataset.stepShow.split(",").map(t=>parseInt(t.trim())).includes(l)?lpUtils.lpShowHideEl(t,1):lpUtils.lpShowHideEl(t,0)})}generatePrompt(t){const{e:e,target:a}=t;e.preventDefault(),lpUtils.lpSetLoadingEl(a,!0);const s=a.closest("form");let i=JSON.parse(a.dataset.send);i=lpUtils.mergeDataWithDatForm(s,i);const l={success:t=>{const{message:e,status:a,data:i}=t;if(lpToastify.show(e,a),"success"===a){s.querySelector("textarea[name=lp-openai-prompt-generated-field]").value=i;s.querySelector(".lp-btn-step[data-action=next]").click()}},error:t=>{lpToastify.show(t,"error")},completed:()=>{lpUtils.lpSetLoadingEl(a,!1)}};window.lpAJAXG.fetchAJAX(i,l)}generateData(t){const{e:e,target:a}=t;e.preventDefault(),lpUtils.lpSetLoadingEl(a,!0);const s=a.closest("form");let i=JSON.parse(a.dataset.send);i=lpUtils.mergeDataWithDatForm(s,i);const l=s.querySelector(".lp-btn-step[data-action=prev]");lpUtils.lpShowHideEl(l,0);const o={success:t=>{const{message:e,status:a,data:i}=t;if(lp_is_generating_course_data&&lpToastify.show(e,a),"success"===a){lp_structure_course=i.lp_structure_course;s.querySelector(".lp-ai-generated-results").innerHTML=i.lp_html_preview;s.querySelector(".lp-btn-step[data-action=next]").click()}},error:t=>{lpToastify.show(t,"error")},completed:()=>{lpUtils.lpSetLoadingEl(a,!1),lpUtils.lpShowHideEl(l,1),lp_is_generating_course_data=!1}};lp_is_generating_course_data=!0,window.lpAJAXG.fetchAJAX(i,o)}applyGeneratedData(t){const{e:e,target:a}=t;e.preventDefault();const s=a.dataset.apply,i=a.dataset.target;if(i)if(isLayoutGutenberg)"set-wp-editor-content"===i?dispatchGutenberg("core/editor").editPost({content:s}):"set-wp-title"===i&&dispatchGutenberg("core/editor").editPost({title:s});else if("set-wp-editor-content"===i)this.setWPEditorContent(s);else if("set-wp-title"===i){const t=document.querySelector(this.getTitleInputSelector());t&&(t.value=s,t.dispatchEvent(new Event("input",{bubbles:!0})))}popupSweetAlert&&SweetAlert.close()}copyGeneratedData(t){const{e:e,target:a}=t;e.preventDefault();const s=a.dataset.copy;if(navigator.clipboard)navigator.clipboard.writeText(s).then(()=>{lpToastify.show("Copied to clipboard!","success")}).catch(t=>{lpToastify.show("Failed to copy text: "+t,"error")});else{const t=document.createElement("textarea");t.value=s,t.style.position="fixed",t.style.left="-9999px",document.body.appendChild(t),t.select();try{const t=document.execCommand("copy");lpToastify.show(t?"Copied to clipboard!":"Failed to copy text",t?"success":"error")}catch(t){lpToastify.show("Failed to copy text: "+t,"error")}document.body.removeChild(t)}}setWPEditorContent(t){const e=this.getEditorId(),a=window.tinymce?.get(e);if(a)return void a.setContent(t);const s=document.querySelector(`#${e}`);s&&(s.value=t,s.dispatchEvent(new Event("input",{bubbles:!0})))}applyImageData(t){const{e:e,target:a}=t;let s=JSON.parse(a.dataset.send);s=lpUtils.mergeDataWithDatForm(a.closest("form"),s),lpUtils.lpSetLoadingEl(a,!0);const i={success:t=>{const{message:e,status:a,data:s}=t;if(lpToastify.show(e,a),"success"===a){if(this.options.isCourseBuilder){const t=parseInt(s?.attachment_id||0,10),e=this.extractImageSourceFromHtml(s?.html_image||"");if(!this.applyCourseBuilderFeaturedImage(t,e))return void lpToastify.show("Failed to apply image to course featured image.","error")}else if(isLayoutGutenberg)dispatchGutenberg("core/editor").editPost({featured_media:s.attachment_id});else{document.querySelector("#postimagediv .inside").outerHTML=s.html_image}popupSweetAlert&&SweetAlert.close()}},error:t=>{lpToastify.show(t,"error")},completed:()=>{lpUtils.lpSetLoadingEl(a,!1)}};window.lpAJAXG.fetchAJAX(s,i)}} |