github.com/imran-kn/cilium-fork@v1.6.9/Documentation/_themes/sphinx_rtd_theme/js/theme.js (about)

     1  var jQuery = (typeof(window) != 'undefined') ? window.jQuery : require('jquery');
     2  
     3  // Sphinx theme nav state
     4  function ThemeNav () {
     5  
     6      var nav = {
     7          navBar: null,
     8          win: null,
     9          winScroll: false,
    10          winResize: false,
    11          linkScroll: false,
    12          winPosition: 0,
    13          winHeight: null,
    14          docHeight: null,
    15          isRunning: false
    16      };
    17  
    18      nav.enable = function () {
    19          var self = this;
    20  
    21          if (!self.isRunning) {
    22              self.isRunning = true;
    23              jQuery(function ($) {
    24                  self.init($);
    25  
    26                  self.reset();
    27                  self.win.on('hashchange', self.reset);
    28  
    29                  // Set scroll monitor
    30                  self.win.on('scroll', function () {
    31                      if (!self.linkScroll) {
    32                          self.winScroll = true;
    33                      }
    34                  });
    35                  setInterval(function () { if (self.winScroll) self.onScroll(); }, 25);
    36  
    37                  // Set resize monitor
    38                  self.win.on('resize', function () {
    39                      self.winResize = true;
    40                  });
    41                  setInterval(function () { if (self.winResize) self.onResize(); }, 25);
    42                  self.onResize();
    43              });
    44          };
    45      };
    46  
    47      nav.init = function ($) {
    48          var doc = $(document),
    49              self = this;
    50  
    51          this.navBar = $('div.wy-side-scroll:first');
    52          this.win = $(window);
    53  
    54          // Set up javascript UX bits
    55          $(document)
    56              // Shift nav in mobile when clicking the menu.
    57              .on('click', "[data-toggle='wy-nav-top']", function() {
    58                  $("[data-toggle='wy-nav-shift']").toggleClass("shift");
    59                  $("[data-toggle='rst-versions']").toggleClass("shift");
    60              })
    61  
    62              // Nav menu link click operations
    63              .on('click', ".wy-menu-vertical .current ul li a", function() {
    64                  var target = $(this);
    65                  // Close menu when you click a link.
    66                  $("[data-toggle='wy-nav-shift']").removeClass("shift");
    67                  $("[data-toggle='rst-versions']").toggleClass("shift");
    68                  // Handle dynamic display of l3 and l4 nav lists
    69                  self.toggleCurrent(target);
    70                  self.hashChange();
    71              })
    72              .on('click', "[data-toggle='rst-current-version']", function() {
    73                  $("[data-toggle='rst-versions']").toggleClass("shift-up");
    74              })
    75  
    76          // Make tables responsive
    77          $("table.docutils:not(.field-list)")
    78              .wrap("<div class='wy-table-responsive'></div>");
    79  
    80          // Add expand links to all parents of nested ul
    81          $('.wy-menu-vertical ul').not('.simple').siblings('a').each(function () {
    82              var link = $(this);
    83                  expand = $('<span class="toctree-expand"></span>');
    84              expand.on('click', function (ev) {
    85                  self.toggleCurrent(link);
    86                  ev.stopPropagation();
    87                  return false;
    88              });
    89              link.prepend(expand);
    90          });
    91      };
    92  
    93      nav.reset = function () {
    94          // Get anchor from URL and open up nested nav
    95          var anchor = encodeURI(window.location.hash);
    96          if (anchor) {
    97              try {
    98                  var link = $('.wy-menu-vertical')
    99                      .find('[href="' + anchor + '"]');
   100                  // If we didn't find a link, it may be because we clicked on
   101                  // something that is not in the sidebar (eg: when using
   102                  // sphinxcontrib.httpdomain it generates headerlinks but those
   103                  // aren't picked up and placed in the toctree). So let's find
   104                  // the closest header in the document and try with that one.
   105                  if (link.length === 0) {
   106                    var doc_link = $('.document a[href="' + anchor + '"]');
   107                    var closest_section = doc_link.closest('div.section');
   108                    // Try again with the closest section entry.
   109                    link = $('.wy-menu-vertical')
   110                      .find('[href="#' + closest_section.attr("id") + '"]');
   111                  }
   112                  // If we found a matching link then reset current and re-apply
   113                  // otherwise retain the existing match
   114                  if (link.length > 0) {
   115                      $('.wy-menu-vertical li.toctree-l1 li.current').removeClass('current');
   116                      link.closest('li.toctree-l2').addClass('current');
   117                      link.closest('li.toctree-l3').addClass('current');
   118                      link.closest('li.toctree-l4').addClass('current');
   119                  }
   120              }
   121              catch (err) {
   122                  console.log("Error expanding nav for anchor", err);
   123              }
   124          }
   125      };
   126  
   127      nav.onScroll = function () {
   128          this.winScroll = false;
   129          var newWinPosition = this.win.scrollTop(),
   130              winBottom = newWinPosition + this.winHeight,
   131              navPosition = this.navBar.scrollTop(),
   132              newNavPosition = navPosition + (newWinPosition - this.winPosition);
   133          if (newWinPosition < 0 || winBottom > this.docHeight) {
   134              return;
   135          }
   136          this.navBar.scrollTop(newNavPosition);
   137          this.winPosition = newWinPosition;
   138      };
   139  
   140      nav.onResize = function () {
   141          this.winResize = false;
   142          this.winHeight = this.win.height();
   143          this.docHeight = $(document).height();
   144      };
   145  
   146      nav.hashChange = function () {
   147          this.linkScroll = true;
   148          this.win.one('hashchange', function () {
   149              this.linkScroll = false;
   150          });
   151      };
   152  
   153      nav.toggleCurrent = function (elem) {
   154          var parent_li = elem.closest('li');
   155          parent_li.siblings('li.current').removeClass('current');
   156          parent_li.siblings().find('li.current').removeClass('current');
   157          parent_li.find('> ul li.current').removeClass('current');
   158          parent_li.toggleClass('current');
   159      }
   160  
   161      return nav;
   162  };
   163  
   164  module.exports.ThemeNav = ThemeNav();
   165  
   166  if (typeof(window) != 'undefined') {
   167      window.SphinxRtdTheme = { StickyNav: module.exports.ThemeNav };
   168  }