|
var adm = { |
|
// (A) INIT |
|
pgA : null, // html page a |
|
pgB : null, // html page b |
|
pgL : null, // html pages list |
|
pgS : [], // html list of pages |
|
pgD : null, // current page being dragged |
|
init : () => { |
|
// (A1) GET HTML ELEMENTS |
|
adm.pgA = document.getElementById("pgA"); |
|
adm.pgB = document.getElementById("pgB"); |
|
adm.pgL = document.getElementById("pgList"); |
|
|
|
// (A2) INIT LOAD PAGES LIST |
|
adm.list(); |
|
}, |
|
|
|
// (B) SUPPORT FUNCTION - AJAX FETCH |
|
fetch : (data, load) => { |
|
// (B1) FORM DATA |
|
let form = new FormData(); |
|
for (let [k,v] of Object.entries(data)) { form.append(k,v); } |
|
|
|
// (B2) FETCH |
|
fetch("3c-admin-ajax.php", { method:"POST", body:form }) |
|
.then(res => res.text()) |
|
.then(txt => load(txt)) |
|
.catch(err => console.error(err)); |
|
}, |
|
|
|
// (C) SUPPORT FUNCTION - TOGGLE PAGE |
|
toggle : pg => { |
|
adm.pgA.classList.add("hide"); |
|
adm.pgB.classList.add("hide"); |
|
document.getElementById("pg"+pg).classList.remove("hide"); |
|
}, |
|
|
|
// (D) LIST ALL PAGES |
|
list : () => { |
|
// (D1) AJAX FETCH |
|
adm.fetch({ req : "list" }, |
|
txt => { |
|
// (D2) PUT INTO HTML CONTAINER |
|
adm.pgL.innerHTML = txt; |
|
adm.toggle("A"); |
|
|
|
// (D3) SORTABLE |
|
adm.pgS = document.querySelectorAll("#pgList .row"); |
|
if (adm.pgS.length>0) { for (let div of adm.pgS) { |
|
// (D3-1) ON DRAG START - ADD DROPPABLE HINTS |
|
div.ondragstart = e => { |
|
adm.pgD = e.target; |
|
for (let p of adm.pgS) { |
|
p.classList.add("drag"); |
|
if (p != adm.pgD) { p.classList.add("hint"); } |
|
} |
|
}; |
|
|
|
// (D3-2) ON DRAG ENTER - HIGHLIGHT DROPZONE |
|
div.counter = 0; |
|
div.ondragenter = e => { |
|
div.counter++; |
|
if (div != adm.pgD ) { div.classList.add("active"); } |
|
}; |
|
|
|
// (D3-3) DRAG LEAVE - REMOVE HIGHLIGHT DROPZONE |
|
div.ondragleave = e => { |
|
div.counter--; |
|
if (div.counter==0) { div.classList.remove("active"); } |
|
}; |
|
|
|
// (D3-4) DRAG END - REMOVE ALL HIGHLIGHTS |
|
div.ondragend = e => { for (let p of adm.pgS) { |
|
p.counter = 0; |
|
p.classList.remove("drag"); |
|
p.classList.remove("hint"); |
|
p.classList.remove("active"); |
|
}}; |
|
|
|
// (D3-5) DRAG OVER - PREVENT DEFAULT "DROP", SO WE CAN DO OUR OWN |
|
div.ondragover = e => e.preventDefault(); |
|
|
|
// (D3-6) ON DROP - REORDER NOTES & SAVE |
|
div.ondrop = e => { |
|
// (D3-6-1) PREVENT DEFAULT BROWSER DROP ACTION |
|
e.preventDefault(); |
|
|
|
if (div != adm.pgD) { |
|
// (D3-6-2) GET CURRENT & DROPPED POSITIONS |
|
let idrag = 0, // index of currently dragged |
|
idrop = 0; // index of dropped location |
|
for (let i=0; i<adm.pgS.length; i++) { |
|
if (adm.pgD == adm.pgS[i]) { idrag = i; } |
|
if (div == adm.pgS[i]) { idrop = i; } |
|
} |
|
|
|
// (D3-6-3) REORDER HTML NOTES |
|
if (idrag > idrop) { |
|
adm.pgL.insertBefore(adm.pgD, div); |
|
} else { |
|
adm.pgL.insertBefore(adm.pgD, div.nextSibling); |
|
} |
|
|
|
// (D3-6-4) GET NEW ORDER |
|
adm.pgS = adm.pgL.querySelectorAll(".row"); |
|
let order = []; |
|
for (let n of adm.pgS) { order.push(n.dataset.id); } |
|
|
|
// (D3-6-5) AJAX SAVE ORDER |
|
adm.fetch({ |
|
req : "order", |
|
order : JSON.stringify(order) |
|
}, txt => { |
|
if (txt != "OK") { alert(txt); } |
|
}); |
|
} |
|
}; |
|
}} |
|
}); |
|
}, |
|
|
|
// (E) SHOW PAGE - ADD OR EDIT |
|
show : id => { |
|
// (E1) AJAX FETCH FORM |
|
adm.fetch({ |
|
req : "show", |
|
id : (id==undefined ? "" : id) |
|
}, txt => { |
|
// (E2) CONTENTS INTO <DIV ID="PGB"> |
|
document.getElementById("pgB").innerHTML = txt; |
|
adm.toggle("B"); |
|
|
|
// (E3) TINYMCE |
|
// https://www.tiny.cloud/docs/advanced/available-menu-items/ |
|
tinymce.remove(); |
|
tinymce.init({ |
|
selector : "#pgTxt", |
|
menubar : false, |
|
plugins : "image textcolor lists code", |
|
toolbar: "fontfamily fontsize | bold italic underline | backcolor forecolor | alignleft aligncenter alignright alignjustify | image | code" |
|
}); |
|
}); |
|
}, |
|
|
|
// (F) SAVE PAGE |
|
save : () => { |
|
// (F1) GET FORM |
|
var data = { |
|
req : "save", |
|
title : document.getElementById("pgTitle").value, |
|
txt : tinymce.get("pgTxt").getContent(), |
|
id : document.getElementById("pgId").value |
|
}; |
|
|
|
// (F2) CHECKS |
|
if (data.title=="") { |
|
alert("Please enter the title."); |
|
return false; |
|
} |
|
if (data.txt=="") { |
|
alert("Please fill in the page content."); |
|
return false; |
|
} |
|
|
|
// (F3) AJAX SAVE |
|
adm.fetch(data, txt => { |
|
if (txt=="OK") { adm.list(); alert("Page saved"); } |
|
else { alert(txt); } |
|
}); |
|
return false; |
|
}, |
|
|
|
// (G) DELETE PAGE |
|
del : id => { if (confirm("Delete page?")) { |
|
adm.fetch({ |
|
req : "del", |
|
id : id |
|
}, txt => { |
|
if (txt=="OK") { adm.list(); alert("Page deleted"); } |
|
else { alert(txt); } |
|
}); |
|
}} |
|
}; |
|
window.onload = adm.init; |