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: linkedModel values not correctly setup on scale when inherited #111

Merged
merged 2 commits into from
May 30, 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`.

**\_showNumber** (boolean): When set to `true`, a numeric value appears on the marker described in **\_showScaleIndicator**. The value indicates the slider's position on the scale. The default is `true`. Note that **\_showScaleIndicator** must be set to `true` in order for this to work.

Expand Down
28 changes: 16 additions & 12 deletions js/ConfidenceSliderModel.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import Adapt from 'core/js/adapt';
import data from 'core/js/data';
import logging from 'core/js/logging';
import BUTTON_STATE from 'core/js/enums/buttonStateEnum';
import SliderModel from 'components/adapt-contrib-slider/js/SliderModel';
import logging from 'core/js/logging';

export default class ConfidenceSliderModel extends SliderModel {

init() {
super.init();
if (!this.get('_linkedToId')) return;
this.set('originalBody', this.get('body'));
this.setupEventListeners();
}

setupEventListeners() {
this.listenToOnce(Adapt, 'adapt:initialize', this._setupLinkedModel);
}

/* override */
Expand All @@ -25,10 +21,11 @@ export default class ConfidenceSliderModel extends SliderModel {

/* override to indicate that all options are correct */
setupModelItems() {
this._setupLinkedModel();
const items = [];
const start = this.get('_scaleStart') ?? 1;
const end = this.get('_scaleEnd') ?? 1;
const step = this.get('_scaleStep') || 1;
const start = this.get('_scaleStart');
const end = this.get('_scaleEnd');
const step = this.get('_scaleStep');
for (let i = start; i <= end; i += step) {
items.push({
value: i,
Expand All @@ -45,6 +42,11 @@ export default class ConfidenceSliderModel extends SliderModel {
});
}

onAdaptInitialize() {
this.updateFromLinkedModel();
super.onAdaptInitialize();
}

/* override */
canSubmit() {
return (!this.linkedModel || this.linkedModel.get('_isSubmitted'));
Expand Down Expand Up @@ -116,6 +118,7 @@ export default class ConfidenceSliderModel extends SliderModel {
}

updateFromLinkedModel() {
if (!this.linkedModel) return;
const isSubmitted = this.linkedModel.get('_isSubmitted');
this.set('body', isSubmitted ? this.get('originalBody') : this.get('disabledBody'));
this.set('_isEnabled', isSubmitted);
Expand All @@ -130,9 +133,11 @@ export default class ConfidenceSliderModel extends SliderModel {
}

_setupLinkedModel() {
this.linkedModel = Adapt.components.findWhere({ _id: this.get('_linkedToId') });
const linkedToId = this.get('_linkedToId');
if (!linkedToId) return;
this.linkedModel = data.findById(linkedToId);
if (!this.linkedModel) {
return logging.error('Please check that you have set _linkedToId correctly!');
return logging.error(`Could not find \`_linkedToId: ${linkedToId}\` for ${this.get('_id')}`);
}
if (this.linkedModel.get('_component') !== 'confidenceSlider') {
return logging.warn(`The component you have linked confidenceSlider ${this.get('_id')} to is not a confidenceSlider component!`);
Expand All @@ -148,7 +153,6 @@ export default class ConfidenceSliderModel extends SliderModel {
_scaleEnd: this.linkedModel.get('_scaleEnd')
});
this._listenToLinkedModel();
this.updateFromLinkedModel();
if (this.get('_attempts') < 0) this.linkedModel.set('_attempts', 1);
}

Expand Down
2 changes: 1 addition & 1 deletion properties.schema
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
"_scaleEnd": {
"type": "number",
"required": true,
"default": 1,
"default": 10,
"title": "Scale End",
"inputType": "Number",
"validators": ["required", "number"],
Expand Down
3 changes: 2 additions & 1 deletion schema/component.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,15 @@
"type": "number",
"title": "Scale End",
"description": "What number the scale should end on",
"default": 1,
"default": 10,
"properties": {}
},
"_scaleStep": {
"type": "number",
"title": "Scale Step",
"description": "The amount the scale should increment by",
"default": 1,
"exclusiveMinimum": 0,
"properties": {}
},
"_correctAnswer": {
Expand Down