Browse Source

[matrix] Refactor docs JS

* Refactor custom.js

* Refactor scorer.js

* tables variable shoudn't be in global scope
pull/6176/head
Josh 5 years ago
committed by Rapptz
parent
commit
70c73084b3
  1. 63
      docs/_static/custom.js
  2. 71
      docs/_static/scorer.js

63
docs/_static/custom.js

@ -1,34 +1,11 @@
$(document).ready(function () {
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');
}
'use-strict';
});
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]');
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');
}
});

71
docs/_static/scorer.js

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

Loading…
Cancel
Save