Skip to content

Commit 0e341d4

Browse files
authored
Code refactoring in main.js (#754)
* code fragment duplicate issue resolved * code style improvement
1 parent 5f28cd4 commit 0e341d4

File tree

1 file changed

+36
-63
lines changed

1 file changed

+36
-63
lines changed

src/main.js

Lines changed: 36 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ function BlackboxLogViewer() {
344344
}
345345

346346
function updateCanvasSize() {
347-
let
347+
const
348348
width = $(canvas).width(),
349349
height = $(canvas).height();
350350

@@ -360,18 +360,17 @@ function BlackboxLogViewer() {
360360
}
361361

362362
function renderSeekBarPicker(){
363-
let
363+
const
364364
seekBarContainer = $(".seekBar-selection"),
365-
seekBarPicker,
365+
seekBarPicker = $('<select id="seekbarTypeSelect", class="seekbarTypeSelect">'),
366366
seekBarItems = [
367367
["avgThrottle", "Average motor throttle"],
368368
["maxRC", "Maximum stick input"],
369369
["maxMotorDiff", "Maximum motor differential"],
370370
];
371371
seekBarContainer.empty();
372-
seekBarPicker = $('<select id="seekbarTypeSelect", class="seekbarTypeSelect">');
373372
seekBarPicker.change(function() {
374-
let
373+
const
375374
activity = flightLog.getActivitySummary(),
376375
displayItem = $(this).val();
377376
seekBarMode = displayItem;
@@ -391,15 +390,14 @@ function BlackboxLogViewer() {
391390
function renderLogFileInfo(file) {
392391
$(".log-filename").text(file.name);
393392

394-
let
393+
const
395394
logIndexContainer = $(".log-index"),
396-
logIndexPicker,
397395
logCount = flightLog.getLogCount();
398396

399397
logIndexContainer.empty();
400398

399+
const logIndexPicker = $('<select class="log-index form-control no-wheel">');
401400
if (logCount > 1) {
402-
logIndexPicker = $('<select class="log-index form-control no-wheel">');
403401

404402
logIndexPicker.change(function() {
405403
selectLog(parseInt($(this).val(), 10));
@@ -411,12 +409,8 @@ function BlackboxLogViewer() {
411409
}
412410

413411
for (let index = 0; index < logCount; index++) {
414-
let
415-
logLabel,
416-
option, holder,
417-
error;
418-
419-
error = flightLog.getLogError(index);
412+
let logLabel;
413+
const error = flightLog.getLogError(index);
420414

421415
if (error) {
422416
logLabel = error;
@@ -427,7 +421,7 @@ function BlackboxLogViewer() {
427421
}
428422

429423
if (logCount > 1) {
430-
option = $("<option></option>");
424+
const option = $("<option></option>");
431425

432426
option.text((index + 1) + "/" + (flightLog.getLogCount()) + ": " + logLabel);
433427
option.attr("value", index);
@@ -437,8 +431,7 @@ function BlackboxLogViewer() {
437431

438432
logIndexPicker.append(option);
439433
} else {
440-
holder = $('<div class="form-control-static no-wheel"></div>');
441-
434+
const holder = $('<div class="form-control-static no-wheel"></div>');
442435
holder.text(logLabel);
443436
logIndexContainer.append(holder);
444437
}
@@ -475,7 +468,7 @@ function BlackboxLogViewer() {
475468
**/
476469

477470
// Add log version information to status bar
478-
let sysConfig = flightLog.getSysConfig();
471+
const sysConfig = flightLog.getSysConfig();
479472
$('.version', statusBar).text( ((sysConfig['Craft name']!=null)?(sysConfig['Craft name'] + ' : '):'') +
480473
((sysConfig['Firmware revision']!=null)?(sysConfig['Firmware revision']):''));
481474
$('.looptime', statusBar).text( stringLoopTime(sysConfig.looptime, sysConfig.pid_process_denom, sysConfig.unsynced_fast_pwm, sysConfig.motor_pwm_rate));
@@ -484,7 +477,7 @@ function BlackboxLogViewer() {
484477
seekBar.setTimeRange(flightLog.getMinTime(), flightLog.getMaxTime(), currentBlackboxTime);
485478
seekBar.setActivityRange(flightLog.getSysConfig().motorOutput[0], flightLog.getSysConfig().motorOutput[1]);
486479

487-
let
480+
const
488481
activity = flightLog.getActivitySummary();
489482

490483
seekBar.setActivity(activity.times, activity[seekBarMode], activity.hasEvent);
@@ -501,7 +494,7 @@ function BlackboxLogViewer() {
501494

502495
function setGraphState(newState) {
503496
graphState = newState;
504-
let btnLogPlayPause = $(".log-play-pause");
497+
const btnLogPlayPause = $(".log-play-pause");
505498

506499
lastRenderTime = false;
507500

@@ -743,12 +736,12 @@ function BlackboxLogViewer() {
743736
}
744737

745738
function loadLogFile(file) {
746-
let reader = new FileReader();
739+
const reader = new FileReader();
747740

748741
reader.onload = function(e) {
749-
let bytes = e.target.result;
742+
const bytes = e.target.result;
750743

751-
let fileContents = String.fromCharCode.apply(null, new Uint8Array(bytes, 0,100));
744+
const fileContents = String.fromCharCode.apply(null, new Uint8Array(bytes, 0,100));
752745

753746
if (fileContents.match(/# dump|# diff/i)) { // this is actually a configuration file
754747
try{
@@ -884,7 +877,7 @@ function BlackboxLogViewer() {
884877
}
885878

886879
this.getBookmarks = function() { // get bookmark events
887-
let bookmarks = [];
880+
const bookmarks = [];
888881
try {
889882
if (bookmarkTimes!=null) {
890883
for (let i=0; i<=9; i++) {
@@ -918,7 +911,7 @@ function BlackboxLogViewer() {
918911
data = JSON.stringify(workspaceGraphConfigs, undefined, 4);
919912
}
920913

921-
let blob = new Blob([data], {type: 'text/json'}),
914+
const blob = new Blob([data], {type: 'text/json'}),
922915
e = document.createEvent('MouseEvents'),
923916
a = document.createElement('a');
924917

@@ -934,7 +927,7 @@ function BlackboxLogViewer() {
934927
// Check if upgrade is needed
935928
if (!oldFormat.graphConfig) { return oldFormat }
936929

937-
let newFormat = [];
930+
const newFormat = [];
938931

939932
oldFormat.graphConfig.forEach((element, id) => {
940933
if (element) {
@@ -957,10 +950,10 @@ function BlackboxLogViewer() {
957950
}
958951

959952
function loadWorkspaces(file) {
960-
let reader = new FileReader();
953+
const reader = new FileReader();
961954

962955
reader.onload = function(e) {
963-
let data = e.target.result;
956+
const data = e.target.result;
964957
let tmp = JSON.parse(data);
965958
if (tmp.graphConfig) {
966959
window.alert('Old Workspace format. Upgrading...');
@@ -981,7 +974,7 @@ function BlackboxLogViewer() {
981974
console.debug("Empty data, nothing to save");
982975
return;
983976
}
984-
let blob = new Blob([data], {type: fileType}),
977+
const blob = new Blob([data], {type: fileType}),
985978
e = document.createEvent('MouseEvents'),
986979
a = document.createElement('a');
987980
a.download = file || $(".log-filename").text() + "." + fileExtension;
@@ -1114,8 +1107,7 @@ function BlackboxLogViewer() {
11141107
});
11151108

11161109
$(".file-open").change(function(e) {
1117-
let
1118-
files = e.target.files;
1110+
const files = e.target.files;
11191111

11201112
loadFiles(files);
11211113

@@ -1163,7 +1155,7 @@ function BlackboxLogViewer() {
11631155
if (activeGraphConfig.selectedFieldName != null) {
11641156
hasAnalyser = !hasAnalyser;
11651157
} else {
1166-
let graphs = activeGraphConfig.getGraphs();
1158+
const graphs = activeGraphConfig.getGraphs();
11671159
if (graphs.length == 0 || graphs[0].fields.length == 0) {
11681160
hasAnalyser = false;
11691161
} else {
@@ -1225,7 +1217,7 @@ function BlackboxLogViewer() {
12251217
if (slow) { scrollTime = (1/60) * 1000000; } // Assume 60Hz video
12261218
setVideoTime(video.currentTime - scrollTime / 1000000);
12271219
} else {
1228-
let currentFrame = flightLog.getCurrentFrameAtTime((hasVideo)?video.currentTime:currentBlackboxTime);
1220+
const currentFrame = flightLog.getCurrentFrameAtTime((hasVideo)?video.currentTime:currentBlackboxTime);
12291221
if (currentFrame && currentFrame.previous && slow) {
12301222
setCurrentBlackboxTime(currentFrame.previous[FlightLogParser.prototype.FLIGHT_LOG_FIELD_INDEX_TIME]);
12311223
} else {
@@ -1245,7 +1237,7 @@ function BlackboxLogViewer() {
12451237
if (slow) { scrollTime = (1/60) * 1000000; } // Assume 60Hz video
12461238
setVideoTime(video.currentTime + scrollTime / 1000000);
12471239
} else {
1248-
let currentFrame = flightLog.getCurrentFrameAtTime((hasVideo)?video.currentTime:currentBlackboxTime);
1240+
const currentFrame = flightLog.getCurrentFrameAtTime((hasVideo)?video.currentTime:currentBlackboxTime);
12491241
if (currentFrame && currentFrame.next && slow) {
12501242
setCurrentBlackboxTime(currentFrame.next[FlightLogParser.prototype.FLIGHT_LOG_FIELD_INDEX_TIME]);
12511243
} else {
@@ -1386,7 +1378,7 @@ function BlackboxLogViewer() {
13861378
invalidateGraph();
13871379

13881380
}
1389-
let
1381+
const
13901382
graphConfigDialog = new GraphConfigurationDialog(
13911383
$("#dlgGraphConfiguration"),
13921384
function(newConfig, redrawChart) {
@@ -1402,7 +1394,7 @@ function BlackboxLogViewer() {
14021394
flightLog.setSysConfig(newSysConfig);
14031395

14041396
// Save Current Position then re-calculate all the log information
1405-
let activePosition = (hasVideo)?video.currentTime:currentBlackboxTime;
1397+
const activePosition = (hasVideo)?video.currentTime:currentBlackboxTime;
14061398

14071399
selectLog(null);
14081400
if (hasVideo) {
@@ -1543,7 +1535,7 @@ function BlackboxLogViewer() {
15431535
$(window).resize(function() { updateCanvasSize(); /*updateHeaderSize()*/ });
15441536

15451537
function updateHeaderSize() {
1546-
let newHeight = $(".video-top-controls").height() - 20; // 23px offset
1538+
const newHeight = $(".video-top-controls").height() - 20; // 23px offset
15471539
$(".log-graph").css("top", newHeight+"px");
15481540
$(".log-graph-config").css("top", newHeight+"px");
15491541
$(".log-seek-bar").css("top", newHeight+"px");
@@ -1732,7 +1724,7 @@ function BlackboxLogViewer() {
17321724
if (e.which != 2) return; // is it the middle mouse button, no, then ignore
17331725

17341726
if($(e.target).hasClass('graph-legend-group') || $(e.target).hasClass('graph-legend-field')) {
1735-
let refreshRequired = restorePenDefaults(activeGraphConfig.getGraphs(), $(e.target).attr('graph'), $(e.target).attr('field'));
1727+
const refreshRequired = restorePenDefaults(activeGraphConfig.getGraphs(), $(e.target).attr('graph'), $(e.target).attr('field'));
17361728

17371729
if (refreshRequired) {
17381730
graph.refreshGraphConfig();
@@ -1752,7 +1744,7 @@ function BlackboxLogViewer() {
17521744
}
17531745

17541746
if (graph && $(e.target).parents('.modal').length == 0){
1755-
let delta = Math.max(-1, Math.min(1, (e.originalEvent.wheelDelta)));
1747+
const delta = Math.max(-1, Math.min(1, (e.originalEvent.wheelDelta)));
17561748
if (delta!=0) {
17571749
if($(e.target).attr('id') == 'graphCanvas') { // we are scrolling the graph
17581750
if (delta < 0) { // scroll down (or left)
@@ -1801,7 +1793,7 @@ function BlackboxLogViewer() {
18011793
// Pressing any key hides dropdown menus
18021794
//$(".dropdown-toggle").dropdown("toggle");
18031795

1804-
let shifted = (e.altKey || e.shiftKey || e.ctrlKey || e.metaKey);
1796+
const shifted = (e.altKey || e.shiftKey || e.ctrlKey || e.metaKey);
18051797
if (e.which === 13 && e.target.type === 'text' && $(e.target).parents('.modal').length == 0) {
18061798
// pressing return on a text field clears the focus.
18071799
$(e.target).blur();
@@ -1899,7 +1891,7 @@ function BlackboxLogViewer() {
18991891
case "9".charCodeAt(0):
19001892
try {
19011893
if (!e.altKey) { // Workspaces feature
1902-
let id = e.which - 48;
1894+
const id = e.which - 48;
19031895
if (!e.shiftKey) { // retrieve graph configuration from workspace
19041896
if (workspaceGraphConfigs[id] != null) {
19051897
onSwitchWorkspace(workspaceGraphConfigs, id)
@@ -2058,7 +2050,7 @@ function BlackboxLogViewer() {
20582050
loadeddata: videoLoaded
20592051
});
20602052

2061-
let percentageFormat = {
2053+
const percentageFormat = {
20622054
to: function(value) {
20632055
return value.toFixed(0) + "%";
20642056
},
@@ -2133,7 +2125,7 @@ function BlackboxLogViewer() {
21332125
const item = e.dataTransfer.items[0];
21342126
const entry = item.webkitGetAsEntry();
21352127
if (entry.isFile) {
2136-
let file = e.dataTransfer.files[0];
2128+
const file = e.dataTransfer.files[0];
21372129
loadFiles([file]);
21382130
}
21392131
return false;
@@ -2170,25 +2162,6 @@ function BlackboxLogViewer() {
21702162
}
21712163
});
21722164

2173-
// New workspaces feature; local storage of user configurations
2174-
prefs.get('workspaceGraphConfigs', function(item) {
2175-
if (item) {
2176-
workspaceGraphConfigs = upgradeWorkspaceFormat(item);
2177-
} else {
2178-
workspaceGraphConfigs = defaultWorkspaceGraphConfigs;
2179-
}
2180-
});
2181-
2182-
prefs.get('activeWorkspace', function (id){
2183-
if (id) {
2184-
activeWorkspace = id
2185-
}
2186-
else {
2187-
activeWorkspace = 1
2188-
}
2189-
});
2190-
onSwitchWorkspace(workspaceGraphConfigs, activeWorkspace);
2191-
21922165
// Get the offsetCache buffer
21932166
prefs.get('offsetCache', function(item) {
21942167
if (item) {
@@ -2201,7 +2174,7 @@ function BlackboxLogViewer() {
22012174

22022175
// Close the dropdowns if not clicking a descendant of the dropdown
22032176
$(document).click(function (e) {
2204-
let p = $(e.target).closest(".dropdown");
2177+
const p = $(e.target).closest(".dropdown");
22052178
if (!p.length) {
22062179
$(".dropdown").removeClass("open");
22072180
}

0 commit comments

Comments
 (0)