save bytes

This commit is contained in:
Azareal 2020-03-09 20:37:41 +10:00
parent aa303dc14d
commit 42b38ae3d8
10 changed files with 128 additions and 130 deletions

View File

@ -1,6 +1,5 @@
function memStuff(window, document, Chartist) {
'use strict';
Chartist.plugins = Chartist.plugins || {};
Chartist.plugins.byteUnits = function(options) {
options = Chartist.extend({}, {}, options);
@ -17,7 +16,7 @@ function memStuff(window, document, Chartist) {
for(let i = 0; i < vbits.length; i++) {
tbits[i] = vbits[i].innerHTML;
}
console.log("tbits:",tbits);
console.log("tbits",tbits);
const calc = (places) => {
if(places==3) return;
@ -40,7 +39,6 @@ function memStuff(window, document, Chartist) {
function perfStuff(window, document, Chartist) {
'use strict';
Chartist.plugins = Chartist.plugins || {};
Chartist.plugins.perfUnits = function(options) {
options = Chartist.extend({}, {}, options);
@ -85,18 +83,18 @@ const Terabyte = Gigabyte * 1024;
const Petabyte = Terabyte * 1024;
function convertByteUnit(bytes, places = 0) {
let out;
if(bytes >= Petabyte) out = [bytes / Petabyte, "PB"];
else if(bytes >= Terabyte) out = [bytes / Terabyte, "TB"];
else if(bytes >= Gigabyte) out = [bytes / Gigabyte, "GB"];
else if(bytes >= Megabyte) out = [bytes / Megabyte, "MB"];
else if(bytes >= Kilobyte) out = [bytes / Kilobyte, "KB"];
else out = [bytes,"b"];
let o;
if(bytes >= Petabyte) o = [bytes / Petabyte, "PB"];
else if(bytes >= Terabyte) o = [bytes / Terabyte, "TB"];
else if(bytes >= Gigabyte) o = [bytes / Gigabyte, "GB"];
else if(bytes >= Megabyte) o = [bytes / Megabyte, "MB"];
else if(bytes >= Kilobyte) o = [bytes / Kilobyte, "KB"];
else o = [bytes,"b"];
if(places==0) return Math.ceil(out[0]) + out[1];
if(places==0) return Math.ceil(o[0]) + o[1];
else {
let ex = Math.pow(10, places);
return (Math.round(out[0], ex) / ex) + out[1];
return (Math.round(o[0], ex) / ex) + o[1];
}
}
@ -106,18 +104,18 @@ let min = sec * 60;
let hour = min * 60;
let day = hour * 24;
function convertPerfUnit(quan, places = 0) {
let out;
if(quan >= day) out = [quan / day, "d"];
else if(quan >= hour) out = [quan / hour, "h"];
else if(quan >= min) out = [quan / min, "m"];
else if(quan >= sec) out = [quan / sec, "s"];
else if(quan >= ms) out = [quan / ms, "ms"];
else out = [quan,"μs"];
let o;
if(quan >= day) o = [quan / day, "d"];
else if(quan >= hour) o = [quan / hour, "h"];
else if(quan >= min) o = [quan / min, "m"];
else if(quan >= sec) o = [quan / sec, "s"];
else if(quan >= ms) o = [quan / ms, "ms"];
else o = [quan,"μs"];
if(places==0) return Math.ceil(out[0]) + out[1];
if(places==0) return Math.ceil(o[0]) + o[1];
else {
let ex = Math.pow(10, places);
return (Math.round(out[0], ex) / ex) + out[1];
return (Math.round(o[0], ex) / ex) + o[1];
}
}
@ -125,7 +123,7 @@ function convertPerfUnit(quan, places = 0) {
// TODO: Load rawLabels and seriesData dynamically rather than potentially fiddling with nonces for the CSP?
function buildStatsChart(rawLabels, seriesData, timeRange, legendNames, typ=0) {
console.log("buildStatsChart");
console.log("seriesData:",seriesData);
console.log("seriesData",seriesData);
let labels = [];
let aphrases = phraseBox["analytics"];
if(timeRange=="one-year") {
@ -158,19 +156,19 @@ function buildStatsChart(rawLabels, seriesData, timeRange, legendNames, typ=0) {
continue;
}
let date = new Date(rawLabels[i]*1000);
console.log("date:", date);
console.log("date", date);
let minutes = "0" + date.getMinutes();
let label = date.getHours() + ":" + minutes.substr(-2);
console.log("label:", label);
console.log("label", label);
labels.push(label);
}
} else {
for(const i in rawLabels) {
let date = new Date(rawLabels[i]*1000);
console.log("date:", date);
console.log("date", date);
let minutes = "0" + date.getMinutes();
let label = date.getHours() + ":" + minutes.substr(-2);
console.log("label:", label);
console.log("label", label);
labels.push(label);
}
}

View File

@ -119,7 +119,7 @@ function updateAlertList(menuAlerts) {
}
bindToAlerts();
console.log("alertCount:",alertCount)
console.log("alertCount",alertCount)
runInitHook("after_update_alert_list", alertCount);
}
@ -192,7 +192,7 @@ function SplitN(data,ch,n) {
}
function wsAlertEvent(data) {
console.log("wsAlertEvent:",data)
console.log("wsAlertEvent",data)
addAlert(data, true);
alertCount++;
@ -409,7 +409,7 @@ function Paginate(currentPage, lastPage, maxPages) {
function mainInit(){
runInitHook("start_init");
$(".more_topics").click((ev) => {
$(".more_topics").click(ev => {
ev.preventDefault();
let moreTopicBlocks = document.getElementsByClassName("more_topic_block_active");
for(let i = 0; i < moreTopicBlocks.length; i++) {
@ -505,10 +505,10 @@ function mainInit(){
// TODO: Try to de-duplicate some of these fetch calls
fetch(url+q+"&js=1", {credentials: "same-origin"})
.then((resp) => {
.then(resp => {
if(!resp.ok) throw(url+q+"&js=1 failed to load");
return resp.json();
}).then((data) => {
}).then(data => {
if(!"Topics" in data) throw("no Topics in data");
let topics = data["Topics"];
console.log("ajax navigated to different page");
@ -522,29 +522,29 @@ function mainInit(){
history.pushState(obj, obj.Title, obj.Url);
rebuildPaginator(data.LastPage);
rebindPaginator();
}).catch((ex) => {
}).catch(ex => {
console.log("Unable to get script '"+url+q+"&js=1"+"'");
console.log("ex: ", ex);
console.log("ex", ex);
console.trace();
});
});
}
// TODO: Render a headless topics.html instead of the individual topic rows and a bit of JS glue
$(".filter_item").click(function(event) {
$(".filter_item").click(function(ev) {
if(!window.location.pathname.startsWith("/topics/")) return
event.preventDefault();
ev.preventDefault();
let that = this;
let fid = this.getAttribute("data-fid");
// TODO: Take mostviewed into account
let url = "//"+window.location.host+"/topics/?fids="+fid;
fetch(url+"&js=1", {credentials: "same-origin"})
.then((resp) => {
.then(resp => {
if(!resp.ok) throw(url+"&js=1 failed to load");
return resp.json();
}).then((data) => {
console.log("data:",data);
}).then(data => {
console.log("data",data);
if(!"Topics" in data) throw("no Topics in data");
let topics = data["Topics"];
console.log("ajax navigated to "+that.innerText);
@ -568,9 +568,9 @@ function mainInit(){
});
that.classList.add("filter_selected");
$(".topic_list_title h1").text(that.innerText);
}).catch((ex) => {
}).catch(ex => {
console.log("Unable to get script '"+url+"&js=1"+"'");
console.log("ex: ", ex);
console.log("ex", ex);
console.trace();
});
});
@ -592,10 +592,10 @@ function mainInit(){
// TODO: Try to de-duplicate some of these fetch calls
fetch(url+q+"&js=1", {credentials: "same-origin"})
.then((resp) => {
.then(resp => {
if(!resp.ok) throw(url+q+"&js=1 failed to load");
return resp.json();
}).then((data) => {
}).then(data => {
if(!"Topics" in data) throw("no Topics in data");
let topics = data["Topics"];
console.log("ajax navigated to search page");
@ -613,22 +613,22 @@ function mainInit(){
history.pushState(obj, obj.Title, obj.Url);
rebuildPaginator(data.LastPage);
rebindPaginator();
}).catch((ex) => {
}).catch(ex => {
console.log("Unable to get script '"+url+q+"&js=1"+"'");
console.log("ex: ", ex);
console.log("ex", ex);
console.trace();
});
});
$(".open_edit").click((event) => {
event.preventDefault();
$(".open_edit").click(ev => {
ev.preventDefault();
$('.hide_on_edit').addClass("edit_opened");
$('.show_on_edit').addClass("edit_opened");
runHook("open_edit");
});
$(".topic_item .submit_edit").click(function(event){
event.preventDefault();
$(".topic_item .submit_edit").click(function(ev){
ev.preventDefault();
let topicNameInput = $(".topic_name_input").val();
$(".topic_name").html(topicNameInput);
$(".topic_name").attr(topicNameInput);
@ -658,53 +658,53 @@ function mainInit(){
});
});
$(".delete_item").click(function(event) {
postLink(event);
$(".delete_item").click(function(ev) {
postLink(ev);
$(this).closest('.deletable_block').remove();
});
// Miniature implementation of the parser to avoid sending as much data back and forth
function quickParse(msg) {
msg = msg.replace(":)", "😀")
msg = msg.replace(":(", "😞")
msg = msg.replace(":D", "😃")
msg = msg.replace(":P", "😛")
msg = msg.replace(":O", "😲")
msg = msg.replace(":p", "😛")
msg = msg.replace(":o", "😲")
msg = msg.replace(";)", "😉")
msg = msg.replace("\n","<br>")
return msg
function quickParse(m) {
m = m.replace(":)", "😀")
m = m.replace(":(", "😞")
m = m.replace(":D", "😃")
m = m.replace(":P", "😛")
m = m.replace(":O", "😲")
m = m.replace(":p", "😛")
m = m.replace(":o", "😲")
m = m.replace(";)", "😉")
m = m.replace("\n","<br>")
return m
}
$(".edit_item").click(function(event){
event.preventDefault();
$(".edit_item").click(function(ev){
ev.preventDefault();
let blockParent = this.closest('.editable_parent');
$(blockParent).find('.hide_on_edit').addClass("edit_opened");
$(blockParent).find('.show_on_edit').addClass("edit_opened");
$(blockParent).find('.hide_on_block_edit').addClass("edit_opened");
$(blockParent).find('.show_on_block_edit').addClass("edit_opened");
let srcNode = blockParent.querySelector(".edit_source");
let block = blockParent.querySelector('.editable_block');
let bp = this.closest('.editable_parent');
$(bp).find('.hide_on_edit').addClass("edit_opened");
$(bp).find('.show_on_edit').addClass("edit_opened");
$(bp).find('.hide_on_block_edit').addClass("edit_opened");
$(bp).find('.show_on_block_edit').addClass("edit_opened");
let srcNode = bp.querySelector(".edit_source");
let block = bp.querySelector('.editable_block');
block.classList.add("in_edit");
let source = "";
if(srcNode!=null) source = srcNode.innerText;
else source = block.innerHTML;
block.innerHTML = Template_topic_c_edit_post({
ID: blockParent.getAttribute("id").slice("post-".length),
ID: bp.getAttribute("id").slice("post-".length),
Source: source,
Ref: this.closest('a').getAttribute("href")
})
runHook("edit_item_pre_bind");
$(".submit_edit").click(function(event){
event.preventDefault();
$(blockParent).find('.hide_on_edit').removeClass("edit_opened");
$(blockParent).find('.show_on_edit').removeClass("edit_opened");
$(blockParent).find('.hide_on_block_edit').removeClass("edit_opened");
$(blockParent).find('.show_on_block_edit').removeClass("edit_opened");
$(".submit_edit").click(function(ev){
ev.preventDefault();
$(bp).find('.hide_on_edit').removeClass("edit_opened");
$(bp).find('.show_on_edit').removeClass("edit_opened");
$(bp).find('.hide_on_block_edit').removeClass("edit_opened");
$(bp).find('.show_on_block_edit').removeClass("edit_opened");
block.classList.remove("in_edit");
let content = block.querySelector('textarea').value;
block.innerHTML = quickParse(content);
@ -725,16 +725,16 @@ function mainInit(){
});
});
$(".edit_field").click(function(event) {
event.preventDefault();
let blockParent = $(this).closest('.editable_parent');
let block = blockParent.find('.editable_block').eq(0);
$(".edit_field").click(function(ev) {
ev.preventDefault();
let bp = $(this).closest('.editable_parent');
let block = bp.find('.editable_block').eq(0);
block.html("<input name='edit_field' value='" + block.text() + "' type='text'/><a href='" + $(this).closest('a').attr("href") + "'><button class='submit_edit' type='submit'>Update</button></a>");
$(".submit_edit").click(function(event) {
event.preventDefault();
let blockParent = $(this).closest('.editable_parent');
let block = blockParent.find('.editable_block').eq(0);
$(".submit_edit").click(function(ev) {
ev.preventDefault();
let bp = $(this).closest('.editable_parent');
let block = bp.find('.editable_block').eq(0);
let content = block.find('input').eq(0).val();
block.html(content);
@ -749,8 +749,8 @@ function mainInit(){
});
});
$(".edit_fields").click(function(event) {
event.preventDefault();
$(".edit_fields").click(function(ev) {
ev.preventDefault();
if($(this).find("input").length !== 0) return;
//console.log("clicked .edit_fields");
var blockParent = $(this).closest('.editable_parent');
@ -784,11 +784,11 @@ function mainInit(){
// Remove any handlers already attached to the submitter
$(".submit_edit").unbind("click");
$(".submit_edit").click(function(event) {
event.preventDefault();
$(".submit_edit").click(function(ev) {
ev.preventDefault();
var outData = {js: 1}
var blockParent = $(this).closest('.editable_parent');
blockParent.find('.editable_block').each(function() {
var bp = $(this).closest('.editable_parent');
bp.find('.editable_block').each(function() {
var fieldName = this.getAttribute("data-field");
var fieldType = this.getAttribute("data-type");
if(fieldType=="list") {
@ -806,11 +806,11 @@ function mainInit(){
});
var formAction = $(this).closest('a').attr("href");
//console.log("Form Action:", formAction);
//console.log("Form Action", formAction);
//console.log(outData);
$.ajax({ url: formAction + "?s=" + me.User.S, type:"POST", dataType:"json", data: outData, error: ajaxError });
blockParent.find('.hide_on_edit').removeClass("edit_opened");
blockParent.find('.show_on_edit').removeClass("edit_opened");
bp.find('.hide_on_edit').removeClass("edit_opened");
bp.find('.show_on_edit').removeClass("edit_opened");
});
});
@ -939,7 +939,7 @@ function mainInit(){
$("#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) => {
}).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;
@ -953,8 +953,8 @@ function mainInit(){
}
$("#poll_results_"+pollID+" .user_content").html("<div id='poll_results_chart_"+pollID+"'></div>");
console.log("rawData: ", rawData);
console.log("series: ", data);
console.log("rawData", rawData);
console.log("series", data);
Chartist.Pie('#poll_results_chart_'+pollID, {
series: data,
}, {
@ -965,4 +965,4 @@ function mainInit(){
runInitHook("almost_end_init");
runInitHook("end_init");
};
}

View File

@ -8,19 +8,19 @@ formVars = {
var forums = {};
let items = document.getElementsByClassName("panel_forum_item");
for(let i = 0; item = items[i]; i++) forums[i] = item.getAttribute("data-fid");
console.log("forums:",forums);
console.log("forums",forums);
Sortable.create(document.getElementById("panel_forums"), {
sort: true,
onEnd: (evt) => {
console.log("pre forums:", forums)
console.log("evt:", evt)
console.log("pre forums", forums)
console.log("evt", evt)
let oldFid = forums[evt.newIndex];
forums[evt.oldIndex] = oldFid;
let newFid = evt.item.getAttribute("data-fid");
console.log("newFid:", newFid);
console.log("newFid", newFid);
forums[evt.newIndex] = newFid;
console.log("post forums:", forums);
console.log("post forums", forums);
}
});
@ -37,12 +37,12 @@ document.getElementById("panel_forums_order_button").addEventListener("click", (
if(req.status!==200) return;
let resp = JSON.parse(req.responseText);
console.log("resp:", resp);
console.log("resp", resp);
// TODO: Should we move other notices into TmplPhrases like this one?
pushNotice(phraseBox["panel"]["panel.forums_order_updated"]);
if(resp.success==1) return;
} catch(ex) {
console.error("exception: ", ex)
console.error("ex", ex)
}
console.trace();
}
@ -56,4 +56,4 @@ document.getElementById("panel_forums_order_button").addEventListener("click", (
});
});
})();
})()

View File

@ -9,14 +9,14 @@ for(let i = 0; item = items[i]; i++) menuItems[i] = item.getAttribute("data-miid
Sortable.create(document.getElementById("panel_menu_item_holder"), {
sort: true,
onEnd: (evt) => {
console.log("pre menuItems:", menuItems)
console.log("evt:", evt)
console.log("pre menuItems", menuItems)
console.log("evt", evt)
let oldMiid = menuItems[evt.newIndex];
menuItems[evt.oldIndex] = oldMiid;
let newMiid = evt.item.getAttribute("data-miid");
console.log("newMiid:", newMiid);
console.log("newMiid", newMiid);
menuItems[evt.newIndex] = newMiid;
console.log("post menuItems:", menuItems);
console.log("post menuItems", menuItems);
}
});
@ -32,13 +32,13 @@ document.getElementById("panel_menu_items_order_button").addEventListener("click
// TODO: Signal the error with a notice
if(req.status===200) {
let resp = JSON.parse(req.responseText);
console.log("resp:", resp);
console.log("resp", resp);
// TODO: Should we move other notices into TmplPhrases like this one?
pushNotice(phraseBox["panel"]["panel.themes_menus_items_order_updated"]);
if(resp.success==1) return;
}
} catch(ex) {
console.error("exception:", ex)
console.error("ex", ex)
}
console.trace();
}
@ -53,4 +53,4 @@ document.getElementById("panel_menu_items_order_button").addEventListener("click
});
});
})();
})()

View File

@ -4,11 +4,11 @@
.then(resp => {
if(resp.status!==200) {
console.log("error");
console.log("response:", resp);
console.log("resp:", resp);
return;
}
resp.text().then(data => eval(data));
})
.catch(err => console.log("err:", err));
.catch(err => console.log("err", err));
});
})();
})()

View File

@ -4,14 +4,14 @@
<a href="{{.Creator.Link}}"><img src="{{.Creator.MicroAvatar}}" height=64 alt="Avatar" title="{{.Creator.Name}}'s Avatar" aria-hidden="true"/></a>
<span class="topic_inner_left">
<a class="rowtopic" href="{{.Link}}" itemprop="itemListElement" title="{{.Title}}"><span>{{.Title}}</span></a> {{if .ForumName}}<a class="rowsmall parent_forum" href="{{.ForumLink}}" title="{{.ForumName}}">{{.ForumName}}</a>{{end}}
<br /><a class="rowsmall starter" href="{{.Creator.Link}}" title="{{.Creator.Name}}">{{.Creator.Name}}</a>
<br><a class="rowsmall starter" href="{{.Creator.Link}}" title="{{.Creator.Name}}">{{.Creator.Name}}</a>
{{/** TODO: Avoid the double '|' when both .IsClosed and .Sticky are set to true. We could probably do this with CSS **/}}
{{if .IsClosed}}<span class="rowsmall topic_status_e topic_status_closed" title="{{lang "status.closed_tooltip"}}"> | &#x1F512;&#xFE0E</span>{{end}}
{{if .Sticky}}<span class="rowsmall topic_status_e topic_status_sticky" title="{{lang "status.pinned_tooltip"}}"> | &#x1F4CD;&#xFE0E</span>{{end}}
</span>
{{/** TODO: Phase this out of Cosora and remove it **/}}
<div class="topic_inner_right rowsmall">
<span class="replyCount">{{.PostCount}}</span><br />
<span class="replyCount">{{.PostCount}}</span><br>
<span class="likeCount">{{.LikeCount}}</span>
</div>
</div>

View File

@ -3,7 +3,7 @@
<a href="{{.Creator.Link}}"><img src="{{.Creator.MicroAvatar}}" height=64 alt="Avatar" title="{{.Creator.Name}}'s Avatar" aria-hidden="true"/></a>
<span class="topic_inner_left">
<span class="rowtopic" itemprop="itemListElement" title="{{.Title}}"><a href="{{.Link}}">{{.Title}}</a>{{if .ForumName}}<a class="parent_forum_sep">-</a><a href="{{.ForumLink}}" title="{{.ForumName}}" class="rowsmall parent_forum">{{.ForumName}}</a>{{end}}</span>
<br /><a class="rowsmall starter" href="{{.Creator.Link}}" title="{{.Creator.Name}}">{{.Creator.Name}}</a>
<br><a class="rowsmall starter" href="{{.Creator.Link}}" title="{{.Creator.Name}}">{{.Creator.Name}}</a>
</span>
</div>
<div class="topic_middle">