diff --git a/wcms-admin.min.js b/wcms-admin.min.js index ffde70d..c6f747a 100644 --- a/wcms-admin.min.js +++ b/wcms-admin.min.js @@ -1 +1 @@ -class wcmsAdmin{constructor(){const e=this,t=document.querySelectorAll('[data-toggle="wcms-modal"]'),n=document.querySelectorAll('[data-dismiss="wcms-modal"]'),s=document.getElementsByClassName("wcms-modal");t.forEach(e=>{e.addEventListener("click",function(){wcmsAdminActions.toggleModal(this,!0)})}),document.addEventListener("click",function(e){([...s].includes(e.target)||[...n].includes(e.target))&&wcmsAdminActions.toggleModal(this,!1)}),document.querySelectorAll("ul.nav-tabs > li > a").forEach(e=>{e.addEventListener("click",function(e){e.preventDefault(),wcmsAdminActions.openTabAction(this)})}),document.querySelectorAll("[data-loader-id]").forEach(e=>{e.addEventListener("click",function(e){wcmsAdminActions.showLoader(!0,this.dataset.loaderId)})}),document.querySelectorAll("div.editText:not(.editTextOpen)").forEach(t=>{t.addEventListener("click",e.constructEditableFieldsAction)}),document.querySelectorAll("i.menu-toggle").forEach(t=>{t.addEventListener("click",e.hideOrShowMenuItemsAction)}),document.querySelectorAll("a.menu-item-add").forEach(e=>{e.addEventListener("click",e=>wcmsAdminActions.createNewPage(e.target))}),document.querySelectorAll(".menu-item-up, .menu-item-down").forEach(t=>{t.addEventListener("click",e.reorderMenuItemsAction)}),document.getElementById("changeDefaultPage").addEventListener("change",wcmsAdminActions.changeDefaultPage)}constructEditableFieldsAction(){if(this.classList.contains("editTextOpen"))return;this.classList.add("editTextOpen"),wcmsAdminActions.editableTextArea(this),this.firstChild.focus();const e=this.getElementsByTagName("textarea");autosize(e),tabOverride.set(e)}reorderMenuItemsAction(){const e=this.classList.contains("menu-item-up")?"-1":"1";wcmsAdminActions.sendPostRequest("menuItems",e,"menuItemOrder",this.dataset.menu)}hideOrShowMenuItemsAction(){const e=this;let t=null;if(e.classList.contains("menu-item-hide"))e.classList.remove("eyeShowIcon","menu-item-hide"),e.classList.add("eyeHideIcon","menu-item-show"),e.setAttribute("title","Hide page from menu"),t="hide";else{if(!e.classList.contains("menu-item-show"))return;e.classList.add("eyeShowIcon","menu-item-hide"),e.classList.remove("eyeHideIcon","menu-item-show"),e.setAttribute("title","Show page in menu"),t="show"}e.setAttribute("data-visibility",t),wcmsAdminActions.sendPostRequest("menuItems"," ","menuItemVsbl",e.dataset.menu,t)}}const wcmsAdminActions={openTabAction(e){const t=e.closest(".nav-tabs"),n=e.getAttribute("href").replace("#",""),s=document.getElementById(n),i=s.closest(".tab-content");t.querySelector(".active").classList.remove("active"),i.querySelector(".active").classList.remove("active"),s.classList.add("active"),e.classList.add("active")},toggleModal(e,t){if(t){const t=e.getAttribute("href").replace("#","");document.body.classList.add("modal-open"),document.getElementById(t).style.display="block";const n=e.dataset.targetTab;if(n){const e=document.querySelector('ul.nav-tabs > li > a[href="'+n+'"]');e&&wcmsAdminActions.openTabAction(e)}}else document.body.classList.remove("modal-open"),e.dataset&&e.dataset.dismiss?e.closest(".wcms-modal").style.display="none":[].forEach.call(document.getElementsByClassName("wcms-modal"),function(e){e.style.display="none"})},createNewPage:e=>{let t=prompt("Enter page name");if(!t)return!1;t=t.replace(/[`~;:'",.<>\{\}\[\]\\\/]/gi,"").trim(),wcmsAdminActions.sendPostRequest("menuItems",t,"menuItemCreate",e.dataset.menu,"hide")},changeDefaultPage:e=>{wcmsAdminActions.sendPostRequest("defaultPage",e.target.value,"config")},contentSave:(e,t,n,s,i,c)=>{if(t!==c)return void wcmsAdminActions.sendPostRequest(e,t,n,s,i);const o=document.getElementById(e);o.classList.remove("editTextOpen"),o.innerHTML=t},sendPostRequest:(e,t,n,s,i=null)=>{wcmsAdminActions.showLoader(!0);const c={fieldname:e,token:token,content:encodeURIComponent(t),target:n,menu:s,visibility:i},o=Object.keys(c).map(function(e,t){return[e,c[e]].join("=")}).join("&"),a=new XMLHttpRequest;a.onreadystatechange=function(){setTimeout(()=>window.location.reload(),50)},a.open("POST","",!1),a.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"),a.send(o)},showLoader:(e,t="save")=>{document.getElementById(t).style.display=e?"block":"none"},editableTextArea:e=>{const t=e,n=t.innerHTML,s=t.getAttribute("id"),i=document.createElement("textarea");i.onblur=function(){wcmsAdminActions.contentSave(s,this.value,t.dataset.target,t.dataset.menu,t.dataset.visibility,n)},i.setAttribute("id",s+"_field"),i.innerHTML=n,e.innerHTML="",e.appendChild(i)}};document.addEventListener("DOMContentLoaded",function(){new wcmsAdmin}); \ No newline at end of file +class wcmsAdmin{constructor(){const e=this,t=document.querySelectorAll('[data-toggle="wcms-modal"]'),n=document.querySelectorAll('[data-dismiss="wcms-modal"]'),s=document.getElementsByClassName("wcms-modal");t.forEach(e=>{e.addEventListener("click",function(){wcmsAdminActions.toggleModal(this,!0)})}),document.addEventListener("click",function(e){([...s].includes(e.target)||[...n].includes(e.target))&&wcmsAdminActions.toggleModal(this,!1)}),document.querySelectorAll("ul.nav-tabs > li > a").forEach(e=>{e.addEventListener("click",function(e){e.preventDefault(),wcmsAdminActions.openTabAction(this)})}),document.querySelectorAll("[data-loader-id]").forEach(e=>{e.addEventListener("click",function(e){wcmsAdminActions.showLoader(!0,this.dataset.loaderId)})}),document.querySelectorAll("div.editText:not(.editTextOpen)").forEach(t=>{t.addEventListener("click",e.constructEditableFieldsAction)}),document.querySelectorAll("i.menu-toggle").forEach(t=>{t.addEventListener("click",e.hideOrShowMenuItemsAction)}),document.querySelectorAll("a.menu-item-add").forEach(e=>{e.addEventListener("click",e=>wcmsAdminActions.createNewPage(e.target))}),document.querySelectorAll(".menu-item-up, .menu-item-down").forEach(t=>{t.addEventListener("click",e.reorderMenuItemsAction)}),document.getElementById("changeDefaultPage").addEventListener("change",wcmsAdminActions.changeDefaultPage)}constructEditableFieldsAction(){if(this.classList.contains("editTextOpen"))return;this.classList.add("editTextOpen"),wcmsAdminActions.editableTextArea(this),this.firstChild.focus();const e=this.getElementsByTagName("textarea");autosize(e),tabOverride.set(e)}reorderMenuItemsAction(){const e=this.classList.contains("menu-item-up")?"-1":"1";wcmsAdminActions.sendPostRequest("menuItems",e,"menuItemOrder",this.dataset.menu)}hideOrShowMenuItemsAction(){const e=this;let t=null;if(e.classList.contains("menu-item-hide"))e.classList.remove("eyeShowIcon","menu-item-hide"),e.classList.add("eyeHideIcon","menu-item-show"),e.setAttribute("title","Hide page from menu"),t="hide";else{if(!e.classList.contains("menu-item-show"))return;e.classList.add("eyeShowIcon","menu-item-hide"),e.classList.remove("eyeHideIcon","menu-item-show"),e.setAttribute("title","Show page in menu"),t="show"}e.setAttribute("data-visibility",t),wcmsAdminActions.sendPostRequest("menuItems"," ","menuItemVsbl",e.dataset.menu,t)}}const wcmsAdminActions={openTabAction(e){const t=e.closest(".nav-tabs"),n=e.getAttribute("href").replace("#",""),s=document.getElementById(n),i=s.closest(".tab-content");t.querySelector(".active").classList.remove("active"),i.querySelector(".active").classList.remove("active"),s.classList.add("active"),e.classList.add("active")},toggleModal(e,t){if(t){const t=e.getAttribute("href").replace("#","");document.body.classList.add("modal-open"),document.getElementById(t).style.display="block";const n=e.dataset.targetTab;if(n){const e=document.querySelector('ul.nav-tabs > li > a[href="'+n+'"]');e&&wcmsAdminActions.openTabAction(e)}}else document.body.classList.remove("modal-open"),e.dataset&&e.dataset.dismiss?e.closest(".wcms-modal").style.display="none":[].forEach.call(document.getElementsByClassName("wcms-modal"),function(e){e.style.display="none"})},createNewPage:e=>{let t=prompt("Enter page name");if(!t)return!1;t=t.replace(/[`~;:'",.<>\{\}\[\]\\\/]/gi,"").trim(),wcmsAdminActions.sendPostRequest("menuItems",t,"menuItemCreate",e.dataset.menu,"hide")},changeDefaultPage:e=>{wcmsAdminActions.sendPostRequest("defaultPage",e.target.value,"config")},contentSave:(e,t,n,s,i,o)=>{if(t!==o)return void wcmsAdminActions.sendPostRequest(e,t,n,s,i);const c=document.getElementById(e);c.classList.remove("editTextOpen"),c.innerHTML=t},sendPostRequest:(e,t,n,s,i=null)=>{if("undefined"!=typeof saveChangesPopup&&saveChangesPopup&&!confirm("Save new changes?"))event.preventDefault(),alert("Changed are not saved, you can continue to edit or refresh the page.");else{wcmsAdminActions.showLoader(!0);const o={fieldname:e,token:token,content:encodeURIComponent(t),target:n,menu:s,visibility:i},c=Object.keys(o).map(function(e,t){return[e,o[e]].join("=")}).join("&"),a=new XMLHttpRequest;a.onreadystatechange=function(){setTimeout(()=>window.location.reload(),50)},a.open("POST","",!1),a.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"),a.send(c)}},showLoader:(e,t="save")=>{document.getElementById(t).style.display=e?"block":"none"},editableTextArea:e=>{const t=e,n=t.innerHTML,s=t.getAttribute("id"),i=document.createElement("textarea");i.onblur=function(){wcmsAdminActions.contentSave(s,this.value,t.dataset.target,t.dataset.menu,t.dataset.visibility,n)},i.setAttribute("id",s+"_field"),i.innerHTML=n,e.innerHTML="",e.appendChild(i)}};document.addEventListener("DOMContentLoaded",function(){new wcmsAdmin});