navtree.js 14 KB

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