From 1c0e9d7eb9108bdd99b3ec73c2c0cc40be316546 Mon Sep 17 00:00:00 2001 From: Oliver Foster Date: Mon, 5 Feb 2024 11:10:11 +0000 Subject: [PATCH] Fix: Keep active item position on save/restore (fixes #295) (#296) * Fix: Keep active item position on save/restore (fixes #295) * Fix: storeUserAnswer on setActiveItem Moved from https://github.com/adaptlearning/adapt-contrib-core/pull/494 * Revert * FW bump for core dependency --------- Co-authored-by: joe-allen-89 <85872286+joe-allen-89@users.noreply.github.com> --- bower.json | 2 +- js/NarrativeModel.js | 23 ++++++++++++++++++++++- package.json | 2 +- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/bower.json b/bower.json index 44c4015..9e86576 100644 --- a/bower.json +++ b/bower.json @@ -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", diff --git a/js/NarrativeModel.js b/js/NarrativeModel.js index 4b45ec0..80adfd7 100644 --- a/js/NarrativeModel.js +++ b/js/NarrativeModel.js @@ -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); + } +} diff --git a/package.json b/package.json index 44c4015..9e86576 100644 --- a/package.json +++ b/package.json @@ -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",