55 <span class =" ctrlName" >{{name}}</span >
66 </div >
77 <div class =" slider-container" >
8- <input type =" number" :value =" min " v-on:change =" setMin" />
8+ <input type =" number" :value =" displayMin " v-on:change =" setMin" />
99 <!-- <vue-slider v-model="value" :min="min" :max="max" :interval="interval" /> -->
10- <vue-slider v-model =" value" :data =" data" @change =" putVal" />
11- <input type =" number" :value =" max " v-on:change =" setMax" />
10+ <vue-slider v-model =" value" :data =" data" :adsorb = " true " @change =" putVal" />
11+ <input type =" number" :value =" displayMax " v-on:change =" setMax" />
1212 </div >
1313 </div >
1414</template >
1515
1616<script >
1717import VueSlider from " vue-slider-component" ;
1818import " vue-slider-component/theme/default.css" ;
19- import { getVal , putVal } from " ../../lib/odrive_utils.js" ;
19+ import { getVal , putVal , parseMath } from " ../../lib/odrive_utils.js" ;
20+ import { numberDisplay } from " ../../lib/utils.js" ;
2021
2122export default {
2223 name: " CtrlSlider" ,
@@ -49,6 +50,12 @@ export default {
4950 sliderData : function () {
5051 let interval = (this .max - this .min ) / 100 ;
5152 return Array .from (Array (101 ), (_ , i ) => this .min + interval * i);
53+ },
54+ displayMin () {
55+ return numberDisplay (this .min );
56+ },
57+ displayMax () {
58+ return numberDisplay (this .max );
5259 }
5360 },
5461 methods: {
@@ -59,27 +66,41 @@ export default {
5966 putVal (keys .join (' .' ), value);
6067 },
6168 setMin : function (e ) {
62- this .min = parseFloat (e .target .value );
69+ this .min = parseMath (e .target .value );
6370 this .data = Array .from (Array (101 ), (_ , i ) => this .min + (this .max - this .min ) / 100 * i);
71+ this .value = this .findNearest (this .data , this .value );
6472 },
6573 setMax : function (e ) {
66- this .max = parseFloat (e .target .value );
74+ this .max = parseMath (e .target .value );
6775 this .data = Array .from (Array (101 ), (_ , i ) => this .min + (this .max - this .min ) / 100 * i);
76+ this .value = this .findNearest (this .data , this .value );
6877 },
6978 deleteCtrl : function () {
7079 // commit a mutation in the store with the relevant information
7180 this .$store .commit (" removeCtrlFromDash" , {dashID: this .dashID , path: this .path });
81+ },
82+ findNearest (data , value ) {
83+ // find item in data closest to value
84+ let diff = Number .POSITIVE_INFINITY ;
85+ let retVal = value;
86+ for (const val of data) {
87+ if (Math .abs (val - value) < diff) {
88+ diff = Math .abs (val - value);
89+ retVal = val;
90+ }
91+ }
92+ return retVal;
7293 }
7394 },
7495 mounted () {
7596 let initVal = () => {
7697 let keys = this .path .split (" ." );
7798 keys .shift (); // don't need first key here
78- return getVal (keys .join (' .' ));
99+ return parseFloat ( getVal (keys .join (' .' ) ));
79100 };
80101 this .value = initVal ();
81- this .max = parseFloat (( this .value * 4 ). toFixed ( 3 )) ;
82- this .min = parseFloat (( this .value / 4 ). toFixed ( 3 )) ;
102+ this .max = this .value * 4 ;
103+ this .min = this .value / 4 ;
83104 this .data = Array .from (Array (101 ), (_ , i ) => this .min + (this .max - this .min ) / 100 * i);
84105 },
85106};
0 commit comments