navtree.js 14 KB

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