experiment with fixing ajax topics link

This commit is contained in:
Azareal 2020-05-09 09:15:44 +10:00
parent 6746c9c91b
commit ef7af76e91
1 changed files with 34 additions and 26 deletions

View File

@ -24,11 +24,11 @@ function pushNotice(msg) {
} }
// TODO: Write a friendlier error handler which uses a .notice or something, we could have a specialised one for alerts // TODO: Write a friendlier error handler which uses a .notice or something, we could have a specialised one for alerts
function ajaxError(xhr,status,er) { function ajaxError(xhr,status,e) {
console.log("The AJAX request failed"); console.log("The AJAX request failed");
console.log("xhr",xhr); console.log("xhr",xhr);
console.log("status",status); console.log("status",status);
console.log("er",er); console.log("e",e);
if(status=="parsererror") console.log("The server didn't respond with a valid JSON response"); if(status=="parsererror") console.log("The server didn't respond with a valid JSON response");
console.trace(); console.trace();
} }
@ -518,10 +518,11 @@ function mainInit(){
} }
function rebindPaginator() { function rebindPaginator() {
$(".pageitem a").unbind("click");
$(".pageitem a").click(function() {
event.preventDefault();
// TODO: Take mostviewed into account // TODO: Take mostviewed into account
// TODO: Get this to work with topics too
$(".pageitem a").unbind("click");
$(".pageitem a").click(function(ev) {
ev.preventDefault();
let url = "//"+window.location.host+window.location.pathname; let url = "//"+window.location.host+window.location.pathname;
let urlParams = new URLSearchParams(window.location.search); let urlParams = new URLSearchParams(window.location.search);
urlParams.set("page",new URLSearchParams(this.getAttribute("href")).get("page")); urlParams.set("page",new URLSearchParams(this.getAttribute("href")).get("page"));
@ -549,8 +550,7 @@ function mainInit(){
rebuildPaginator(dat.LastPage); rebuildPaginator(dat.LastPage);
rebindPaginator(); rebindPaginator();
}).catch(e => { }).catch(e => {
console.log("Unable to get script '"+url+q+"&js=1"+"'"); console.log("Unable to get script '"+url+q+"&js=1"+"'",e);
console.log("e",e);
console.trace(); console.trace();
}); });
}); });
@ -607,6 +607,7 @@ function mainInit(){
// TODO: Show a search button when JS is disabled? // TODO: Show a search button when JS is disabled?
$(".widget_search_input").keypress(function(e) { $(".widget_search_input").keypress(function(e) {
if(e.keyCode!='13') return; if(e.keyCode!='13') return;
// TODO: Only fire on /topics/
event.preventDefault(); event.preventDefault();
// TODO: Take mostviewed into account // TODO: Take mostviewed into account
let url = "//"+window.location.host+window.location.pathname; let url = "//"+window.location.host+window.location.pathname;
@ -762,15 +763,6 @@ function mainInit(){
$("input,textarea,select,option").keyup(ev => ev.stopPropagation()) $("input,textarea,select,option").keyup(ev => ev.stopPropagation())
$(".create_topic_link").click(ev => {
ev.preventDefault();
$(".topic_create_form").removeClass("auto_hide");
});
$(".topic_create_form .close_form").click(ev => {
ev.preventDefault();
$(".topic_create_form").addClass("auto_hide");
});
$("#themeSelectorSelect").change(function(){ $("#themeSelectorSelect").change(function(){
console.log("Changing the theme to "+this.options[this.selectedIndex].getAttribute("value")); console.log("Changing the theme to "+this.options[this.selectedIndex].getAttribute("value"));
$.ajax({ $.ajax({
@ -872,7 +864,7 @@ function mainInit(){
return name.split('?')[0]; return name.split('?')[0];
} }
function loadArb(base,href) { function loadArb(base,href,h = null) {
fetch(href,{credentials:"same-origin"}) fetch(href,{credentials:"same-origin"})
.then(resp => { .then(resp => {
if(!resp.ok) throw(href+" failed to load"); if(!resp.ok) throw(href+" failed to load");
@ -892,8 +884,7 @@ function mainInit(){
return resp.text(); return resp.text();
}).then(dat => { }).then(dat => {
document.querySelector("#back").outerHTML = dat; document.querySelector("#back").outerHTML = dat;
unbindTopic(); if(h!==null) h(dat);
bindTopic();
$(".elapsed").remove(); $(".elapsed").remove();
let obj = {Title:document.title,Url:base}; let obj = {Title:document.title,Url:base};
history.pushState(obj,obj.Title,obj.Url); history.pushState(obj,obj.Title,obj.Url);
@ -905,14 +896,20 @@ function mainInit(){
$(".rowtopic a,a.rowtopic,a.forum_poster").click(function(ev) { $(".rowtopic a,a.rowtopic,a.forum_poster").click(function(ev) {
let base = this.getAttribute("href"); let base = this.getAttribute("href");
loadArb(base,base+"?i=1"); loadArb(base,base+"?i=1", () => {
unbindTopic();
bindTopic();
});
ev.stopPropagation(); ev.stopPropagation();
ev.preventDefault(); ev.preventDefault();
}) })
$("a").click(function(ev) { $("a").click(function(ev) {
let base = this.getAttribute("href"); let base = this.getAttribute("href");
if(base!="/topics/") return; if(base!="/topics/") return;
loadArb(base,base+"?i=1"); loadArb(base,base+"?i=1", () => {
unbindPage();
bindPage();
});
ev.stopPropagation(); ev.stopPropagation();
ev.preventDefault(); ev.preventDefault();
}) })
@ -922,10 +919,21 @@ function mainInit(){
} }
function bindPage() { function bindPage() {
$(".create_topic_link").click(ev => {
ev.preventDefault();
$(".topic_create_form").removeClass("auto_hide");
});
$(".topic_create_form .close_form").click(ev => {
ev.preventDefault();
$(".topic_create_form").addClass("auto_hide");
});
bindTopic(); bindTopic();
} }
function unbindPage() { function unbindPage() {
$(".create_topic_link").unbind("click");
$(".topic_create_form .close_form").unbind("click");
unbindTopic(); unbindTopic();
} }