navtree.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. @licstart The following is the entire license notice for the JavaScript code in this file.
  3. The MIT License (MIT)
  4. Copyright (C) 1997-2020 by Dimitri van Heesch
  5. Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  6. and associated documentation files (the "Software"), to deal in the Software without restriction,
  7. including without limitation the rights to use, copy, modify, merge, publish, distribute,
  8. sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all copies or
  11. substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  13. BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. @licend The above is the entire license notice for the JavaScript code in this file
  18. */
  19. function initNavTree(toroot,relpath) {
  20. let navTreeSubIndices = [];
  21. const ARROW_DOWN = '▼';
  22. const ARROW_RIGHT = '►';
  23. const NAVPATH_COOKIE_NAME = ''+'navpath';
  24. const getData = function(varName) {
  25. const i = varName.lastIndexOf('/');
  26. const n = i>=0 ? varName.substring(i+1) : varName;
  27. return eval(n.replace(/-/g,'_'));
  28. }
  29. const stripPath = function(uri) {
  30. return uri.substring(uri.lastIndexOf('/')+1);
  31. }
  32. const stripPath2 = function(uri) {
  33. const i = uri.lastIndexOf('/');
  34. const s = uri.substring(i+1);
  35. const m = uri.substring(0,i+1).match(/\/d\w\/d\w\w\/$/);
  36. return m ? uri.substring(i-6) : s;
  37. }
  38. const hashValue = function() {
  39. return $(location).attr('hash').substring(1).replace(/[^\w-]/g,'');
  40. }
  41. const hashUrl = function() {
  42. return '#'+hashValue();
  43. }
  44. const pathName = function() {
  45. return $(location).attr('pathname').replace(/[^-A-Za-z0-9+&@#/%?=~_|!:,.;()]/g, '');
  46. }
  47. const storeLink = function(link) {
  48. if (!$("#nav-sync").hasClass('sync')) {
  49. Cookie.writeSetting(NAVPATH_COOKIE_NAME,link,0);
  50. }
  51. }
  52. const deleteLink = function() {
  53. Cookie.eraseSetting(NAVPATH_COOKIE_NAME);
  54. }
  55. const cachedLink = function() {
  56. return Cookie.readSetting(NAVPATH_COOKIE_NAME,'');
  57. }
  58. const getScript = function(scriptName,func) {
  59. const head = document.getElementsByTagName("head")[0];
  60. const script = document.createElement('script');
  61. script.id = scriptName;
  62. script.type = 'text/javascript';
  63. script.onload = func;
  64. script.src = scriptName+'.js';
  65. head.appendChild(script);
  66. }
  67. const createIndent = function(o,domNode,node) {
  68. let level=-1;
  69. let n = node;
  70. while (n.parentNode) { level++; n=n.parentNode; }
  71. if (node.childrenData) {
  72. const imgNode = document.createElement("span");
  73. imgNode.className = 'arrow';
  74. imgNode.style.paddingLeft=(16*level).toString()+'px';
  75. imgNode.innerHTML=ARROW_RIGHT;
  76. node.plus_img = imgNode;
  77. node.expandToggle = document.createElement("a");
  78. node.expandToggle.href = "javascript:void(0)";
  79. node.expandToggle.onclick = function() {
  80. if (node.expanded) {
  81. $(node.getChildrenUL()).slideUp("fast");
  82. node.plus_img.innerHTML=ARROW_RIGHT;
  83. node.expanded = false;
  84. } else {
  85. expandNode(o, node, false, true);
  86. }
  87. }
  88. node.expandToggle.appendChild(imgNode);
  89. domNode.appendChild(node.expandToggle);
  90. } else {
  91. let span = document.createElement("span");
  92. span.className = 'arrow';
  93. span.style.width = 16*(level+1)+'px';
  94. span.innerHTML = ' ';
  95. domNode.appendChild(span);
  96. }
  97. }
  98. let animationInProgress = false;
  99. const gotoAnchor = function(anchor,aname) {
  100. let pos, docContent = $('#doc-content');
  101. let ancParent = $(anchor.parent());
  102. if (ancParent.hasClass('memItemLeft') || ancParent.hasClass('memtitle') ||
  103. ancParent.hasClass('fieldname') || ancParent.hasClass('fieldtype') ||
  104. ancParent.is(':header')) {
  105. pos = ancParent.position().top;
  106. } else if (anchor.position()) {
  107. pos = anchor.position().top;
  108. }
  109. if (pos) {
  110. const dcOffset = docContent.offset().top;
  111. const dcHeight = docContent.height();
  112. const dcScrHeight = docContent[0].scrollHeight
  113. const dcScrTop = docContent.scrollTop();
  114. let dist = Math.abs(Math.min(pos-dcOffset,dcScrHeight-dcHeight-dcScrTop));
  115. animationInProgress = true;
  116. docContent.animate({
  117. scrollTop: pos + dcScrTop - dcOffset
  118. },Math.max(50,Math.min(500,dist)),function() {
  119. animationInProgress=false;
  120. if (anchor.parent().attr('class')=='memItemLeft') {
  121. let rows = $('.memberdecls tr[class$="'+hashValue()+'"]');
  122. glowEffect(rows.children(),300); // member without details
  123. } else if (anchor.parent().attr('class')=='fieldname') {
  124. glowEffect(anchor.parent().parent(),1000); // enum value
  125. } else if (anchor.parent().attr('class')=='fieldtype') {
  126. glowEffect(anchor.parent().parent(),1000); // struct field
  127. } else if (anchor.parent().is(":header")) {
  128. glowEffect(anchor.parent(),1000); // section header
  129. } else {
  130. glowEffect(anchor.next(),1000); // normal member
  131. }
  132. });
  133. }
  134. }
  135. const newNode = function(o, po, text, link, childrenData, lastNode) {
  136. const node = {
  137. children : [],
  138. childrenData : childrenData,
  139. depth : po.depth + 1,
  140. relpath : po.relpath,
  141. isLast : lastNode,
  142. li : document.createElement("li"),
  143. parentNode : po,
  144. itemDiv : document.createElement("div"),
  145. labelSpan : document.createElement("span"),
  146. label : document.createTextNode(text),
  147. expanded : false,
  148. childrenUL : null,
  149. getChildrenUL : function() {
  150. if (!this.childrenUL) {
  151. this.childrenUL = document.createElement("ul");
  152. this.childrenUL.className = "children_ul";
  153. this.childrenUL.style.display = "none";
  154. this.li.appendChild(node.childrenUL);
  155. }
  156. return node.childrenUL;
  157. },
  158. };
  159. node.itemDiv.className = "item";
  160. node.labelSpan.className = "label";
  161. createIndent(o,node.itemDiv,node);
  162. node.itemDiv.appendChild(node.labelSpan);
  163. node.li.appendChild(node.itemDiv);
  164. const a = document.createElement("a");
  165. node.labelSpan.appendChild(a);
  166. po.getChildrenUL().appendChild(node.li);
  167. a.appendChild(node.label);
  168. if (link) {
  169. let url;
  170. if (link.substring(0,1)=='^') {
  171. url = link.substring(1);
  172. link = url;
  173. } else {
  174. url = node.relpath+link;
  175. }
  176. a.className = stripPath(link.replace('#',':'));
  177. if (link.indexOf('#')!=-1) {
  178. const aname = '#'+link.split('#')[1];
  179. const srcPage = stripPath(pathName());
  180. const targetPage = stripPath(link.split('#')[0]);
  181. a.href = srcPage!=targetPage ? url : aname;
  182. a.onclick = function() {
  183. storeLink(link);
  184. aPPar = $(a).parent().parent();
  185. if (!aPPar.hasClass('selected')) {
  186. $('.item').removeClass('selected');
  187. $('.item').removeAttr('id');
  188. aPPar.addClass('selected');
  189. aPPar.attr('id','selected');
  190. }
  191. const anchor = $(aname);
  192. gotoAnchor(anchor,aname);
  193. };
  194. } else {
  195. a.href = url;
  196. a.onclick = () => storeLink(link);
  197. }
  198. } else if (childrenData != null) {
  199. a.className = "nolink";
  200. a.href = "javascript:void(0)";
  201. a.onclick = node.expandToggle.onclick;
  202. }
  203. return node;
  204. }
  205. const showRoot = function() {
  206. const headerHeight = $("#top").height();
  207. const footerHeight = $("#nav-path").height();
  208. const windowHeight = $(window).height() - headerHeight - footerHeight;
  209. (function() { // retry until we can scroll to the selected item
  210. try {
  211. const navtree=$('#nav-tree');
  212. navtree.scrollTo('#selected',100,{offset:-windowHeight/2});
  213. } catch (err) {
  214. setTimeout(arguments.callee, 0);
  215. }
  216. })();
  217. }
  218. const expandNode = function(o, node, imm, setFocus) {
  219. if (node.childrenData && !node.expanded) {
  220. if (typeof(node.childrenData)==='string') {
  221. const varName = node.childrenData;
  222. getScript(node.relpath+varName,function() {
  223. node.childrenData = getData(varName);
  224. expandNode(o, node, imm, setFocus);
  225. });
  226. } else {
  227. if (!node.childrenVisited) {
  228. getNode(o, node);
  229. }
  230. $(node.getChildrenUL()).slideDown("fast");
  231. node.plus_img.innerHTML = ARROW_DOWN;
  232. node.expanded = true;
  233. if (setFocus) {
  234. $(node.expandToggle).focus();
  235. }
  236. }
  237. }
  238. }
  239. const glowEffect = function(n,duration) {
  240. n.addClass('glow').delay(duration).queue(function(next) {
  241. $(this).removeClass('glow');next();
  242. });
  243. }
  244. const highlightAnchor = function() {
  245. const aname = hashUrl();
  246. const anchor = $(aname);
  247. gotoAnchor(anchor,aname);
  248. }
  249. const selectAndHighlight = function(hash,n) {
  250. let a;
  251. if (hash) {
  252. const link=stripPath(pathName())+':'+hash.substring(1);
  253. a=$('.item a[class$="'+link+'"]');
  254. }
  255. if (a && a.length) {
  256. a.parent().parent().addClass('selected');
  257. a.parent().parent().attr('id','selected');
  258. highlightAnchor();
  259. } else if (n) {
  260. $(n.itemDiv).addClass('selected');
  261. $(n.itemDiv).attr('id','selected');
  262. }
  263. let topOffset=5;
  264. if ($('#nav-tree-contents .item:first').hasClass('selected')) {
  265. topOffset+=25;
  266. }
  267. $('#nav-sync').css('top',topOffset+'px');
  268. showRoot();
  269. }
  270. const showNode = function(o, node, index, hash) {
  271. if (node && node.childrenData) {
  272. if (typeof(node.childrenData)==='string') {
  273. const varName = node.childrenData;
  274. getScript(node.relpath+varName,function() {
  275. node.childrenData = getData(varName);
  276. showNode(o,node,index,hash);
  277. });
  278. } else {
  279. if (!node.childrenVisited) {
  280. getNode(o, node);
  281. }
  282. $(node.getChildrenUL()).css({'display':'block'});
  283. node.plus_img.innerHTML = ARROW_DOWN;
  284. node.expanded = true;
  285. const n = node.children[o.breadcrumbs[index]];
  286. if (index+1<o.breadcrumbs.length) {
  287. showNode(o,n,index+1,hash);
  288. } else if (typeof(n.childrenData)==='string') {
  289. const varName = n.childrenData;
  290. getScript(n.relpath+varName,function() {
  291. n.childrenData = getData(varName);
  292. node.expanded=false;
  293. showNode(o,node,index,hash); // retry with child node expanded
  294. });
  295. } else {
  296. const rootBase = stripPath(o.toroot.replace(/\..+$/, ''));
  297. if (rootBase=="index" || rootBase=="pages" || rootBase=="search") {
  298. expandNode(o, n, true, false);
  299. }
  300. selectAndHighlight(hash,n);
  301. }
  302. }
  303. } else {
  304. selectAndHighlight(hash);
  305. }
  306. }
  307. const removeToInsertLater = function(element) {
  308. const parentNode = element.parentNode;
  309. const nextSibling = element.nextSibling;
  310. parentNode.removeChild(element);
  311. return function() {
  312. if (nextSibling) {
  313. parentNode.insertBefore(element, nextSibling);
  314. } else {
  315. parentNode.appendChild(element);
  316. }
  317. };
  318. }
  319. const getNode = function(o, po) {
  320. const insertFunction = removeToInsertLater(po.li);
  321. po.childrenVisited = true;
  322. const l = po.childrenData.length-1;
  323. for (let i in po.childrenData) {
  324. const nodeData = po.childrenData[i];
  325. po.children[i] = newNode(o, po, nodeData[0], nodeData[1], nodeData[2], i==l);
  326. }
  327. insertFunction();
  328. }
  329. const gotoNode = function(o,subIndex,root,hash,relpath) {
  330. const nti = navTreeSubIndices[subIndex][root+hash];
  331. o.breadcrumbs = $.extend(true, [], nti ? nti : navTreeSubIndices[subIndex][root]);
  332. if (!o.breadcrumbs && root!=NAVTREE[0][1]) { // fallback: show index
  333. navTo(o,NAVTREE[0][1],"",relpath);
  334. $('.item').removeClass('selected');
  335. $('.item').removeAttr('id');
  336. }
  337. if (o.breadcrumbs) {
  338. o.breadcrumbs.unshift(0); // add 0 for root node
  339. showNode(o, o.node, 0, hash);
  340. }
  341. }
  342. const navTo = function(o,root,hash,relpath) {
  343. const link = cachedLink();
  344. if (link) {
  345. const parts = link.split('#');
  346. root = parts[0];
  347. hash = parts.length>1 ? '#'+parts[1].replace(/[^\w-]/g,'') : '';
  348. }
  349. if (hash.match(/^#l\d+$/)) {
  350. const anchor=$('a[name='+hash.substring(1)+']');
  351. glowEffect(anchor.parent(),1000); // line number
  352. hash=''; // strip line number anchors
  353. }
  354. const url=root+hash;
  355. let i=-1;
  356. while (NAVTREEINDEX[i+1]<=url) i++;
  357. if (i==-1) { i=0; root=NAVTREE[0][1]; } // fallback: show index
  358. if (navTreeSubIndices[i]) {
  359. gotoNode(o,i,root,hash,relpath)
  360. } else {
  361. getScript(relpath+'navtreeindex'+i,function() {
  362. navTreeSubIndices[i] = eval('NAVTREEINDEX'+i);
  363. if (navTreeSubIndices[i]) {
  364. gotoNode(o,i,root,hash,relpath);
  365. }
  366. });
  367. }
  368. }
  369. const showSyncOff = function(n,relpath) {
  370. n.html('<img src="'+relpath+'sync_off.png" title="'+SYNCOFFMSG+'"/>');
  371. }
  372. const showSyncOn = function(n,relpath) {
  373. n.html('<img src="'+relpath+'sync_on.png" title="'+SYNCONMSG+'"/>');
  374. }
  375. const o = {
  376. toroot : toroot,
  377. node : {
  378. childrenData : NAVTREE,
  379. children : [],
  380. childrenUL : document.createElement("ul"),
  381. getChildrenUL : function() { return this.childrenUL },
  382. li : document.getElementById("nav-tree-contents"),
  383. depth : 0,
  384. relpath : relpath,
  385. expanded : false,
  386. isLast : true,
  387. plus_img : document.createElement("span"),
  388. },
  389. };
  390. o.node.li.appendChild(o.node.childrenUL);
  391. o.node.plus_img.className = 'arrow';
  392. o.node.plus_img.innerHTML = ARROW_RIGHT;
  393. const navSync = $('#nav-sync');
  394. if (cachedLink()) {
  395. showSyncOff(navSync,relpath);
  396. navSync.removeClass('sync');
  397. } else {
  398. showSyncOn(navSync,relpath);
  399. }
  400. navSync.click(() => {
  401. const navSync = $('#nav-sync');
  402. if (navSync.hasClass('sync')) {
  403. navSync.removeClass('sync');
  404. showSyncOff(navSync,relpath);
  405. storeLink(stripPath2(pathName())+hashUrl());
  406. } else {
  407. navSync.addClass('sync');
  408. showSyncOn(navSync,relpath);
  409. deleteLink();
  410. }
  411. });
  412. navTo(o,toroot,hashUrl(),relpath);
  413. showRoot();
  414. $(window).bind('hashchange', () => {
  415. if (!animationInProgress) {
  416. if (window.location.hash && window.location.hash.length>1) {
  417. let a;
  418. if ($(location).attr('hash')) {
  419. const clslink=stripPath(pathName())+':'+hashValue();
  420. a=$('.item a[class$="'+clslink.replace(/</g,'\\3c ')+'"]');
  421. }
  422. if (a==null || !$(a).parent().parent().hasClass('selected')) {
  423. $('.item').removeClass('selected');
  424. $('.item').removeAttr('id');
  425. }
  426. const link=stripPath2(pathName());
  427. navTo(o,link,hashUrl(),relpath);
  428. } else {
  429. $('#doc-content').scrollTop(0);
  430. $('.item').removeClass('selected');
  431. $('.item').removeAttr('id');
  432. navTo(o,toroot,hashUrl(),relpath);
  433. }
  434. }
  435. });
  436. $("div.toc a[href]").click(function(e) {
  437. e.preventDefault();
  438. const aname = $(this).attr("href");
  439. gotoAnchor($(aname),aname);
  440. });
  441. }
  442. /* @license-end */