Chromium Code Reviews| Index: chrome/common/extensions/docs/static/js/article.js |
| diff --git a/chrome/common/extensions/docs/static/js/article.js b/chrome/common/extensions/docs/static/js/article.js |
| index 1c8ef09f409ee44053c6c329d9394e53fad7e403..57adea5056f96b0b96452640f44d6c3966cfe508 100644 |
| --- a/chrome/common/extensions/docs/static/js/article.js |
| +++ b/chrome/common/extensions/docs/static/js/article.js |
| @@ -13,7 +13,6 @@ var isLargerThanMobileQuery = |
| window.matchMedia('screen and (min-width: 581px)'); |
| var sidebar = document.querySelector('.inline-toc'); |
| -var sidebarOffsetTop = null; |
| var articleBody = document.querySelector('[itemprop="articleBody"]'); |
| // Bomb out unless we're on an article page and have a TOC. |
| @@ -21,6 +20,9 @@ if (!(sidebar && articleBody)) { |
| return; |
| } |
| +var toc = sidebar.querySelector('#toc'); |
| +var tocOffsetTop = sidebar.offsetParent.offsetTop + toc.offsetTop; |
| + |
| function addPermalink(el) { |
| el.classList.add('has-permalink'); |
| var id = el.id || el.textContent.toLowerCase().replace(' ', '-'); |
| @@ -37,16 +39,19 @@ function addPermalinkHeadings(container) { |
| } |
| } |
| +function toggleStickySidenav(){ |
| + toc.classList.toggle('sticky', window.scrollY >= tocOffsetTop); |
| +} |
| + |
| function onScroll(e) { |
| - window.scrollY >= sidebarOffsetTop ? sidebar.classList.add('sticky') : |
| - sidebar.classList.remove('sticky'); |
| + toggleStickySidenav(); |
| } |
| function onMediaQuery(e) { |
| if (e.matches) { |
| // On tablet & desktop, show permalinks, manage TOC position. |
| document.body.classList.remove('no-permalink'); |
| - sidebarOffsetTop = sidebar.offsetParent.offsetTop |
| + tocOffsetTop = sidebar.offsetParent.offsetTop + toc.offsetTop; |
|
not at google - send to devlin
2014/06/05 20:35:39
so yeah... re-setting this global variable implies
pearlchen
2014/06/05 21:15:47
A get, or a set?
e.g.
function updateTocOffsetTo
not at google - send to devlin
2014/06/05 22:28:35
a get; try to take away the global variable
|
| document.addEventListener('scroll', onScroll); |
| } else { |
| // On mobile, hide permalinks. TOC is hidden, doesn't need to scroll. |
| @@ -62,14 +67,20 @@ articleBody.addEventListener('click', function(e) { |
| } |
| }); |
| -sidebar.querySelector('#toc').addEventListener('click', function(e) { |
| - var parent = e.target.parentElement; |
| - if (e.target.localName == 'a' && parent.classList.contains('toplevel')) { |
| - // Allow normal link click if h2 toplevel heading doesn't have h3s. |
| - if (parent.querySelector('.toc')) { |
| - e.preventDefault(); |
| - parent.classList.toggle('active'); |
| - } |
| +toc.addEventListener('click', function(e) { |
| + // React only if clicking on a toplevel menu anchor item |
| + // that is not currently open |
| + if (e.target.classList.contains('hastoc') && |
| + !e.target.parentElement.classList.contains('active')) { |
| + e.stopPropagation(); |
| + |
| + // close any previously open subnavs |
| + [].forEach.call(toc.querySelectorAll('.active'), function(li) { |
| + li.classList.remove('active'); |
| + }); |
| + |
| + // then open the clicked one |
| + e.target.parentElement.classList.add('active'); |
| } |
| }); |
| @@ -82,6 +93,7 @@ sidebar.querySelector('#toc').addEventListener('click', function(e) { |
| isLargerThanMobileQuery.addListener(onMediaQuery); |
| onMediaQuery(isLargerThanMobileQuery); |
| +toggleStickySidenav(); |
| addPermalinkHeadings(articleBody); |