fix poll results on ajax topic and eliminate unnecessary allocs for resources that aren't used

This commit is contained in:
Azareal 2020-03-13 12:56:23 +10:00
parent aa32bd335c
commit 372508bf98
7 changed files with 133 additions and 60 deletions

View File

@ -46,9 +46,9 @@ type Header struct {
LooseCSP bool LooseCSP bool
//StartedAt time.Time //StartedAt time.Time
StartedAt int64 StartedAt int64
Elapsed1 string Elapsed1 string
Writer http.ResponseWriter Writer http.ResponseWriter
ExtData ExtData ExtData ExtData
} }
func (h *Header) AddScript(name string) { func (h *Header) AddScript(name string) {
@ -101,6 +101,26 @@ func (h *Header) AddSheet(name string) {
h.Stylesheets = append(h.Stylesheets, name) h.Stylesheets = append(h.Stylesheets, name)
} }
// ! Experimental
func (h *Header) AddXRes(names ...string) {
var o string
for i, name := range names {
if name[0] == '/' && name[1] == '/' {
} else {
file, ok := StaticFiles.Get("/s/" + name)
if ok {
name = file.OName
}
}
if i != 0 {
o += "," + name
} else {
o += name
}
}
h.Writer.Header().Set("X-Res", o)
}
func (h *Header) AddNotice(name string) { func (h *Header) AddNotice(name string) {
h.NoticeList = append(h.NoticeList, p.GetNoticePhrase(name)) h.NoticeList = append(h.NoticeList, p.GetNoticePhrase(name))
} }
@ -356,12 +376,12 @@ type PanelAnalyticsActiveMemory struct {
MemType int MemType int
} }
type PanelAnalyticsPerf struct { type PanelAnalyticsPerf struct {
Graph PanelTimeGraph Graph PanelTimeGraph
ViewItems []PanelAnalyticsItemUnit ViewItems []PanelAnalyticsItemUnit
TimeRange string TimeRange string
Unit string Unit string
TimeType string TimeType string
PerfType int PerfType int
} }
type PanelStats struct { type PanelStats struct {
@ -476,7 +496,7 @@ type PanelAnalyticsRoutesPage struct {
type PanelAnalyticsRoutesPerfItem struct { type PanelAnalyticsRoutesPerfItem struct {
Route string Route string
Count int Count int
Unit string Unit string
} }
type PanelAnalyticsRoutesPerfPage struct { type PanelAnalyticsRoutesPerfPage struct {
@ -677,11 +697,11 @@ type PanelRegLogsPage struct {
} }
type DebugPageTasks struct { type DebugPageTasks struct {
HalfSecond int HalfSecond int
Second int Second int
FifteenMinute int FifteenMinute int
Hour int Hour int
Shutdown int Shutdown int
} }
type DebugPageCache struct { type DebugPageCache struct {
@ -742,8 +762,8 @@ type PanelDebugPage struct {
Goroutines int Goroutines int
CPUs int CPUs int
Tasks DebugPageTasks Tasks DebugPageTasks
MemStats runtime.MemStats MemStats runtime.MemStats
Cache DebugPageCache Cache DebugPageCache
Database DebugPageDatabase Database DebugPageDatabase
Disk DebugPageDisk Disk DebugPageDisk

View File

@ -341,6 +341,12 @@ function runWebSockets(resume=false) {
} }
} }
// TODO: Surely, there's a prettier and more elegant way of doing this?
function getExt(name) {
if(!name.indexOf('.' > -1)) throw("This file doesn't have an extension");
return name.split('.').pop();
}
(() => { (() => {
addInitHook("pre_init", () => { addInitHook("pre_init", () => {
runInitHook("pre_global"); runInitHook("pre_global");
@ -813,42 +819,57 @@ function mainInit(){
if(ev.which == 39) this.querySelectorAll("#nextFloat a")[0].click(); if(ev.which == 39) this.querySelectorAll("#nextFloat a")[0].click();
}; };
//id="poll_results_{{.Poll.ID}}" class="poll_results auto_hide" function asyncGetSheet(src) {
$(".poll_results_button").click(function(){ return new Promise((resolve,reject) => {
let pollID = $(this).attr("data-poll-id"); let res = document.createElement('link');
$("#poll_results_" + pollID).removeClass("auto_hide"); res.async = true;
fetch("/poll/results/" + pollID, {
credentials: 'same-origin' const onloadHandler = (e,isAbort) => {
}).then(resp => resp.text()).catch(err => console.error("err",err)).then(rawData => { if (isAbort || !res.readyState || /loaded|complete/.test(res.readyState)) {
// TODO: Make sure the received data is actually a list of integers res.onload = null;
let data = JSON.parse(rawData); res.onreadystatechange = null;
let allZero = true; res = undefined;
for(let i = 0; i < data.length; i++) {
if(data[i] != "0") allZero = false; isAbort ? reject(e) : resolve();
} }
if(allZero) {
$("#poll_results_"+pollID+" .poll_no_results").removeClass("auto_hide");
console.log("all zero")
return;
} }
$("#poll_results_"+pollID+" .user_content").html("<div id='poll_results_chart_"+pollID+"'></div>"); res.onerror = (e) => {
console.log("rawData",rawData); reject(e);
console.log("series",data); };
Chartist.Pie('#poll_results_chart_'+pollID, { res.onload = onloadHandler;
series: data, res.onreadystatechange = onloadHandler;
}, { res.href = src;
height: '120px', res.rel = "stylesheet";
}); res.type = "text/css";
})
});
$(".rowtopic a, a.rowtopic").click(function(ev) { const prior = document.getElementsByTagName('link')[0];
prior.parentNode.insertBefore(res,prior);
});
}
function stripQ(name) {
return name.split('?')[0];
}
$(".rowtopic a,a.rowtopic").click(function(ev) {
let base = this.getAttribute("href"); let base = this.getAttribute("href");
let href = base + "?i=1"; let href = base + "?i=1";
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");
let xRes = resp.headers.get("x-res")
for(let res of xRes.split(",")) {
let pro;
if(stripQ(getExt(res)) == "css") pro = asyncGetSheet("/s/"+res)
else pro = asyncGetScript("/s/"+res)
pro.then(() => console.log("Loaded " + res))
.catch(e => {
console.log("Unable to get res '"+res+"'");
console.log("e",e);
console.trace();
});
}
return resp.text(); return resp.text();
}).then(data => { }).then(data => {
document.querySelector("#back").outerHTML = data; document.querySelector("#back").outerHTML = data;
@ -1002,6 +1023,36 @@ function bindTopic() {
quoteItemCallback(src.innerHTML,item); quoteItemCallback(src.innerHTML,item);
}); });
//id="poll_results_{{.Poll.ID}}" class="poll_results auto_hide"
$(".poll_results_button").click(function(){
let pollID = $(this).attr("data-poll-id");
$("#poll_results_" + pollID).removeClass("auto_hide");
fetch("/poll/results/" + pollID, {
credentials: 'same-origin'
}).then(resp => resp.text()).catch(err => console.error("err",err)).then(rawData => {
// TODO: Make sure the received data is actually a list of integers
let data = JSON.parse(rawData);
let allZero = true;
for(let i = 0; i < data.length; i++) {
if(data[i] != "0") allZero = false;
}
if(allZero) {
$("#poll_results_"+pollID+" .poll_no_results").removeClass("auto_hide");
console.log("all zero")
return;
}
$("#poll_results_"+pollID+" .user_content").html("<div id='poll_results_chart_"+pollID+"'></div>");
console.log("rawData",rawData);
console.log("series",data);
Chartist.Pie('#poll_results_chart_'+pollID, {
series: data,
}, {
height: '120px',
});
})
});
runHook("end_bind_topic"); runHook("end_bind_topic");
} }
@ -1012,5 +1063,6 @@ 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");
$(".poll_results_button").unbind("click");
runHook("end_unbind_topic"); runHook("end_unbind_topic");
} }

View File

@ -110,7 +110,7 @@ function notifyOnScriptW(name,complete,success) {
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);
}); });
@ -134,11 +134,11 @@ function loadScript(name,callback,fail) {
.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);
}); });
@ -156,7 +156,7 @@ function RelativeTime(date) {
return date; return date;
} }
function initPhrases(loggedIn, panel = false) { function initPhrases(loggedIn, panel=false) {
console.log("in initPhrases") console.log("in initPhrases")
console.log("tmlInits",tmplInits) console.log("tmlInits",tmplInits)
let e = ""; let e = "";
@ -167,7 +167,7 @@ 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");

View File

@ -14,12 +14,6 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
document.body.removeChild(el); document.body.removeChild(el);
} }
// TODO: Surely, there's a prettier and more elegant way of doing this?
function getExt(name) {
if(!name.indexOf('.' > -1)) throw("This file doesn't have an extension");
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];
@ -289,7 +283,7 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
function addPollInput() { function addPollInput() {
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({
@ -297,7 +291,7 @@ var imageExts = ["png", "jpg", "jpe","jpeg","jif","jfi","jfif", "svg", "bmp", "g
Place: phraseBox["topic"]["topic.reply_add_poll_option"].replace("%d",pollInputIndex), Place: phraseBox["topic"]["topic.reply_add_poll_option"].replace("%d",pollInputIndex),
})); }));
pollInputIndex++; pollInputIndex++;
console.log("new pollInputIndex", pollInputIndex); console.log("new pollInputIndex",pollInputIndex);
$(".pollinputinput").off("click"); $(".pollinputinput").off("click");
$(".pollinputinput").click(addPollInput); $(".pollinputinput").click(addPollInput);
} }

View File

@ -119,7 +119,9 @@ func FootHeaders(w http.ResponseWriter, header *c.Header) {
func renderTemplate3(tmplName, hookName string, w http.ResponseWriter, r *http.Request, h *c.Header, pi interface{}) error { func renderTemplate3(tmplName, hookName string, w http.ResponseWriter, r *http.Request, h *c.Header, pi interface{}) error {
s := h.Stylesheets s := h.Stylesheets
h.Stylesheets = nil h.Stylesheets = nil
c.PrepResources(&h.CurrentUser, h, h.Theme) if r.FormValue("i") != "1" {
c.PrepResources(&h.CurrentUser, h, h.Theme)
}
for _, ss := range s { for _, ss := range s {
h.Stylesheets = append(h.Stylesheets, ss) h.Stylesheets = append(h.Stylesheets, ss)
} }

View File

@ -115,8 +115,6 @@ func ViewTopic(w http.ResponseWriter, r *http.Request, user c.User, header *c.He
return c.InternalError(err, w, r) return c.InternalError(err, w, r)
} }
poll = pPoll.Copy() poll = pPoll.Copy()
header.AddSheet("chartist/chartist.min.css")
header.AddScript("chartist/chartist.min.js")
} }
if topic.LikeCount > 0 && user.Liked > 0 { if topic.LikeCount > 0 && user.Liked > 0 {
@ -166,6 +164,9 @@ func ViewTopic(w http.ResponseWriter, r *http.Request, user c.User, header *c.He
var rerr c.RouteError var rerr c.RouteError
tmpl := forum.Tmpl tmpl := forum.Tmpl
if r.FormValue("i") == "1" { if r.FormValue("i") == "1" {
if tpage.Poll.ID != 0 {
header.AddXRes("chartist/chartist.min.css", "chartist/chartist.min.js")
}
if tmpl == "" { if tmpl == "" {
rerr = renderTemplate("topic_mini", w, r, header, tpage) rerr = renderTemplate("topic_mini", w, r, header, tpage)
} else { } else {
@ -176,6 +177,10 @@ func ViewTopic(w http.ResponseWriter, r *http.Request, user c.User, header *c.He
} }
} }
} else { } else {
if tpage.Poll.ID != 0 {
header.AddSheet("chartist/chartist.min.css")
header.AddScript("chartist/chartist.min.js")
}
if tmpl == "" { if tmpl == "" {
rerr = renderTemplate("topic", w, r, header, tpage) rerr = renderTemplate("topic", w, r, header, tpage)
} else { } else {

View File

@ -3,10 +3,10 @@
<head> <head>
<title>{{.Title}} | {{.Header.Site.Name}}</title> <title>{{.Title}} | {{.Header.Site.Name}}</title>
{{range .Header.Stylesheets}} {{range .Header.Stylesheets}}
<link href="/s/{{.}}" rel="stylesheet" type="text/css">{{end}} <link href="/s/{{.}}"rel="stylesheet"type="text/css">{{end}}
{{range .Header.PreScriptsAsync}} {{range .Header.PreScriptsAsync}}
<script async src="/s/{{.}}"></script>{{end}} <script async src="/s/{{.}}"></script>{{end}}
<meta property="x-loggedin" content="{{.CurrentUser.Loggedin}}"/> <meta property="x-loggedin"content="{{.CurrentUser.Loggedin}}">
<script src="/s/init.js?i=10"></script> <script src="/s/init.js?i=10"></script>
{{range .Header.ScriptsAsync}} {{range .Header.ScriptsAsync}}
<script async src="/s/{{.}}"></script>{{end}} <script async src="/s/{{.}}"></script>{{end}}