summaryrefslogtreecommitdiffstats
path: root/docs/_static/sphinx_design.js
blob: d080f2d634ab7948025c54108ff98c13d5710688 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

(function () {
  "use strict";

  var dropdownClassName = "sd-dropdown";

  function getDropdownElement() {
    var dropdownId = window.location.hash;
    if (!dropdownId) {
      return false;
    }

    var dropdownElement = document.getElementById(dropdownId.substring(1));
    if (
      !dropdownElement ||
      !dropdownElement.classList.contains(dropdownClassName)
    ) {
      return false;
    }

    return dropdownElement;
  }

  function setupDropdownLink() {
    var dropdowns = document.getElementsByClassName(dropdownClassName);
    for (var i = 0; i < dropdowns.length; i++) {
      for (var j = 0; j < dropdowns[i].classList.length; j++) {
        if (dropdowns[i].classList[j].startsWith("anchor-id-")) {
          dropdowns[i].id = dropdowns[i].classList[j].replace("anchor-id-", "");
        }
      }

      var aTag = document.createElement("a");
      aTag.setAttribute("href", "#" + dropdowns[i].id);
      aTag.classList.add("dropdown-link");
      aTag.innerHTML = "¶";

      var summaryElement =
        dropdowns[i].getElementsByClassName("sd-summary-title")[0];
      summaryElement.insertBefore(
        aTag,
        summaryElement.getElementsByClassName("docutils")[0]
      );
    }
  }

  function scrollToDropdown() {
    var dropdownElement = getDropdownElement();
    if (dropdownElement) {
      dropdownElement.open = true;
      dropdownElement.scrollIntoView(true);
    }
  }

  // Initiallize dropdown link
  window.addEventListener("DOMContentLoaded", () => {
    if (document.getElementsByClassName(dropdownClassName).length) {
      setupDropdownLink();
      window.onhashchange = scrollToDropdown;
    }
  });

  // Scroll to and open the dropdown direct links
  window.onload = () => {
    if (document.getElementsByClassName(dropdownClassName).length) {
      scrollToDropdown();
    }
  };
})();