Skip to content

Commit

Permalink
Load jquery if isn't found
Browse files Browse the repository at this point in the history
Sphinx is removing jquery on the 6.x version.
Our theme depends a lot on jquery.
  • Loading branch information
stsewd committed Jun 21, 2022
1 parent 0da22b8 commit 86bcbd7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
10 changes: 5 additions & 5 deletions docs/_static/debug.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Add debug actions to flyout menu

$(function () {
$("[data-toggle='rst-debug-badge']").on("click", function () {
// Add debug actions to flyout menu.
document.addEventListener("DOMContentLoaded", function () {
let element = document.querySelector("[data-toggle='rst-debug-badge']");
element.addEventListener("click", function () {
$("[data-toggle='rst-versions']").toggleClass("rst-badge");
});
})
});
4 changes: 2 additions & 2 deletions sphinx_rtd_theme/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@
{% include "versions.html" -%}

<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable({{ 'true' if theme_sticky_navigation|tobool else 'false' }});
document.addEventListener("DOMContentLoaded", function () {
SphinxRtdTheme.Navigation.enable({{ 'true' if theme_sticky_navigation|tobool else 'false' }});
});
</script>

Expand Down
4 changes: 3 additions & 1 deletion sphinx_rtd_theme/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
{%- endblock %}
{% block footer %}
<script>
jQuery(function() { Search.loadIndex("{{ pathto('searchindex.js', 1) }}"); });
document.addEventListener("DOMContentLoaded", function () {
Search.loadIndex("{{ pathto('searchindex.js', 1) }}");
});
</script>
{# this is used when loading the search index using $.ajax fails,
such as on Chrome for documents on localhost #}
Expand Down
2 changes: 1 addition & 1 deletion sphinx_rtd_theme/static/js/theme.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 20 additions & 4 deletions src/theme.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
var jQuery = (typeof(window) != 'undefined') ? window.jQuery : require('jquery');
/**
* Inject jquery if isn't present already.
*
* @param {function} fn - Function to be executed after jquery has been loaded.
*/
function injectJQuery(fn) {
if (!window.jQuery) {
console.log("JQuery not found. Injecting.");
var script = document.createElement("script");
script.type = 'text/javascript';
script.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js";
script.onload = fn;
document.head.appendChild(script);
} else {
fn();
}
}

// Sphinx theme nav state
function ThemeNav () {
Expand Down Expand Up @@ -32,8 +48,8 @@ function ThemeNav () {
}

self.isRunning = true;
jQuery(function ($) {
self.init($);
injectJQuery(function () {
self.init();

self.reset();
self.win.on('hashchange', self.reset);
Expand Down Expand Up @@ -69,7 +85,7 @@ function ThemeNav () {
this.enable(true);
};

nav.init = function ($) {
nav.init = function () {
var doc = $(document),
self = this;

Expand Down

0 comments on commit 86bcbd7

Please sign in to comment.