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. 51
      docs/_static/custom.js
  2. 57
      docs/_static/scorer.js

51
docs/_static/custom.js

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

57
docs/_static/scorer.js

@ -1,13 +1,16 @@
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);
@ -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) => {
score: function(result) {
// only inflate the score of things that are actual API reference things // only inflate the score of things that are actual API reference things
if(_pattern !== null && result[1].startsWith('discord.')) { const [, title, , , score] = result;
let _score = __score(result[1], _pattern);
if (pattern !== null && title.startsWith('discord.')) {
let _score = __score(title, pattern);
if (_score === Number.MAX_VALUE) { if (_score === Number.MAX_VALUE) {
return result[4]; return score;
} }
if(_beenScored.has(result[1])) { if (beenScored.has(title)) {
return 0; return 0;
} }
_beenScored.add(result[1]); beenScored.add(title);
let newScore = 100 + _queryBeingDone.length - _score; let newScore = 100 + queryBeingDone.length - _score;
// console.log(`${result[1]}: ${result[4]} -> ${newScore} (${_score})`); // console.log(`${title}: ${score} -> ${newScore} (${_score})`);
return newScore; return newScore;
} }
return result[4]; return score;
}, },
// 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: {
0: 15, // used to be importantResults
1: 7, // used to be objectResults 1: 7, // used to be objectResults
2: -5}, // used to be unimportantResults 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