Skip to content

Commit 208f5bf

Browse files
committed
refactor to resolve sonar issues
1 parent 6346097 commit 208f5bf

File tree

1 file changed

+86
-43
lines changed

1 file changed

+86
-43
lines changed

js/graph_map.js

Lines changed: 86 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
"use strict";
22

33
function MapGrapher() {
4-
var userSettings,
4+
let userSettings,
55
myMap,
6-
that = this,
76
currentLogStartDateTime,
87
currentTime,
98
craftPosition,
@@ -70,7 +69,9 @@ function MapGrapher() {
7069
};
7170

7271
this.initialize = function () {
73-
if (myMap) return;
72+
if (myMap) {
73+
return;
74+
}
7475

7576
myMap = L.map("mapContainer", mapOptions);
7677

@@ -110,27 +111,44 @@ function MapGrapher() {
110111
currentLogStartDateTime = newLogStartDateTime;
111112
}
112113

114+
const logIndex = flightLog.getLogIndex();
115+
116+
// if this log is already proccesed its skipped
117+
if (trailLayers.has(logIndex)) return;
118+
119+
this.setFlightLogIndexs();
120+
121+
let { latlngs, maxAlt, minAlt } = this.getPolylinesData();
122+
123+
const polyline = L.polyline(latlngs, polylineOptions);
124+
125+
const polylineC = this.createAltitudeColoredPolyline(latlngs, maxAlt, minAlt);
126+
127+
trailLayers.set(logIndex, { polyline, polylineC });
128+
129+
if (latlngs.length > 0) {
130+
homePosition = this.getHomeCoordinatesFromFlightLog(flightLog);
131+
}
132+
};
133+
134+
this.setFlightLogIndexs = function () {
113135
latIndexAtFrame = flightLog.getMainFieldIndexByName("GPS_coord[0]");
114136
lngIndexAtFrame = flightLog.getMainFieldIndexByName("GPS_coord[1]");
115137
altitudeIndexAtFrame = flightLog.getMainFieldIndexByName("GPS_altitude");
116138
groundCourseIndexAtFrame =
117139
flightLog.getMainFieldIndexByName("GPS_ground_course");
140+
};
118141

119-
const logIndex = flightLog.getLogIndex();
142+
this.getPolylinesData = function () {
143+
let latlngs = [];
144+
let maxAlt = Number.MIN_VALUE;
145+
let minAlt = Number.MAX_VALUE;
120146

121147
const chunks = flightLog.getChunksInTimeRange(
122148
flightLog.getMinTime(),
123149
flightLog.getMaxTime()
124150
);
125151

126-
let latlngs = [];
127-
128-
// if this log is already proccesed its skipped
129-
if (trailLayers.has(logIndex)) return;
130-
131-
let maxAlt = Number.MIN_VALUE;
132-
let minAlt = Number.MAX_VALUE;
133-
134152
for (const chunk of chunks) {
135153
for (const fi in chunk.frames) {
136154
const frame = chunk.frames[fi];
@@ -142,24 +160,28 @@ function MapGrapher() {
142160
);
143161

144162
// if there are no coordinates the frame is skipped
145-
if (!coordinates) continue;
163+
if (!coordinates) {
164+
continue;
165+
}
146166

167+
// Altitude max and min values can be obtained from the stats but fixing the index at 4 doesn't seem safe
168+
// const maxAlt = flightLog.getStats().frame.G.field[4].max / altitudeDivider;
169+
// const minAlt = flightLog.getStats().frame.G.field[4].min / altitudeDivider;
147170
maxAlt = coordinates.alt > maxAlt ? coordinates.alt : maxAlt;
148171
minAlt = coordinates.alt < minAlt ? coordinates.alt : minAlt;
149172

150173
// 1/4 of the dots is enough to draw the line
151-
if (fi % 4 == 0) latlngs.push(coordinates);
174+
if (fi % 4 == 0) {
175+
latlngs.push(coordinates);
176+
}
152177
}
153178
}
179+
return { latlngs, maxAlt, minAlt };
180+
};
154181

155-
const polyline = L.polyline(latlngs, polylineOptions);
156-
182+
this.createAltitudeColoredPolyline = function (latlngs, maxAlt, minAlt) {
157183
const divider = colorTrailGradient.length - 1;
158184

159-
// FIXME altitude max and min values can be obtained from the stats but fixing the index at 4 doesn't seem safe
160-
// const maxAlt2 = flightLog.getStats().frame.G.field[4].max / altitudeDivider; //197
161-
// const minAlt2 = flightLog.getStats().frame.G.field[4].min / altitudeDivider; //54
162-
163185
const delta = maxAlt - minAlt;
164186

165187
const thresholdIncrement = delta / divider;
@@ -172,7 +194,7 @@ function MapGrapher() {
172194
altThresholds.push(threshold);
173195
}
174196

175-
const polylineC = L.multiOptionsPolyline(latlngs, {
197+
return L.multiOptionsPolyline(latlngs, {
176198
multiOptions: {
177199
optionIdxFn: function (latLng) {
178200
for (const i in altThresholds) {
@@ -189,11 +211,6 @@ function MapGrapher() {
189211
opacity: 1,
190212
smoothFactor: 1,
191213
});
192-
193-
trailLayers.set(logIndex, { polyline, polylineC });
194-
195-
if (latlngs.length > 0)
196-
homePosition = this.getHomeCoordinatesFromFlightLog(flightLog);
197214
};
198215

199216
this.updateCurrentPosition = function () {
@@ -216,10 +233,17 @@ function MapGrapher() {
216233
userSettings = newUserSettings;
217234
};
218235

219-
this.redraw = function () {
220-
if (trailLayers.size <= 0) return;
221-
if (!myMap) return;
236+
this.redrawAll = function () {
237+
if (trailLayers.size <= 0 || !myMap) {
238+
return;
239+
}
240+
241+
this.redrawFlightTrail();
242+
this.redrawHomeMarker();
243+
this.redrawCraftMarker();
244+
};
222245

246+
this.redrawFlightTrail = function () {
223247
// If flightLog has changed redraw flight trail
224248
const currentLogIndex = flightLog.getLogIndex();
225249
if (previousLogIndex != currentLogIndex) {
@@ -234,14 +258,17 @@ function MapGrapher() {
234258

235259
previousLogIndex = currentLogIndex;
236260
}
261+
};
237262

238-
// home
263+
this.redrawHomeMarker = function () {
239264
if (homePosition) {
240265
if (homeMarker) {
241266
homeMarker.icon.setLatLng(homePosition).addTo(myMap);
242267

243268
// debug circle
244-
if (debugCircle) homeMarker.circle.setLatLng(homePosition).addTo(myMap);
269+
if (debugCircle) {
270+
homeMarker.circle.setLatLng(homePosition).addTo(myMap);
271+
}
245272
} else {
246273
homeMarker = {};
247274

@@ -250,21 +277,24 @@ function MapGrapher() {
250277
}).addTo(myMap);
251278

252279
// debug circle
253-
if (debugCircle)
280+
if (debugCircle) {
254281
homeMarker.circle = L.circle(homePosition, debugCircleOptions).addTo(
255282
myMap
256283
);
284+
}
257285
}
258286
}
287+
};
259288

260-
// aircraft
289+
this.redrawCraftMarker = function () {
261290
if (craftPosition) {
262291
if (craftMarker) {
263292
craftMarker.icon.setLatLng(craftPosition);
264293
craftMarker.icon.setRotationAngle(groundCourse).addTo(myMap);
265294
// debug circle
266-
if (debugCircle)
295+
if (debugCircle) {
267296
homeMarker.circle.setLatLng(craftPosition).addTo(myMap);
297+
}
268298
} else {
269299
craftMarker = {};
270300
craftMarker.icon = L.rotatedMarker(craftPosition, {
@@ -274,11 +304,12 @@ function MapGrapher() {
274304
}).addTo(myMap);
275305

276306
// debug circle
277-
if (debugCircle)
307+
if (debugCircle) {
278308
craftMarker.circle = L.circle(
279309
craftPosition,
280310
debugCircleOptions
281311
).addTo(myMap);
312+
}
282313
}
283314
}
284315
};
@@ -287,23 +318,35 @@ function MapGrapher() {
287318
if (trailLayers.has(index)) {
288319
const p = trailLayers.get(index).polyline;
289320
const pc = trailLayers.get(index).polylineC;
290-
if (p) myMap.removeLayer(p);
291-
if (pc) myMap.removeLayer(pc);
321+
if (p) {
322+
myMap.removeLayer(p);
323+
}
324+
if (pc) {
325+
myMap.removeLayer(pc);
326+
}
292327
}
293328
if (homeMarker) {
294-
if (myMap.hasLayer(homeMarker.icon)) myMap.removeLayer(homeMarker.icon);
295-
if (debugCircle && myMap.hasLayer(homeMarker.circle))
329+
if (myMap.hasLayer(homeMarker.icon)) {
330+
myMap.removeLayer(homeMarker.icon);
331+
}
332+
if (debugCircle && myMap.hasLayer(homeMarker.circle)) {
296333
myMap.removeLayer(homeMarker.circle);
334+
}
297335
}
298336
if (craftMarker) {
299-
if (myMap.hasLayer(craftMarker.icon)) myMap.removeLayer(craftMarker.icon);
300-
if (debugCircle && myMap.hasLayer(craftMarker.circle))
337+
if (myMap.hasLayer(craftMarker.icon)) {
338+
myMap.removeLayer(craftMarker.icon);
339+
}
340+
if (debugCircle && myMap.hasLayer(craftMarker.circle)) {
301341
myMap.removeLayer(craftMarker.circle);
342+
}
302343
}
303344
};
304345

305346
this.resize = function (width, height) {
306-
if (!userSettings) return;
347+
if (!userSettings) {
348+
return;
349+
}
307350
const containerstyle = {
308351
height: (height * parseInt(userSettings.map.size)) / 100.0,
309352
width: (width * parseInt(userSettings.map.size)) / 100.0,
@@ -345,6 +388,6 @@ function MapGrapher() {
345388
this.setCurrentTime = function (newTime) {
346389
currentTime = newTime;
347390
this.updateCurrentPosition();
348-
this.redraw();
391+
this.redrawAll();
349392
};
350393
}

0 commit comments

Comments
 (0)