3465e4c08f
The update system now uses the database as the source of truth for the last version rather than lastSchema.json Refactored several structs and bits of code, so we can avoid allocations for contexts where we never use a relative time. Clicking on the relative times on the topic list and the forum page should now take you to the post on the last page rather than just the last page. Added the reltime template function. Fixed some obsolete bits of code. Fixed some spelling mistakes. Fixed a bug where MaxBytesReader was capped at the maxFileSize rather than r.ContentLength. All of the client side templates should work again now. Shortened some statement names to save some horizontal space. accUpdateBuilder and SimpleUpdate now use updatePrebuilder behind the scenes to simplify things. Renamed selectItem to builder in AccSelectBuilder. Added a Total() method to accCountBuilder to reduce the amount of boilerplate used for row count queries. The "_builder" strings have been replaced with empty strings to help save memory, to make things slightly faster and to open the door to removing the query name in many contexts down the line. Added the open_edit and close_edit client hooks. Removed many query name checks. Split the attachment logic into separate functions and de-duplicated it between replies and topics. Improved the UI for editing topics in Nox. Used type aliases to reduce the amount of boilerplate in tables.go and patches.go Reduced the amount of boilerplate in the action post logic. Eliminated a map and a slice in the topic page for users who haven't given any likes. E.g. Guests. Fixed some long out-dated parts of the update instructions. Updated the update instructions to remove mention of the obsolete lastSchema.json Fixed a bug in init.js where /api/me was being loaded for guests. Added the MiniTopicGet, GlobalCount and CountInTopic methods to AttachmentStore. Added the MiniAttachment struct. Split the mod floaters out into their own template to reduce duplication. Removed a couple of redundant ParseForms. Added the common.skipUntilIfExistsOrLine function. Added the NotFoundJS and NotFoundJSQ functions. Added the lastReplyID and attachCount columns to the topics table.
155 lines
3.6 KiB
JavaScript
155 lines
3.6 KiB
JavaScript
'use strict';
|
|
|
|
var me = {};
|
|
var phraseBox = {};
|
|
var tmplInits = {};
|
|
var tmplPhrases = []; // [key] array of phrases indexed by order of use
|
|
var hooks = {
|
|
"pre_iffe": [],
|
|
"pre_init": [],
|
|
"start_init": [],
|
|
"end_init": [],
|
|
"after_phrases":[],
|
|
"after_add_alert":[],
|
|
"after_update_alert_list":[],
|
|
"open_edit":[],
|
|
"close_edit":[],
|
|
};
|
|
var ranInitHooks = {}
|
|
|
|
function runHook(name, ...args) {
|
|
if(!(name in hooks)) {
|
|
console.log("Couldn't find hook '" + name + "'");
|
|
return;
|
|
}
|
|
console.log("Running hook '"+name+"'");
|
|
|
|
let hook = hooks[name];
|
|
for (const index in hook) {
|
|
hook[index](...args);
|
|
}
|
|
}
|
|
|
|
function addHook(name, callback) {
|
|
hooks[name].push(callback);
|
|
}
|
|
|
|
// InitHooks are slightly special, as if they are run, then any adds after the initial run will run immediately, this is to deal with the async nature of script loads
|
|
function runInitHook(name, ...args) {
|
|
runHook(name,...args);
|
|
ranInitHooks[name] = true;
|
|
}
|
|
|
|
function addInitHook(name, callback) {
|
|
addHook(name, callback);
|
|
if(name in ranInitHooks) {
|
|
callback();
|
|
}
|
|
}
|
|
|
|
// Temporary hack for templates
|
|
function len(item) {
|
|
return item.length;
|
|
}
|
|
|
|
const asyncGetScript = (source) => {
|
|
return new Promise((resolve, reject) => {
|
|
let script = document.createElement('script');
|
|
script.async = true;
|
|
|
|
const onloadHander = (haha, isAbort) => {
|
|
if (isAbort || !script.readyState || /loaded|complete/.test(script.readyState)) {
|
|
script.onload = null;
|
|
script.onreadystatechange = null;
|
|
script = undefined;
|
|
|
|
isAbort ? reject(haha) : resolve();
|
|
}
|
|
}
|
|
|
|
script.onerror = (haha) => {
|
|
reject(haha);
|
|
};
|
|
script.onload = onloadHander;
|
|
script.onreadystatechange = onloadHander;
|
|
script.src = source;
|
|
|
|
const prior = document.getElementsByTagName('script')[0];
|
|
prior.parentNode.insertBefore(script, prior);
|
|
});
|
|
};
|
|
|
|
function loadScript(name, callback) {
|
|
let url = "/static/"+name
|
|
asyncGetScript(url)
|
|
.then(callback)
|
|
.catch((haha) => {
|
|
console.log("Unable to get script '"+url+"'");
|
|
console.log("haha: ", haha);
|
|
console.trace();
|
|
});
|
|
}
|
|
|
|
function DoNothingButPassBack(item) {
|
|
return item;
|
|
}
|
|
|
|
function fetchPhrases() {
|
|
fetch("/api/phrases/?query=status,topic_list,alerts")
|
|
.then((resp) => resp.json())
|
|
.then((data) => {
|
|
console.log("loaded phrase endpoint data");
|
|
console.log("data:",data);
|
|
Object.keys(tmplInits).forEach((key) => {
|
|
let phrases = [];
|
|
let tmplInit = tmplInits[key];
|
|
for(let phraseName of tmplInit) {
|
|
phrases.push(data[phraseName]);
|
|
}
|
|
console.log("Adding phrases");
|
|
console.log("key:",key);
|
|
console.log("phrases:",phrases);
|
|
tmplPhrases[key] = phrases;
|
|
});
|
|
|
|
let prefixes = {};
|
|
Object.keys(data).forEach((key) => {
|
|
let prefix = key.split(".")[0];
|
|
if(prefixes[prefix]===undefined) {
|
|
prefixes[prefix] = {};
|
|
}
|
|
prefixes[prefix][key] = data[key];
|
|
});
|
|
Object.keys(prefixes).forEach((prefix) => {
|
|
console.log("adding phrase prefix '"+prefix+"' to box");
|
|
phraseBox[prefix] = prefixes[prefix];
|
|
});
|
|
|
|
runInitHook("after_phrases");
|
|
});
|
|
}
|
|
|
|
(() => {
|
|
runInitHook("pre_iife");
|
|
let loggedIn = document.head.querySelector("[property='x-loggedin']").content;
|
|
if(loggedIn=="true") {
|
|
fetch("/api/me/")
|
|
.then((resp) => resp.json())
|
|
.then((data) => {
|
|
console.log("loaded me endpoint data");
|
|
console.log("data:",data);
|
|
me = data;
|
|
runInitHook("pre_init");
|
|
});
|
|
|
|
let toLoad = 1;
|
|
loadScript("template_topics_topic.js", () => {
|
|
console.log("Loaded template_topics_topic.js");
|
|
toLoad--;
|
|
if(toLoad===0) fetchPhrases();
|
|
});
|
|
} else {
|
|
me = {User:{ID:0,Session:""},Site:{"MaxRequestSize":0}};
|
|
runInitHook("pre_init");
|
|
}
|
|
})(); |