Debian Codegraph @

public_html/formatter.js

raw
1 // -*- mode: javascript; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 "use strict";
3
4 var NBSP='\u00a0';
5 var DEGREES='\u00b0'
6 var UP_TRIANGLE='\u25b2'; // U+25B2 BLACK UP-POINTING TRIANGLE
7 var DOWN_TRIANGLE='\u25bc'; // U+25BC BLACK DOWN-POINTING TRIANGLE
8
9 var TrackDirections = ["North","Northeast","East","Southeast","South","Southwest","West","Northwest"];
10
11 // formatting helpers
12
13 // track in degrees (0..359)
14 function format_track_brief(track) {
15 if (track === null){
16 return "";
17 }
18
19 return Math.round(track);
20 }
21
22 // track in degrees (0..359)
23 function format_track_long(track) {
24 if (track === null){
25 return "n/a";
26 }
27
28 var trackDir = Math.floor((360 + track % 360 + 22.5) / 45) % 8;
29 return Math.round(track) + DEGREES + NBSP + "(" + TrackDirections[trackDir] + ")";
30 }
31
32 // altitude (input: alt in feet)
33 // brief will always show either Metric or Imperial
34 function format_altitude_brief(alt, vr) {
35 var alt_text;
36
37 if (alt === null){
38 return "";
39 } else if (alt === "ground"){
40 return "ground";
41 }
42
43 if (Metric) {
44 alt_text = Math.round(alt * 0.3048) + NBSP; // Altitude to meters
45 } else {
46 alt_text = Math.round(alt) + NBSP;
47 }
48
49 // Vertical Rate Triangle
50 if (vr > 128){
51 return alt_text + UP_TRIANGLE;
52 } else if (vr < -128){
53 return alt_text + DOWN_TRIANGLE;
54 } else {
55 return alt_text + NBSP;
56 }
57 }
58
59 // alt in ft
60 function _alt_to_unit(alt, m) {
61 if (m)
62 return Math.round(alt * 0.3048) + NBSP + "m";
63 else
64 return Math.round(alt) + NBSP + "ft";
65 }
66
67 function format_altitude_long(alt, vr) {
68 var alt_text = "";
69
70 if (alt === null) {
71 return "n/a";
72 } else if (alt === "ground") {
73 return "on ground";
74 }
75
76 // Primary unit
77 alt_text = _alt_to_unit(alt, Metric);
78
79 // Secondary unit
80 if (ShowOtherUnits) {
81 alt_text = alt_text + ' | ' + _alt_to_unit(alt, !Metric);
82 }
83
84 if (vr > 128) {
85 return UP_TRIANGLE + NBSP + alt_text;
86 } else if (vr < -128) {
87 return DOWN_TRIANGLE + NBSP + alt_text;
88 } else {
89 return alt_text;
90 }
91 }
92
93 //input: speed in kts
94 function format_speed_brief(speed) {
95 if (speed === null) {
96 return "";
97 }
98
99 if (Metric) {
100 return Math.round(speed * 1.852); // knots to kilometers per hour
101 } else {
102 return Math.round(speed); // knots
103 }
104 }
105
106 // speed in kts
107
108 function _speed_to_unit(speed, m) {
109 if (m)
110 return Math.round(speed * 1.852) + NBSP + "km/h";
111 else
112 return Math.round(speed) + NBSP + "kt";
113 }
114
115 function format_speed_long(speed) {
116 if (speed === null) {
117 return "n/a";
118 }
119
120 // Primary unit
121 var speed_text = _speed_to_unit(speed, Metric);
122
123 // Secondary unit
124 if (ShowOtherUnits) {
125 speed_text = speed_text + ' | ' + _speed_to_unit(speed, !Metric);
126 }
127
128 return speed_text;
129 }
130
131 // dist in meters
132 function format_distance_brief(dist) {
133 if (dist === null) {
134 return "";
135 }
136
137 if (Metric) {
138 return (dist/1000).toFixed(1); // meters to kilometers
139 } else {
140 return (dist/1852).toFixed(1); // meters to nautocal miles
141 }
142 }
143
144 // dist in metres
145
146 function _dist_to_unit(dist, m) {
147 if (m)
148 return (dist/1000).toFixed(1) + NBSP + "km";
149 else
150 return (dist/1852).toFixed(1) + NBSP + "NM";
151 }
152
153 function format_distance_long(dist) {
154 if (dist === null) {
155 return "n/a";
156 }
157
158 // Primary unit
159 var dist_text = _dist_to_unit(dist, Metric);
160
161 // Secondary unit
162 if (ShowOtherUnits) {
163 dist_text = dist_text + ' | ' + _dist_to_unit(dist, !Metric);
164 }
165
166 return dist_text;
167 }
168
169 // p is a [lon, lat] coordinate
170 function format_latlng(p) {
171 return p[1].toFixed(3) + DEGREES + "," + NBSP + p[0].toFixed(3) + DEGREES;
172 }