From 02305870bd86b3b628cd222b20d7596666625667 Mon Sep 17 00:00:00 2001 From: danielghost Date: Tue, 21 May 2024 14:00:37 +0100 Subject: [PATCH 1/3] Fix: added default attribute values for `_scaleStart`, `_scaleEnd` and `_scaleStep`. Changed `_scaleEnd` schema default from 1 to 10. (fixes #204). --- README.md | 6 +++--- js/SliderModel.js | 3 +++ properties.schema | 2 +- schema/component.schema.json | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a882312..976f662 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/js/SliderModel.js b/js/SliderModel.js index 2417326..3ad4e1e 100644 --- a/js/SliderModel.js +++ b/js/SliderModel.js @@ -5,6 +5,9 @@ export default class SliderModel extends QuestionModel { defaults() { return QuestionModel.resultExtend('defaults', { + _scaleStart: 1, + _scaleEnd: 10, + _scaleStep: 1, _showScale: true, _showScaleNumbers: true, _showScaleIndicator: true, diff --git a/properties.schema b/properties.schema index 13f105a..3efc93a 100644 --- a/properties.schema +++ b/properties.schema @@ -187,7 +187,7 @@ "_scaleEnd": { "type": "number", "required": true, - "default": 1, + "default": 10, "title": "Scale End", "inputType": "Number", "validators": ["required", "number"], diff --git a/schema/component.schema.json b/schema/component.schema.json index b660cbe..7783a01 100644 --- a/schema/component.schema.json +++ b/schema/component.schema.json @@ -116,7 +116,7 @@ "_scaleEnd": { "type": "number", "title": "Scale end number", - "default": 1 + "default": 10 }, "_scaleStep": { "type": "number", From bee1e3bae02e14e6dba251449edf7d277522c80b Mon Sep 17 00:00:00 2001 From: danielghost Date: Tue, 21 May 2024 15:28:19 +0100 Subject: [PATCH 2/3] Removed `_scaleStep` fallbacks as these are no longer needed with a default model value. --- js/SliderModel.js | 4 ++-- js/SliderView.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/js/SliderModel.js b/js/SliderModel.js index 3ad4e1e..1d907fa 100644 --- a/js/SliderModel.js +++ b/js/SliderModel.js @@ -52,7 +52,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); @@ -180,7 +180,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; diff --git a/js/SliderView.js b/js/SliderView.js index f721b80..5313c8b 100644 --- a/js/SliderView.js +++ b/js/SliderView.js @@ -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; From ae6f90511455618b1ffdaec36b2cbab077c76463 Mon Sep 17 00:00:00 2001 From: danielghost Date: Wed, 22 May 2024 18:00:42 +0100 Subject: [PATCH 3/3] Restrict `_scaleStep` to a positive number, to prevent errors. --- js/SliderModel.js | 7 ++++++- schema/component.schema.json | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/js/SliderModel.js b/js/SliderModel.js index 1d907fa..4dbb013 100644 --- a/js/SliderModel.js +++ b/js/SliderModel.js @@ -1,5 +1,6 @@ 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 { @@ -17,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(); } diff --git a/schema/component.schema.json b/schema/component.schema.json index 7783a01..4ca19f7 100644 --- a/schema/component.schema.json +++ b/schema/component.schema.json @@ -122,7 +122,8 @@ "type": "number", "title": "Scale step", "description": "The amount the scale should increment by", - "default": 1 + "default": 1, + "exclusiveMinimum": 0 }, "scaleStepPrefix": { "type": "string",