Skip to content

Commit 06a653f

Browse files
committed
Fixed bug calling setValue() before the component is rendered in Slider
1 parent 7b61013 commit 06a653f

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

src/slider/Slider.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if(!javaxt.dhtml) javaxt.dhtml={};
1111
* div (DOM element) and a minimal config. See config settings for a full
1212
* range of options.
1313
<pre>
14-
var checkbox = new javaxt.dhtml.Slider(div, {
14+
var slider = new javaxt.dhtml.Slider(div, {
1515
units: "percent"
1616
});
1717
</pre>
@@ -36,6 +36,7 @@ javaxt.dhtml.Slider = function(parent, config) {
3636
*/
3737
value: 0,
3838

39+
3940
/** If true, the slider will be disabled when it is initialized. The
4041
* slider can be enabled and disabled using the enable() and disable()
4142
* methods.
@@ -79,6 +80,7 @@ javaxt.dhtml.Slider = function(parent, config) {
7980
};
8081

8182

83+
var isRendered = false;
8284
var handle, slider, mask;
8385
var value; //number - leave undefined initially
8486

@@ -175,6 +177,7 @@ javaxt.dhtml.Slider = function(parent, config) {
175177

176178
if (config.disabled===true) me.disable();
177179

180+
isRendered = true;
178181
me.setValue(config.value, true);
179182
me.onRender();
180183
});
@@ -320,29 +323,44 @@ javaxt.dhtml.Slider = function(parent, config) {
320323
//** setValue
321324
//**************************************************************************
322325
/** Used to set the position of the slider.
323-
* @param x Accepts a number representing pixels from the left or a string
324-
* representing a percentage value (e.g. '50%')
326+
* @param x A number or a string representing a percentage value (e.g. '50%').
327+
* If a number is provided and the "units" config is set to "percent", then
328+
* the number will be interpreted as a percent value from 0-100. Otherwise,
329+
* the number will be interpreted as pixels from the left of the slider.
325330
* @param silent If true, will not fire the onChange event
326331
*/
327332
this.setValue = function(x, silent){
328333

334+
if (!isRendered){
335+
config.value = x;
336+
return;
337+
}
338+
339+
340+
341+
var setValue = function(x){
342+
handle.style.left = (x-(handleWidth/2)) + 'px';
343+
updateSlider(x, silent);
344+
};
345+
329346
if (javaxt.dhtml.utils.isString(x)){
330347
if (x.lastIndexOf("%")===x.length-1){
331-
x = parseInt(x.substring(0,x.length-1));
348+
x = parseFloat(x.substring(0,x.length-1));
332349
if (isNaN(x) || x<0 || x>100) return;
333350
x = me.getWidth() * (x/100);
351+
setValue(x);
334352
}
335353
}
336-
337-
if (javaxt.dhtml.utils.isNumber(x)){
338-
x = parseFloat(x);
339-
}
340-
else {
341-
return;
354+
else{
355+
x = parseFloat(x+"");
356+
if (x<0) return;
357+
if (config.units==="percent"){
358+
if (x>100) return;
359+
x = me.getWidth() * (x/100);
360+
}
361+
setValue(x);
342362
}
343363

344-
handle.style.left = (x-(handleWidth/2)) + 'px';
345-
updateSlider(x, silent);
346364
};
347365

348366

0 commit comments

Comments
 (0)