Skip to content

Commit

Permalink
Fix: Keep active item position on save/restore (fixes #295)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverfoster authored Jan 31, 2024
1 parent bba5e20 commit b8367cf
Showing 1 changed file with 22 additions and 1 deletion.
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);
}
}

0 comments on commit b8367cf

Please sign in to comment.