Commit 965b6d8e41b3c8fac95a6bbaa5e1a588264a76c3

Authored by Sergio Crisostomo
1 parent 45f823c0

Fix slider for min/max values greater than 100

Showing 1 changed file with 20 additions and 21 deletions   Show diff stats
src/components/slider/slider.vue
@@ -110,7 +110,6 @@ @@ -110,7 +110,6 @@
110 startX: 0, 110 startX: 0,
111 currentX: 0, 111 currentX: 0,
112 startPos: 0, 112 startPos: 0,
113 - newPos: null,  
114 oldValue: val 113 oldValue: val
115 }; 114 };
116 }, 115 },
@@ -162,30 +161,30 @@ @@ -162,30 +161,30 @@
162 }, 161 },
163 minPosition () { 162 minPosition () {
164 const val = this.currentValue; 163 const val = this.currentValue;
165 - return (val[0] - this.min) / (this.max - this.min) * 100; 164 + return (val[0] - this.min) / this.valueRange * 100;
166 }, 165 },
167 maxPosition: function () { 166 maxPosition: function () {
168 const val = this.currentValue; 167 const val = this.currentValue;
169 168
170 - return (val[1] - this.min) / (this.max - this.min) * 100; 169 + return (val[1] - this.min) / this.valueRange * 100;
171 }, 170 },
172 barStyle () { 171 barStyle () {
173 172
174 const style = { 173 const style = {
175 - width: (this.currentValue[0] - this.min) / (this.max - this.min) * 100 + '%' 174 + width: (this.currentValue[0] - this.min) / this.valueRange * 100 + '%'
176 }; 175 };
177 176
178 if (this.range) { 177 if (this.range) {
179 - style.left = (this.currentValue[0] - this.min) / (this.max - this.min) * 100 + '%';  
180 - style.width = (this.currentValue[1] - this.currentValue[0]) / (this.max - this.min) * 100 + '%'; 178 + style.left = (this.currentValue[0] - this.min) / this.valueRange * 100 + '%';
  179 + style.width = (this.currentValue[1] - this.currentValue[0]) / this.valueRange * 100 + '%';
181 } 180 }
182 181
183 return style; 182 return style;
184 }, 183 },
185 stops () { 184 stops () {
186 - let stopCount = (this.max - this.min) / this.step; 185 + let stopCount = this.valueRange / this.step;
187 let result = []; 186 let result = [];
188 - let stepWidth = 100 * this.step / (this.max - this.min); 187 + let stepWidth = 100 * this.step / this.valueRange;
189 for (let i = 1; i < stopCount; i++) { 188 for (let i = 1; i < stopCount; i++) {
190 result.push(i * stepWidth); 189 result.push(i * stepWidth);
191 } 190 }
@@ -196,6 +195,9 @@ @@ -196,6 +195,9 @@
196 }, 195 },
197 tipDisabled () { 196 tipDisabled () {
198 return this.tipFormat(this.currentValue[0]) === null || this.showTip === 'never'; 197 return this.tipFormat(this.currentValue[0]) === null || this.showTip === 'never';
  198 + },
  199 + valueRange(){
  200 + return this.max - this.min;
199 } 201 }
200 }, 202 },
201 methods: { 203 methods: {
@@ -203,11 +205,11 @@ @@ -203,11 +205,11 @@
203 return e.type.indexOf('touch') !== -1 ? e.touches[0].clientX : e.clientX; 205 return e.type.indexOf('touch') !== -1 ? e.touches[0].clientX : e.clientX;
204 }, 206 },
205 checkLimits ([min, max]) { 207 checkLimits ([min, max]) {
206 - min = Math.max(0, min);  
207 - min = Math.min(100, min); 208 + min = Math.max(this.min, min);
  209 + min = Math.min(this.max, min);
208 210
209 - max = Math.max(0, min, max);  
210 - max = Math.min(100, max); 211 + max = Math.max(this.min, min, max);
  212 + max = Math.min(this.max, max);
211 return [min, max]; 213 return [min, max];
212 }, 214 },
213 onPointerDown (event, type) { 215 onPointerDown (event, type) {
@@ -224,22 +226,20 @@ @@ -224,22 +226,20 @@
224 onPointerDragStart (event) { 226 onPointerDragStart (event) {
225 this.dragging = false; 227 this.dragging = false;
226 this.startX = this.getPointerX(event); 228 this.startX = this.getPointerX(event);
227 - this.startPos = parseInt(this[`${this.pointerDown}Position`], 10); 229 + this.startPos = (this[`${this.pointerDown}Position`] * this.valueRange / 100) + this.min;
228 }, 230 },
229 onPointerDrag (event) { 231 onPointerDrag (event) {
230 this.dragging = true; 232 this.dragging = true;
231 this.$refs[`${this.pointerDown}Tooltip`].visible = true; 233 this.$refs[`${this.pointerDown}Tooltip`].visible = true;
232 this.currentX = this.getPointerX(event); 234 this.currentX = this.getPointerX(event);
  235 + const diff = (this.currentX - this.startX) / this.sliderWidth * this.valueRange;
233 236
234 - const diff = (this.currentX - this.startX) / this.sliderWidth * 100;  
235 - this.newPos = this.startPos + diff;  
236 - this.changeButtonPosition(this.newPos); 237 + this.changeButtonPosition(this.startPos + diff);
237 }, 238 },
238 onPointerDragEnd () { 239 onPointerDragEnd () {
239 if (this.dragging) { 240 if (this.dragging) {
240 this.dragging = false; 241 this.dragging = false;
241 this.$refs[`${this.pointerDown}Tooltip`].visible = false; 242 this.$refs[`${this.pointerDown}Tooltip`].visible = false;
242 - this.changeButtonPosition(this.newPos);  
243 } 243 }
244 244
245 this.pointerDown = ''; 245 this.pointerDown = '';
@@ -249,17 +249,16 @@ @@ -249,17 +249,16 @@
249 off(window, 'touchend', this.onPointerDragEnd); 249 off(window, 'touchend', this.onPointerDragEnd);
250 }, 250 },
251 changeButtonPosition (newPos, forceType) { 251 changeButtonPosition (newPos, forceType) {
252 -  
253 const type = forceType || this.pointerDown; 252 const type = forceType || this.pointerDown;
254 const index = type === 'min' ? 0 : 1; 253 const index = type === 'min' ? 0 : 1;
255 if (type === 'min') newPos = this.checkLimits([newPos, this.maxPosition])[0]; 254 if (type === 'min') newPos = this.checkLimits([newPos, this.maxPosition])[0];
256 else newPos = this.checkLimits([this.minPosition, newPos])[1]; 255 else newPos = this.checkLimits([this.minPosition, newPos])[1];
257 256
258 - const lengthPerStep = 100 / ((this.max - this.min) / this.step); 257 + const lengthPerStep = this.valueRange / 100 * this.step;
259 const steps = Math.round(newPos / lengthPerStep); 258 const steps = Math.round(newPos / lengthPerStep);
260 259
261 const value = this.currentValue; 260 const value = this.currentValue;
262 - value[index] = Math.round(steps * lengthPerStep * (this.max - this.min) * 0.01 + this.min); 261 + value[index] = Math.round(steps * lengthPerStep);
263 this.currentValue = [...value]; 262 this.currentValue = [...value];
264 263
265 if (!this.dragging) { 264 if (!this.dragging) {
@@ -276,7 +275,7 @@ @@ -276,7 +275,7 @@
276 if (this.disabled) return; 275 if (this.disabled) return;
277 const currentX = this.getPointerX(event); 276 const currentX = this.getPointerX(event);
278 const sliderOffsetLeft = this.$refs.slider.getBoundingClientRect().left; 277 const sliderOffsetLeft = this.$refs.slider.getBoundingClientRect().left;
279 - const newPos = (currentX - sliderOffsetLeft) / this.sliderWidth * 100; 278 + let newPos = ((currentX - sliderOffsetLeft) / this.sliderWidth * this.valueRange) + this.min;
280 279
281 if (!this.range || newPos <= this.minPosition) this.changeButtonPosition(newPos, 'min'); 280 if (!this.range || newPos <= this.minPosition) this.changeButtonPosition(newPos, 'min');
282 else if (newPos >= this.maxPosition) this.changeButtonPosition(newPos, 'max'); 281 else if (newPos >= this.maxPosition) this.changeButtonPosition(newPos, 'max');