fix poll results on ajax topic and eliminate unnecessary allocs for resources that aren't used
This commit is contained in:
parent
aa32bd335c
commit
372508bf98
|
@ -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))
|
||||||
}
|
}
|
||||||
|
|
106
public/global.js
106
public/global.js
|
@ -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");
|
||||||
}
|
}
|
|
@ -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");
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
if r.FormValue("i") != "1" {
|
||||||
c.PrepResources(&h.CurrentUser, h, h.Theme)
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -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}}
|
||||||
|
|
Loading…
Reference in New Issue