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(redwood): receive XBlock visibility from the Learning MFE #688

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
46 changes: 44 additions & 2 deletions lms/static/completion/js/ViewedEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ export class ViewedEventTracker {
constructor() {
this.elementViewings = new Set();
this.handlers = [];
this.registerDomHandlers();
if (window === window.parent) {
// Preview (legacy LMS frontend).
this.registerDomHandlers();
} else {
// Learning MFE.
window.addEventListener('message', this.handleVisibilityMessage.bind(this));
}
}

/** Add an element to track. */
Expand All @@ -121,7 +127,11 @@ export class ViewedEventTracker {
(el, event) => this.callHandlers(el, event),
),
);
this.updateVisible();
// Update visibility status immediately after adding the element (in case it's already visible).
// We don't need this for the Learning MFE because it will send a message once the iframe is loaded.
if (window === window.parent) {
this.updateVisible();
}
}

/** Register a new handler to be called when an element has been viewed. */
Expand Down Expand Up @@ -177,4 +187,36 @@ export class ViewedEventTracker {
handler(el, event);
});
}

/** Handle a unit.visibilityStatus message from the Learning MFE. */
handleVisibilityMessage(event) {
if (event.data.type === 'unit.visibilityStatus') {
const { topPosition, viewportHeight } = event.data.data;

this.elementViewings.forEach((elv) => {
const rect = elv.getBoundingRect();
let visible = false;

// Convert iframe-relative rect coordinates to be relative to the parent's viewport.
const elTopPosition = rect.top + topPosition;
const elBottomPosition = rect.bottom + topPosition;

// Check if the element is visible in the parent's viewport.
if (elTopPosition < viewportHeight && elTopPosition >= 0) {
elv.markTopSeen();
visible = true;
}
if (elBottomPosition <= viewportHeight && elBottomPosition > 0) {
elv.markBottomSeen();
visible = true;
}

if (visible) {
elv.handleVisible();
} else {
elv.handleNotVisible();
}
});
}
}
}
Loading