[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/webroot/rsrc/js/core/ -> behavior-active-nav.js (source)

   1  /**
   2   * @provides javelin-behavior-phabricator-active-nav
   3   * @requires javelin-behavior
   4   *           javelin-stratcom
   5   *           javelin-vector
   6   *           javelin-dom
   7   *           javelin-uri
   8   */
   9  
  10  JX.behavior('phabricator-active-nav', function(config) {
  11  
  12    var local = JX.$(config.localID);
  13  
  14    /**
  15     * Select the navigation item corresponding to a given anchor.
  16     */
  17    var selectnav = function(anchor) {
  18      var links = JX.DOM.scry(local, 'a');
  19      var link;
  20      var link_anchor;
  21      var selected;
  22      for (var ii = 0; ii < links.length; ii++) {
  23        link = links[ii];
  24        link_anchor = JX.$U(link.href).getFragment();
  25  
  26        selected = (link_anchor == anchor);
  27        JX.DOM.alterClass(
  28          link,
  29          'phabricator-active-nav-focus',
  30          selected);
  31      }
  32    };
  33  
  34  
  35    /**
  36     * Identify the current anchor based on the document scroll position.
  37     */
  38    var updateposition = function() {
  39      // Find all the markers in the document.
  40      var scroll_position = JX.Vector.getScroll().y;
  41      var document_size = JX.Vector.getDocument();
  42      var viewport_size = JX.Vector.getViewport();
  43  
  44      // If we're scrolled all the way down, we always want to select the last
  45      // anchor.
  46      var is_at_bottom = (viewport_size.y + scroll_position >= document_size.y);
  47  
  48      var markers = JX.DOM.scry(document.body, 'legend', 'marker');
  49  
  50      // Sort the markers by Y position, descending.
  51      var markinfo = [];
  52      var ii;
  53      for (ii = 0; ii < markers.length; ii++) {
  54        markinfo.push({
  55          marker: markers[ii],
  56          position: JX.$V(markers[ii]).y - 15
  57        });
  58      }
  59      markinfo.sort(function(u, v) { return (v.position - u.position); });
  60  
  61      // Find the first marker above the current scroll position, or the first
  62      // marker in the document if we're above all the markers.
  63      var active = null;
  64      for (ii = 0; ii < markinfo.length; ii++) {
  65        active = markinfo[ii].marker;
  66        if (markinfo[ii].position <= scroll_position) {
  67          break;
  68        }
  69        if (is_at_bottom) {
  70          break;
  71        }
  72      }
  73  
  74      // If we get above the first marker, select it.
  75      selectnav(active && JX.Stratcom.getData(active).anchor);
  76    };
  77  
  78    var pending = null;
  79    var onviewportchange = function() {
  80      pending && clearTimeout(pending);
  81      pending = setTimeout(updateposition, 100);
  82    };
  83  
  84    JX.Stratcom.listen('scroll', null, onviewportchange);
  85    JX.Stratcom.listen('resize', null, onviewportchange);
  86    JX.Stratcom.listen('hashchange', null, onviewportchange);
  87  });


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1