try to iron out more ajax topic kinks

This commit is contained in:
Azareal 2020-03-11 19:13:05 +10:00
parent 64ec820453
commit e19e478e32
3 changed files with 49 additions and 39 deletions

View File

@ -1001,6 +1001,8 @@ function bindTopic() {
// For custom / third party text editors // For custom / third party text editors
quoteItemCallback(src.innerHTML,item); quoteItemCallback(src.innerHTML,item);
}); });
runHook("end_bind_topic");
} }
function unbindTopic() { function unbindTopic() {
@ -1010,4 +1012,5 @@ function unbindTopic() {
$(".edit_item").unbind("click"); $(".edit_item").unbind("click");
$(".submit_edit").unbind("click"); $(".submit_edit").unbind("click");
$(".quote_item").unbind("click"); $(".quote_item").unbind("click");
runHook("end_unbind_topic");
} }

View File

@ -2,13 +2,12 @@
var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "gif", "tiff","tif", "webp"]; var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "gif", "tiff","tif", "webp"];
(() => { (() => {
addInitHook("almost_end_init", () => {
function copyToClipboard(str) { function copyToClipboard(str) {
const el = document.createElement('textarea'); const el=document.createElement('textarea');
el.value = str; el.value=str;
el.setAttribute('readonly', ''); el.setAttribute('readonly','');
el.style.position = 'absolute'; el.style.position='absolute';
el.style.left = '-9999px'; el.style.left='-9999px';
document.body.appendChild(el); document.body.appendChild(el);
el.select(); el.select();
document.execCommand('copy'); document.execCommand('copy');
@ -16,11 +15,11 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
} }
// TODO: Surely, there's a prettier and more elegant way of doing this? // TODO: Surely, there's a prettier and more elegant way of doing this?
function getExt(filename) { function getExt(name) {
if(!filename.indexOf('.' > -1)) throw("This file doesn't have an extension"); if(!name.indexOf('.' > -1)) throw("This file doesn't have an extension");
return filename.split('.').pop(); return name.split('.').pop();
} }
function uploadFileHandler(fileList, maxFiles = 5, step1 = () => {}, step2 = () => {}) { function uploadFileHandler(fileList, maxFiles = 5, step1 = () => {}, step2 = () => {}) {
let files = []; let files = [];
for(var i = 0; i < fileList.length && i < 5; i++) files[i] = fileList[i]; for(var i = 0; i < fileList.length && i < 5; i++) files[i] = fileList[i];
@ -30,9 +29,7 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
console.log("files[" + i + "]",files[i]); console.log("files[" + i + "]",files[i]);
totalSize += files[i]["size"]; totalSize += files[i]["size"];
} }
if(totalSize > me.Site.MaxRequestSize) { if(totalSize > me.Site.MaxRequestSize) throw("You can't upload this much at once, max: " + me.Site.MaxRequestSize);
throw("You can't upload this much at once, max: " + me.Site.MaxRequestSize);
}
for(let i = 0; i < files.length; i++) { for(let i = 0; i < files.length; i++) {
let filename = files[i]["name"]; let filename = files[i]["name"];
@ -67,9 +64,8 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
let fileDock = this.closest(".attach_edit_bay"); let fileDock = this.closest(".attach_edit_bay");
try { try {
uploadFileHandler(this.files, 5, () => {}, uploadFileHandler(this.files, 5, () => {},
(e, hash, filename) => { (e,hash,filename) => {
console.log("hash",hash); console.log("hash",hash);
let formData = new FormData(); let formData = new FormData();
formData.append("s",me.User.S); formData.append("s",me.User.S);
for(let i = 0; i < this.files.length; i++) formData.append("upload_files",this.files[i]); for(let i = 0; i < this.files.length; i++) formData.append("upload_files",this.files[i]);
@ -132,7 +128,7 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
console.log("content.value", content.value); console.log("content.value", content.value);
let attachItem; let attachItem;
if(content.value == "") attachItem = "//" + window.location.host + "/attachs/" + hash + "." + ext; if(content.value=="") attachItem = "//" + window.location.host + "/attachs/" + hash + "." + ext;
else attachItem = "\r\n//" + window.location.host + "/attachs/" + hash + "." + ext; else attachItem = "\r\n//" + window.location.host + "/attachs/" + hash + "." + ext;
content.value = content.value + attachItem; content.value = content.value + attachItem;
console.log("content.value", content.value); console.log("content.value", content.value);
@ -147,18 +143,9 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
} }
} }
let uploadFiles = document.getElementById("upload_files");
if(uploadFiles != null) {
uploadFiles.addEventListener("change", uploadAttachHandler, false);
}
let uploadFilesOp = document.getElementById("upload_files_op");
if(uploadFilesOp != null) {
uploadFilesOp.addEventListener("change", uploadAttachHandler2, false);
}
function bindAttachManager() { function bindAttachManager() {
let uploadFiles = document.getElementsByClassName("upload_files_post"); let uploadFiles = document.getElementsByClassName("upload_files_post");
if(uploadFiles == null) return; if(uploadFiles==null) return;
for(let i = 0; i < uploadFiles.length; i++) { for(let i = 0; i < uploadFiles.length; i++) {
let uploader = uploadFiles[i]; let uploader = uploadFiles[i];
uploader.value = ""; uploader.value = "";
@ -166,6 +153,20 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
uploader.addEventListener("change", uploadAttachHandler2, false); uploader.addEventListener("change", uploadAttachHandler2, false);
} }
} }
addInitHook("start_init", () => {
addHook("end_bind_topic", () => {
let uploadFiles = document.getElementById("upload_files");
if(uploadFiles!=null) {
uploadFiles.removeEventListener("change", uploadAttachHandler, false);
uploadFiles.addEventListener("change", uploadAttachHandler, false);
}
let uploadFilesOp = document.getElementById("upload_files_op");
if(uploadFilesOp!=null) {
uploadFilesOp.removeEventListener("change", uploadAttachHandler2, false);
uploadFilesOp.addEventListener("change", uploadAttachHandler2, false);
}
bindAttachManager(); bindAttachManager();
function bindAttachItems() { function bindAttachItems() {
@ -183,7 +184,8 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
}); });
} }
bindAttachItems(); bindAttachItems();
$(".attach_item_delete").unbind("click");
$(".attach_item_delete").click(function(){ $(".attach_item_delete").click(function(){
let formData = new URLSearchParams(); let formData = new URLSearchParams();
formData.append("s",me.User.S); formData.append("s",me.User.S);
@ -191,7 +193,7 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
let post = this.closest(".post_item"); let post = this.closest(".post_item");
let aidList = ""; let aidList = "";
let elems = post.getElementsByClassName("attach_item_selected"); let elems = post.getElementsByClassName("attach_item_selected");
if(elems == null) return; if(elems==null) return;
for(let i = 0; i < elems.length; i++) { for(let i = 0; i < elems.length; i++) {
let pathNode = elems[i].querySelector(".attach_item_path"); let pathNode = elems[i].querySelector(".attach_item_path");
@ -217,6 +219,8 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
bindAttachManager(); bindAttachManager();
}); });
$(".moderate_link").unbind("click");
$(".mod_floater_submit").unbind("click");
$(".moderate_link").click(ev => { $(".moderate_link").click(ev => {
ev.preventDefault(); ev.preventDefault();
$(".pre_opt").removeClass("auto_hide"); $(".pre_opt").removeClass("auto_hide");
@ -235,7 +239,7 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
}); });
}); });
let bulkActionSender = function(action, selectedTopics, fragBit) { let bulkActionSender = function(action,selectedTopics,fragBit) {
let url = "/topic/"+action+"/submit/"+fragBit+"?s="+me.User.S; let url = "/topic/"+action+"/submit/"+fragBit+"?s="+me.User.S;
$.ajax({ $.ajax({
url: url, url: url,
@ -248,6 +252,7 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
} }
}); });
}; };
// TODO: Should we unbind this here to avoid binding multiple listeners to this accidentally?
$(".mod_floater_submit").click(function(ev){ $(".mod_floater_submit").click(function(ev){
ev.preventDefault(); ev.preventDefault();
let selectNode = this.form.querySelector(".mod_floater_options"); let selectNode = this.form.querySelector(".mod_floater_options");
@ -263,9 +268,9 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
$("#mod_topic_mover .pane_row").click(function(){ $("#mod_topic_mover .pane_row").click(function(){
modTopicMover.find(".pane_row").removeClass("pane_selected"); modTopicMover.find(".pane_row").removeClass("pane_selected");
let fid = this.getAttribute("data-fid"); let fid = this.getAttribute("data-fid");
if (fid == null) return; if (fid==null) return;
this.classList.add("pane_selected"); this.classList.add("pane_selected");
console.log("fid", fid); console.log("fid",fid);
forumToMoveTo = fid; forumToMoveTo = fid;
$("#mover_submit").unbind("click"); $("#mover_submit").unbind("click");
@ -285,8 +290,8 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
console.log("clicked on pollinputinput"); console.log("clicked on pollinputinput");
let dataPollInput = $(this).parent().attr("data-pollinput"); let dataPollInput = $(this).parent().attr("data-pollinput");
console.log("dataPollInput", dataPollInput); console.log("dataPollInput", dataPollInput);
if(dataPollInput == undefined) return; if(dataPollInput==undefined) return;
if(dataPollInput != (pollInputIndex-1)) return; if(dataPollInput!=(pollInputIndex-1)) return;
$(".poll_content_row .formitem").append(Template_topic_c_poll_input({ $(".poll_content_row .formitem").append(Template_topic_c_poll_input({
Index: pollInputIndex, Index: pollInputIndex,
Place: phraseBox["topic"]["topic.reply_add_poll_option"].replace("%d",pollInputIndex), Place: phraseBox["topic"]["topic.reply_add_poll_option"].replace("%d",pollInputIndex),
@ -297,7 +302,8 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
$(".pollinputinput").click(addPollInput); $(".pollinputinput").click(addPollInput);
} }
var pollInputIndex = 1; let pollInputIndex = 1;
$("#add_poll_button").unbind("click");
$("#add_poll_button").click(ev => { $("#add_poll_button").click(ev => {
ev.preventDefault(); ev.preventDefault();
$(".poll_content_row").removeClass("auto_hide"); $(".poll_content_row").removeClass("auto_hide");
@ -305,4 +311,5 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
$(".pollinputinput").click(addPollInput); $(".pollinputinput").click(addPollInput);
}); });
}); });
})(); });
})()

View File

@ -13,14 +13,14 @@ console.log("forums",forums);
Sortable.create(document.getElementById("panel_forums"), { Sortable.create(document.getElementById("panel_forums"), {
sort: true, sort: true,
onEnd: (evt) => { onEnd: (evt) => {
console.log("pre forums", forums) console.log("pre forums",forums)
console.log("evt", evt) console.log("evt",evt)
let oldFid = forums[evt.newIndex]; let oldFid = forums[evt.newIndex];
forums[evt.oldIndex] = oldFid; forums[evt.oldIndex] = oldFid;
let newFid = evt.item.getAttribute("data-fid"); let newFid = evt.item.getAttribute("data-fid");
console.log("newFid", newFid); console.log("newFid",newFid);
forums[evt.newIndex] = newFid; forums[evt.newIndex] = newFid;
console.log("post forums", forums); console.log("post forums",forums);
} }
}); });