8
8
{ Presets} = require ( "./presets.js" ) ,
9
9
PresetPicker = require ( "./preset_picker.js" ) ;
10
10
11
+ function formatTinyInterval ( nanos ) {
12
+ let
13
+ millis = Math . floor ( nanos / 1000 ) ;
14
+
15
+ nanos = nanos % 1000 ;
16
+
17
+ if ( millis > 0 ) {
18
+ if ( nanos == 0 ) {
19
+ return millis + "ms" ;
20
+ } else {
21
+ return ( millis + nanos / 1000 ) . toFixed ( 1 ) + "ms" ;
22
+ }
23
+ } else {
24
+ return nanos + "ns" ;
25
+ }
26
+ }
27
+
11
28
/**
12
29
*
13
30
* @param {HTMLElement } dialog - Root dialog element to attach to
14
31
* @param {Presets } graphPresets
15
32
* @constructor
16
33
*/
17
34
function GraphConfigurationDialog ( dialog , graphPresets ) {
35
+ const
36
+ // Some fields it doesn't make sense to graph
37
+ BLACKLISTED_FIELDS = { time :true , loopIteration :true } ,
38
+
39
+ SUGGESTED_SMOOTHING_INTERVALS = [
40
+ 500 , 1000 , 1500 , 2000 , 3000 , 4000 , 5000 , 6000 , 7000 , 8000 , 9000 , 10000 , 15000 , 20000 , 30000 , 40000 , 50000
41
+ ] ,
42
+
43
+ SMOOTHING_OPTIONS = SUGGESTED_SMOOTHING_INTERVALS . map ( interval => ( {
44
+ name : formatTinyInterval ( interval ) ,
45
+ value : interval
46
+ } ) ) ,
47
+
48
+ that = this ;
49
+
18
50
var
19
- // Some fields it doesn't make sense to graph
20
- BLACKLISTED_FIELDS = { time :true , loopIteration :true } ,
21
51
offeredFieldNames = [ ] ,
22
52
exampleGraphs = [ ] ,
23
53
@@ -41,17 +71,14 @@ function GraphConfigurationDialog(dialog, graphPresets) {
41
71
42
72
}
43
73
44
- function renderFieldOption ( fieldName , selectedName ) {
45
- var
46
- option = $ ( "<option></option>" )
47
- . text ( FlightLogFieldPresenter . fieldNameToFriendly ( fieldName ) )
48
- . attr ( "value" , fieldName ) ;
49
-
50
- if ( fieldName == selectedName ) {
51
- option . attr ( "selected" , "selected" ) ;
52
- }
53
-
54
- return option ;
74
+ function renderOptionElem ( value , displayName ) {
75
+ var
76
+ result = document . createElement ( "option" ) ;
77
+
78
+ result . innerText = displayName ;
79
+ result . value = value ;
80
+
81
+ return result ;
55
82
}
56
83
57
84
/**
@@ -60,19 +87,46 @@ function GraphConfigurationDialog(dialog, graphPresets) {
60
87
*/
61
88
function renderField ( field ) {
62
89
let
63
- elem = $ (
64
- '<li class="config-graph-field">'
65
- + '<select class="form-control"><option value="">(choose a field)</option></select>'
66
- + '<button type="button" class="btn btn-default btn-sm">Remove</button>'
67
- + '</li>'
68
- ) ,
69
- select = $ ( 'select' , elem ) ,
90
+ elem = $ ( `
91
+ <tr class="config-graph-field">
92
+ <td>
93
+ <select class="form-control graph-field-name">
94
+ <option value="">(choose a field)</option>
95
+ </select>
96
+ </td>
97
+ <td>
98
+ <select class="form-control graph-field-smoothing"></select>
99
+ </td>
100
+ <td>
101
+ <button type="button" class="btn btn-default btn-sm">Remove</button>
102
+ </td>
103
+ </tr>
104
+ ` ) ,
105
+ fieldNameSelect = $ ( '.graph-field-name' , elem ) ,
106
+ smoothingSelect = $ ( '.graph-field-smoothing' , elem ) ,
107
+
70
108
selectedFieldName = field ? field . name : false ;
71
-
72
- for ( let fieldName of offeredFieldNames ) {
73
- select . append ( renderFieldOption ( fieldName , selectedFieldName ) ) ;
74
- }
75
-
109
+
110
+ fieldNameSelect . append ( offeredFieldNames . map ( name => renderOptionElem ( name , FlightLogFieldPresenter . fieldNameToFriendly ( name ) ) ) ) ;
111
+ fieldNameSelect . val ( selectedFieldName ) ;
112
+
113
+ let
114
+ defaultSmoothingMessage = "Default" ;
115
+
116
+ if ( selectedFieldName ) {
117
+ let
118
+ defaultSmoothing = GraphConfig . getDefaultSmoothingForField ( that . flightLog , selectedFieldName ) ;
119
+
120
+ defaultSmoothingMessage += " (" + ( defaultSmoothing ? formatTinyInterval ( defaultSmoothing ) : "none" ) + ")" ;
121
+ }
122
+
123
+ smoothingSelect . append ( renderOptionElem ( "default" , defaultSmoothingMessage ) ) ;
124
+ smoothingSelect . append ( renderOptionElem ( "0" , "None" ) ) ;
125
+
126
+ smoothingSelect . append ( SMOOTHING_OPTIONS . map ( suggestion => renderOptionElem ( suggestion . value , suggestion . name ) ) ) ;
127
+
128
+ smoothingSelect . val ( field . smoothing === undefined ? "default" : field . smoothing ) ;
129
+
76
130
return elem ;
77
131
}
78
132
@@ -96,7 +150,23 @@ function GraphConfigurationDialog(dialog, graphPresets) {
96
150
<div class="form-group">
97
151
<label class="col-sm-2 control-label">Fields</label>
98
152
<div class="col-sm-10">
99
- <ul class="config-graph-field-list form-group-sm form-inline list-unstyled"></ul>
153
+ <table class="config-graph-field-list form-group-sm table table-condensed">
154
+ <thead>
155
+ <tr>
156
+ <th>
157
+ Field name
158
+ </th>
159
+ <th>
160
+ Smoothing
161
+ </th>
162
+ <th>
163
+
164
+ </th>
165
+ </tr>
166
+ </thead>
167
+ <tbody>
168
+ </tbody>
169
+ </table>
100
170
<button type="button" class="btn btn-default btn-sm add-field-button"><span class="glyphicon glyphicon-plus"></span> Add field</button>
101
171
</div>
102
172
</div>
@@ -105,7 +175,7 @@ function GraphConfigurationDialog(dialog, graphPresets) {
105
175
</dl>
106
176
</li>
107
177
` ) ,
108
- fieldList = $ ( ".config-graph-field-list" , graphElem ) ,
178
+ fieldList = $ ( ".config-graph-field-list tbody " , graphElem ) ,
109
179
graphNameField = $ ( "input" , graphElem ) ;
110
180
111
181
graphNameField
@@ -130,14 +200,25 @@ function GraphConfigurationDialog(dialog, graphPresets) {
130
200
. append ( graph . fields . map ( renderField ) )
131
201
132
202
// Catch field dropdown changes
133
- . on ( "change" , "select " , function ( e ) {
203
+ . on ( "change" , ".graph-field-name " , function ( e ) {
134
204
let
135
205
fieldIndex = $ ( this ) . parents ( '.config-graph-field' ) . index ( ) ;
136
206
137
207
updateActivePreset ( preset => preset . graphs [ graphIndex ] . fields [ fieldIndex ] . name = $ ( e . target ) . val ( ) ) ;
138
208
} )
139
-
140
- // Remove field button
209
+ . on ( "change" , ".graph-field-smoothing" , function ( e ) {
210
+ let
211
+ fieldIndex = $ ( this ) . parents ( '.config-graph-field' ) . index ( ) ,
212
+ newVal = $ ( e . target ) . val ( ) ;
213
+
214
+ if ( ! isNaN ( parseInt ( newVal , 10 ) ) ) {
215
+ newVal = parseInt ( newVal , 10 ) ;
216
+ }
217
+
218
+ updateActivePreset ( preset => preset . graphs [ graphIndex ] . fields [ fieldIndex ] . smoothing = newVal ) ;
219
+ } )
220
+
221
+ // Remove field button
141
222
. on ( 'click' , 'button' , function ( e ) {
142
223
let
143
224
fieldIndex = $ ( this ) . parents ( '.config-graph-field' ) . index ( ) ;
@@ -248,6 +329,8 @@ function GraphConfigurationDialog(dialog, graphPresets) {
248
329
// We'll restore this backup if the user cancels the dialog
249
330
graphPresetsBackup = graphPresets . clone ( ) ;
250
331
332
+ this . flightLog = flightLog ;
333
+
251
334
populateExampleGraphs ( flightLog , exampleGraphsMenu ) ;
252
335
buildOfferedFieldNamesList ( flightLog , graphPresets . getActivePreset ( ) . graphs ) ;
253
336
0 commit comments