Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 44 additions & 30 deletions doxyconfig-readthedocs-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,23 @@ class ReadtheDocsSearch {
}

static init() {
const originalSearchBox = globalThis.SearchBox;
const realSearchBox = globalThis.SearchBox;
globalThis.SearchBox = function(name, resultsPath, extension) {
originalSearchBox.call(this, name, resultsPath, extension);
if (realSearchBox) {
realSearchBox.call(this, name, resultsPath, extension);
}

this.OnSearchFieldFocus = function() {};

const originalCloseResultsWindow = this.CloseResultsWindow.bind(this);
const originalClose = this.CloseResultsWindow;
this.CloseResultsWindow = function() {
const field = this.DOMSearchField();
if (originalClose) {
originalClose.call(this);
}
const field = this.DOMSearchField ? this.DOMSearchField() : null;
if (field?.id === document.activeElement?.id) {
return;
}
originalCloseResultsWindow();
};
};

Expand All @@ -47,7 +51,14 @@ class ReadtheDocsSearch {
field.focus();
}
});
observer.observe(document.body, { childList: true, subtree: true });

if (document.body) {
observer.observe(document.body, { childList: true, subtree: true });
} else {
document.addEventListener('DOMContentLoaded', function() {
observer.observe(document.body, { childList: true, subtree: true });
});
}

document.addEventListener('focusin', function(e) {
if (e.target?.id === 'MSearchField') {
Expand Down Expand Up @@ -94,20 +105,20 @@ class ReadtheDocsSearch {
let projectSlug = ReadtheDocsSearch.getMetaValue("readthedocs-project-slug") || "doxyconfig";
let projectVersion = ReadtheDocsSearch.getMetaValue("readthedocs-version-slug") || "latest";

// pull requests are not indexed, so use the default version
if (/^\d+$/.test(projectVersion)) {
console.log('Pull request detected, getting default version from ReadTheDocs API');
ReadtheDocsSearch.getReadTheDocsDefaultVersion(projectSlug);
}

let url = `${ReadtheDocsSearch.serverUrl}search/?q=project:${projectSlug}/${projectVersion}+${query}&page=${page + 1}&page_size=${count}`;
console.log(url);

const ctx = {
query, resultSummary, resultList, titleInterval, pageTitle, originalTitle, firstUrl: true
};

ReadtheDocsSearch.fetchResults(url, ctx);
// pull requests are not indexed, so use the default version
const versionReady = /^\d+$/.test(projectVersion)
? ReadtheDocsSearch.getReadTheDocsDefaultVersion(projectSlug)
: Promise.resolve(projectVersion);

versionReady.then(function(resolvedVersion) {
const url = `${ReadtheDocsSearch.serverUrl}search/?q=project:${projectSlug}/${resolvedVersion}+${query}&page=${page + 1}&page_size=${count}`;
console.log(url);
ReadtheDocsSearch.fetchResults(url, ctx);
});
}
}

Expand Down Expand Up @@ -192,19 +203,22 @@ class ReadtheDocsSearch {
}

static getReadTheDocsDefaultVersion(project) {
let url = `${ReadtheDocsSearch.serverUrl}projects/${project}/`;
$.ajax({
url: url,
dataType: 'json',
success: function(data) {
console.log(data);
return data.default_version;
},
error: function(jqXHR, textStatus, errorThrown) {
console.error('Error:', textStatus, errorThrown);
console.log(`Cannot determine default version for ${project}, assuming "latest"`);
return "latest";
}
})
console.log('Pull request detected, getting default version from ReadTheDocs API');
const url = `${ReadtheDocsSearch.serverUrl}projects/${project}/`;
return new Promise(function(resolve) {
$.ajax({
url: url,
dataType: 'json',
success: function(data) {
console.log(data);
resolve(data.default_version || 'latest');
},
error: function(jqXHR, textStatus, errorThrown) {
console.error('Error:', textStatus, errorThrown);
console.log(`Cannot determine default version for ${project}, assuming "latest"`);
resolve('latest');
}
});
});
}
}
Loading