Browse Source

[matrix] Refactor docs JS

* Refactor custom.js

* Refactor scorer.js

* tables variable shoudn't be in global scope
pull/4143/head
Josh 5 years ago
committed by GitHub
parent
commit
1e471b64e6
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 63
      docs/_static/custom.js
  2. 71
      docs/_static/scorer.js

63
docs/_static/custom.js

@ -1,34 +1,11 @@
$(document).ready(function () { 'use-strict';
var sections = $('div.section');
var activeLink = null;
var bottomHeightThreshold = $(document).height() - 30;
$(window).scroll(function (event) {
var distanceFromTop = $(this).scrollTop();
var currentSection = null;
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');
}
}); let activeLink = null;
let bottomHeightThreshold, sections;
document.addEventListener('DOMContentLoaded', () => {
bottomHeightThreshold = document.documentElement.scrollHeight - 30;
sections = document.querySelectorAll('div.section');
const tables = document.querySelectorAll('.py-attribute-table[data-move-to-id]'); const tables = document.querySelectorAll('.py-attribute-table[data-move-to-id]');
tables.forEach(table => { tables.forEach(table => {
@ -38,3 +15,29 @@ $(document).ready(function () {
parent.insertBefore(table, element.nextSibling); 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');
}
});

71
docs/_static/scorer.js

@ -1,17 +1,20 @@
var _queryBeingDone = null; 'use-strict';
var _pattern = null;
var _escapedRegex = /[-\/\\^$*+?.()|[\]{}]/g; let queryBeingDone = null;
let pattern = null;
const escapedRegex = /[-\/\\^$*+?.()|[\]{}]/g;
function escapeRegex(e) { function escapeRegex(e) {
return e.replace(_escapedRegex, '\\$&'); return e.replace(escapedRegex, '\\$&');
} }
// for some reason Sphinx shows some entries twice // for some reason Sphinx shows some entries twice
// if something has been scored already I'd rather sort it to the bottom // if something has been scored already I'd rather sort it to the bottom
var _beenScored = new Set(); const beenScored = new Set();
function __score(haystack, regex) { function __score(haystack, regex) {
let match = regex.exec(haystack); let match = regex.exec(haystack);
if(match == null) { if (match == null) {
return Number.MAX_VALUE; return Number.MAX_VALUE;
} }
let subLength = match[0].length; let subLength = match[0].length;
@ -24,38 +27,41 @@ function __cleanNamespaces(query) {
return query.replace(/(discord\.(ext\.)?)?(.+)/, '$3'); return query.replace(/(discord\.(ext\.)?)?(.+)/, '$3');
} }
var Scorer = { Scorer = {
// Implement the following function to further tweak the score for each result // Implement the following function to further tweak the score for each result
// The function takes a result array [filename, title, anchor, descr, score] // The function takes a result array [filename, title, anchor, descr, score]
// and returns the new score. // and returns the new score.
score: (result) => {
// only inflate the score of things that are actual API reference things
const [, title, , , score] = result;
score: function(result) { if (pattern !== null && title.startsWith('discord.')) {
// only inflate the score of things that are actual API reference things let _score = __score(title, pattern);
if(_pattern !== null && result[1].startsWith('discord.')) { if (_score === Number.MAX_VALUE) {
let _score = __score(result[1], _pattern); return score;
if(_score === Number.MAX_VALUE) { }
return result[4]; if (beenScored.has(title)) {
} return 0;
if(_beenScored.has(result[1])) { }
return 0; beenScored.add(title);
let newScore = 100 + queryBeingDone.length - _score;
// console.log(`${title}: ${score} -> ${newScore} (${_score})`);
return newScore;
} }
_beenScored.add(result[1]); return score;
let newScore = 100 + _queryBeingDone.length - _score;
// console.log(`${result[1]}: ${result[4]} -> ${newScore} (${_score})`);
return newScore;
}
return result[4];
}, },
// query matches the full name of an object // query matches the full name of an object
objNameMatch: 15, objNameMatch: 15,
// or matches in the last dotted part of the object name // or matches in the last dotted part of the object name
objPartialMatch: 11, objPartialMatch: 11,
// Additive scores depending on the priority of the object // Additive scores depending on the priority of the object
objPrio: {0: 15, // used to be importantResults objPrio: {
1: 7, // used to be objectResults 0: 15, // used to be importantResults
2: -5}, // used to be unimportantResults 1: 7, // used to be objectResults
2: -5 // used to be unimportantResults
},
// Used when the priority is not in the mapping. // Used when the priority is not in the mapping.
objPrioDefault: 0, objPrioDefault: 0,
@ -67,12 +73,11 @@ var Scorer = {
partialTerm: 2 partialTerm: 2
}; };
document.addEventListener('DOMContentLoaded', () => {
$(document).ready(function() { const params = new URLSearchParams(window.location.search);
let params = $.getQueryParameters(); queryBeingDone = params.get('q');
if(params.q) { if (queryBeingDone) {
_queryBeingDone = params.q[0]; let pattern = Array.from(queryBeingDone).map(escapeRegex).join('.*?');
let pattern = Array.from(_queryBeingDone).map(escapeRegex).join('.*?'); pattern = new RegExp(pattern, 'i');
_pattern = new RegExp(pattern, 'i');
} }
}); });

Loading…
Cancel
Save