Skip to content

Commit

Permalink
Fix: Keep active item position on save/restore (fixes #295) (#296)
Browse files Browse the repository at this point in the history
* Fix: Keep active item position on save/restore (fixes #295)

* Fix: storeUserAnswer on setActiveItem

Moved from adaptlearning/adapt-contrib-core#494

* Revert

* FW bump for core dependency

---------

Co-authored-by: joe-allen-89 <[email protected]>
  • Loading branch information
oliverfoster and joe-allen-89 authored Feb 5, 2024
1 parent 5f0df20 commit 1c0e9d7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "adapt-contrib-narrative",
"version": "7.8.0",
"framework": ">=5.31.2",
"framework": ">=5.35.6",
"homepage": "https://github.com/adaptlearning/adapt-contrib-narrative",
"bugs": "https://github.com/adaptlearning/adapt-contrib-narrative/issues",
"component": "narrative",
Expand Down
23 changes: 22 additions & 1 deletion js/NarrativeModel.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
import ItemsComponentModel from 'core/js/models/itemsComponentModel';

export default class NarrativeModel extends ItemsComponentModel {}
export default class NarrativeModel extends ItemsComponentModel {
restoreUserAnswers() {
const numberArray = this.get('_userAnswer');
if (!numberArray) return;
const children = this.getChildren();
const shouldRestoreActiveItem = (numberArray.length > children.length);
if (shouldRestoreActiveItem) {
const activeItemIndex = numberArray.pop();
this.setActiveItem(activeItemIndex);
}
children.forEach(child => child.set('_isVisited', Boolean(numberArray[child.get('_index')])));
}

storeUserAnswer() {
const items = this.getChildren().slice(0);
items.sort((a, b) => a.get('_index') - b.get('_index'));
const numberArray = items.map(child => child.get('_isVisited') ? 1 : 0);
const activeItem = this.getActiveItem();
if (activeItem) numberArray.push(activeItem.get('_index'));
this.set('_userAnswer', numberArray);
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "adapt-contrib-narrative",
"version": "7.8.0",
"framework": ">=5.31.2",
"framework": ">=5.35.6",
"homepage": "https://github.com/adaptlearning/adapt-contrib-narrative",
"bugs": "https://github.com/adaptlearning/adapt-contrib-narrative/issues",
"component": "narrative",
Expand Down

0 comments on commit 1c0e9d7

Please sign in to comment.