stage JS: performance on high numbers of lines

This commit is contained in:
sbcgua 2017-02-18 20:06:19 +02:00
parent bb78c444ee
commit 5f7a18328c

View File

@ -87,6 +87,37 @@ function confirmInitialized() {
debugOutput("js: OK"); // Final final confirmation :) debugOutput("js: OK"); // Final final confirmation :)
} }
/**********************************************************
* Performance utils (for debugging)
**********************************************************/
var gPerf = [];
function perfOut(prefix) {
var totals = {};
for (var i = gPerf.length - 1; i >= 0; i--) {
if (!totals[gPerf[i].name]) totals[gPerf[i].name] = {count: 0, time: 0};
totals[gPerf[i].name].time += gPerf[i].time;
totals[gPerf[i].name].count += 1;
}
var keys = Object.keys(totals);
for (var i = keys.length - 1; i >= 0; i--) {
console.log(prefix
+ " " + keys[i] + ": "
+ totals[keys[i]].time.toFixed(3) + "ms"
+ " (" + totals[keys[i]].count.toFixed() +")");
}
}
function perfLog(name, startTime) {
gPerf.push({name: name, time: window.performance.now() - startTime});
}
function perfClear() {
gPerf = [];
}
/********************************************************** /**********************************************************
* STAGE PAGE Logic * STAGE PAGE Logic
**********************************************************/ **********************************************************/
@ -113,10 +144,17 @@ function StageHelper(params) {
// Constants // Constants
this.HIGHLIGHT_STYLE = "highlight"; this.HIGHLIGHT_STYLE = "highlight";
this.STATUS = { this.STATUS = {
"add": "A", add: "A",
"remove": "R", remove: "R",
"ignore": "I", ignore: "I",
"reset": "?" reset: "?",
isValid: function (status) { return "ARI?".indexOf(status) == -1; }
};
this.TEMPLATES = {
cmdReset: "<a>reset</a>",
cmdLocal: "<a>add</a>",
cmdRemote: "<a>ignore</a><a>remove</a>"
}; };
this.setHooks(); this.setHooks();
@ -154,25 +192,15 @@ StageHelper.prototype.onPageUnload = function() {
// Re-store table state on entering the page // Re-store table state on entering the page
StageHelper.prototype.onPageLoad = function() { StageHelper.prototype.onPageLoad = function() {
var data = window.sessionStorage && JSON.parse(window.sessionStorage.getItem(this.pageSeed)); var data = window.sessionStorage && JSON.parse(window.sessionStorage.getItem(this.pageSeed));
if (data) { this.iterateStageTab(true, function (row) {
this.iterateStageTab(function (row) { var status = data && data[row.cells[this.col.name].innerText];
var status = data[row.cells[this.col.name].innerText]; this.updateRow(row, status || this.STATUS.reset);
this.updateRow(row, status || this.STATUS.reset); });
});
debugOutput("StageHelper.onPageLoad from Storage");
} else { // Render initial commands
this.iterateStageTab(function (row) {
this.updateRow(row, this.STATUS.reset);
});
debugOutput("StageHelper.onPageLoad initial state");
}
this.updateMenu(); this.updateMenu();
debugOutput("StageHelper.onPageLoad: " + ((data) ? "from Storage" : "initial state"));
} }
// Table event handler, change status // Table event handler, change status
@ -189,7 +217,6 @@ StageHelper.prototype.onTableClick = function (event) {
} else return; } else return;
} else return; } else return;
if (["TD","TH"].indexOf(td.tagName) == -1 || td.className != "cmd") return; if (["TD","TH"].indexOf(td.tagName) == -1 || td.className != "cmd") return;
var status = this.STATUS[target.innerText]; // Convert anchor text to status var status = this.STATUS[target.innerText]; // Convert anchor text to status
@ -198,9 +225,9 @@ StageHelper.prototype.onTableClick = function (event) {
if (td.tagName === "TD") { if (td.tagName === "TD") {
this.updateRow(targetRow, status); this.updateRow(targetRow, status);
} else { // TH } else { // TH
this.iterateStageTab(function (row) { this.iterateStageTab(true, function (row) {
if (row.style.display !== "none" // Not filtered out if (row.style.display !== "none" // Not filtered out
&& row.className === targetRow.className // Same context as header && row.className === targetRow.className // Same context as header
) { ) {
this.updateRow(row, status); this.updateRow(row, status);
} }
@ -217,7 +244,7 @@ StageHelper.prototype.onSearch = function (e) {
|| e.type === "keypress" && e.which === 13 ) { || e.type === "keypress" && e.which === 13 ) {
this.lastSearchValue = e.target.value; this.lastSearchValue = e.target.value;
this.iterateStageTab(this.applyFilterToRow, e.target.value); this.iterateStageTab(true, this.applyFilterToRow, e.target.value);
} }
} }
@ -228,14 +255,10 @@ StageHelper.prototype.applyFilterToRow = function (row, filter) {
if (filter) { if (filter) {
newTxt = origTxt.replace(filter, "<mark>"+filter+"</mark>"); newTxt = origTxt.replace(filter, "<mark>"+filter+"</mark>");
if (newTxt !== origTxt) { // fits filter row.style.display = (newTxt !== origTxt) ? "" : "none";
row.style.display = "table-row";
} else {
row.style.display = "none";
}
} else { // No filter -> just reset the value } else { // No filter -> just reset the value
newTxt = origTxt; newTxt = origTxt;
row.style.display = "table-row"; row.style.display = ""; // default, visible
} }
if (td.firstChild.tagName === "A") { if (td.firstChild.tagName === "A") {
@ -249,7 +272,7 @@ StageHelper.prototype.applyFilterToRow = function (row, filter) {
StageHelper.prototype.getStatusImpact = function (status) { StageHelper.prototype.getStatusImpact = function (status) {
if (typeof status !== "string" if (typeof status !== "string"
|| status.length !== 1 || status.length !== 1
|| "ARI?".indexOf(status) == -1) { || this.STATUS.isValid(status) ) {
alert("Unknown status"); alert("Unknown status");
} else { } else {
return (status !== this.STATUS.reset) ? 1 : 0; return (status !== this.STATUS.reset) ? 1 : 0;
@ -261,37 +284,41 @@ StageHelper.prototype.updateRow = function (row, newStatus) {
var oldStatus = row.cells[this.col.status].innerText; var oldStatus = row.cells[this.col.status].innerText;
if (oldStatus !== newStatus) { if (oldStatus !== newStatus) {
row.cells[this.col.status].innerText = newStatus; this.updateRowStatus(row, newStatus);
if (newStatus === this.STATUS.reset) {
row.cells[this.col.status].classList.remove(this.HIGHLIGHT_STYLE);
} else {
row.cells[this.col.status].classList.add(this.HIGHLIGHT_STYLE);
}
this.updateRowCommand(row, newStatus); this.updateRowCommand(row, newStatus);
} else if (!row.cells[this.col.cmd].innerText) { } else if (!row.cells[this.col.cmd].children.length) {
this.updateRowCommand(row, newStatus); // For initial run this.updateRowCommand(row, newStatus); // For initial run
} }
this.choiseCount += this.getStatusImpact(newStatus) - this.getStatusImpact(oldStatus); this.choiseCount += this.getStatusImpact(newStatus) - this.getStatusImpact(oldStatus);
} }
// Update command cell (render set of commands) // Update Status cell (render set of commands)
StageHelper.prototype.updateRowCommand = function (row, status) { StageHelper.prototype.updateRowStatus = function (row, status) {
var CMD_RESET = "<a>reset</a>"; row.cells[this.col.status].innerText = status;
var CMD_LOCAL = "<a>add</a>";
var CMD_REMOTE = "<a>ignore</a><a>remove</a>";
if (status === this.STATUS.reset) { if (status === this.STATUS.reset) {
row.cells[this.col.cmd].innerHTML = (row.className == "local") ? CMD_LOCAL : CMD_REMOTE; row.cells[this.col.status].classList.remove(this.HIGHLIGHT_STYLE);
} else { } else {
row.cells[this.col.cmd].innerHTML = CMD_RESET; row.cells[this.col.status].classList.add(this.HIGHLIGHT_STYLE);
}
}
// Update Command cell (render set of commands)
StageHelper.prototype.updateRowCommand = function (row, status) {
var cell = row.cells[this.col.cmd];
if (status === this.STATUS.reset) {
cell.innerHTML = (row.className == "local")
? this.TEMPLATES.cmdLocal
: this.TEMPLATES.cmdRemote;
} else {
cell.innerHTML = this.TEMPLATES.cmdReset;
} }
} }
// Update menu items visibility // Update menu items visibility
StageHelper.prototype.updateMenu = function () { StageHelper.prototype.updateMenu = function () {
this.dom.commitBtn.style.display = (this.choiseCount > 0) ? "inline" : "none"; this.dom.commitBtn.style.display = (this.choiseCount > 0) ? "" : "none";
this.dom.commitAllBtn.style.display = (this.choiseCount > 0) ? "none" : "inline"; this.dom.commitAllBtn.style.display = (this.choiseCount > 0) ? "none" : "";
this.dom.fileCounter.innerHTML = this.choiseCount.toString(); this.dom.fileCounter.innerHTML = this.choiseCount.toString();
} }
@ -303,19 +330,36 @@ StageHelper.prototype.submit = function () {
// Extract data from the table // Extract data from the table
StageHelper.prototype.collectData = function () { StageHelper.prototype.collectData = function () {
var data = {}; var data = {};
this.iterateStageTab(function (row) { this.iterateStageTab(false, function (row) {
data[row.cells[this.col.name].innerText] = row.cells[this.col.status].innerText; data[row.cells[this.col.name].innerText] = row.cells[this.col.status].innerText;
}); });
return data; return data;
} }
// table iteration helper // Table iteration helper
StageHelper.prototype.iterateStageTab = function (cb /*, ...*/) { StageHelper.prototype.iterateStageTab = function (changeMode, cb /*, ...*/) {
for (var b = 0, bN = this.dom.stageTab.tBodies.length; b < bN; b++) {
var tbody = this.dom.stageTab.tBodies[b]; var restArgs = Array.prototype.slice.call(arguments, 2);
var table = this.dom.stageTab;
if (changeMode) {
var scrollOffset = window.pageYOffset;
this.dom.stageTab.style.display = "none";
// var stageTabParent = this.dom.stageTab.parentNode;
// table = stageTabParent.removeChild(this.dom.stageTab);
}
for (var b = 0, bN = table.tBodies.length; b < bN; b++) {
var tbody = table.tBodies[b];
for (var r = 0, rN = tbody.rows.length; r < rN; r++) { for (var r = 0, rN = tbody.rows.length; r < rN; r++) {
args = [tbody.rows[r]].concat(Array.prototype.slice.call(arguments, 1)); args = [tbody.rows[r]].concat(restArgs);
cb.apply(this, args); // callback cb.apply(this, args); // callback
} }
} }
if (changeMode) {
this.dom.stageTab.style.display = "";
// stageTabParent.appendChild(table);
window.scrollTo(0, scrollOffset);
}
} }