Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: missing attribute values causing runtime error #205

Merged
merged 3 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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