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: respect locked content when returning to location (fixes #51) #71

Merged
merged 6 commits into from
Jun 20, 2024
Merged
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
40 changes: 36 additions & 4 deletions js/adapt-contrib-bookmarking.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ class Bookmarking extends Backbone.Controller {
resumeButtonAriaLabel
} = this.globals._extensions._bookmarking;
const $target = $(event.target);
const label = $target.attr('label') || $target.html() || resumeButtonText || null;
const ariaLabel = $target.attr('aria-label') || resumeButtonAriaLabel || null;
const label = $target.attr('label') || $target.html() || resumeButtonText || null;
const ariaLabel = $target.attr('aria-label') || resumeButtonAriaLabel || null;
const _location = $target.attr('location') || this.location;
const id = this.getLocationId(_location);
const isDisabled = ['', undefined, 'current'].includes(id);
Expand Down Expand Up @@ -171,15 +171,47 @@ class Bookmarking extends Backbone.Controller {
});
}

/**
* If the bookmark is preceded by locked content then find the nearest appropriate location
* @param {string} id the bookmarked location
* @returns the identifier of the location to return the user
*/
resolveLocking(id) {
const model = data.findById(id);

const isCourse = model.isTypeGroup('course');
if (isCourse) return id;

const isContentObject = model.isTypeGroup('contentobject');
// do not allow navigation to a locked page/menu
const isLockedContentObject = isContentObject && model.get('_isLocked');

// bookmark is by contentobject or article/block/component
const descendants = isContentObject
? Adapt.course.getAllDescendantModels(true).filter(descendant => descendant.isTypeGroup('contentobject'))
: model.findAncestor('page').getAllDescendantModels(true);

const precedingModels = descendants.slice(0, descendants.indexOf(model));
const isPrecededByLockedContent = precedingModels.some(precedingModel => precedingModel.get('_isLocked'));

if (!isPrecededByLockedContent && !isLockedContentObject) return id;

const nearestUnlockedIndex = precedingModels.reverse().findIndex(precedingModel => !precedingModel.get('_isLocked'));
return precedingModels[nearestUnlockedIndex]?.get('_id');
}

navigateTo(location = this.location) {
const id = this.getLocationId(location);
if (['', undefined, 'current'].includes(id)) return;
const target = this.resolveLocking(id);
if (!target) return;
_.defer(async () => {
try {

const isSinglePage = (Adapt.contentObjects.models.length === 1);
await router.navigateToElement(id, { trigger: true, replace: isSinglePage, duration: 400 });
await router.navigateToElement(target, { trigger: true, replace: isSinglePage, duration: 400 });
} catch (err) {
logging.warn(`Bookmarking cannot navigate to id: ${id}\n`, err);
logging.warn(`Bookmarking cannot navigate to id: ${target}\n`, err);
}
});
this.stopListening(Adapt, 'bookmarking:cancel');
Expand Down