@@ -10,6 +10,8 @@ var Settings = (function () {
1010 } ) ;
1111 return Promise . mapSeries ( inputs , function ( input , ii ) {
1212 var settingName = input . data ( 'setting' ) ;
13+ var inputUnit = input . data ( 'unit' ) ;
14+
1315 return mspHelper . getSetting ( settingName ) . then ( function ( s ) {
1416 // Check if the input declares a parent
1517 // to be hidden in case of the setting not being available.
@@ -25,6 +27,7 @@ var Settings = (function () {
2527 return ;
2628 }
2729 parent . show ( ) ;
30+
2831 if ( input . prop ( 'tagName' ) == 'SELECT' || s . setting . table ) {
2932 if ( input . attr ( 'type' ) == 'checkbox' ) {
3033 input . prop ( 'checked' , s . value > 0 ) ;
@@ -67,11 +70,13 @@ var Settings = (function () {
6770 } else {
6871 var multiplier = parseFloat ( input . data ( 'setting-multiplier' ) || 1 ) ;
6972 input . attr ( 'type' , 'number' ) ;
70- input . attr ( 'step' , 1 / multiplier ) ;
71- input . attr ( 'min' , s . setting . min / multiplier ) ;
72- input . attr ( 'max' , s . setting . max / multiplier ) ;
7373 input . val ( ( s . value / multiplier ) . toFixed ( Math . log10 ( multiplier ) ) ) ;
7474 }
75+
76+ // If data is defined, We want to convert this value into
77+ // something matching the units
78+ self . convertToUnitSetting ( input , inputUnit ) ;
79+
7580 input . data ( 'setting-info' , s . setting ) ;
7681 if ( input . data ( 'live' ) ) {
7782 input . change ( function ( ) {
@@ -82,6 +87,108 @@ var Settings = (function () {
8287 } ) ;
8388 } ;
8489
90+
91+ /**
92+ *
93+ * @param {JQuery Element } input
94+ * @param {String } inputUnit Unit from HTML Dom input
95+ */
96+ self . convertToUnitSetting = function ( element , inputUnit ) {
97+
98+ // One of the following;
99+ // none, OSD, imperial, metric
100+ const configUnitType = globalSettings . unitType ;
101+
102+ // Small closure to grab the unit as described by either
103+ // the app settings or the app OSD settings, confused? yeah
104+ const getUnitDisplayTypeValue = ( ) => {
105+ // Try and match the values
106+ switch ( configUnitType ) {
107+ case UnitType . OSD : // Match the OSD value on the UI
108+ return globalSettings . osdUnits ;
109+ break ;
110+ case UnitType . imperial :
111+ return 0 ; // Imperial OSD Value
112+ break ;
113+ case UnitType . metric :
114+ return 1 ; // Metric + MPH OSD Value
115+ break ;
116+ case UnitType . none :
117+ default :
118+ // Something went wrong
119+ return - 1 ;
120+ }
121+ }
122+
123+ // Sets the int value of the way we want to display the
124+ // units. We use the OSD unit values here for easy
125+ const uiUnitValue = getUnitDisplayTypeValue ( ) ;
126+
127+ const oldValue = element . val ( ) ;
128+
129+ // Ensure we can do conversions
130+ if ( configUnitType === UnitType . none || uiUnitValue === - 1 || ! inputUnit || ! oldValue || ! element ) {
131+ return ;
132+ }
133+
134+ // Used to convert between a value and a value matching the int
135+ // unit display value. Eg 1 = Metric
136+ // units. We use the OSD unit values here for easy
137+ const conversionTable = {
138+ 1 : {
139+ 'cm' : { multiplier : 100 , unitName : 'm' } ,
140+ 'cms' : { multiplier : 27.77777777777778 , unitName : 'Km/h' }
141+ } ,
142+ 2 : {
143+ 'cm' : { multiplier : 100 , unitName : 'm' } ,
144+ } ,
145+ 4 : {
146+ 'cms' : { multiplier : 51.44444444444457 , unitName : 'Kt' }
147+ } ,
148+ default : {
149+ 'cm' : { multiplier : 30.48 , unitName : 'ft' } ,
150+ 'cms' : { multiplier : 44.704 , unitName : 'mph' } ,
151+ 'ms' : { multiplier : 1000 , unitName : 'sec' }
152+ } ,
153+ }
154+
155+ // Small closure to try and get the multiplier
156+ // needed from the conversion table
157+ const getUnitMultiplier = ( ) => {
158+ if ( conversionTable [ uiUnitValue ] && conversionTable [ uiUnitValue ] [ inputUnit ] ) {
159+ return conversionTable [ uiUnitValue ] [ inputUnit ] ;
160+ }
161+
162+ return conversionTable [ 'default' ] [ inputUnit ] ;
163+ }
164+
165+ // Get the default multi obj or the custom
166+ const multiObj = getUnitMultiplier ( ) ;
167+
168+ if ( ! multiObj ) {
169+ return ;
170+ }
171+
172+ const multiplier = multiObj . multiplier ;
173+ const unitName = multiObj . unitName ;
174+
175+ // Update the step, min, and max; as we have the multiplier here.
176+ if ( element . attr ( 'type' ) == 'number' ) {
177+ element . attr ( 'step' , ( ( multiplier != 1 ) ? '0.01' : '1' ) ) ;
178+ element . attr ( 'min' , ( element . attr ( 'min' ) / multiplier ) . toFixed ( 2 ) ) ;
179+ element . attr ( 'max' , ( element . attr ( 'max' ) / multiplier ) . toFixed ( 2 ) ) ;
180+ }
181+
182+ // Update the input with a new formatted unit
183+ const convertedValue = Number ( ( oldValue / multiplier ) . toFixed ( 2 ) ) ;
184+ const newValue = Number . isInteger ( convertedValue ) ? Math . round ( convertedValue ) : convertedValue ;
185+ element . val ( newValue ) ;
186+ element . data ( 'setting-multiplier' , multiplier ) ;
187+
188+ // Now wrap the input in a display that shows the unit
189+ element . wrap ( `<div data-unit="${ unitName } " class="unit_wrapper unit"></div>` ) ;
190+ }
191+
85192 self . saveInput = function ( input ) {
86193 var settingName = input . data ( 'setting' ) ;
87194 var setting = input . data ( 'setting-info' ) ;
0 commit comments