Skip to content

Commit

Permalink
Adds loading of scrollspy only if page id unequal 1
Browse files Browse the repository at this point in the history
  • Loading branch information
mwehr authored and abias committed Jan 7, 2024
1 parent 9aa84e0 commit c2fa1bc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 42 deletions.
11 changes: 10 additions & 1 deletion amd/build/scrollspy.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion amd/build/scrollspy.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 37 additions & 38 deletions amd/src/scrollspy.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,54 +34,53 @@
* (See original implementation: https://raw.githubusercontent.com/dbnschools/moodle-theme_fordson/master/javascript/scrollspy.js)
*/
const initScrollSpy = () => {
// Unfortunately the editmode-switch carries no unique ID
let editToggle = document.querySelector('form.editmode-switch-form');

if (!editToggle) {
// Do not continue when there is no edit toggle.
return;
}
// Check if .section.main exist.
if (document.querySelector('.section.main')) {
// Unfortunately the editmode-switch carries no unique ID
let editToggle = document.querySelector('form.editmode-switch-form');
if (!editToggle) {
// Do not continue when there is no edit toggle.
return;
}
editToggle.addEventListener('click', () => {

editToggle.addEventListener('click', () => {
window.sessionStorage.setItem('edittoggled', true);

window.sessionStorage.setItem('edittoggled', true);
let viewporttop = window.scrollY;
let closest = null;
let closestoffset = null;

let viewporttop = window.scrollY;
let closest = null;
let closestoffset = null;
document.querySelectorAll('.section.main').forEach((node) => {
let thisoffset = node.offsetTop;

document.querySelectorAll('.section.main').forEach((node) => {
let thisoffset = node.offsetTop;
if (closest && closest.offsetTop) {
closestoffset = closest.offsetTop;
}
if (closest === null || Math.abs(thisoffset - viewporttop) < Math.abs(closestoffset - viewporttop)) {
closest = node;
}
});

if (closest && closest.offsetTop) {
closestoffset = closest.offsetTop;
}
if (closest === null || Math.abs(thisoffset - viewporttop) < Math.abs(closestoffset - viewporttop)) {
closest = node;
}
window.sessionStorage.setItem('closestid', closest.id);
window.sessionStorage.setItem('closestdelta', viewporttop - closest.offsetTop);
});
let edittoggled = window.sessionStorage.getItem('edittoggled');
if (edittoggled) {

window.sessionStorage.setItem('closestid', closest.id);
window.sessionStorage.setItem('closestdelta', viewporttop - closest.offsetTop);
});
let closestid = window.sessionStorage.getItem('closestid');
let closestdelta = window.sessionStorage.getItem('closestdelta');

let edittoggled = window.sessionStorage.getItem('edittoggled');
if (closestid && closestdelta) {
let closest = document.getElementById(closestid);
let y = closest.offsetTop + parseInt(closestdelta);

if (edittoggled) {

let closestid = window.sessionStorage.getItem('closestid');
let closestdelta = window.sessionStorage.getItem('closestdelta');

if (closestid && closestdelta) {
let closest = document.getElementById(closestid);
let y = closest.offsetTop + parseInt(closestdelta);
window.scrollTo(0, y);
}

window.scrollTo(0, y);
window.sessionStorage.removeItem('edittoggled');
window.sessionStorage.removeItem('closestid');
window.sessionStorage.removeItem('closestdelta');
}

window.sessionStorage.removeItem('edittoggled');
window.sessionStorage.removeItem('closestid');
window.sessionStorage.removeItem('closestdelta');
}
};

Expand All @@ -101,5 +100,5 @@ const docReady = (callback) => {
};

export const init = () => {
docReady(initScrollSpy());
docReady(initScrollSpy);
};
5 changes: 3 additions & 2 deletions layout/includes/scrollspy.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@

$scrollspy = get_config('theme_boost_union', 'scrollspy');

// Add scroll-spy AMD module if the feature is enabled.
if ($scrollspy == THEME_BOOST_UNION_SETTING_SELECT_YES) {
// Add scroll-spy AMD module if we are on a course page (but not the frontpage) and the feature is enabled.
if ($PAGE->pagelayout == 'course' && $PAGE->course->id != SITEID &&
$scrollspy == THEME_BOOST_UNION_SETTING_SELECT_YES) {
$PAGE->requires->js_call_amd('theme_boost_union/scrollspy', 'init');
}

0 comments on commit c2fa1bc

Please sign in to comment.