docs.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. // Platforms we have logos for and their formatted titles. Used for
  18. // converting "mark" elements to logos
  19. var platforms = {
  20. 'android': 'Android',
  21. 'ios': 'iOS',
  22. 'windows': 'Windows',
  23. 'ubuntu': 'Ubuntu',
  24. 'blackberry': 'Blackberry',
  25. 'fireos': 'FireOS',
  26. 'firefoxos': 'FirefoxOS',
  27. 'webos': 'webOS',
  28. 'osx': 'OS X',
  29. 'browser': 'Browser',
  30. 'electron': 'Electron'
  31. };
  32. $(document).ready(function () {
  33. var HEADER_OFFSET = 50; // in pixels
  34. var TOC_TOP_OFFSET = HEADER_OFFSET + 40;
  35. // if this page's ToC entry can be found, scroll the ToC to it
  36. var thisPageEntry = $(".site-toc-container .this-page");
  37. if (thisPageEntry.length > 0) {
  38. $(".site-toc-container").scrollTop(thisPageEntry.first().position().top - TOC_TOP_OFFSET);
  39. }
  40. function slugifyLikeGitHub(originalText) {
  41. var text = originalText;
  42. // convert to lowercase
  43. text = text.toLowerCase();
  44. // replace spaces with dashes
  45. text = text.replace(/ /g, '-');
  46. // remove unaccepted characters
  47. // NOTE:
  48. // a better regex would have been /[^\d\s\w-_]/ug, but the 'u' flag
  49. // (Unicode) is not supported in some browsers, and we support
  50. // many languages that use Unicode characters
  51. text = text.replace(/[\[\]\(\)\;\:\=\+\?\!\.\,\{\}\\\/\>\<]/g, '');
  52. // trim remaining whitespace
  53. text = text.trim();
  54. return text;
  55. }
  56. function getIdForHeading(heading) {
  57. if (heading.id) {
  58. return heading.id;
  59. } else if (heading.name) {
  60. return heading.name;
  61. } else {
  62. return slugifyLikeGitHub(heading.innerText);
  63. }
  64. }
  65. function permalinkTo(id) {
  66. var anchor = document.createElement("a");
  67. anchor.className = "header-link";
  68. anchor.href = "#" + id;
  69. anchor.innerHTML = "<i class=\"glyphicon glyphicon-link\"></i>";
  70. return anchor;
  71. }
  72. function anchorFor(id) {
  73. var anchor = document.createElement("a");
  74. anchor.className = "fragment-anchor";
  75. anchor.id = id;
  76. return anchor;
  77. }
  78. // go through all headings in the content and add some links
  79. $('#page-toc-source h1, #page-toc-source h2, #page-toc-source h3, #page-toc-source h4, #page-toc-source h5, #page-toc-source h6')
  80. .each(function (i, heading) {
  81. headingId = getIdForHeading(heading);
  82. // add an anchor for linking to the heading
  83. // NOTE:
  84. // we could have set the ID on the heading itself but we didn't
  85. // because the <a> has some extra CSS that floats it above the
  86. // heading and makes it easier to see when linked
  87. $(heading).before(anchorFor(headingId))
  88. // add a permalink to all but the first heading
  89. if (i > 0) {
  90. $(heading).append(permalinkTo(headingId));
  91. }
  92. });
  93. // Table of Contents
  94. $('#page-toc').toc({
  95. 'selectors': 'h1,h2', // elements to use as headings
  96. 'container': '#page-toc-source', // element to find all selectors in
  97. 'prefix': '', // prefix for anchor tags and class names
  98. 'onHighlight': function(el) {}, // called when a new section is highlighted
  99. 'highlightOnScroll': true, // add class to heading that is currently in focus
  100. 'highlightOffset': 100, // offset to trigger the next headline
  101. 'anchorName': function(i, heading, prefix) { // custom function for anchor name
  102. return getIdForHeading(heading);
  103. },
  104. 'headerText': function(i, heading, $heading) { // custom function building the header-item text
  105. return $heading.text();
  106. },
  107. 'itemClass': function(i, heading, $heading, prefix) { // custom function for item class
  108. // don't assign any special classes to the toc entry itself
  109. return '';
  110. }
  111. });
  112. $('mark').each(function(index, element) {
  113. var platform = element.innerText.toLowerCase();
  114. platform = platform.replace(/ /g, '');
  115. if (platforms[platform]) {
  116. $(element).addClass('logo');
  117. $(element).addClass(platform);
  118. $(element).attr('title', platforms[platform]);
  119. element.innerText = '';
  120. }
  121. });
  122. });