Skip to content

Commit 028f565

Browse files
authored
Merge pull request #4 from amichair/cleanup
Cleanup
2 parents 7132854 + 7868260 commit 028f565

1 file changed

Lines changed: 83 additions & 105 deletions

File tree

‎index.html‎

Lines changed: 83 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
var data = {};
2626
var elementData = [];
2727
var nViews = views.length;
28-
var nElements = 1*(views[nViews-1]["elements"][views[nViews-1]["elements"].length-1]["index"])+1;
28+
var nElements = 1*(views[nViews - 1].elements[views[nViews - 1].elements.length - 1].index)+1;
2929

3030
var measuring = true;
3131
var countDown = 0;
@@ -205,65 +205,60 @@
205205
}
206206

207207
function composeVarStr(name, partial, partialComponent) {
208-
var namePart = encodeURIComponent(name);
209-
if (partial == "full")
210-
return namePart + "=full";
211-
if (partial == "partial" || partial == "partialXYZ") {
212-
if ((!forceFull) && data.hasOwnProperty(partialComponent) && data[partialComponent]["data"].length > 0 && data.hasOwnProperty(name) && data[name]["data"].length > 0) {
213-
if (partialComponent == name)
214-
return namePart + "=" + data[partialComponent]["data"][data[partialComponent]["data"].length-1];
215-
else {
216-
var minLength = Math.min(data[partialComponent]["data"].length, data[name]["data"].length);
217-
return namePart + "=" + encodeURIComponent(data[partialComponent]["data"][minLength-1] + "|" + partialComponent);
208+
var value; // empty (one sample) by default
209+
if (partial == "full") {
210+
value = "full";
211+
} else if (partial == "partial" || partial == "partialXYZ") {
212+
value = "full"; // fallback to full
213+
if (!forceFull && data.hasOwnProperty(partialComponent) && data.hasOwnProperty(name)) {
214+
var length = Math.min(data[partialComponent].data.length, data[name].data.length);
215+
if (length > 0) {
216+
value = data[partialComponent].data[length - 1];
217+
if (partialComponent !== name)
218+
value += encodeURIComponent("|" + partialComponent);
218219
}
219-
} else
220-
return namePart + "=full";
220+
}
221221
}
222-
return namePart;
222+
return encodeURIComponent(name) + (value ? '=' + value : '');
223223
}
224224

225225
function updateUsedVarStr() {
226-
var usedVars = [];
227-
var modePriority = [];
228-
views[currentView]["elements"].forEach(function(ve) {
226+
var vars = {};
227+
var priorities = {};
228+
views[currentView].elements.forEach(function(ve) {
229229
if (ve.hasOwnProperty("dataInput")) {
230-
for (var index = 0; index < ve["dataInput"].length; index++) {
231-
if (ve["dataInput"][index] == null)
230+
let dataInput = ve.dataInput;
231+
let updateMode = ve.updateMode;
232+
for (var i = 0; i < dataInput.length; i++) {
233+
if (dataInput[i] == null)
232234
continue;
233-
let varStr = ve["dataInput"][index];
234-
let varKey = "buffer:" + varStr;
235-
if (ve["updateMode"] == "single" || ve["updateMode"] == "input") {
236-
if (modePriority[varKey] >= 1)
237-
continue;
238-
usedVars[varKey] = composeVarStr(varStr, "single");
239-
modePriority[varKey] = 1;
240-
} else if (ve["updateMode"] == "partial") {
241-
if (modePriority[varKey] >= 2)
242-
continue;
243-
var dependentVar = ((ve["dataInput"].length > index + 1) && (index % 2 == 0)) ? ve["dataInput"][index+1] : ve["dataInput"][index];
244-
usedVars[varKey] = composeVarStr(varStr, "partial", dependentVar);
245-
modePriority[varKey] = 2;
246-
} else if (ve["updateMode"] == "partialXYZ") {
247-
if (modePriority[varKey] >= 3)
248-
continue;
249-
var dependentVar = ve["dataInput"][Math.floor(index / 3)];
250-
usedVars[varKey] = composeVarStr(varStr, "partialXYZ", dependentVar);
251-
modePriority[varKey] = 3;
252-
} else if (ve["updateMode"] == "full") {
253-
if (modePriority[varKey] >= 4)
254-
continue;
255-
usedVars[varKey] = composeVarStr(varStr, "full");
256-
modePriority[varKey] = 4;
235+
let name = dataInput[i];
236+
let key = "buffer:" + name;
237+
let priority = priorities[key] || 0;
238+
if (priority < 1 && (updateMode == "single" || updateMode == "input")) {
239+
vars[key] = composeVarStr(name, "single");
240+
priorities[key] = 1;
241+
} else if (priority < 2 && updateMode == "partial") {
242+
var dependentVar = ((dataInput.length > i + 1) && (i % 2 == 0)) ? dataInput[i + 1] : dataInput[i];
243+
vars[key] = composeVarStr(name, "partial", dependentVar);
244+
priorities[key] = 2;
245+
} else if (priority < 3 && updateMode == "partialXYZ") {
246+
var dependentVar = dataInput[Math.floor(i / 3)];
247+
vars[key] = composeVarStr(name, "partialXYZ", dependentVar);
248+
priorities[key] = 3;
249+
} else if (priority < 4 && updateMode == "full") {
250+
vars[key] = composeVarStr(name, "full");
251+
priorities[key] = 4;
257252
}
258253
}
259254
}
260255
});
261256

262257
usedVarStr = "";
263-
for (var usedVar in usedVars) {
258+
for (var v in vars) {
264259
if (usedVarStr != "")
265260
usedVarStr += "&";
266-
usedVarStr += usedVars[usedVar];
261+
usedVarStr += vars[v];
267262
}
268263
forceFull = false;
269264
}
@@ -280,25 +275,28 @@
280275
function updateData() {
281276
updateUsedVarStr();
282277
ajax("get?" + usedVarStr,
283-
function( jsonData ) {
284-
for (var index in jsonData["buffer"]) {
285-
if (jsonData["buffer"][index]["updateMode"] == "partial" && data.hasOwnProperty(index) && data[index]["data"].length > 0) {
286-
if (jsonData["buffer"][index]["buffer"].length == 0) {
287-
data[index]["changed"] = false;
278+
function(response) {
279+
for (var name in response.buffer) {
280+
let buffer = response.buffer[name];
281+
let existing = data[name];
282+
if (buffer.updateMode == "partial" && existing && existing.data.length > 0) {
283+
if (buffer.buffer.length == 0) {
284+
existing.changed = false;
288285
continue;
289286
}
290-
data[index]["data"] = data[index]["data"].concat(nullToNaN(jsonData["buffer"][index]["buffer"]));
291-
if (jsonData["buffer"][index]["size"] > 0)
292-
data[index]["data"].splice(0, data[index]["data"].length - jsonData["buffer"][index]["size"]);
293-
data[index]["changed"] = true;
287+
existing.data = existing.data.concat(nullToNaN(buffer.buffer));
288+
if (buffer.size > 0)
289+
existing.data.splice(0, existing.data.length - buffer.size);
290+
existing.changed = true;
294291
} else {
295-
data[index] = {};
296-
data[index]["data"] = nullToNaN(jsonData["buffer"][index]["buffer"]);
297-
data[index]["changed"] = true;
292+
data[name] = {
293+
data: nullToNaN(buffer.buffer),
294+
changed: true
295+
};
298296
}
299297
}
300298
updateViews();
301-
updateMeasuring(jsonData["status"]);
299+
updateMeasuring(response.status);
302300
errorNode.style.display = "none";
303301
},
304302
function() {
@@ -307,10 +305,7 @@
307305
forceFull = true;
308306
},
309307
function() {
310-
if (measuring)
311-
setTimeout(updateData, 30);
312-
else
313-
setTimeout(updateData, 250);
308+
setTimeout(updateData, measuring ? 30 : 250);
314309
},
315310
10000
316311
);
@@ -327,25 +322,19 @@
327322
if (fatalStop)
328323
return;
329324

330-
if (measuring != s["measuring"])
325+
if (measuring != s.measuring)
331326
forceFull = true;
332327
if (sessionID == "")
333-
sessionID = s["session"];
334-
else if (sessionID != s["session"]) {
328+
sessionID = s.session;
329+
else if (sessionID != s.session) {
335330
fatalStop = true;
336331
askSessionReload();
337332
}
338-
measuring = s["measuring"];
339-
timedRun = s["timedRun"];
340-
countDown = s["countDown"];
341-
if (measuring)
342-
header.classList.add("active");
343-
else
344-
header.classList.remove("active");
345-
if (countDown != 0)
346-
header.classList.add("countdown");
347-
else
348-
header.classList.remove("countdown");
333+
measuring = s.measuring;
334+
timedRun = s.timedRun;
335+
countDown = s.countDown;
336+
header.classList.toggle("active", measuring);
337+
header.classList.toggle("countdown", countDown != 0);
349338
if (countDown != 0) {
350339
document.getElementById("countdown").textContent = Math.ceil(countDown/1000) + "s";
351340
} else {
@@ -359,10 +348,10 @@
359348
}
360349

361350
function updateViews() {
362-
views[currentView]["elements"].forEach(function(ve) {
351+
views[currentView].elements.forEach(function(ve) {
363352
if (ve.hasOwnProperty("dataInputFunction"))
364-
ve["dataInputFunction"](data);
365-
ve["dataCompleteFunction"]();
353+
ve.dataInputFunction(data);
354+
ve.dataCompleteFunction();
366355
});
367356
}
368357

@@ -371,8 +360,8 @@
371360
for (i = 0; i < nElements; i++)
372361
elementData[i] = {};
373362

374-
views[currentView]["elements"].forEach(function(ve) {
375-
viewsNode.innerHTML += ("<div class=\"elementBlock\">" + ve["html"] + "</div>");
363+
views[currentView].elements.forEach(function(ve) {
364+
viewsNode.innerHTML += ("<div class=\"elementBlock\">" + ve.html + "</div>");
376365
});
377366
adjustColors();
378367
forceFull = true;
@@ -386,12 +375,8 @@
386375

387376
var liElements = viewSelector.getElementsByTagName("li");
388377
for (var i = 0; i < liElements.length; i++) {
389-
var el = liElements[i];
390-
if (i == currentView)
391-
el.classList.add("active");
392-
else
393-
el.classList.remove("active");
394-
};
378+
liElements[i].classList.toggle("active", i == currentView);
379+
}
395380
recreateViews();
396381
}
397382

@@ -441,9 +426,8 @@
441426

442427
var elements = document.getElementsByClassName("exclusive");
443428
for (var i = 0; i < elements.length; i++) {
444-
var elem = elements[i];
445-
elem.classList.remove("exclusive");
446-
};
429+
elements[i].classList.remove("exclusive");
430+
}
447431
}
448432

449433
function toggleExclusive(i) {
@@ -454,33 +438,27 @@
454438
setExclusive(i);
455439
}
456440

457-
function zoomLarger(e) {
441+
function zoom(e, factor) {
458442
leaveExclusive();
459-
if (scaleFactor < 3)
460-
scaleFactor *= 1.2;
443+
scaleFactor = factor;
461444
saveSetting("scaleFactor", scaleFactor);
462445
body.style.fontSize = (100*scaleFactor)+"%";
463446
e.stopPropagation();
464447
recreateViews();
465448
}
466449

450+
function zoomLarger(e) {
451+
if (scaleFactor < 3)
452+
zoom(e, scaleFactor * 1.2);
453+
}
454+
467455
function zoomSmaller(e) {
468-
leaveExclusive();
469456
if (scaleFactor > 0.5)
470-
scaleFactor /= 1.2;
471-
saveSetting("scaleFactor", scaleFactor);
472-
body.style.fontSize = (100*scaleFactor)+"%";
473-
e.stopPropagation();
474-
recreateViews();
457+
zoom(e, scaleFactor / 1.2);
475458
}
476459

477460
function zoomDefault(e) {
478-
leaveExclusive();
479-
scaleFactor = 1.0;
480-
saveSetting("scaleFactor", scaleFactor);
481-
body.style.fontSize = (100*scaleFactor)+"%";
482-
e.stopPropagation();
483-
recreateViews();
461+
zoom(e, 1.0);
484462
}
485463

486464
function toggleBrightMode(e) {

0 commit comments

Comments
 (0)