navtree.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. @licstart The following is the entire license notice for the
  3. JavaScript code in this file.
  4. Copyright (C) 1997-2017 by Dimitri van Heesch
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. @licend The above is the entire license notice
  17. for the JavaScript code in this file
  18. */
  19. var navTreeSubIndices = new Array();
  20. var arrowDown = '▼';
  21. var arrowRight = '►';
  22. function getData(varName)
  23. {
  24. var i = varName.lastIndexOf('/');
  25. var n = i>=0 ? varName.substring(i+1) : varName;
  26. return eval(n.replace(/\-/g,'_'));
  27. }
  28. function stripPath(uri)
  29. {
  30. return uri.substring(uri.lastIndexOf('/')+1);
  31. }
  32. function stripPath2(uri)
  33. {
  34. var i = uri.lastIndexOf('/');
  35. var s = uri.substring(i+1);
  36. var m = uri.substring(0,i+1).match(/\/d\w\/d\w\w\/$/);
  37. return m ? uri.substring(i-6) : s;
  38. }
  39. function hashValue()
  40. {
  41. return $(location).attr('hash').substring(1).replace(/[^\w\-]/g,'');
  42. }
  43. function hashUrl()
  44. {
  45. return '#'+hashValue();
  46. }
  47. function pathName()
  48. {
  49. return $(location).attr('pathname').replace(/[^-A-Za-z0-9+&@#/%?=~_|!:,.;\(\)]/g, '');
  50. }
  51. function localStorageSupported()
  52. {
  53. try {
  54. return 'localStorage' in window && window['localStorage'] !== null && window.localStorage.getItem;
  55. }
  56. catch(e) {
  57. return false;
  58. }
  59. }
  60. function storeLink(link)
  61. {
  62. if (!$("#nav-sync").hasClass('sync') && localStorageSupported()) {
  63. window.localStorage.setItem('navpath',link);
  64. }
  65. }
  66. function deleteLink()
  67. {
  68. if (localStorageSupported()) {
  69. window.localStorage.setItem('navpath','');
  70. }
  71. }
  72. function cachedLink()
  73. {
  74. if (localStorageSupported()) {
  75. return window.localStorage.getItem('navpath');
  76. } else {
  77. return '';
  78. }
  79. }
  80. function getScript(scriptName,func,show)
  81. {
  82. var head = document.getElementsByTagName("head")[0];
  83. var script = document.createElement('script');
  84. script.id = scriptName;
  85. script.type = 'text/javascript';
  86. script.onload = func;
  87. script.src = scriptName+'.js';
  88. if ($.browser.msie && $.browser.version<=8) {
  89. // script.onload does not work with older versions of IE
  90. script.onreadystatechange = function() {
  91. if (script.readyState=='complete' || script.readyState=='loaded') {
  92. func(); if (show) showRoot();
  93. }
  94. }
  95. }
  96. head.appendChild(script);
  97. }
  98. function createIndent(o,domNode,node,level)
  99. {
  100. var level=-1;
  101. var n = node;
  102. while (n.parentNode) { level++; n=n.parentNode; }
  103. if (node.childrenData) {
  104. var imgNode = document.createElement("span");
  105. imgNode.className = 'arrow';
  106. imgNode.style.paddingLeft=(16*level).toString()+'px';
  107. imgNode.innerHTML=arrowRight;
  108. node.plus_img = imgNode;
  109. node.expandToggle = document.createElement("a");
  110. node.expandToggle.href = "javascript:void(0)";
  111. node.expandToggle.onclick = function() {
  112. if (node.expanded) {
  113. $(node.getChildrenUL()).slideUp("fast");
  114. node.plus_img.innerHTML=arrowRight;
  115. node.expanded = false;
  116. } else {
  117. expandNode(o, node, false, false);
  118. }
  119. }
  120. node.expandToggle.appendChild(imgNode);
  121. domNode.appendChild(node.expandToggle);
  122. } else {
  123. var span = document.createElement("span");
  124. span.className = 'arrow';
  125. span.style.width = 16*(level+1)+'px';
  126. span.innerHTML = '&#160;';
  127. domNode.appendChild(span);
  128. }
  129. }
  130. var animationInProgress = false;
  131. function gotoAnchor(anchor,aname,updateLocation)
  132. {
  133. var pos, docContent = $('#doc-content');
  134. var ancParent = $(anchor.parent());
  135. if (ancParent.hasClass('memItemLeft') ||
  136. ancParent.hasClass('fieldname') ||
  137. ancParent.hasClass('fieldtype') ||
  138. ancParent.is(':header'))
  139. {
  140. pos = ancParent.position().top;
  141. } else if (anchor.position()) {
  142. pos = anchor.position().top;
  143. }
  144. if (pos) {
  145. var dist = Math.abs(Math.min(
  146. pos-docContent.offset().top,
  147. docContent[0].scrollHeight-
  148. docContent.height()-docContent.scrollTop()));
  149. animationInProgress=true;
  150. docContent.animate({
  151. scrollTop: pos + docContent.scrollTop() - docContent.offset().top
  152. },Math.max(50,Math.min(500,dist)),function(){
  153. if (updateLocation) window.location.href=aname;
  154. animationInProgress=false;
  155. });
  156. }
  157. }
  158. function newNode(o, po, text, link, childrenData, lastNode)
  159. {
  160. var node = new Object();
  161. node.children = Array();
  162. node.childrenData = childrenData;
  163. node.depth = po.depth + 1;
  164. node.relpath = po.relpath;
  165. node.isLast = lastNode;
  166. node.li = document.createElement("li");
  167. po.getChildrenUL().appendChild(node.li);
  168. node.parentNode = po;
  169. node.itemDiv = document.createElement("div");
  170. node.itemDiv.className = "item";
  171. node.labelSpan = document.createElement("span");
  172. node.labelSpan.className = "label";
  173. createIndent(o,node.itemDiv,node,0);
  174. node.itemDiv.appendChild(node.labelSpan);
  175. node.li.appendChild(node.itemDiv);
  176. var a = document.createElement("a");
  177. node.labelSpan.appendChild(a);
  178. node.label = document.createTextNode(text);
  179. node.expanded = false;
  180. a.appendChild(node.label);
  181. if (link) {
  182. var url;
  183. if (link.substring(0,1)=='^') {
  184. url = link.substring(1);
  185. link = url;
  186. } else {
  187. url = node.relpath+link;
  188. }
  189. a.className = stripPath(link.replace('#',':'));
  190. if (link.indexOf('#')!=-1) {
  191. var aname = '#'+link.split('#')[1];
  192. var srcPage = stripPath(pathName());
  193. var targetPage = stripPath(link.split('#')[0]);
  194. a.href = srcPage!=targetPage ? url : "javascript:void(0)";
  195. a.onclick = function(){
  196. storeLink(link);
  197. if (!$(a).parent().parent().hasClass('selected'))
  198. {
  199. $('.item').removeClass('selected');
  200. $('.item').removeAttr('id');
  201. $(a).parent().parent().addClass('selected');
  202. $(a).parent().parent().attr('id','selected');
  203. }
  204. var anchor = $(aname);
  205. gotoAnchor(anchor,aname,true);
  206. };
  207. } else {
  208. a.href = url;
  209. a.onclick = function() { storeLink(link); }
  210. }
  211. } else {
  212. if (childrenData != null)
  213. {
  214. a.className = "nolink";
  215. a.href = "javascript:void(0)";
  216. a.onclick = node.expandToggle.onclick;
  217. }
  218. }
  219. node.childrenUL = null;
  220. node.getChildrenUL = function() {
  221. if (!node.childrenUL) {
  222. node.childrenUL = document.createElement("ul");
  223. node.childrenUL.className = "children_ul";
  224. node.childrenUL.style.display = "none";
  225. node.li.appendChild(node.childrenUL);
  226. }
  227. return node.childrenUL;
  228. };
  229. return node;
  230. }
  231. function showRoot()
  232. {
  233. var headerHeight = $("#top").height();
  234. var footerHeight = $("#nav-path").height();
  235. var windowHeight = $(window).height() - headerHeight - footerHeight;
  236. (function (){ // retry until we can scroll to the selected item
  237. try {
  238. var navtree=$('#nav-tree');
  239. navtree.scrollTo('#selected',0,{offset:-windowHeight/2});
  240. } catch (err) {
  241. setTimeout(arguments.callee, 0);
  242. }
  243. })();
  244. }
  245. function expandNode(o, node, imm, showRoot)
  246. {
  247. if (node.childrenData && !node.expanded) {
  248. if (typeof(node.childrenData)==='string') {
  249. var varName = node.childrenData;
  250. getScript(node.relpath+varName,function(){
  251. node.childrenData = getData(varName);
  252. expandNode(o, node, imm, showRoot);
  253. }, showRoot);
  254. } else {
  255. if (!node.childrenVisited) {
  256. getNode(o, node);
  257. } if (imm || ($.browser.msie && $.browser.version>8)) {
  258. // somehow slideDown jumps to the start of tree for IE9 :-(
  259. $(node.getChildrenUL()).show();
  260. } else {
  261. $(node.getChildrenUL()).slideDown("fast");
  262. }
  263. node.plus_img.innerHTML = arrowDown;
  264. node.expanded = true;
  265. }
  266. }
  267. }
  268. function glowEffect(n,duration)
  269. {
  270. n.addClass('glow').delay(duration).queue(function(next){
  271. $(this).removeClass('glow');next();
  272. });
  273. }
  274. function highlightAnchor()
  275. {
  276. var aname = hashUrl();
  277. var anchor = $(aname);
  278. if (anchor.parent().attr('class')=='memItemLeft'){
  279. var rows = $('.memberdecls tr[class$="'+hashValue()+'"]');
  280. glowEffect(rows.children(),300); // member without details
  281. } else if (anchor.parent().attr('class')=='fieldname'){
  282. glowEffect(anchor.parent().parent(),1000); // enum value
  283. } else if (anchor.parent().attr('class')=='fieldtype'){
  284. glowEffect(anchor.parent().parent(),1000); // struct field
  285. } else if (anchor.parent().is(":header")) {
  286. glowEffect(anchor.parent(),1000); // section header
  287. } else {
  288. glowEffect(anchor.next(),1000); // normal member
  289. }
  290. gotoAnchor(anchor,aname,false);
  291. }
  292. function selectAndHighlight(hash,n)
  293. {
  294. var a;
  295. if (hash) {
  296. var link=stripPath(pathName())+':'+hash.substring(1);
  297. a=$('.item a[class$="'+link+'"]');
  298. }
  299. if (a && a.length) {
  300. a.parent().parent().addClass('selected');
  301. a.parent().parent().attr('id','selected');
  302. highlightAnchor();
  303. } else if (n) {
  304. $(n.itemDiv).addClass('selected');
  305. $(n.itemDiv).attr('id','selected');
  306. }
  307. if ($('#nav-tree-contents .item:first').hasClass('selected')) {
  308. $('#nav-sync').css('top','30px');
  309. } else {
  310. $('#nav-sync').css('top','5px');
  311. }
  312. showRoot();
  313. }
  314. function showNode(o, node, index, hash)
  315. {
  316. if (node && node.childrenData) {
  317. if (typeof(node.childrenData)==='string') {
  318. var varName = node.childrenData;
  319. getScript(node.relpath+varName,function(){
  320. node.childrenData = getData(varName);
  321. showNode(o,node,index,hash);
  322. },true);
  323. } else {
  324. if (!node.childrenVisited) {
  325. getNode(o, node);
  326. }
  327. $(node.getChildrenUL()).css({'display':'block'});
  328. node.plus_img.innerHTML = arrowDown;
  329. node.expanded = true;
  330. var n = node.children[o.breadcrumbs[index]];
  331. if (index+1<o.breadcrumbs.length) {
  332. showNode(o,n,index+1,hash);
  333. } else {
  334. if (typeof(n.childrenData)==='string') {
  335. var varName = n.childrenData;
  336. getScript(n.relpath+varName,function(){
  337. n.childrenData = getData(varName);
  338. node.expanded=false;
  339. showNode(o,node,index,hash); // retry with child node expanded
  340. },true);
  341. } else {
  342. var rootBase = stripPath(o.toroot.replace(/\..+$/, ''));
  343. if (rootBase=="index" || rootBase=="pages" || rootBase=="search") {
  344. expandNode(o, n, true, true);
  345. }
  346. selectAndHighlight(hash,n);
  347. }
  348. }
  349. }
  350. } else {
  351. selectAndHighlight(hash);
  352. }
  353. }
  354. function removeToInsertLater(element) {
  355. var parentNode = element.parentNode;
  356. var nextSibling = element.nextSibling;
  357. parentNode.removeChild(element);
  358. return function() {
  359. if (nextSibling) {
  360. parentNode.insertBefore(element, nextSibling);
  361. } else {
  362. parentNode.appendChild(element);
  363. }
  364. };
  365. }
  366. function getNode(o, po)
  367. {
  368. var insertFunction = removeToInsertLater(po.li);
  369. po.childrenVisited = true;
  370. var l = po.childrenData.length-1;
  371. for (var i in po.childrenData) {
  372. var nodeData = po.childrenData[i];
  373. po.children[i] = newNode(o, po, nodeData[0], nodeData[1], nodeData[2],
  374. i==l);
  375. }
  376. insertFunction();
  377. }
  378. function gotoNode(o,subIndex,root,hash,relpath)
  379. {
  380. var nti = navTreeSubIndices[subIndex][root+hash];
  381. o.breadcrumbs = $.extend(true, [], nti ? nti : navTreeSubIndices[subIndex][root]);
  382. if (!o.breadcrumbs && root!=NAVTREE[0][1]) { // fallback: show index
  383. navTo(o,NAVTREE[0][1],"",relpath);
  384. $('.item').removeClass('selected');
  385. $('.item').removeAttr('id');
  386. }
  387. if (o.breadcrumbs) {
  388. o.breadcrumbs.unshift(0); // add 0 for root node
  389. showNode(o, o.node, 0, hash);
  390. }
  391. }
  392. function navTo(o,root,hash,relpath)
  393. {
  394. var link = cachedLink();
  395. if (link) {
  396. var parts = link.split('#');
  397. root = parts[0];
  398. if (parts.length>1) hash = '#'+parts[1].replace(/[^\w\-]/g,'');
  399. else hash='';
  400. }
  401. if (hash.match(/^#l\d+$/)) {
  402. var anchor=$('a[name='+hash.substring(1)+']');
  403. glowEffect(anchor.parent(),1000); // line number
  404. hash=''; // strip line number anchors
  405. }
  406. var url=root+hash;
  407. var i=-1;
  408. while (NAVTREEINDEX[i+1]<=url) i++;
  409. if (i==-1) { i=0; root=NAVTREE[0][1]; } // fallback: show index
  410. if (navTreeSubIndices[i]) {
  411. gotoNode(o,i,root,hash,relpath)
  412. } else {
  413. getScript(relpath+'navtreeindex'+i,function(){
  414. navTreeSubIndices[i] = eval('NAVTREEINDEX'+i);
  415. if (navTreeSubIndices[i]) {
  416. gotoNode(o,i,root,hash,relpath);
  417. }
  418. },true);
  419. }
  420. }
  421. function showSyncOff(n,relpath)
  422. {
  423. n.html('<img src="'+relpath+'sync_off.png" title="'+SYNCOFFMSG+'"/>');
  424. }
  425. function showSyncOn(n,relpath)
  426. {
  427. n.html('<img src="'+relpath+'sync_on.png" title="'+SYNCONMSG+'"/>');
  428. }
  429. function toggleSyncButton(relpath)
  430. {
  431. var navSync = $('#nav-sync');
  432. if (navSync.hasClass('sync')) {
  433. navSync.removeClass('sync');
  434. showSyncOff(navSync,relpath);
  435. storeLink(stripPath2(pathName())+hashUrl());
  436. } else {
  437. navSync.addClass('sync');
  438. showSyncOn(navSync,relpath);
  439. deleteLink();
  440. }
  441. }
  442. function initNavTree(toroot,relpath)
  443. {
  444. var o = new Object();
  445. o.toroot = toroot;
  446. o.node = new Object();
  447. o.node.li = document.getElementById("nav-tree-contents");
  448. o.node.childrenData = NAVTREE;
  449. o.node.children = new Array();
  450. o.node.childrenUL = document.createElement("ul");
  451. o.node.getChildrenUL = function() { return o.node.childrenUL; };
  452. o.node.li.appendChild(o.node.childrenUL);
  453. o.node.depth = 0;
  454. o.node.relpath = relpath;
  455. o.node.expanded = false;
  456. o.node.isLast = true;
  457. o.node.plus_img = document.createElement("span");
  458. o.node.plus_img.className = 'arrow';
  459. o.node.plus_img.innerHTML = arrowRight;
  460. if (localStorageSupported()) {
  461. var navSync = $('#nav-sync');
  462. if (cachedLink()) {
  463. showSyncOff(navSync,relpath);
  464. navSync.removeClass('sync');
  465. } else {
  466. showSyncOn(navSync,relpath);
  467. }
  468. navSync.click(function(){ toggleSyncButton(relpath); });
  469. }
  470. $(window).load(function(){
  471. navTo(o,toroot,hashUrl(),relpath);
  472. showRoot();
  473. });
  474. $(window).bind('hashchange', function(){
  475. if (window.location.hash && window.location.hash.length>1){
  476. var a;
  477. if ($(location).attr('hash')){
  478. var clslink=stripPath(pathName())+':'+hashValue();
  479. a=$('.item a[class$="'+clslink.replace(/</g,'\\3c ')+'"]');
  480. }
  481. if (a==null || !$(a).parent().parent().hasClass('selected')){
  482. $('.item').removeClass('selected');
  483. $('.item').removeAttr('id');
  484. }
  485. var link=stripPath2(pathName());
  486. navTo(o,link,hashUrl(),relpath);
  487. } else if (!animationInProgress) {
  488. $('#doc-content').scrollTop(0);
  489. $('.item').removeClass('selected');
  490. $('.item').removeAttr('id');
  491. navTo(o,toroot,hashUrl(),relpath);
  492. }
  493. })
  494. }
  495. /* @license-end */