(function(){
'use strict';
let quickEditFrontEndDebugEnabled=true;
window.addEventListener('DiadaoWpSdkStarted', event=> {
startScript();
});
function startScript(){
let t_quickEditToggle=jQuery('body a#kwpb-front-editor-toggle');
if((
jQuery('body').hasClass('is-logged-in')
|| jQuery('body').hasClass('logged-in')
)
&& t_quickEditToggle!==undefined
&& t_quickEditToggle.length
){
jQuery('html').addClass('kwpb-fo-edit-enabled');
jQuery('html').addClass('kwpb-fo-edit-inactive');
let t_quickEditButtons=document.querySelectorAll('.diasdk-quickedit-btn');
if(t_quickEditButtons!==undefined
&& t_quickEditButtons!==null
){
t_quickEditButtons.forEach(function(elem){
if(elem.parentElement){
let t_elemParent;
if(elem.parentElement.classList.contains('swiper-wrapper') ){
t_elemParent=elem.parentElement.parentElement;
}else{
t_elemParent=elem.parentElement;
}
t_elemParent.setAttribute('data-posttype', elem.getAttribute('data-posttype') );
t_elemParent.setAttribute('data-postid', elem.getAttribute('data-postid') );
t_elemParent.classList.add('diasdk-quickedit-parent');
}});
}
t_quickEditToggle.click(function(e){
e.preventDefault();
if(jQuery('body').hasClass('kwpb-fo-edit-active') ){
jQuery('html').removeClass('kwpb-fo-edit-active').addClass('kwpb-fo-edit-inactive');
jQuery('body').removeClass('kwpb-fo-edit-active').addClass('kwpb-fo-edit-inactive');
}else{
jQuery('html').removeClass('kwpb-fo-edit-inactive').addClass('kwpb-fo-edit-active');
jQuery('body').removeClass('kwpb-fo-edit-inactive').addClass('kwpb-fo-edit-active');
}});
let quickEditModals=document.querySelectorAll('.diasdk-quickedit-modal');
if(quickEditModals!==undefined
&& quickEditModals!==null
){
quickEditModals.forEach(function(elem){
elem.addEventListener('hide.bs.modal', ()=> {
if(elem.getAttribute('id')!==undefined
&& elem.getAttribute('id')!==null
&& elem.getAttribute('data-key')!==undefined
&& elem.getAttribute('data-key')!==null
&& elem.getAttribute('data-postid')!==undefined
&& elem.getAttribute('data-postid')!==null
&& elem.getAttribute('data-posttype')!==undefined
&& elem.getAttribute('data-posttype')!==null
){
let elemInputStatus=jQuery('#' + elem.getAttribute('id') + ' input.quickedit-update-status');
if(quickEditFrontEndDebugEnabled) console.log('quickedit hiding ID=', elem.getAttribute('id'), elemInputStatus);
if(elemInputStatus!==undefined
&& elemInputStatus!==null
&& elemInputStatus.length
){
if(quickEditFrontEndDebugEnabled) console.log('quickedit hiding status=', elemInputStatus.val());
if(parseInt(elemInputStatus.val())===1){
quickEditSuccessToast(elem.getAttribute('id'), elem.getAttribute('data-key'), elem.getAttribute('data-postid'), elem.getAttribute('data-posttype') );
}}
}});
elem.addEventListener('shown.bs.modal', ()=> {
if(!elem.classList.contains('quickedit-already-opened')){
if(quickEditFrontEndDebugEnabled) console.log('quickedit opened');
elem.querySelector('.modal-header').classList.add('quickedit-loading');
elem.querySelector('.modal-body').classList.add('quickedit-loading');
}});
});
}}else{
jQuery('body a.kwpb-admin-front-end-button').remove();
}}
function quickEditSuccessToast(modal_id, modal_key, post_id, post_type){
if(quickEditFrontEndDebugEnabled) console.log('quickedit status changed:', modal_id, modal_key, post_id, post_type);
let currentQuickEditContent=null;
let currentQuickEditContentButton=null;
let currentQuickEditContentButtonCloneParent=null;
let currentQuickEditContentButtonClone=null;
let currentQuickEditModalContainer=null;
let currentQuickEditModalContainerInputStatus=null;
let currentQuickEditModalIframe=null;
let currentQuickEditModalIframeCloneParent=null;
let currentQuickEditModalIframeClone=null;
let quickEditUpdateSuccess=false;
if(modal_id!==null
&& modal_id!==undefined
&& modal_key!==null
&& modal_key!==undefined
&& post_id!==null
&& post_id!==undefined
&& post_type!==null
&& post_type!==undefined
&&
(
post_type==='traductions'
|| post_type==='diadaoimage'
)
){
currentQuickEditModalContainer=jQuery('#' + modal_id);
if(currentQuickEditModalContainer!==undefined
&& currentQuickEditModalContainer!==null
&& currentQuickEditModalContainer.length
){
currentQuickEditModalIframe=jQuery('#' + modal_id + '-iframe');
currentQuickEditModalContainerInputStatus=currentQuickEditModalContainer.find('input.quickedit-update-status');
if(quickEditFrontEndDebugEnabled) console.log('currentQuickEditModalContainerInputStatus', currentQuickEditModalContainerInputStatus, 'currentQuickEditModalIframe', currentQuickEditModalIframe);
if(currentQuickEditModalContainerInputStatus!==undefined
&& currentQuickEditModalContainerInputStatus!==null
&& currentQuickEditModalContainerInputStatus.length
&& currentQuickEditModalIframe!==undefined
&& currentQuickEditModalIframe!==null
&& currentQuickEditModalIframe.length
){
currentQuickEditModalIframeCloneParent=currentQuickEditModalIframe.parent();
currentQuickEditModalIframeClone=currentQuickEditModalIframe.clone();
currentQuickEditModalIframeClone.removeAttr('data-src');
currentQuickEditModalIframeClone.removeClass('lozad');
currentQuickEditModalIframeClone.attr('src', currentQuickEditModalIframeClone.attr('src').replace('&updated=true', '') );
currentQuickEditContent=jQuery('.diasdk-quickedit-parent[data-posttype="' + post_type + '"][data-postid="' + post_id + '"]');
if(quickEditFrontEndDebugEnabled) console.log('quickedit current content:', currentQuickEditContent);
if(currentQuickEditContent!==undefined
&& currentQuickEditContent!==null
&& currentQuickEditContent.length
){
currentQuickEditContentButton=currentQuickEditContent.find('button.diasdk-quickedit-btn');
if(currentQuickEditContentButton!==undefined
&& currentQuickEditContentButton!==null
&& currentQuickEditContentButton.length
){
currentQuickEditContentButtonCloneParent=currentQuickEditContentButton.parent();
currentQuickEditContentButtonClone=currentQuickEditContentButton.clone();
}else{
currentQuickEditContent=null;
}}else{
currentQuickEditContent=null;
}}
}}
const Toast=Swal.mixin({
toast: true,
position: "bottom-end",
showConfirmButton: false,
timer: 3000,
timerProgressBar: true,
didOpen:(toast)=> {
toast.onmouseenter=Swal.stopTimer;
toast.onmouseleave=Swal.resumeTimer;
}});
Toast.fire({
icon: "success",
title: "Mise à jour effectuée"
});
if(quickEditFrontEndDebugEnabled) console.log('toast displayed');
if(currentQuickEditContent!==null
&& window['diadao_sdk_ajax_base_url']!==undefined
&& window['diadao_sdk_ajax_base_url']!==null
&&
(
post_type==='traductions'
|| post_type==='diadaoimage'
)
){
let htmlCurrentNode=null;
let htmlReplaceAllowed=true;
if(post_type==='traductions'){
htmlCurrentNode=jQuery(jQuery.parseHTML(currentQuickEditContent[0].outerHTML)[0]);
}
else if(post_type==='diadaoimage'){
htmlCurrentNode=jQuery(jQuery.parseHTML(currentQuickEditContent[0].outerHTML)[0]);
}
if(quickEditFrontEndDebugEnabled) console.log('update current html; htmlCurrentNode=', htmlCurrentNode);
if(htmlCurrentNode!==undefined
&& htmlCurrentNode!==null
&& htmlCurrentNode.length
){
jQuery.ajax({
url: window['diadao_sdk_ajax_base_url'] + 'diadao_cron_quickedit_shortcode/',
type: 'POST',
dataType: 'json',
data: {
modal_id:modal_id,
modal_key:modal_key,
post_id:post_id,
post_type:post_type,
},
timeout: 10000,
}).done(( data)=> {
if(quickEditFrontEndDebugEnabled) console.log('quickedit update html ok', data, post_type);
if(data.success){
let htmlNewNode=null;
let htmlNewNodeHtml='';
let htmlNewOldSelectorTarget='';
if(post_type==='traductions'){
htmlNewNode=jQuery(jQuery.parseHTML(data.html)[0]);
}
else if(post_type==='diadaoimage'){
htmlNewNode=jQuery(jQuery.parseHTML(data.html) );
}
if(quickEditFrontEndDebugEnabled) console.log('htmlNewNode length=', htmlNewNode.length, ' classes', htmlNewNode.first().attr('class') );
if(htmlNewNode.length===1
&& htmlNewNode.first().attr('class')!==undefined
&& htmlNewNode.first().attr('class')!==null
&& htmlNewNode.first().attr('class')!==''
&& post_type==='traductions'
){
let htmlNewOldSelectorClassList=jQuery(htmlNewNode)[0].classList;
if(quickEditFrontEndDebugEnabled) console.log('htmlNewOldSelectorTarget', htmlNewOldSelectorClassList);
htmlNewOldSelectorClassList.forEach(( class_name)=> {
if(class_name!==undefined
&& class_name!==null
&& class_name!==''
){
htmlNewOldSelectorTarget +='.' + class_name;
}});
if(htmlNewOldSelectorTarget!==''){
if(quickEditFrontEndDebugEnabled) console.log('htmlNewOldSelectorTarget', htmlNewOldSelectorTarget);
htmlCurrentNode=currentQuickEditContent.find(htmlNewOldSelectorTarget).first();
if(htmlCurrentNode!==undefined
&& htmlCurrentNode!==null
&& htmlCurrentNode.length
){
}else{
htmlCurrentNode=null;
}}
}else{
htmlCurrentNode=currentQuickEditContent.first();
}
if(quickEditFrontEndDebugEnabled) console.log('htmlCurrentNode', htmlCurrentNode, htmlCurrentNode.length);
if(htmlCurrentNode!==undefined
&& htmlCurrentNode!==null
&& htmlCurrentNode.length
){
let htmlNewNodeTotalImg=0;
let htmlNewNodeTotalVideo=0;
if(htmlNewNode.length > 1 
|| post_type==='diadaoimage'
){
htmlNewNodeHtml='';
let tmpObj=null;
let tmpElem=null;
htmlNewNode.each(function(){
tmpObj=jQuery(this);
tmpElem=tmpObj[0];
if(tmpElem.outerHTML!==undefined
&& tmpElem.outerHTML!==null
&& tmpElem.outerHTML!==''
){
htmlNewNodeHtml +=tmpElem.outerHTML;
if(quickEditFrontEndDebugEnabled) console.log('tmpObj', tmpObj, 'tmpElem', tmpElem, 'tmpElem.tagName', tmpElem.tagName);
if(tmpElem.tagName.toLowerCase()==='img'){
htmlNewNodeTotalImg +=1;
}
else if(tmpElem.tagName.toLowerCase()==='video'
|| tmpElem.tagName.toLowerCase()==='hls-video'
|| tmpElem.classList.contains('video-wrapper')
){
htmlNewNodeTotalVideo +=1;
}else{
htmlNewNodeTotalImg +=tmpObj.find('img').length;
htmlNewNodeTotalVideo +=tmpObj.find('video').length;
htmlNewNodeTotalVideo +=tmpObj.find('hls-video').length;
}}
});
}else{
htmlNewNodeHtml=htmlNewNode.html();
}
if(quickEditFrontEndDebugEnabled) console.log('htmlCurrentNode=', htmlCurrentNode, htmlCurrentNode.html(), 'htmlCurrentNodeHtml', htmlCurrentNode.html());
if(quickEditFrontEndDebugEnabled) console.log('htmlNewNode', htmlNewNode, 'htmlNewNodeHtml', htmlNewNodeHtml);
if(post_type==='diadaoimage'){
if(quickEditFrontEndDebugEnabled) console.log('htmlCurrentNode img', htmlCurrentNode.find('img').length, 'htmlNewNode img', htmlNewNodeTotalImg);
if(quickEditFrontEndDebugEnabled) console.log('htmlCurrentNode video', htmlCurrentNode.find('video').length, 'htmlNewNode video', htmlNewNodeTotalVideo);
if(htmlCurrentNode.find('img').length===htmlNewNodeTotalImg
&&(htmlCurrentNode.find('video').length + htmlCurrentNode.find('hls-video').length)===htmlNewNodeTotalVideo
){
}else{
htmlReplaceAllowed=false;
}}
}else{
htmlReplaceAllowed=false;
}
if(htmlReplaceAllowed
&& htmlCurrentNode!==null
&& htmlCurrentNode.html()!==undefined
&& htmlCurrentNode.html()!==null
&& htmlCurrentNode.html()!==''
&& htmlNewNodeHtml!==undefined
&& htmlNewNodeHtml!==null
&& htmlNewNodeHtml!==''
){
if(quickEditFrontEndDebugEnabled) console.log('html replace GO', htmlCurrentNode);
let htmlCurrentNodeButton=htmlCurrentNode.find('button.diasdk-quickedit-btn');
htmlCurrentNode.html(htmlNewNodeHtml);
if(quickEditFrontEndDebugEnabled) console.log('html replace check button edit', htmlCurrentNodeButton);
if(htmlCurrentNodeButton!==undefined
&& htmlCurrentNodeButton!==null
&& htmlCurrentNodeButton.length
){
if(quickEditFrontEndDebugEnabled) console.log('html replace add quick edit button clone');
currentQuickEditContentButtonClone.appendTo(currentQuickEditContentButtonCloneParent);
}
quickEditUpdateSuccess=true;
}
if(quickEditUpdateSuccess){
quickEditSuccessToastReset(currentQuickEditModalContainerInputStatus);
}}
}).fail(()=> {
if(quickEditFrontEndDebugEnabled) console.log('quickedit update html error');
});
}}else{
if(quickEditFrontEndDebugEnabled) console.log('update basic no html refresh');
quickEditSuccessToastReset(currentQuickEditModalContainerInputStatus);
}}
function quickEditSuccessToastReset(currentQuickEditModalContainerInputStatus){
setTimeout(function(){
if(quickEditFrontEndDebugEnabled) console.log('quickedit acf submit RESET');
currentQuickEditModalContainerInputStatus.val(0);
}, 50);
}})(jQuery);