-
Notifications
You must be signed in to change notification settings - Fork 1
/
ksaBodyOps.js
740 lines (639 loc) · 34.2 KB
/
ksaBodyOps.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
// refactor complete
// load a new GeoGebra figure into the main content window
function loadBody(body, flt) {
if (!body.includes("-")) body = body + "-System";
// an attempt was made to load orbital data for an inactive vessel. Can. Not. Compute.
if (body == "inactive") return;
// can't continue if body data hasn't loaded
if (!ops.bodyCatalog.length) return setTimeout(loadBody, 50, body);
// if there is already a body loading then try calling back later
if (ops.bodyCatalog.find(o => o.selected === true) && !isGGBAppletLoaded) return setTimeout(loadBody, 50, body);
// hide the map just in case it's open
hideMap();
// only do any of this if the current page is set to body
// if not, a vessel page is changing the figure because the current vessel body was not loaded
if (ops.pageType == "body") {
// default to kerbol system
if (!body || !body.length) body = "Kerbol-System";
$("#contentHeader").html(body.replace("-", " "));
document.title = "KSA Operations Tracker" + " - " + body.replace("-", " ");
// if this is the first page to load, replace the current history
// don't create a new entry if this is the same page being reloaded
if (!history.state) {
if (window.location.href.includes("&")) var strURL = window.location.href;
else var strURL = "http://www.kerbalspace.agency/TrackerDev1/tracker.asp?body=" + body;
history.replaceState({type: "body", id: body}, document.title, strURL);
} else if (history.state.id != body) {
var strURL = "http://www.kerbalspace.agency/TrackerDev1/tracker.asp?body=" + body;
if (flt) strURL += "&flt=" + flt;
history.pushState({type: "body", id: body}, document.title, strURL);
}
// for tag loading
// $("#contentHeader").spin({ scale: 0.35, position: 'relative', top: '10px', left: (((955/2) + (body.width('bold 32px arial')/2)) + 10) +'px' });
// if body was already loaded & we are switching to it then just exit at this point
if (isGGBAppletLoaded && ops.bodyCatalog.find(o => o.selected === true) && ops.bodyCatalog.find(o => o.selected === true).Body == body.split("-")[0]) {
// if it was loaded behind a vessel page, show all the details for a bit
if (isDirty) {
ggbApplet.reset();
isDirty = false;
}
return;
}
// if this is a vessel page calling the load then set a flag to let us know the figure will need to be reset next time it is shown
} else if (ops.pageType == "vessel") isDirty = true;
// update the current body & system
if (ops.bodyCatalog.find(o => o.selected === true)) ops.bodyCatalog.find(o => o.selected === true).selected = false;
ops.bodyCatalog.find(o => o.Body === body.split("-")[0]).selected = true;
// hide and reset stuff
$("#figureOptions").fadeOut();
isGGBAppletLoaded = false;
// remove and add the GGB figure container
$("#figure").remove();
$("#contentBox").append("<div id='figure'></div>");
// hide it if this isn't a body page
if (ops.pageType != "body") $("#figure").hide();
// setup GeoGebra
// use a random number to always load a new file, never from cache
var parameters = {"prerelease":false,
"width":w2utils.getSize("#contentBox", 'width'),
"height":885,
"showToolBar":false,
"borderColor":null,
"showMenuBar":false,
"showAlgebraInput":false,
"showResetIcon":true,
"enableLabelDrags":false,
"enableShiftDragZoom":true,
"enableRightClick":false,
"capturingThreshold":null,
"showToolBarHelp":false,
"errorDialogsActive":true,
"useBrowserForJS":true,
"filename":"ggb/" + body + ".ggb?" + Math.floor(Math.random() * 1000)};
var views = {"is3D":1};
var applet = new GGBApplet('5.0', parameters, views);
applet.inject('figure');
// restart spinner so it's on top of figure
$("#contentBox").spin({ position: 'relative', top: '50%', left: '50%' });
}
// load the data for all the bodies in the Kerbol system
function loadBodyAJAX(xhttp) {
// separate each of the bodies and their fields
var bodies = xhttp.responseText.split("|");
// push each body into the array and add the selection flag
bodies.forEach(function(item) {
if (item) {
ops.bodyCatalog.push(rsToObj(item));
ops.bodyCatalog[ops.bodyCatalog.length-1].selected = false;
}
});
}
// called by GGB figure after it finishes loading or after user clicks the reset
function ggbOnInit() {
isGGBAppletRefreshing = true;
$("#figureDialog").dialog("close");
// hide and disable vessel filters
$("#vesselOrbitTypes").fadeOut();
$("#asteroid-filter").prop("disabled", true);
$("#debris-filter").prop("disabled", true);
$("#probe-filter").prop("disabled", true);
$("#ship-filter").prop("disabled", true);
$("#station-filter").prop("disabled", true);
// was nulled after initial orbit data load. Reset to array on load of new body
ops.vesselsToLoad = [];
// can't continue if menu data hasn't loaded. Try again in 50ms
if (!isMenuDataLoaded) return setTimeout(ggbOnInit, 50);
// reset all the checkboxes
$("#nodes").prop('checked', true);
$("#labels").prop('checked', true);
$("#orbits").prop('checked', true);
$("#ref").prop('checked', true);
$("#soi").prop('checked', true);
// disable the spinner & show checkboxes if this is the first load and not a vessel page call
if (!isGGBAppletLoaded && ops.pageType == "body") {
$("#contentBox").spin(false);
$("#figureOptions").fadeIn();
}
// prepare to reload any orbiting objects
ops.ggbOrbits.length = 0;
// loop through and catalog all the pre-made objects
var bodyIDs = [];
for (obj=0; obj<ggbApplet.getObjectNumber(); obj++) {
// is this a unique identifier? Look for a letter followed by a number
// ignore "A" since in the GGB spreadsheet that row is used for column labels
if (ggbApplet.getObjectName(obj).charAt(0) != "A" && (bodyIDs.indexOf(ggbApplet.getObjectName(obj).charAt(0)) == -1 && $.isNumeric(ggbApplet.getObjectName(obj).charAt(1)))) {
// add this identifier to the orbits list only if we haven't used it yet
ops.ggbOrbits.push({type: "body", id: ggbApplet.getObjectName(obj).charAt(0), showName: false, showNodes: false, isSelected: false, isHidden: false});
bodyIDs.push(ggbApplet.getObjectName(obj).charAt(0));
}
}
bodyIDs.length = 0;
// bring figure body locations up to date
ggbApplet.setValue("UT", currUT());
// listen for any objects clicked on
ggbApplet.registerClickListener("figureClick");
// select and show it in the menu if this is the proper page type because
// the figure can load after a vessel was already selected
var currBody = ops.bodyCatalog.find(o => o.selected === true).Body;
if (ops.pageType == "body" && !window.location.href.includes("flt")) selectMenuItem(currBody + "-System");
// declutter the view after a few seconds
// make sure a quick figure switch doesn't declutter things too fast
clearTimeout(timeoutHandle);
timeoutHandle = setTimeout(declutterGGB, 2500);
// load additional data
isGGBAppletLoaded = true;
loadMap(currBody);
if (!loadVesselOrbits()) {
isGGBAppletRefreshing = false;
activateEventLinks();
}
}
// adds to the figure the orbits of any vessels around this body
function loadVesselOrbits() {
// we need to stop all AJAX calls if the body is being switched before we finish
if (!isGGBAppletLoaded) {
ops.vesselsToLoad.length = 0;
return;
}
// initialize if this is the first call
if (!ops.vesselsToLoad.length) {
// if the selected body has moons or is Kerbol then we need to append "-System" to get its menu nodes
var strBodyName = ops.bodyCatalog.find(o => o.selected === true).Body;
if (ops.bodyCatalog.find(o => o.selected === true).Moons || strBodyName == "Kerbol") strBodyName += "-System";
// check if the body has any vessels in orbit around it
var strVesselsToload = extractIDs(w2ui['menu'].get(strBodyName).nodes);
if (strVesselsToload.length) {
// stash the vessels in an array, show the loading spinner and make sure the GGB figure doesn't declutter yet
ops.vesselsToLoad = strVesselsToload.substr(0, strVesselsToload.length-1).split(";");
$("#vesselLoaderMsg").spin({ scale: 0.35, position: 'relative', top: '8px', left: '0px' });
$("#vesselLoaderMsg").fadeIn();
clearTimeout(timeoutHandle);
} else {
// no vessels to load
return false;
}
}
// load the vessel orbital data & discard the name to decrease the array size
loadDB("loadVesselOrbitData.asp?db=" + ops.vesselsToLoad.shift() + "&ut=" + currUT(), addGGBOrbitAJAX);
return true;
}
// creates an orbit on the GeoGebra figure if it is loaded
function addGGBOrbitAJAX(xhttp) {
if (!isGGBAppletLoaded) return;
if (!isGGBAppletRefreshing) ggbApplet.unregisterClickListener("figureClick");
// parse data
var vesselID = xhttp.responseText.split("*")[0];
var orbitData = rsToObj(xhttp.responseText.split("*")[1].split("|")[0])
// check to ensure the vessel has an orbital record
if (orbitData) {
// convert the vessel id to a variable name suitable for GeoGebra
ggbID = vesselID.replace("-", "").replace(" ", "");
// if this vessel is already drawn, does it need to be deleted?
var vesselObj = ops.ggbOrbits.find(o => o.id === ggbID);
if (vesselObj && !orbitData.Eph) {
// just remove the visual aspects
ggbApplet.deleteObject(ggbID + 'conic');
ggbApplet.deleteObject(ggbID + 'penode');
ggbApplet.deleteObject(ggbID + 'apnode');
ggbApplet.deleteObject(ggbID + 'anode');
ggbApplet.deleteObject(ggbID + 'dnode');
ggbApplet.deleteObject(ggbID + 'position');
// save the vessel type & remove from the array
var strVesselType = vesselObj.type;
for (idIndex=0; idIndex<ops.ggbOrbits.length; idIndex++){
if (ops.ggbOrbits[idIndex].id == ggbID) {
ops.ggbOrbits.splice(idIndex, 1);
break;
}
}
// if this vessel type is no longer in use, disable the filter selection
if (!ops.ggbOrbits.find(o => o.type === strVesselType)) {
$("#" + strVesselType + "-filter").prop("disabled", true);
$("#" + strVesselType + "-label").css('color', "#C0C0C0");
$("#" + strVesselType + "-filter").prop('checked', false);
}
// otherwise just add or edit the current orbit if orbit data exists
} else if (orbitData.Eph) {
// look up data in the body catalog of the body currently being orbited
var bodyData = ops.bodyCatalog.find(o => o.selected === true);
// type of vessel so we can color things appropriately
var strVesselType = w2ui['menu'].get('activeVessels', vesselID).img.split("-")[1];
// add this vessel type and id to the orbits array for filtering if it's not already there
if (!vesselObj) ops.ggbOrbits.push({type: strVesselType,
id: ggbID,
showName: false,
showNodes: false,
isSelected: false,
isHidden: false});
// enable this vessel type in the filters menu
$("#" + strVesselType + "-filter").removeAttr("disabled");
$("#" + strVesselType + "-filter").prop('checked', true);
$("#" + strVesselType + "-label").css('color', orbitColors[strVesselType]);
// load the vessel into the figure
ggbApplet.evalCommand(ggbID + 'id="' + vesselID + '"');
ggbApplet.evalCommand(ggbID + 'sma=' + orbitData.SMA);
ggbApplet.evalCommand(ggbID + 'pe=' + (orbitData.Periapsis + bodyData.Radius));
ggbApplet.evalCommand(ggbID + 'ap=' + (orbitData.Apoapsis + bodyData.Radius));
ggbApplet.evalCommand(ggbID + 'ecc=' + orbitData.Eccentricity);
ggbApplet.evalCommand(ggbID + 'inc=' + (Math.radians(orbitData.Inclination)));
ggbApplet.evalCommand(ggbID + 'raan=' + (Math.radians(orbitData.RAAN)));
ggbApplet.evalCommand(ggbID + 'arg=' + (Math.radians(orbitData.Arg)));
ggbApplet.evalCommand(ggbID + 'period=' + orbitData.OrbitalPeriod);
ggbApplet.evalCommand(ggbID + 'mean=' + toMeanAnomaly(Math.radians(orbitData.TrueAnom), orbitData.Eccentricity));
ggbApplet.evalCommand(ggbID + 'smna=' + ggbID + 'sma sqrt(1 - ' + ggbID + 'ecc^2)');
ggbApplet.evalCommand(ggbID + 'foci=' + ggbID + 'ap - ' + ggbID + 'pe');
ggbApplet.evalCommand(ggbID + 'meanmotion=2pi / ' + ggbID + 'period');
ggbApplet.evalCommand(ggbID + 'obtaxis=Line(origin, Vector((1; ' + ggbID + 'raan - pi / 2; pi / 2 - ' + ggbID + 'inc)))');
ggbApplet.setVisible(ggbID + 'obtaxis', false);
ggbApplet.evalCommand(ggbID + 'secondfocus=Rotate(Rotate((' + ggbID + 'foci; 0; 0), ' + ggbID + 'raan, zAxis), ' + ggbID + 'arg + pi, ' + ggbID + 'obtaxis)');
ggbApplet.setVisible(ggbID + 'secondfocus', false);
ggbApplet.evalCommand(ggbID + 'refpoint=Rotate(Rotate((' + ggbID + 'sma; 0; 0), ' + ggbID + 'raan, zAxis), ' + ggbID + 'arg - acos(-' + ggbID + 'ecc), ' + ggbID + 'obtaxis)');
ggbApplet.setVisible(ggbID + 'refpoint', false);
ggbApplet.evalCommand(ggbID + 'conic=Ellipse(origin, ' + ggbID + 'secondfocus, ' + ggbID + 'refpoint)');
ggbApplet.setColor(ggbID + 'conic', hexToRgb(orbitColors[strVesselType]).r, hexToRgb(orbitColors[strVesselType]).g, hexToRgb(orbitColors[strVesselType]).b);
ggbApplet.evalCommand(ggbID + 'penode=Point(' + ggbID + 'conic, 0)');
ggbApplet.setCaption(ggbID + 'penode', "Pe");
ggbApplet.setLabelStyle(ggbID + 'penode', 3);
ggbApplet.setLabelVisible(ggbID + 'penode', true);
ggbApplet.setColor(ggbID + 'penode', 0, 153, 255);
ggbApplet.setPointSize(ggbID + 'penode', 3);
ggbApplet.setFixed(ggbID + 'penode', true, false);
ggbApplet.evalCommand(ggbID + 'apnode=Point(' + ggbID + 'conic, 0.5)');
ggbApplet.setCaption(ggbID + 'apnode', "Ap");
ggbApplet.setLabelStyle(ggbID + 'apnode', 3);
ggbApplet.setLabelVisible(ggbID + 'apnode', true);
ggbApplet.setColor(ggbID + 'apnode', 0, 153, 255);
ggbApplet.setPointSize(ggbID + 'apnode', 3);
ggbApplet.setFixed(ggbID + 'apnode', true, false);
ggbApplet.evalCommand(ggbID + 'anode=If(' + ggbID + 'inc != 0, Element({Intersect(xOyPlane, ' + ggbID + 'conic)}, 1), Point(' + ggbID + 'conic, ' + ggbID + 'raan / (2pi)))');
ggbApplet.setCaption(ggbID + 'anode', "AN");
ggbApplet.setLabelStyle(ggbID + 'anode', 3);
ggbApplet.setLabelVisible(ggbID + 'anode', true);
ggbApplet.setColor(ggbID + 'anode', 51, 255, 0);
ggbApplet.setPointSize(ggbID + 'anode', 3);
ggbApplet.setFixed(ggbID + 'anode', true, false);
ggbApplet.evalCommand(ggbID + 'anodeta=If(' + ggbID + 'arg != 0, Angle(' + ggbID + 'penode, origin, ' + ggbID + 'anode), 0)');
ggbApplet.evalCommand(ggbID + 'anodeea=If(' + ggbID + 'anodeta > pi, 2pi - acos((' + ggbID + 'ecc + cos(' + ggbID + 'anodeta)) / (1 + ' + ggbID + 'ecc cos(' + ggbID + 'anodeta))), acos((' + ggbID + 'ecc + cos(' + ggbID + 'anodeta)) / (1 + ' + ggbID + 'ecc cos(' + ggbID + 'anodeta))))');
ggbApplet.evalCommand(ggbID + 'anodema=' + ggbID + 'anodeea - ' + ggbID + 'ecc sin(' + ggbID + 'anodeea)');
ggbApplet.evalCommand(ggbID + 'dnode=If(' + ggbID + 'inc != 0, Element({Intersect(xOyPlane, ' + ggbID + 'conic)}, 2), Point(' + ggbID + 'conic, (' + ggbID + 'raan + pi) / (2pi)))');
ggbApplet.setCaption(ggbID + 'dnode', "DN");
ggbApplet.setLabelStyle(ggbID + 'dnode', 3);
ggbApplet.setLabelVisible(ggbID + 'dnode', true);
ggbApplet.setColor(ggbID + 'dnode', 51, 255, 0);
ggbApplet.setPointSize(ggbID + 'dnode', 3);
ggbApplet.setFixed(ggbID + 'dnode', true, false);
ggbApplet.evalCommand(ggbID + 'maut=Mod(' + ggbID + 'mean + ' + ggbID + 'meanmotion (UT-' + orbitData.Eph + '), 2pi)');
ggbApplet.evalCommand(ggbID + 'eaut=Iteration(M - (M - ' + ggbID + 'ecc sin(M) - ' + ggbID + 'maut) / (1 - ' + ggbID + 'ecc cos(M)), M, {' + ggbID + 'maut}, 20)');
ggbApplet.evalCommand(ggbID + 'position=Point(' + ggbID + 'conic, ' + ggbID + 'eaut / (2pi))');
ggbApplet.setCaption(ggbID + 'position', w2ui['menu'].get('activeVessels', vesselID).text.split(">")[1].split("<")[0]);
ggbApplet.setLabelStyle(ggbID + 'position', 3);
ggbApplet.setPointSize(ggbID + 'position', 2);
ggbApplet.setLabelVisible(ggbID + 'position', true);
ggbApplet.setColor(ggbID + 'position', hexToRgb(orbitColors[strVesselType]).r, hexToRgb(orbitColors[strVesselType]).g, hexToRgb(orbitColors[strVesselType]).b);
}
}
// if we update the figure, for some reason the click callback needs to be re-enabled
if (!isGGBAppletRefreshing) ggbApplet.registerClickListener("figureClick");
// callback if there is still data to load
if (ops.vesselsToLoad && ops.vesselsToLoad.length) setTimeout(loadVesselOrbits, 1);
else if (ops.vesselsToLoad && !ops.vesselsToLoad.length) {
// nullify so any orbit updates after initial loading or GGB refresh don't repeat this code
ops.vesselsToLoad = null;
// finish cleaning up after body load
isGGBAppletRefreshing = false;
activateEventLinks();
$("#vesselLoaderMsg").spin(false);
$("#vesselLoaderMsg").fadeOut();
if ($("#figure").is(":visible") && ops.pageType == "body") $("#vesselOrbitTypes").fadeIn();
timeoutHandle = setTimeout(declutterGGB, 2500);
}
}
// just show only orbits after displaying everything possible to not leave people overwhelmed intiially
function declutterGGB() {
// hide figure elements
ggbApplet.setVisible("RefLine", false);
ops.ggbOrbits.forEach(function(item) {
if (item.type == "body") {
ggbApplet.setVisible(item.id + "26", false);
ggbApplet.setVisible(item.id + "27", false);
ggbApplet.setVisible(item.id + "28", false);
ggbApplet.setVisible(item.id + "32", false);
ggbApplet.setVisible(item.id + "39", false);
ggbApplet.setLabelVisible(item.id + "36", false);
} else {
// someone might have had a chance to select a vessel before now, so only affect unselected ones
if (!item.isSelected) {
ggbApplet.setVisible(item.id + "apnode", false);
ggbApplet.setVisible(item.id + "penode", false);
ggbApplet.setVisible(item.id + "anode", false);
ggbApplet.setVisible(item.id + "dnode", false);
ggbApplet.setLabelVisible(item.id + "position", false);
}
}
});
// uncheck the affected boxes
$("#nodes").prop('checked', false);
$("#labels").prop('checked', false);
$("#ref").prop('checked', false);
$("#soi").prop('checked', false);
// nullify to let anyone else know this has already happened
timeoutHandle = null;
}
// handle any objects that are clicked in the GeoGebra figure
function figureClick(object) {
if (object == "RefLine") {
ggbApplet.evalCommand("SetViewDirection((0,0,1), true)");
return;
}
// clicked on a vessel/asteroid orbit or position
if (object.includes("conic") || object.includes("position")) {
$("#figureDialog").dialog("close");
var clickedObj = ops.ggbOrbits.find(o => o.id === object.replace("position" , "").replace("conic", ""));
// if we clicked on the position after the orbit was selected, then jump to the vessel view & remain selected
if (clickedObj.isSelected && object.includes("position")) {
// use getValueString since we need the actual DB name with any hyphens
swapContent("vessel", ggbApplet.getValueString(object.replace("position", "id")));
return;
}
// unselect any current body & select the orbit if there was nothing already selected or we didn't just unselect ourselves
var selectedObj = unselectBody(object);
if (!selectedObj || selectedObj.id != clickedObj.id) {
ggbApplet.setLabelVisible(clickedObj.id + "position", true);
ggbApplet.setVisible(clickedObj.id + "conic", true);
clickedObj.showName = true;
clickedObj.showNodes = true;
clickedObj.isSelected = true;
// only actually show nodes if orbits are shown
if ($("#orbits").is(":checked") || ($("#nodes").is(":checked") && !$("#orbits").is(":checked"))) {
ggbApplet.setVisible(clickedObj.id + "penode", true);
ggbApplet.setVisible(clickedObj.id + "apnode", true);
ggbApplet.setVisible(clickedObj.id + "anode", true);
ggbApplet.setVisible(clickedObj.id + "dnode", true);
}
}
return;
}
// clicked on a planet?
if (object.includes("36") || object.includes("37")) {
var selectedObj = unselectBody(object);
var clickedObj = ops.ggbOrbits.find(o => o.id === object.charAt(0));
clickedObj.isSelected = true;
// compose the planet data if there's no previous selection or we didn't click on ourselves
if (!selectedObj || (selectedObj && clickedObj.id != selectedObj.id)) {
var strBodyName = ggbApplet.getCaption(object.charAt(0) + "36");
var bodyData = ops.bodyCatalog.find(o => o.Body === strBodyName);
var strHTML = "<table style='border: 0px; border-collapse: collapse;'><tr><td style='vertical-align: top; width: 256px;'>";
if (bodyData.Image) {
strHTML += "<img src='" + bodyData.Image + "' style='background-color:black;'>";
} else {
strHTML += "<img src='https://i.imgur.com/advRrs1.png'>";
}
strHTML += "<i><p>"" + bodyData.Desc + ""</p></i><p><b>- Kerbal Astronomical Society</b></p></td>";
strHTML += "<td style='vertical-align: top; padding: 0px; margin-top: 0px'><b>Orbital Data</b>";
strHTML += "<p>Apoapsis: " + bodyData.Ap + " m<br>";
strHTML += "Periapsis: " + bodyData.Pe + " m<br>";
strHTML += "Eccentricity: " + bodyData.Ecc + "<br>";
strHTML += "Inclination: "+ bodyData.Inc + "°<br>";
strHTML += "Orbital period: " + formatTime(bodyData.ObtPeriod, false) + "<br>";
strHTML += "Orbital velocity: " + bodyData.ObtVel + " m/s</p><p><b>Physical Data</b></p>";
strHTML += "<p>Equatorial radius: " + numeral(bodyData.Radius*1000).format('0,0') + " m<br>";
strHTML += "Mass: " + bodyData.Mass.replace("+", "e") + " kg<br>";
strHTML += "Density: " + bodyData.Density + " kg/m<sup>3</sup><br>";
strHTML += "Surface gravity: " + bodyData.SurfaceG.split(":")[0] + " m/s<sup>2</sup> <i>(" + bodyData.SurfaceG.split(":")[1] + " g)</i><br>";
strHTML += "Escape velocity: " + bodyData.EscapeVel + " m/s<br>";
strHTML += "Rotational period: " + formatTime(bodyData.SolarDay, true) + "<br>";
strHTML += "Atmosphere: " + bodyData.Atmo + "</p>";
if (bodyData.Moons) strHTML += "<p><b>Moons</b></p><p>" + bodyData.Moons + "</p>";
if (ops.surface.Data && ops.surface.Data.Name == strBodyName) {
strHTML += "<p><span onclick='showMap()' style='cursor: pointer; color: blue; text-decoration: none;'>View Surface</span> | ";
} else if (bodyData.Moons && !$("#contentHeader").html().includes(strBodyName)) {
strHTML += "<span class='fauxLink' onclick='loadBody("" + strBodyName + "-System")'>View System</span> | ";
}
strHTML += "<span class='fauxLink' onclick='centerBody("" + object.charAt(0) + "")'>Focus View</span> | ";
// no nodes to show unless body has an eccentric or inclined orbit
if ((parseFloat(bodyData.Ecc) || parseFloat(bodyData.Inc)) && !$("#contentHeader").html().includes(strBodyName)) {
if (clickedObj.showNodes) strHTML += "<span onclick='toggleBodyNodes("" + object + "")' style='cursor: pointer; color: blue;'>Hide Nodes</span> | ";
else strHTML += "<span onclick='toggleBodyNodes("" + object + "")' style='cursor: pointer; color: blue;'>Show Nodes</span> | ";
}
strHTML = strHTML.substring(0, strHTML.length-2);
strHTML += "</p></td></tr></table>";
$("#figureDialog").dialog("option", "title", strBodyName);
$("#figureDialog").html(strHTML);
}
$("#figureDialog").dialog("open");
// toggle the orbit if the orbits are hidden
if (!$("#orbits").is(":checked")) {
ggbApplet.setVisible(clickedObj.id + "23", !ggbApplet.getVisible(clickedObj.id + "23"));
// if orbit is visible, only show the nodes if the box is checked or toggled for the object
if (ggbApplet.getVisible(clickedObj.id + "23") && (clickedObj.showNodes || $("#nodes").is(":checked"))) {
ggbApplet.setVisible(clickedObj.id + "26", true);
ggbApplet.setVisible(clickedObj.id + "27", true);
ggbApplet.setVisible(clickedObj.id + "28", true);
ggbApplet.setVisible(clickedObj.id + "32", true);
// hide the nodes if the orbit is not visible
} else if (!ggbApplet.getVisible(clickedObj.id + "23")) {
ggbApplet.setVisible(clickedObj.id + "26", false);
ggbApplet.setVisible(clickedObj.id + "27", false);
ggbApplet.setVisible(clickedObj.id + "28", false);
ggbApplet.setVisible(clickedObj.id + "32", false);
}
}
}
}
// find whatever other body is selected and unselect it
function unselectBody(clickedObj) {
var selectedObj = ops.ggbOrbits.find(o => o.isSelected === true);
if (selectedObj) {
if (selectedObj.type != "body") {
// if we click on the vessel that was selected, remove orbit if they are hidden
// bodies have to handle this themseleves because they only have the position to click on, not the orbit
if (clickedObj.includes(selectedObj.id) && !$("#orbits").is(":checked")) {
ggbApplet.setVisible(selectedObj.id + "conic", false);
}
// bodies don't show labels when selected and they can have their own toggles for nodes so don't mess with either
if (!$("#labels").is(":checked")) ggbApplet.setLabelVisible(selectedObj.id + "position", false);
if (!$("#nodes").is(":checked") || ($("#nodes").is(":checked") && !$("#orbits").is(":checked"))) {
ggbApplet.setVisible(selectedObj.id + "penode", false);
ggbApplet.setVisible(selectedObj.id + "apnode", false);
ggbApplet.setVisible(selectedObj.id + "anode", false);
ggbApplet.setVisible(selectedObj.id + "dnode", false);
}
selectedObj.showName = false;
selectedObj.showNodes = false;
}
selectedObj.isSelected = false;
}
return selectedObj;
}
// change the view to center on the selected object
function centerBody(objectChar) {
ggbApplet.evalCommand("CenterView((" + ggbApplet.getXcoord(objectChar + "36") + "," + ggbApplet.getYcoord(objectChar + "36") + "))");
}
function toggleBodyNodes(object) {
if ($('#figureDialog').html().includes("Show Nodes")) {
$('#figureDialog').html($('#figureDialog').html().replace("Show Nodes", "Hide Nodes"));
if (ggbApplet.getCaption(object.charAt(0) + "36") == "Priax") object = object.replace("D", "C");
// show nodes only if orbit is visible
if (ggbApplet.getVisible(object.charAt(0) + "23")) {
ggbApplet.setVisible(object.charAt(0) + "26", true);
ggbApplet.setVisible(object.charAt(0) + "27", true);
ggbApplet.setVisible(object.charAt(0) + "28", true);
ggbApplet.setVisible(object.charAt(0) + "32", true);
}
// https://stackoverflow.com/questions/12462318/find-a-value-in-an-array-of-objects-in-javascript
ops.ggbOrbits.find(o => o.id === object.charAt(0)).showNodes = true;
if (ggbApplet.getCaption(object.charAt(0) + "36") == "Polta") ops.ggbOrbits.find(o => o.id === "D").showNodes = true;
if (ggbApplet.getCaption(object.charAt(0) + "36") == "Priax") ops.ggbOrbits.find(o => o.id === "C").showNodes = true;
} else {
$('#figureDialog').html($('#figureDialog').html().replace("Hide Nodes", "Show Nodes"));
if (ggbApplet.getCaption(object.charAt(0) + "36") == "Priax") object = object.replace("D", "C");
// only hide if the diagram checkbox is not checked
if (!$("#nodes").is(":checked")) {
ggbApplet.setVisible(object.charAt(0) + "26", false);
ggbApplet.setVisible(object.charAt(0) + "27", false);
ggbApplet.setVisible(object.charAt(0) + "28", false);
ggbApplet.setVisible(object.charAt(0) + "32", false);
}
ops.ggbOrbits.find(o => o.id === object.charAt(0)).showNodes = false;
if (ggbApplet.getCaption(object.charAt(0) + "36") == "Polta") ops.ggbOrbits.find(o => o.id === "D").showNodes = false;
if (ggbApplet.getCaption(object.charAt(0) + "36") == "Priax") ops.ggbOrbits.find(o => o.id === "C").showNodes = false;
}
}
// handle GeoGebra diagram display options
function toggleNodes(isChecked) {
ops.ggbOrbits.forEach(function(item) {
// if the orbits are shown, affect all bodies or
// if the orbits are not shown, affect all bodies with nodes enabled or bodies with orbits visible
if (($("#orbits").is(":checked") && !item.showNodes && !item.isHidden) || (!$("#orbits").is(":checked") && (item.showNodes || (item.type == "body" && ggbApplet.getVisible(item.id + "23"))))) {
if (item.type == "body") {
ggbApplet.setVisible(item.id + "26", isChecked);
ggbApplet.setVisible(item.id + "27", isChecked);
ggbApplet.setVisible(item.id + "28", isChecked);
ggbApplet.setVisible(item.id + "32", isChecked);
// special case - don't let unchecking this box affect nodes when orbits are hidden but nodes are toggled on for body
// this is needed because of the way showNodes is used slightly differently between bodies and vessels
if (!isChecked && !$("#orbits").is(":checked") && item.showNodes) {
ggbApplet.setVisible(item.id + "26", true);
ggbApplet.setVisible(item.id + "27", true);
ggbApplet.setVisible(item.id + "28", true);
ggbApplet.setVisible(item.id + "32", true);
}
} else {
// if the orbits box is unchecked the node visibility must match that of the conic visibility
if (!$("#orbits").is(":checked")) var nodeVis = ggbApplet.getVisible(item.id + "conic");
else var nodeVis = isChecked;
ggbApplet.setVisible(item.id + "apnode", nodeVis);
ggbApplet.setVisible(item.id + "penode", nodeVis);
ggbApplet.setVisible(item.id + "anode", nodeVis);
ggbApplet.setVisible(item.id + "dnode", nodeVis);
}
}
});
}
function toggleLabels(isChecked) {
ops.ggbOrbits.forEach(function(item) {
if (!item.showName) {
if (item.type == "body") ggbApplet.setLabelVisible(item.id + "36", isChecked);
else ggbApplet.setLabelVisible(item.id + "position", isChecked);
}
});
}
function toggleOrbits(isChecked) {
ops.ggbOrbits.forEach(function(item) {
if (item.type == "body") ggbApplet.setVisible(item.id + "23", isChecked);
else {
if (!item.isHidden) ggbApplet.setVisible(item.id + "conic", isChecked);
}
});
// we need to hide all nodes when hiding orbits, regardless of toggle, because nothing is selected
if (!isChecked) {
if ($("#nodes").is(":checked")) $("#nodes").prop('checked', false);
ops.ggbOrbits.forEach(function(item) {
if (item.type == "body") {
ggbApplet.setVisible(item.id + "26", false);
ggbApplet.setVisible(item.id + "27", false);
ggbApplet.setVisible(item.id + "28", false);
ggbApplet.setVisible(item.id + "32", false);
} else {
ggbApplet.setVisible(item.id + "apnode", false);
ggbApplet.setVisible(item.id + "penode", false);
ggbApplet.setVisible(item.id + "anode", false);
ggbApplet.setVisible(item.id + "dnode", false);
}
});
}
// if we're showing orbits and the nodes box is checked, we have to show all the nodes, regardless of toggle so can't call function
else if ($("#nodes").is(":checked") && isChecked) {
ops.ggbOrbits.forEach(function(item) {
if (item.type == "body") {
ggbApplet.setVisible(item.id + "26", true);
ggbApplet.setVisible(item.id + "27", true);
ggbApplet.setVisible(item.id + "28", true);
ggbApplet.setVisible(item.id + "32", true);
} else {
ggbApplet.setVisible(item.id + "apnode", true);
ggbApplet.setVisible(item.id + "penode", true);
ggbApplet.setVisible(item.id + "anode", true);
ggbApplet.setVisible(item.id + "dnode", true);
}
});
}
// else if the orbits are being shown, show nodes depending on the orbit setting
else if (isChecked) {
ops.ggbOrbits.forEach(function(item) {
if (item.showNodes) {
if (item.type == "body") {
ggbApplet.setVisible(item.id + "26", true);
ggbApplet.setVisible(item.id + "27", true);
ggbApplet.setVisible(item.id + "28", true);
ggbApplet.setVisible(item.id + "32", true);
} else {
ggbApplet.setVisible(item.id + "apnode", true);
ggbApplet.setVisible(item.id + "penode", true);
ggbApplet.setVisible(item.id + "anode", true);
ggbApplet.setVisible(item.id + "dnode", true);
}
}
});
}
}
function toggleRefLine(isChecked) {
ggbApplet.setVisible("RefLine", isChecked);
}
function filterVesselOrbits(id, checked) {
if (checked) {
ops.ggbOrbits.forEach(function(item) {
if (id == item.type) {
ggbApplet.setVisible(item.id + "position", true);
ggbApplet.setVisible(item.id + "penode", $("#nodes").is(':checked'));
ggbApplet.setVisible(item.id + "apnode", $("#nodes").is(':checked'));
ggbApplet.setVisible(item.id + "anode", $("#nodes").is(':checked'));
ggbApplet.setVisible(item.id + "dnode", $("#nodes").is(':checked'));
ggbApplet.setLabelVisible(item.id + "position", $("#labels").is(':checked'));
item.isHidden = false;
// only show orbit if orbits are checked
if ($("#orbits").is(":checked")) ggbApplet.setVisible(item.id + "conic", true);
}
});
} else {
ops.ggbOrbits.forEach(function(item) {
if (id == item.type) {
ggbApplet.setVisible(item.id + "position", false);
ggbApplet.setVisible(item.id + "conic", false);
ggbApplet.setVisible(item.id + "penode", false);
ggbApplet.setVisible(item.id + "apnode", false);
ggbApplet.setVisible(item.id + "anode", false);
ggbApplet.setVisible(item.id + "dnode", false);
ggbApplet.setLabelVisible(item.id + "position", false);
item.isHidden = true;
}
});
}
}
function toggleSOI(isChecked) {
ops.ggbOrbits.forEach(function(item) {
if (item.type == "body") {
ggbApplet.setVisible(item.id + "39", isChecked);
}
});
}