navtree.js 14.3 KB
Newer Older
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1
var navTreeSubIndices = new Array();
2

3 4 5 6
function getData(varName)
{
  var i = varName.lastIndexOf('/');
  var n = i>=0 ? varName.substring(i+1) : varName;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
7
  return eval(n.replace(/\-/g,'_'));
8 9 10 11 12 13 14
}

function stripPath(uri)
{
  return uri.substring(uri.lastIndexOf('/')+1);
}

Dimitri van Heesch's avatar
Dimitri van Heesch committed
15 16 17 18 19 20 21 22
function stripPath2(uri)
{
  var i = uri.lastIndexOf('/');
  var s = uri.substring(i+1);
  var m = uri.substring(0,i+1).match(/\/d\w\/d\w\w\/$/);
  return m ? uri.substring(i-6) : s;
}

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
function hashValue()
{
  return $(location).attr('hash').substring(1).replace(/[^\w\-]/g,'');
}

function hashUrl()
{
  return '#'+hashValue();
}

function pathName()
{
  return $(location).attr('pathname').replace(/[^-A-Za-z0-9+&@#/%?=~_|!:,.;\(\)]/g, '');
}

Dimitri van Heesch's avatar
Dimitri van Heesch committed
38 39
function localStorageSupported()
{
40 41 42 43 44 45
  try {
    return 'localStorage' in window && window['localStorage'] !== null && window.localStorage.getItem;
  }
  catch(e) {
    return false;
  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
46 47
}

48

Dimitri van Heesch's avatar
Dimitri van Heesch committed
49 50 51 52 53 54 55 56 57 58 59
function storeLink(link)
{
  if (!$("#nav-sync").hasClass('sync') && localStorageSupported()) {
      window.localStorage.setItem('navpath',link);
  }
}

function deleteLink()
{
  if (localStorageSupported()) {
    window.localStorage.setItem('navpath','');
60
  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
61 62 63 64 65 66 67 68 69 70 71
}

function cachedLink()
{
  if (localStorageSupported()) {
    return window.localStorage.getItem('navpath');
  } else {
    return '';
  }
}

72 73 74 75 76 77 78 79
function getScript(scriptName,func,show)
{
  var head = document.getElementsByTagName("head")[0]; 
  var script = document.createElement('script');
  script.id = scriptName;
  script.type = 'text/javascript';
  script.onload = func; 
  script.src = scriptName+'.js'; 
80
  if ($.browser.msie && $.browser.version<=8) { 
81
    // script.onload does not work with older versions of IE
82 83 84 85
    script.onreadystatechange = function() {
      if (script.readyState=='complete' || script.readyState=='loaded') { 
        func(); if (show) showRoot(); 
      }
86
    }
87
  }
88 89 90
  head.appendChild(script); 
}

91 92
function createIndent(o,domNode,node,level)
{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
93 94 95 96
  var level=-1;
  var n = node;
  while (n.parentNode) { level++; n=n.parentNode; }
  if (node.childrenData) {
97 98 99 100 101
    var imgNode = document.createElement("img");
    imgNode.style.paddingLeft=(16*level).toString()+'px';
    imgNode.width  = 16;
    imgNode.height = 22;
    imgNode.border = 0;
102 103 104
    node.plus_img = imgNode;
    node.expandToggle = document.createElement("a");
    node.expandToggle.href = "javascript:void(0)";
105 106
    node.expandToggle.onclick = function() {
      if (node.expanded) {
107
        $(node.getChildrenUL()).slideUp("fast");
Dimitri van Heesch's avatar
Dimitri van Heesch committed
108
        node.plus_img.src = node.relpath+"ftv2pnode.png";
109
        node.expanded = false;
110
      } else {
111
        expandNode(o, node, false, false);
112 113 114 115
      }
    }
    node.expandToggle.appendChild(imgNode);
    domNode.appendChild(node.expandToggle);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
116
    imgNode.src = node.relpath+"ftv2pnode.png";
117
  } else {
118 119 120 121
    var span = document.createElement("span");
    span.style.display = 'inline-block';
    span.style.width   = 16*(level+1)+'px';
    span.style.height  = '22px';
122
    span.innerHTML = '&#160;';
123
    domNode.appendChild(span);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
124
  } 
125 126
}

127 128 129 130 131
var animationInProgress = false;

function gotoAnchor(anchor,aname,updateLocation)
{
  var pos, docContent = $('#doc-content');
132 133 134 135 136
  var ancParent = $(anchor.parent());
  if (ancParent.hasClass('memItemLeft') ||
      ancParent.hasClass('fieldname') ||
      ancParent.hasClass('fieldtype') ||
      ancParent.is(':header'))
137
  {
138
    pos = ancParent.position().top;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  } else if (anchor.position()) {
    pos = anchor.position().top;
  }
  if (pos) {
    var dist = Math.abs(Math.min(
               pos-docContent.offset().top,
               docContent[0].scrollHeight-
               docContent.height()-docContent.scrollTop()));
    animationInProgress=true;
    docContent.animate({
      scrollTop: pos + docContent.scrollTop() - docContent.offset().top
    },Math.max(50,Math.min(500,dist)),function(){
      if (updateLocation) window.location.href=aname;
      animationInProgress=false;
    });
  }
}

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
function newNode(o, po, text, link, childrenData, lastNode)
{
  var node = new Object();
  node.children = Array();
  node.childrenData = childrenData;
  node.depth = po.depth + 1;
  node.relpath = po.relpath;
  node.isLast = lastNode;

  node.li = document.createElement("li");
  po.getChildrenUL().appendChild(node.li);
  node.parentNode = po;

  node.itemDiv = document.createElement("div");
  node.itemDiv.className = "item";

  node.labelSpan = document.createElement("span");
  node.labelSpan.className = "label";

  createIndent(o,node.itemDiv,node,0);
  node.itemDiv.appendChild(node.labelSpan);
  node.li.appendChild(node.itemDiv);

  var a = document.createElement("a");
  node.labelSpan.appendChild(a);
  node.label = document.createTextNode(text);
183
  node.expanded = false;
184
  a.appendChild(node.label);
185 186 187 188 189 190 191 192
  if (link) {
    var url;
    if (link.substring(0,1)=='^') {
      url = link.substring(1);
      link = url;
    } else {
      url = node.relpath+link;
    }
193
    a.className = stripPath(link.replace('#',':'));
194
    if (link.indexOf('#')!=-1) {
195
      var aname = '#'+link.split('#')[1];
196
      var srcPage = stripPath(pathName());
197
      var targetPage = stripPath(link.split('#')[0]);
198
      a.href = srcPage!=targetPage ? url : "javascript:void(0)"; 
199
      a.onclick = function(){
Dimitri van Heesch's avatar
Dimitri van Heesch committed
200
        storeLink(link);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
201 202 203 204 205 206 207
        if (!$(a).parent().parent().hasClass('selected'))
        {
          $('.item').removeClass('selected');
          $('.item').removeAttr('id');
          $(a).parent().parent().addClass('selected');
          $(a).parent().parent().attr('id','selected');
        }
208 209
        var anchor = $(aname);
        gotoAnchor(anchor,aname,true);
210
      };
211 212
    } else {
      a.href = url;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
213
      a.onclick = function() { storeLink(link); }
214
    }
215
  } else {
216 217 218 219 220 221 222 223 224
    if (childrenData != null) 
    {
      a.className = "nolink";
      a.href = "javascript:void(0)";
      a.onclick = node.expandToggle.onclick;
    }
  }

  node.childrenUL = null;
225 226
  node.getChildrenUL = function() {
    if (!node.childrenUL) {
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
      node.childrenUL = document.createElement("ul");
      node.childrenUL.className = "children_ul";
      node.childrenUL.style.display = "none";
      node.li.appendChild(node.childrenUL);
    }
    return node.childrenUL;
  };

  return node;
}

function showRoot()
{
  var headerHeight = $("#top").height();
  var footerHeight = $("#nav-path").height();
  var windowHeight = $(window).height() - headerHeight - footerHeight;
243 244
  (function (){ // retry until we can scroll to the selected item
    try {
245
      var navtree=$('#nav-tree');
246 247 248 249 250
      navtree.scrollTo('#selected',0,{offset:-windowHeight/2});
    } catch (err) {
      setTimeout(arguments.callee, 0);
    }
  })();
251 252
}

253
function expandNode(o, node, imm, showRoot)
254
{
255 256
  if (node.childrenData && !node.expanded) {
    if (typeof(node.childrenData)==='string') {
257 258 259 260 261
      var varName    = node.childrenData;
      getScript(node.relpath+varName,function(){
        node.childrenData = getData(varName);
        expandNode(o, node, imm, showRoot);
      }, showRoot);
262 263
    } else {
      if (!node.childrenVisited) {
264
        getNode(o, node);
265 266
      } if (imm || ($.browser.msie && $.browser.version>8)) { 
        // somehow slideDown jumps to the start of tree for IE9 :-(
267
        $(node.getChildrenUL()).show();
268
      } else {
269 270
        $(node.getChildrenUL()).slideDown("fast");
      }
271
      if (node.isLast) {
272
        node.plus_img.src = node.relpath+"ftv2mlastnode.png";
273
      } else {
274 275 276
        node.plus_img.src = node.relpath+"ftv2mnode.png";
      }
      node.expanded = true;
277
    }
278 279 280
  }
}

281 282 283 284 285 286 287
function glowEffect(n,duration)
{
  n.addClass('glow').delay(duration).queue(function(next){
    $(this).removeClass('glow');next();
  });
}

288 289
function highlightAnchor()
{
290
  var aname = hashUrl();
291
  var anchor = $(aname);
292
  if (anchor.parent().attr('class')=='memItemLeft'){
293
    var rows = $('.memberdecls tr[class$="'+hashValue()+'"]');
Dimitri van Heesch's avatar
Dimitri van Heesch committed
294
    glowEffect(rows.children(),300); // member without details
295 296
  } else if (anchor.parent().attr('class')=='fieldname'){
    glowEffect(anchor.parent().parent(),1000); // enum value
Dimitri van Heesch's avatar
Dimitri van Heesch committed
297 298
  } else if (anchor.parent().attr('class')=='fieldtype'){
    glowEffect(anchor.parent().parent(),1000); // struct field
299
  } else if (anchor.parent().is(":header")) {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
300
    glowEffect(anchor.parent(),1000); // section header
301
  } else {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
302
    glowEffect(anchor.next(),1000); // normal member
303
  }
304
  gotoAnchor(anchor,aname,false);
305 306
}

Dimitri van Heesch's avatar
Dimitri van Heesch committed
307
function selectAndHighlight(hash,n)
308 309
{
  var a;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
310
  if (hash) {
311 312
    var link=stripPath(pathName())+':'+hash.substring(1);
    a=$('.item a[class$="'+link+'"]');
313 314 315 316 317 318 319 320
  }
  if (a && a.length) {
    a.parent().parent().addClass('selected');
    a.parent().parent().attr('id','selected');
    highlightAnchor();
  } else if (n) {
    $(n.itemDiv).addClass('selected');
    $(n.itemDiv).attr('id','selected');
321
  }
322 323 324 325 326
  if ($('#nav-tree-contents .item:first').hasClass('selected')) {
    $('#nav-sync').css('top','30px');
  } else {
    $('#nav-sync').css('top','5px');
  }
327
  showRoot();
328 329
}

Dimitri van Heesch's avatar
Dimitri van Heesch committed
330
function showNode(o, node, index, hash)
331
{
332
  if (node && node.childrenData) {
333
    if (typeof(node.childrenData)==='string') {
334 335 336
      var varName    = node.childrenData;
      getScript(node.relpath+varName,function(){
        node.childrenData = getData(varName);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
337
        showNode(o,node,index,hash);
338
      },true);
339 340
    } else {
      if (!node.childrenVisited) {
341 342
        getNode(o, node);
      }
343
      $(node.getChildrenUL()).css({'display':'block'});
344
      if (node.isLast) {
345
        node.plus_img.src = node.relpath+"ftv2mlastnode.png";
346
      } else {
347 348 349 350
        node.plus_img.src = node.relpath+"ftv2mnode.png";
      }
      node.expanded = true;
      var n = node.children[o.breadcrumbs[index]];
351
      if (index+1<o.breadcrumbs.length) {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
352
        showNode(o,n,index+1,hash);
353 354
      } else {
        if (typeof(n.childrenData)==='string') {
355 356 357 358
          var varName = n.childrenData;
          getScript(n.relpath+varName,function(){
            n.childrenData = getData(varName);
            node.expanded=false;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
359
            showNode(o,node,index,hash); // retry with child node expanded
360
          },true);
361
        } else {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
362
          var rootBase = stripPath(o.toroot.replace(/\..+$/, ''));
Dimitri van Heesch's avatar
Dimitri van Heesch committed
363
          if (rootBase=="index" || rootBase=="pages" || rootBase=="search") {
364 365
            expandNode(o, n, true, true);
          }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
366
          selectAndHighlight(hash,n);
367 368
        }
      }
369
    }
370
  } else {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
371
    selectAndHighlight(hash);
372 373 374
  }
}

375 376 377 378 379 380 381 382 383 384 385 386 387
function removeToInsertLater(element) {
  var parentNode = element.parentNode;
  var nextSibling = element.nextSibling;
  parentNode.removeChild(element);
  return function() {
    if (nextSibling) {
      parentNode.insertBefore(element, nextSibling);
    } else {
      parentNode.appendChild(element);
    }
  };
}

388 389
function getNode(o, po)
{
390
  var insertFunction = removeToInsertLater(po.li);
391 392
  po.childrenVisited = true;
  var l = po.childrenData.length-1;
393
  for (var i in po.childrenData) {
394 395
    var nodeData = po.childrenData[i];
    po.children[i] = newNode(o, po, nodeData[0], nodeData[1], nodeData[2],
396
      i==l);
397
  }
398
  insertFunction();
399 400
}

Dimitri van Heesch's avatar
Dimitri van Heesch committed
401
function gotoNode(o,subIndex,root,hash,relpath)
402
{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
403
  var nti = navTreeSubIndices[subIndex][root+hash];
Dimitri van Heesch's avatar
Dimitri van Heesch committed
404
  o.breadcrumbs = $.extend(true, [], nti ? nti : navTreeSubIndices[subIndex][root]);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
405 406 407 408 409 410
  if (!o.breadcrumbs && root!=NAVTREE[0][1]) { // fallback: show index
    navTo(o,NAVTREE[0][1],"",relpath);
    $('.item').removeClass('selected');
    $('.item').removeAttr('id');
  }
  if (o.breadcrumbs) {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
411
    o.breadcrumbs.unshift(0); // add 0 for root node
Dimitri van Heesch's avatar
Dimitri van Heesch committed
412
    showNode(o, o.node, 0, hash);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
413 414 415
  }
}

Dimitri van Heesch's avatar
Dimitri van Heesch committed
416
function navTo(o,root,hash,relpath)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
417
{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
418 419 420 421
  var link = cachedLink();
  if (link) {
    var parts = link.split('#');
    root = parts[0];
422
    if (parts.length>1) hash = '#'+parts[1].replace(/[^\w\-]/g,'');
Dimitri van Heesch's avatar
Dimitri van Heesch committed
423 424
    else hash='';
  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
425 426 427
  if (hash.match(/^#l\d+$/)) {
    var anchor=$('a[name='+hash.substring(1)+']');
    glowEffect(anchor.parent(),1000); // line number
Dimitri van Heesch's avatar
Dimitri van Heesch committed
428 429 430 431
    hash=''; // strip line number anchors
  }
  var url=root+hash;
  var i=-1;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
432
  while (NAVTREEINDEX[i+1]<=url) i++;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
433
  if (i==-1) { i=0; root=NAVTREE[0][1]; } // fallback: show index
Dimitri van Heesch's avatar
Dimitri van Heesch committed
434
  if (navTreeSubIndices[i]) {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
435
    gotoNode(o,i,root,hash,relpath)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
436 437 438 439
  } else {
    getScript(relpath+'navtreeindex'+i,function(){
      navTreeSubIndices[i] = eval('NAVTREEINDEX'+i);
      if (navTreeSubIndices[i]) {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
440
        gotoNode(o,i,root,hash,relpath);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
441 442 443
      }
    },true);
  }
444 445
}

446 447 448 449 450 451 452
function showSyncOff(n,relpath)
{
    n.html('<img src="'+relpath+'sync_off.png" title="'+SYNCOFFMSG+'"/>');
}

function showSyncOn(n,relpath)
{
453
    n.html('<img src="'+relpath+'sync_on.png" title="'+SYNCONMSG+'"/>');
454 455
}

Dimitri van Heesch's avatar
Dimitri van Heesch committed
456 457 458 459 460
function toggleSyncButton(relpath)
{
  var navSync = $('#nav-sync');
  if (navSync.hasClass('sync')) {
    navSync.removeClass('sync');
461
    showSyncOff(navSync,relpath);
462
    storeLink(stripPath2(pathName())+hashUrl());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
463 464
  } else {
    navSync.addClass('sync');
465
    showSyncOn(navSync,relpath);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
466 467 468 469
    deleteLink();
  }
}

470 471 472 473 474 475 476 477 478 479 480 481 482
function initNavTree(toroot,relpath)
{
  var o = new Object();
  o.toroot = toroot;
  o.node = new Object();
  o.node.li = document.getElementById("nav-tree-contents");
  o.node.childrenData = NAVTREE;
  o.node.children = new Array();
  o.node.childrenUL = document.createElement("ul");
  o.node.getChildrenUL = function() { return o.node.childrenUL; };
  o.node.li.appendChild(o.node.childrenUL);
  o.node.depth = 0;
  o.node.relpath = relpath;
483 484 485 486 487 488
  o.node.expanded = false;
  o.node.isLast = true;
  o.node.plus_img = document.createElement("img");
  o.node.plus_img.src = relpath+"ftv2pnode.png";
  o.node.plus_img.width = 16;
  o.node.plus_img.height = 22;
489

Dimitri van Heesch's avatar
Dimitri van Heesch committed
490 491 492
  if (localStorageSupported()) {
    var navSync = $('#nav-sync');
    if (cachedLink()) {
493
      showSyncOff(navSync,relpath);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
494 495
      navSync.removeClass('sync');
    } else {
496
      showSyncOn(navSync,relpath);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
497 498 499 500
    }
    navSync.click(function(){ toggleSyncButton(relpath); });
  }

501
  $(window).load(function(){
502
    navTo(o,toroot,hashUrl(),relpath);
503
    showRoot();
504
  });
505

506 507 508 509
  $(window).bind('hashchange', function(){
     if (window.location.hash && window.location.hash.length>1){
       var a;
       if ($(location).attr('hash')){
510
         var clslink=stripPath(pathName())+':'+hashValue();
511
         a=$('.item a[class$="'+clslink.replace(/</g,'\\3c ')+'"]');
512
       }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
513
       if (a==null || !$(a).parent().parent().hasClass('selected')){
514 515 516
         $('.item').removeClass('selected');
         $('.item').removeAttr('id');
       }
517 518
       var link=stripPath2(pathName());
       navTo(o,link,hashUrl(),relpath);
519 520 521 522
     } else if (!animationInProgress) {
       $('#doc-content').scrollTop(0);
       $('.item').removeClass('selected');
       $('.item').removeAttr('id');
523
       navTo(o,toroot,hashUrl(),relpath);
524 525
     }
  })
526 527
}