Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Scroll handling. | 5 // Scroll handling. |
| 6 // | 6 // |
| 7 // Switches the sidebar between floating on the left and position:fixed | 7 // Switches the sidebar between floating on the left and position:fixed |
| 8 // depending on whether it's scrolled into view, and manages the scroll-to-top | 8 // depending on whether it's scrolled into view, and manages the scroll-to-top |
| 9 // button: click logic, and when to show it. | 9 // button: click logic, and when to show it. |
| 10 (function() { | 10 (function() { |
| 11 | 11 |
| 12 var sidebar = document.querySelector('.inline-toc'); | |
| 13 var articleBody = document.querySelector('[itemprop="articleBody"]'); | |
| 14 | |
| 15 // Bomb out unless we're on an article page and have a TOC. | |
| 16 // Ideally, template shouldn't load this JS file on a non-article page | |
| 17 if (!(sidebar && articleBody)) { | |
| 18 return; | |
| 19 } | |
| 20 | |
| 12 var isLargerThanMobileQuery = | 21 var isLargerThanMobileQuery = |
| 13 window.matchMedia('screen and (min-width: 581px)'); | 22 window.matchMedia('screen and (min-width: 581px)'); |
| 14 | 23 |
| 15 var sidebar = document.querySelector('.inline-toc'); | 24 var toc = sidebar.querySelector('#toc'); |
| 16 var sidebarOffsetTop = null; | |
| 17 var articleBody = document.querySelector('[itemprop="articleBody"]'); | |
| 18 | |
| 19 // Bomb out unless we're on an article page and have a TOC. | |
| 20 if (!(sidebar && articleBody)) { | |
| 21 return; | |
| 22 } | |
| 23 | 25 |
| 24 function addPermalink(el) { | 26 function addPermalink(el) { |
| 25 el.classList.add('has-permalink'); | 27 el.classList.add('has-permalink'); |
| 26 var id = el.id || el.textContent.toLowerCase().replace(' ', '-'); | 28 var id = el.id || el.textContent.toLowerCase().replace(' ', '-'); |
| 27 el.insertAdjacentHTML('beforeend', | 29 el.insertAdjacentHTML('beforeend', |
| 28 '<a class="permalink" title="Permalink" href="#' + id + '">#</a>'); | 30 '<a class="permalink" title="Permalink" href="#' + id + '">#</a>'); |
| 29 } | 31 } |
| 30 | 32 |
| 31 // Add permalinks to heading elements. | 33 // Add permalinks to heading elements. |
| 32 function addPermalinkHeadings(container) { | 34 function addPermalinkHeadings(container) { |
| 33 if (container) { | 35 if (container) { |
| 34 ['h2','h3','h4'].forEach(function(h, i) { | 36 ['h2','h3','h4'].forEach(function(h, i) { |
| 35 [].forEach.call(container.querySelectorAll(h), addPermalink); | 37 [].forEach.call(container.querySelectorAll(h), addPermalink); |
| 36 }); | 38 }); |
| 37 } | 39 } |
| 38 } | 40 } |
| 39 | 41 |
| 42 function toggleStickySidenav(){ | |
| 43 // Note: Attempting to read offsetTop with sticky on causes toc overlap | |
| 44 toc.classList.remove('sticky'); | |
|
pearlchen
2014/06/06 17:36:55
(Documenting bug here since I don't know where els
not at google - send to devlin
2014/06/06 17:40:16
ah. so that is avoided by caching the tocOffsetTop
| |
| 45 var tocOffsetTop = sidebar.offsetParent.offsetTop + toc.offsetTop; | |
|
pearlchen
2014/06/06 17:36:56
As requested, tocOffsetTop moved into this functio
not at google - send to devlin
2014/06/06 17:40:16
is this performance hit by the adding/removal of t
pearlchen
2014/06/06 18:30:03
From what I know, evening *reading* offsetTop will
| |
| 46 toc.classList.toggle('sticky', window.scrollY >= tocOffsetTop); | |
| 47 } | |
| 48 | |
| 40 function onScroll(e) { | 49 function onScroll(e) { |
| 41 window.scrollY >= sidebarOffsetTop ? sidebar.classList.add('sticky') : | 50 toggleStickySidenav(); |
| 42 sidebar.classList.remove('sticky'); | |
| 43 } | 51 } |
| 44 | 52 |
| 45 function onMediaQuery(e) { | 53 function onMediaQuery(e) { |
| 46 if (e.matches) { | 54 if (e.matches) { |
| 47 // On tablet & desktop, show permalinks, manage TOC position. | 55 // On tablet & desktop, show permalinks, manage TOC position. |
| 48 document.body.classList.remove('no-permalink'); | 56 document.body.classList.remove('no-permalink'); |
| 49 sidebarOffsetTop = sidebar.offsetParent.offsetTop | |
| 50 document.addEventListener('scroll', onScroll); | 57 document.addEventListener('scroll', onScroll); |
| 58 toggleStickySidenav(); | |
| 51 } else { | 59 } else { |
| 52 // On mobile, hide permalinks. TOC is hidden, doesn't need to scroll. | 60 // On mobile, hide permalinks. TOC is hidden, doesn't need to scroll. |
| 53 document.body.classList.add('no-permalink'); | 61 document.body.classList.add('no-permalink'); |
| 54 document.removeEventListener('scroll', onScroll); | 62 document.removeEventListener('scroll', onScroll); |
| 55 } | 63 } |
| 56 } | 64 } |
| 57 | 65 |
| 58 // Toggle collapsible sections (mobile). | 66 // Toggle collapsible sections (mobile). |
| 59 articleBody.addEventListener('click', function(e) { | 67 articleBody.addEventListener('click', function(e) { |
| 60 if (e.target.localName == 'h2' && !isLargerThanMobileQuery.matches) { | 68 if (e.target.localName == 'h2' && !isLargerThanMobileQuery.matches) { |
| 61 e.target.parentElement.classList.toggle('active'); | 69 e.target.parentElement.classList.toggle('active'); |
| 62 } | 70 } |
| 63 }); | 71 }); |
| 64 | 72 |
| 65 sidebar.querySelector('#toc').addEventListener('click', function(e) { | 73 toc.addEventListener('click', function(e) { |
| 66 var parent = e.target.parentElement; | 74 // React only if clicking on a toplevel menu anchor item |
| 67 if (e.target.localName == 'a' && parent.classList.contains('toplevel')) { | 75 // that is not currently open |
| 68 // Allow normal link click if h2 toplevel heading doesn't have h3s. | 76 if (e.target.classList.contains('hastoc') && |
| 69 if (parent.querySelector('.toc')) { | 77 !e.target.parentElement.classList.contains('active')) { |
| 70 e.preventDefault(); | 78 e.stopPropagation(); |
| 71 parent.classList.toggle('active'); | 79 |
| 72 } | 80 // close any previously open subnavs |
| 81 [].forEach.call(toc.querySelectorAll('.active'), function(li) { | |
| 82 li.classList.remove('active'); | |
| 83 }); | |
| 84 | |
| 85 // then open the clicked one | |
| 86 e.target.parentElement.classList.add('active'); | |
| 73 } | 87 } |
| 74 }); | 88 }); |
| 75 | 89 |
| 76 // Add +/- expander to headings with subheading children. | 90 // Add +/- expander to headings with subheading children. |
| 77 [].forEach.call(toc.querySelectorAll('.toplevel'), function(heading) { | 91 [].forEach.call(toc.querySelectorAll('.toplevel'), function(heading) { |
| 78 if (heading.querySelector('.toc')) { | 92 if (heading.querySelector('.toc')) { |
| 79 heading.firstChild.classList.add('hastoc'); | 93 heading.firstChild.classList.add('hastoc'); |
| 80 } | 94 } |
| 81 }); | 95 }); |
| 82 | 96 |
| 83 isLargerThanMobileQuery.addListener(onMediaQuery); | 97 isLargerThanMobileQuery.addListener(onMediaQuery); |
| 84 onMediaQuery(isLargerThanMobileQuery); | 98 onMediaQuery(isLargerThanMobileQuery); |
| 85 | 99 |
| 86 addPermalinkHeadings(articleBody); | 100 addPermalinkHeadings(articleBody); |
| 87 | 101 |
| 88 }()); | 102 }()); |
| OLD | NEW |