save bytes

This commit is contained in:
Azareal 2020-03-10 07:41:17 +10:00
parent d586220df2
commit bba83ef727
7 changed files with 53 additions and 71 deletions

View File

@ -6,4 +6,4 @@ addInitHook("end_init", () => {
$("#dash_username button").show(); $("#dash_username button").show();
}); });
}); });
})(); })()

View File

@ -1,26 +1,12 @@
'use strict'; 'use strict';
var me={}; var me={};
var phraseBox={}; var phraseBox={};
if(tmplInits===undefined) var tmplInits={}; if(tmplInits===undefined) var tmplInits={};
var tmplPhrases=[]; // [key] array of phrases indexed by order of use var tmplPhrases=[]; // [key] array of phrases indexed by order of use
var hooks={ // Shorten this list by binding the hooks just in time? var hooks={};
"pre_iffe":[],
"pre_init":[],
"start_init":[],
"almost_end_init":[],
"end_init":[],
"after_phrases":[],
"after_add_alert":[],
"after_update_alert_list":[],
"open_edit":[],
"close_edit":[],
"edit_item_pre_bind":[],
"analytics_loaded":[],
};
var ranInitHooks={} var ranInitHooks={}
function runHook(name, ...args) { function runHook(name,...args) {
if(!(name in hooks)) { if(!(name in hooks)) {
console.log("Couldn't find hook '" + name + "'"); console.log("Couldn't find hook '" + name + "'");
return; return;
@ -33,19 +19,19 @@ function runHook(name, ...args) {
return ret; return ret;
} }
function addHook(name, h) { function addHook(name,h) {
if(hooks[name]===undefined) hooks[name] = []; if(hooks[name]===undefined) hooks[name]=[];
hooks[name].push(h); hooks[name].push(h);
} }
// 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 // 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) { function runInitHook(name,...args) {
let ret = runHook(name,...args); let ret = runHook(name,...args);
ranInitHooks[name] = true; ranInitHooks[name] = true;
return ret; return ret;
} }
function addInitHook(name, h) { function addInitHook(name,h) {
addHook(name, h); addHook(name, h);
if(name in ranInitHooks) h(); if(name in ranInitHooks) h();
} }
@ -78,26 +64,26 @@ function asyncGetScript(src) {
script.src = src; script.src = src;
const prior = document.getElementsByTagName('script')[0]; const prior = document.getElementsByTagName('script')[0];
prior.parentNode.insertBefore(script, prior); prior.parentNode.insertBefore(script,prior);
}); });
} }
function notifyOnScript(src) { function notifyOnScript(src) {
src = "/s/"+src; src = "/s/"+src;
return new Promise((resolve, reject) => { return new Promise((resolve,reject) => {
let ss = src.replace("/s/",""); let ss = src.replace("/s/","");
try { try {
let ssp = ss.charAt(0).toUpperCase() + ss.slice(1) let ssp = ss.charAt(0).toUpperCase() + ss.slice(1)
console.log("ssp:",ssp) console.log("ssp",ssp)
if(window[ssp]) { if(window[ssp]) {
resolve(); resolve();
return; return;
} }
} catch(e) {} } catch(e) {}
console.log("src:",src) console.log("src",src)
let script = document.querySelectorAll('[src^="'+src+'"]')[0]; let script = document.querySelectorAll('[src^="'+src+'"]')[0];
console.log("script:",script); console.log("script",script);
if(script===undefined) { if(script===undefined) {
reject("no script found"); reject("no script found");
return; return;
@ -108,7 +94,6 @@ function notifyOnScript(src) {
script.onreadystatechange = null; script.onreadystatechange = null;
resolve(); resolve();
} }
script.onerror = (e) => { script.onerror = (e) => {
reject(e); reject(e);
}; };
@ -123,9 +108,9 @@ function notifyOnScriptW(name,complete,success) {
console.log("Loaded " +name+".js"); console.log("Loaded " +name+".js");
complete(); complete();
if(success!==undefined) success(); if(success!==undefined) success();
}).catch((e) => { }).catch(e => {
console.log("Unable to get script name '"+name+"'"); console.log("Unable to get script name '"+name+"'");
console.log("e: ", e); console.log("e", e);
console.trace(); console.trace();
complete(e); complete(e);
}); });
@ -147,29 +132,26 @@ function loadScript(name,callback,fail) {
if(fname!=name) { if(fname!=name) {
asyncGetScript(iurl) asyncGetScript(iurl)
.then(callback) .then(callback)
.catch((e) => { .catch(e => {
console.log("Unable to get script '"+iurl+"'"); console.log("Unable to get script '"+iurl+"'");
console.log("e:", e); console.log("e", e);
console.trace(); console.trace();
}); });
} }
console.log("e:", e); console.log("e", e);
console.trace(); console.trace();
fail(e); fail(e);
}); });
} }
/* /*function loadTmpl(name,callback) {
function loadTmpl(name,callback) {
let url = "/s/"+name let url = "/s/"+name
let worker = new Worker(url); let worker = new Worker(url);
} }*/
*/
function DoNothingButPassBack(it) { function DoNothingButPassBack(it) {
return it; return it;
} }
function RelativeTime(date) { function RelativeTime(date) {
return date; return date;
} }
@ -186,27 +168,27 @@ function initPhrases(loggedIn, panel = false) {
function fetchPhrases(plist) { function fetchPhrases(plist) {
fetch("/api/phrases/?q="+plist, {cache: "no-cache"}) fetch("/api/phrases/?q="+plist, {cache: "no-cache"})
.then((resp) => resp.json()) .then(resp => resp.json())
.then((data) => { .then(data => {
console.log("loaded phrase endpoint data"); console.log("loaded phrase endpoint data");
console.log("data:",data); console.log("data",data);
Object.keys(tmplInits).forEach((key) => { Object.keys(tmplInits).forEach(key => {
let phrases = []; let phrases = [];
let tmplInit = tmplInits[key]; let tmplInit = tmplInits[key];
for(let phraseName of tmplInit) phrases.push(data[phraseName]); for(let phraseName of tmplInit) phrases.push(data[phraseName]);
console.log("Adding phrases"); console.log("Adding phrases");
console.log("key:",key); console.log("key",key);
console.log("phrases:",phrases); console.log("phrases",phrases);
tmplPhrases[key] = phrases; tmplPhrases[key] = phrases;
}); });
let prefixes = {}; let prefixes = {};
Object.keys(data).forEach((key) => { Object.keys(data).forEach(key => {
let prefix = key.split(".")[0]; let prefix = key.split(".")[0];
if(prefixes[prefix]===undefined) prefixes[prefix] = {}; if(prefixes[prefix]===undefined) prefixes[prefix] = {};
prefixes[prefix][key] = data[key]; prefixes[prefix][key] = data[key];
}); });
Object.keys(prefixes).forEach((prefix) => { Object.keys(prefixes).forEach(prefix => {
console.log("adding phrase prefix '"+prefix+"' to box"); console.log("adding phrase prefix '"+prefix+"' to box");
phraseBox[prefix] = prefixes[prefix]; phraseBox[prefix] = prefixes[prefix];
}); });
@ -217,7 +199,7 @@ function fetchPhrases(plist) {
(() => { (() => {
runInitHook("pre_iife"); runInitHook("pre_iife");
let loggedIn = document.head.querySelector("[property='x-loggedin']").content == "true"; let loggedIn = document.head.querySelector("[property='x-loggedin']").content=="true";
let panel = window.location.pathname.startsWith("/panel/"); let panel = window.location.pathname.startsWith("/panel/");
let toLoad = 1; let toLoad = 1;
@ -246,7 +228,7 @@ function fetchPhrases(plist) {
.then(resp => resp.json()) .then(resp => resp.json())
.then(data => { .then(data => {
console.log("loaded me endpoint data"); console.log("loaded me endpoint data");
console.log("data:",data); console.log("data",data);
me = data; me = data;
runInitHook("pre_init"); runInitHook("pre_init");
}); });

View File

@ -78,7 +78,7 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
let req = new XMLHttpRequest(); let req = new XMLHttpRequest();
req.addEventListener("load", () => { req.addEventListener("load", () => {
let data = JSON.parse(req.responseText); let data = JSON.parse(req.responseText);
//console.log("rdata:",data); //console.log("rdata",data);
let fileItem = document.createElement("div"); let fileItem = document.createElement("div");
let ext = getExt(filename); let ext = getExt(filename);
// TODO: Push ImageFileExts to the client from the server in some sort of gen.js? // TODO: Push ImageFileExts to the client from the server in some sort of gen.js?
@ -217,8 +217,8 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
bindAttachManager(); bindAttachManager();
}); });
$(".moderate_link").click((event) => { $(".moderate_link").click(ev => {
event.preventDefault(); ev.preventDefault();
$(".pre_opt").removeClass("auto_hide"); $(".pre_opt").removeClass("auto_hide");
$(".moderate_link").addClass("moderate_open"); $(".moderate_link").addClass("moderate_open");
$(".topic_row").each(function(){ $(".topic_row").each(function(){

View File

@ -9,7 +9,7 @@
<div id="poweredBy"> <div id="poweredBy">
<a id="poweredByName" href="https://github.com/Azareal/Gosora">{{lang "footer_powered_by"}}</a><span id="poweredByDash"> - </span><span id="poweredByMaker">{{lang "footer_made_with_love"}}</span> <a id="poweredByName" href="https://github.com/Azareal/Gosora">{{lang "footer_powered_by"}}</a><span id="poweredByDash"> - </span><span id="poweredByMaker">{{lang "footer_made_with_love"}}</span>
</div> </div>
{{if .CurrentUser.IsAdmin}}<div title="start to before tmpl" class="elapsed">{{.Header.Elapsed1}}</div><div title="start to footer" class="elapsed">{{elapsed .Header.StartedAt}}</div>{{end}} {{if .CurrentUser.IsAdmin}}<div title="start to before tmpl"class="elapsed">{{.Header.Elapsed1}}</div><div title="start to footer"class="elapsed">{{elapsed .Header.StartedAt}}</div>{{end}}
<form action="/theme/" method="post"> <form action="/theme/" method="post">
<div id="themeSelector"> <div id="themeSelector">
<select id="themeSelectorSelect" name="theme" aria-label="{{lang "footer_theme_selector_aria"}}">{{range .Header.Themes}}{{if not .HideFromThemes}} <select id="themeSelectorSelect" name="theme" aria-label="{{lang "footer_theme_selector_aria"}}">{{range .Header.Themes}}{{if not .HideFromThemes}}

View File

@ -1,6 +1,12 @@
"use strict"; "use strict";
(() => { (() => {
function newElement(etype, eclass) {
let element = document.createElement(etype);
element.className = eclass;
return element;
}
function moveAlerts() { function moveAlerts() {
// Move the alerts under the first header // Move the alerts under the first header
let colSel = $(".colstack_right .colstack_head:first"); let colSel = $(".colstack_right .colstack_head:first");
@ -15,7 +21,7 @@
//console.log("bf") //console.log("bf")
addInitHook("end_init", () => { addInitHook("end_init", () => {
//console.log("af") //console.log("af")
let loggedIn = document.head.querySelector("[property='x-loggedin']").content == "true"; let loggedIn = document.head.querySelector("[property='x-loggedin']").content=="true";
if(loggedIn) { if(loggedIn) {
if(navigator.userAgent.indexOf("Firefox") != -1) $.trumbowyg.svgPath = "/s/trumbowyg/ui/icons.svg"; if(navigator.userAgent.indexOf("Firefox") != -1) $.trumbowyg.svgPath = "/s/trumbowyg/ui/icons.svg";
@ -29,7 +35,7 @@
$('#input_content').trumbowyg('html', currentContent); $('#input_content').trumbowyg('html', currentContent);
} }
$(".topic_name_row").click(function(){ $(".topic_name_row").click(() => {
$(".topic_create_form").addClass("selectedInput"); $(".topic_create_form").addClass("selectedInput");
}); });
@ -99,11 +105,11 @@
// Fill in the placeholder grid nodes // Fill in the placeholder grid nodes
let rowCount = 4; let rowCount = 4;
console.log("rowCount: ",rowCount); console.log("rowCount",rowCount);
console.log("gridElementCount: ",gridElementCount); console.log("gridElementCount",gridElementCount);
if(gridElementCount%rowCount != 0) { if(gridElementCount%rowCount != 0) {
let fillerNodes = (rowCount - (gridElementCount%rowCount)); let fillerNodes = (rowCount - (gridElementCount%rowCount));
console.log("fillerNodes: ",fillerNodes); console.log("fillerNodes",fillerNodes);
for(let i = 0; i < fillerNodes;i++ ) { for(let i = 0; i < fillerNodes;i++ ) {
console.log("added a gridFiller"); console.log("added a gridFiller");
buttonGrid.appendChild(newElement("div","gridFiller")); buttonGrid.appendChild(newElement("div","gridFiller"));
@ -118,10 +124,4 @@
}); });
addInitHook("after_notice", moveAlerts); addInitHook("after_notice", moveAlerts);
})(); })()
function newElement(etype, eclass) {
let element = document.createElement(etype);
element.className = eclass;
return element;
}

View File

@ -3,13 +3,13 @@ addInitHook("end_init", () => {
// TODO: Run this when the image is loaded rather than when the document is ready? // TODO: Run this when the image is loaded rather than when the document is ready?
$(".topic_list img").each(function(){ $(".topic_list img").each(function(){
let aspectRatio = this.naturalHeight / this.naturalWidth; let aspectRatio = this.naturalHeight / this.naturalWidth;
console.log("aspectRatio ",aspectRatio); console.log("aspectRatio",aspectRatio);
console.log("this.height ",this.naturalHeight); console.log("this.height",this.naturalHeight);
console.log("this.width ",this.naturalWidth); console.log("this.width",this.naturalWidth);
$(this).css({ $(this).css({
height: aspectRatio * this.width height: aspectRatio * this.width
}); });
}); });
}); });
})(); })()

View File

@ -3,13 +3,13 @@ addInitHook("end_init", () => {
// TODO: Run this when the image is loaded rather than when the document is ready? // TODO: Run this when the image is loaded rather than when the document is ready?
$(".topic_list img").each(function(){ $(".topic_list img").each(function(){
let aspectRatio = this.naturalHeight / this.naturalWidth; let aspectRatio = this.naturalHeight / this.naturalWidth;
console.log("aspectRatio ",aspectRatio); console.log("aspectRatio",aspectRatio);
console.log("this.height ",this.naturalHeight); console.log("this.height",this.naturalHeight);
console.log("this.width ",this.naturalWidth); console.log("this.width",this.naturalWidth);
$(this).css({ $(this).css({
height: aspectRatio * this.width height: aspectRatio * this.width
}); });
}); });
}); });
})(); })()