navtree.js 14 KB

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