Skip to content

Commit

Permalink
Fix: missing attribute values causing runtime error (#205)
Browse files Browse the repository at this point in the history
* Fix: added default attribute values for `_scaleStart`, `_scaleEnd` and `_scaleStep`. Changed  `_scaleEnd` schema default from 1 to 10. (fixes #204).

* Removed `_scaleStep` fallbacks as these are no longer needed with a default model value.

* Restrict `_scaleStep` to a positive number, to prevent errors.
  • Loading branch information
danielghost authored May 29, 2024
1 parent 4e21470 commit a85cdaf
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ guide the learner’s interaction with the component.

**labelEnd** (string): Text/characters that appear at the end of the slider scale.

**\_scaleStart** (number): This value is the numeric start of the scale. It is used to calculate the slider's position on the scale.
**\_scaleStart** (number): This value is the numeric start of the scale. It is used to calculate the slider's position on the scale. The default is `1`.

**\_scaleEnd** (number): This value is the numeric end of the scale. It is used to calculate the slider's position on the scale.
**\_scaleEnd** (number): This value is the numeric end of the scale. It is used to calculate the slider's position on the scale. The default is `10`.

**\_scaleStep** (number): Defines the amount the scale should be incremented by.
**\_scaleStep** (number): Defines the amount the scale should be incremented by. The default is `1`.

**scaleStepPrefix** (string): Prefix to add to each slider step. For example, a "$" can be used as a prefix to indicate currency in dollars (ex. $100).

Expand Down
14 changes: 11 additions & 3 deletions js/SliderModel.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import Adapt from 'core/js/adapt';
import QuestionModel from 'core/js/models/questionModel';
import logging from 'core/js/logging';

export default class SliderModel extends QuestionModel {

defaults() {
return QuestionModel.resultExtend('defaults', {
_scaleStart: 1,
_scaleEnd: 10,
_scaleStep: 1,
_showScale: true,
_showScaleNumbers: true,
_showScaleIndicator: true,
Expand All @@ -14,7 +18,11 @@ export default class SliderModel extends QuestionModel {

init() {
QuestionModel.prototype.init.call(this);

// safeguard against `_scaleStep` of 0 or less
if (this.get('_scaleStep') <= 0) {
logging.warn(`\`_scaleStep\` must be a positive number, restoring default of 1 for ${this.get('_id')}`);
this.set('_scaleStep', 1);
}
this.setupModelItems();
this.selectDefaultItem();
}
Expand Down Expand Up @@ -49,7 +57,7 @@ export default class SliderModel extends QuestionModel {
const range = this.get('_correctRange');
const start = this.get('_scaleStart');
const end = this.get('_scaleEnd');
const step = this.get('_scaleStep') || 1;
const step = this.get('_scaleStep');

const dp = this.getDecimalPlaces(step);

Expand Down Expand Up @@ -177,7 +185,7 @@ export default class SliderModel extends QuestionModel {
return answers;
}
let answer = bottom;
const step = this.get('_scaleStep') || 1;
const step = this.get('_scaleStep');
while (answer <= top) {
answers.push(answer);
answer += step;
Expand Down
2 changes: 1 addition & 1 deletion js/SliderView.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SliderView extends QuestionView {
// this shoud give the index of item using given slider value
getIndexFromValue(value) {
value = parseFloat(value);
const step = this.model.get('_scaleStep') ?? 1;
const step = this.model.get('_scaleStep');
return this.model.get('_items').reduce((found, item) => {
if (found) return found;
if (item.value === value) return item;
Expand Down
2 changes: 1 addition & 1 deletion properties.schema
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
"_scaleEnd": {
"type": "number",
"required": true,
"default": 1,
"default": 10,
"title": "Scale End",
"inputType": "Number",
"validators": ["required", "number"],
Expand Down
5 changes: 3 additions & 2 deletions schema/component.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,14 @@
"_scaleEnd": {
"type": "number",
"title": "Scale end number",
"default": 1
"default": 10
},
"_scaleStep": {
"type": "number",
"title": "Scale step",
"description": "The amount the scale should increment by",
"default": 1
"default": 1,
"exclusiveMinimum": 0
},
"scaleStepPrefix": {
"type": "string",
Expand Down

0 comments on commit a85cdaf

Please sign in to comment.