[matrix] Refactor docs JS

* Refactor custom.js

* Refactor scorer.js

* tables variable shoudn't be in global scope
This commit is contained in:
Josh
2020-05-27 14:56:38 +10:00
committed by Rapptz
parent 51f6787692
commit 70c73084b3
2 changed files with 70 additions and 62 deletions

View File

@ -1,34 +1,11 @@
$(document).ready(function () {
var sections = $('div.section');
var activeLink = null;
var bottomHeightThreshold = $(document).height() - 30;
'use-strict';
$(window).scroll(function (event) {
var distanceFromTop = $(this).scrollTop();
var currentSection = null;
let activeLink = null;
let bottomHeightThreshold, sections;
if(distanceFromTop + window.innerHeight > bottomHeightThreshold) {
currentSection = $(sections[sections.length - 1]);
}
else {
sections.each(function () {
var section = $(this);
if (section.offset().top - 1 < distanceFromTop) {
currentSection = section;
}
});
}
if (activeLink) {
activeLink.parent().removeClass('active');
}
if (currentSection) {
activeLink = $('.sphinxsidebar a[href="#' + currentSection.attr('id') + '"]');
activeLink.parent().addClass('active');
}
});
document.addEventListener('DOMContentLoaded', () => {
bottomHeightThreshold = document.documentElement.scrollHeight - 30;
sections = document.querySelectorAll('div.section');
const tables = document.querySelectorAll('.py-attribute-table[data-move-to-id]');
tables.forEach(table => {
@ -38,3 +15,29 @@ $(document).ready(function () {
parent.insertBefore(table, element.nextSibling);
});
});
window.addEventListener('scroll', () => {
let currentSection = null;
if (window.scrollY + window.innerHeight > bottomHeightThreshold) {
currentSection = sections[sections.length - 1];
}
else {
sections.forEach(section => {
let rect = section.getBoundingClientRect();
if (rect.top + document.body.scrollTop - 1 < window.scrollY) {
currentSection = section;
}
});
}
if (activeLink) {
activeLink.parentElement.classList.remove('active');
}
if (currentSection) {
activeLink = document.querySelector(`.sphinxsidebar a[href="#${currentSection.id}"]`);
activeLink.parentElement.classList.add('active');
}
});