| 1 |
// jQuery is included via the sphinxcontrib-jquery extension that we depend on |
| 2 |
var jQuery = (typeof(window) != 'undefined') ? window.jQuery : require('jquery'); |
| 3 |
|
| 4 |
// Sphinx theme nav state |
| 5 |
function ThemeNav () { |
| 6 |
|
| 7 |
var nav = { |
| 8 |
navBar: null, |
| 9 |
win: null, |
| 10 |
winScroll: false, |
| 11 |
winResize: false, |
| 12 |
linkScroll: false, |
| 13 |
winPosition: 0, |
| 14 |
winHeight: null, |
| 15 |
docHeight: null, |
| 16 |
isRunning: false |
| 17 |
}; |
| 18 |
|
| 19 |
nav.enable = function (withStickyNav) { |
| 20 |
var self = this; |
| 21 |
|
| 22 |
// TODO this can likely be removed once the theme javascript is broken |
| 23 |
// out from the RTD assets. This just ensures old projects that are |
| 24 |
// calling `enable()` get the sticky menu on by default. All other cals |
| 25 |
// to `enable` should include an argument for enabling the sticky menu. |
| 26 |
if (typeof(withStickyNav) == 'undefined') { |
| 27 |
withStickyNav = true; |
| 28 |
} |
| 29 |
|
| 30 |
if (self.isRunning) { |
| 31 |
// Only allow enabling nav logic once |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
self.isRunning = true; |
| 36 |
jQuery(function ($) { |
| 37 |
self.init($); |
| 38 |
|
| 39 |
self.reset(); |
| 40 |
self.win.on('hashchange', self.reset); |
| 41 |
|
| 42 |
if (withStickyNav) { |
| 43 |
// Set scroll monitor |
| 44 |
self.win.on('scroll', function () { |
| 45 |
if (!self.linkScroll) { |
| 46 |
if (!self.winScroll) { |
| 47 |
self.winScroll = true; |
| 48 |
requestAnimationFrame(function() { self.onScroll(); }); |
| 49 |
} |
| 50 |
} |
| 51 |
}); |
| 52 |
} |
| 53 |
|
| 54 |
// Set resize monitor |
| 55 |
self.win.on('resize', function () { |
| 56 |
if (!self.winResize) { |
| 57 |
self.winResize = true; |
| 58 |
requestAnimationFrame(function() { self.onResize(); }); |
| 59 |
} |
| 60 |
}); |
| 61 |
|
| 62 |
self.onResize(); |
| 63 |
}); |
| 64 |
|
| 65 |
}; |
| 66 |
|
| 67 |
// TODO remove this with a split in theme and Read the Docs JS logic as |
| 68 |
// well, it's only here to support 0.3.0 installs of our theme. |
| 69 |
nav.enableSticky = function() { |
| 70 |
this.enable(true); |
| 71 |
}; |
| 72 |
|
| 73 |
nav.init = function ($) { |
| 74 |
var doc = $(document), |
| 75 |
self = this; |
| 76 |
|
| 77 |
this.navBar = $('div.wy-side-scroll:first'); |
| 78 |
this.win = $(window); |
| 79 |
|
| 80 |
// Set up javascript UX bits |
| 81 |
$(document) |
| 82 |
// Shift nav in mobile when clicking the menu. |
| 83 |
.on('click', "[data-toggle='wy-nav-top']", function() { |
| 84 |
$("[data-toggle='wy-nav-shift']").toggleClass("shift"); |
| 85 |
$("[data-toggle='rst-versions']").toggleClass("shift"); |
| 86 |
}) |
| 87 |
|
| 88 |
// Nav menu link click operations |
| 89 |
.on('click', ".wy-menu-vertical .current ul li a", function() { |
| 90 |
var target = $(this); |
| 91 |
// Close menu when you click a link. |
| 92 |
$("[data-toggle='wy-nav-shift']").removeClass("shift"); |
| 93 |
$("[data-toggle='rst-versions']").toggleClass("shift"); |
| 94 |
// Handle dynamic display of l3 and l4 nav lists |
| 95 |
self.toggleCurrent(target); |
| 96 |
self.hashChange(); |
| 97 |
}) |
| 98 |
.on('click', "[data-toggle='rst-current-version']", function() { |
| 99 |
$("[data-toggle='rst-versions']").toggleClass("shift-up"); |
| 100 |
}) |
| 101 |
|
| 102 |
// Make tables responsive |
| 103 |
$("table.docutils:not(.field-list,.footnote,.citation)") |
| 104 |
.wrap("<div class='wy-table-responsive'></div>"); |
| 105 |
|
| 106 |
// Add extra class to responsive tables that contain |
| 107 |
// footnotes or citations so that we can target them for styling |
| 108 |
$("table.docutils.footnote") |
| 109 |
.wrap("<div class='wy-table-responsive footnote'></div>"); |
| 110 |
$("table.docutils.citation") |
| 111 |
.wrap("<div class='wy-table-responsive citation'></div>"); |
| 112 |
|
| 113 |
// Add expand links to all parents of nested ul |
| 114 |
$('.wy-menu-vertical ul').not('.simple').siblings('a').each(function () { |
| 115 |
var link = $(this); |
| 116 |
expand = |
| 117 |
$('<button class="toctree-expand" title="Open/close menu"></button>'); |
| 118 |
expand.on('click', function (ev) { |
| 119 |
self.toggleCurrent(link); |
| 120 |
ev.stopPropagation(); |
| 121 |
return false; |
| 122 |
}); |
| 123 |
link.prepend(expand); |
| 124 |
}); |
| 125 |
}; |
| 126 |
|
| 127 |
nav.reset = function () { |
| 128 |
// Get anchor from URL and open up nested nav |
| 129 |
var anchor = encodeURI(window.location.hash) || '#'; |
| 130 |
|
| 131 |
try { |
| 132 |
var vmenu = $('.wy-menu-vertical'); |
| 133 |
var link = vmenu.find('[href="' + anchor + '"]'); |
| 134 |
if (link.length === 0) { |
| 135 |
// this link was not found in the sidebar. |
| 136 |
// Find associated id element, then its closest section |
| 137 |
// in the document and try with that one. |
| 138 |
var id_elt = $('.document [id="' + anchor.substring(1) + '"]'); |
| 139 |
var closest_section = id_elt.closest('div.section'); |
| 140 |
link = vmenu.find('[href="#' + closest_section.attr("id") + '"]'); |
| 141 |
if (link.length === 0) { |
| 142 |
// still not found in the sidebar. fall back to main section |
| 143 |
link = vmenu.find('[href="#"]'); |
| 144 |
} |
| 145 |
} |
| 146 |
// If we found a matching link then reset current and re-apply |
| 147 |
// otherwise retain the existing match |
| 148 |
if (link.length > 0) { |
| 149 |
$('.wy-menu-vertical .current') |
| 150 |
.removeClass('current') |
| 151 |
.attr('aria-expanded','false'); |
| 152 |
link.addClass('current') |
| 153 |
.attr('aria-expanded','true'); |
| 154 |
link.closest('li.toctree-l1') |
| 155 |
.parent() |
| 156 |
.addClass('current') |
| 157 |
.attr('aria-expanded','true'); |
| 158 |
for (let i = 1; i <= 10; i++) { |
| 159 |
link.closest('li.toctree-l' + i) |
| 160 |
.addClass('current') |
| 161 |
.attr('aria-expanded','true'); |
| 162 |
} |
| 163 |
link[0].scrollIntoView(); |
| 164 |
} |
| 165 |
} |
| 166 |
catch (err) { |
| 167 |
console.log("Error expanding nav for anchor", err); |
| 168 |
} |
| 169 |
|
| 170 |
}; |
| 171 |
|
| 172 |
nav.onScroll = function () { |
| 173 |
this.winScroll = false; |
| 174 |
var newWinPosition = this.win.scrollTop(), |
| 175 |
winBottom = newWinPosition + this.winHeight, |
| 176 |
navPosition = this.navBar.scrollTop(), |
| 177 |
newNavPosition = navPosition + (newWinPosition - this.winPosition); |
| 178 |
if (newWinPosition < 0 || winBottom > this.docHeight) { |
| 179 |
return; |
| 180 |
} |
| 181 |
this.navBar.scrollTop(newNavPosition); |
| 182 |
this.winPosition = newWinPosition; |
| 183 |
}; |
| 184 |
|
| 185 |
nav.onResize = function () { |
| 186 |
this.winResize = false; |
| 187 |
this.winHeight = this.win.height(); |
| 188 |
this.docHeight = $(document).height(); |
| 189 |
}; |
| 190 |
|
| 191 |
nav.hashChange = function () { |
| 192 |
this.linkScroll = true; |
| 193 |
this.win.one('hashchange', function () { |
| 194 |
this.linkScroll = false; |
| 195 |
}); |
| 196 |
}; |
| 197 |
|
| 198 |
nav.toggleCurrent = function (elem) { |
| 199 |
var parent_li = elem.closest('li'); |
| 200 |
parent_li |
| 201 |
.siblings('li.current') |
| 202 |
.removeClass('current') |
| 203 |
.attr('aria-expanded','false'); |
| 204 |
parent_li |
| 205 |
.siblings() |
| 206 |
.find('li.current') |
| 207 |
.removeClass('current') |
| 208 |
.attr('aria-expanded','false'); |
| 209 |
var children = parent_li.find('> ul li'); |
| 210 |
// Don't toggle terminal elements. |
| 211 |
if (children.length) { |
| 212 |
children |
| 213 |
.removeClass('current') |
| 214 |
.attr('aria-expanded','false'); |
| 215 |
parent_li |
| 216 |
.toggleClass('current') |
| 217 |
.attr('aria-expanded', function(i, old) { |
| 218 |
return old == 'true' ? 'false' : 'true'; |
| 219 |
}); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
return nav; |
| 224 |
}; |
| 225 |
|
| 226 |
module.exports.ThemeNav = ThemeNav(); |
| 227 |
|
| 228 |
if (typeof(window) != 'undefined') { |
| 229 |
window.SphinxRtdTheme = { |
| 230 |
Navigation: module.exports.ThemeNav, |
| 231 |
// TODO remove this once static assets are split up between the theme |
| 232 |
// and Read the Docs. For now, this patches 0.3.0 to be backwards |
| 233 |
// compatible with a pre-0.3.0 layout.html |
| 234 |
StickyNav: module.exports.ThemeNav, |
| 235 |
}; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel |
| 240 |
// https://gist.github.com/paulirish/1579671 |
| 241 |
// MIT license |
| 242 |
|
| 243 |
(function() { |
| 244 |
var lastTime = 0; |
| 245 |
var vendors = ['ms', 'moz', 'webkit', 'o']; |
| 246 |
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { |
| 247 |
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame']; |
| 248 |
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] |
| 249 |
|| window[vendors[x]+'CancelRequestAnimationFrame']; |
| 250 |
} |
| 251 |
|
| 252 |
if (!window.requestAnimationFrame) |
| 253 |
window.requestAnimationFrame = function(callback, element) { |
| 254 |
var currTime = new Date().getTime(); |
| 255 |
var timeToCall = Math.max(0, 16 - (currTime - lastTime)); |
| 256 |
var id = window.setTimeout(function() { callback(currTime + timeToCall); }, |
| 257 |
timeToCall); |
| 258 |
lastTime = currTime + timeToCall; |
| 259 |
return id; |
| 260 |
}; |
| 261 |
|
| 262 |
if (!window.cancelAnimationFrame) |
| 263 |
window.cancelAnimationFrame = function(id) { |
| 264 |
clearTimeout(id); |
| 265 |
}; |
| 266 |
}()); |