mirror of
https://github.com/danbulant/api_docs
synced 2026-05-19 04:08:40 +00:00
Reposition headers based on score
Enforce min score
This commit is contained in:
parent
adcf6c7099
commit
2384d9af3f
1 changed files with 45 additions and 3 deletions
|
|
@ -42,6 +42,23 @@
|
|||
.on('blur', inactive);
|
||||
}
|
||||
|
||||
function refToHeader (itemRef) {
|
||||
return $('.tocify-item[data-unique=' + itemRef + ']').closest('.tocify-header');
|
||||
}
|
||||
|
||||
function sortDescending (obj2, obj1) {
|
||||
var s1 = parseInt(obj1.id.replace(/[^\d]/g, ''), 10);
|
||||
var s2 = parseInt(obj2.id.replace(/[^\d]/g, ''), 10);
|
||||
return s1 === s2 ? 0 : s1 < s2 ? -1 : 1;
|
||||
}
|
||||
|
||||
function resetHeaderLocations () {
|
||||
var headers = $(".tocify-header").sort(sortDescending);
|
||||
$.each(headers, function (index, item) {
|
||||
$(item).insertBefore($("#toc ul:first-child"));
|
||||
});
|
||||
}
|
||||
|
||||
function search (event) {
|
||||
var sections = $('section, #toc .tocify-header');
|
||||
|
||||
|
|
@ -53,12 +70,37 @@
|
|||
|
||||
if (this.value) {
|
||||
sections.hide();
|
||||
var results = index.search(this.value);
|
||||
// results are sorted by score in descending order
|
||||
var tmpResults = index.search(this.value);
|
||||
var results = [];
|
||||
|
||||
// remove low score matches
|
||||
$.each(tmpResults, function (index, item) {
|
||||
if (item.score >= 0.0001) results.push(item);
|
||||
});
|
||||
|
||||
if (results.length) {
|
||||
lastRef = null;
|
||||
resetHeaderLocations();
|
||||
$.each(results, function (index, item) {
|
||||
$('#section-' + item.ref).show();
|
||||
$('.tocify-item[data-unique=' + item.ref + ']').closest('.tocify-header').show();
|
||||
var itemRef = item.ref;
|
||||
$('#section-' + itemRef).show();
|
||||
// headers must be repositioned in the DOM
|
||||
var closestHeader = refToHeader(itemRef);
|
||||
if (lastRef) {
|
||||
refToHeader(lastRef).insertBefore(closestHeader);
|
||||
}
|
||||
closestHeader.show();
|
||||
lastRef = itemRef;
|
||||
});
|
||||
|
||||
// position first element. it wasn't positioned above if len > 1
|
||||
if (results.length > 1) {
|
||||
var firstRef = results[0].ref;
|
||||
var secondRef = results[1].ref
|
||||
refToHeader(firstRef).insertBefore(refToHeader(secondRef));
|
||||
}
|
||||
|
||||
highlight.call(this);
|
||||
} else {
|
||||
sections.show();
|
||||
|
|
|
|||
Loading…
Reference in a new issue