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 isLargerThanMobileQuery = | 12 var isLargerThanMobileQuery = |
| 13 window.matchMedia('screen and (min-width: 581px)'); | 13 window.matchMedia('screen and (min-width: 581px)'); |
| 14 | 14 |
| 15 var sidebar = document.querySelector('.inline-toc'); | 15 var sidebar = document.querySelector('.inline-toc'); |
| 16 var sidebarOffsetTop = null; | |
| 17 var articleBody = document.querySelector('[itemprop="articleBody"]'); | 16 var articleBody = document.querySelector('[itemprop="articleBody"]'); |
| 18 | 17 |
| 19 // Bomb out unless we're on an article page and have a TOC. | 18 // Bomb out unless we're on an article page and have a TOC. |
| 20 if (!(sidebar && articleBody)) { | 19 if (!(sidebar && articleBody)) { |
| 21 return; | 20 return; |
| 22 } | 21 } |
| 23 | 22 |
| 23 var toc = sidebar.querySelector('#toc'); | |
| 24 var tocOffsetTop = null; | |
|
not at google - send to devlin
2014/06/03 17:44:00
caching this doesn't save much, and I'm not sure w
pearlchen
2014/06/03 22:52:13
But then this value would have to be calculated (s
not at google - send to devlin
2014/06/05 17:09:40
what's the problem with adding up a couple of valu
pearlchen
2014/06/05 19:38:46
Doesn't continual reading of the DOM affect perfor
| |
| 25 var debounceTimeout; | |
| 26 | |
| 24 function addPermalink(el) { | 27 function addPermalink(el) { |
| 25 el.classList.add('has-permalink'); | 28 el.classList.add('has-permalink'); |
| 26 var id = el.id || el.textContent.toLowerCase().replace(' ', '-'); | 29 var id = el.id || el.textContent.toLowerCase().replace(' ', '-'); |
| 27 el.insertAdjacentHTML('beforeend', | 30 el.insertAdjacentHTML('beforeend', |
| 28 '<a class="permalink" title="Permalink" href="#' + id + '">#</a>'); | 31 '<a class="permalink" title="Permalink" href="#' + id + '">#</a>'); |
| 29 } | 32 } |
| 30 | 33 |
| 31 // Add permalinks to heading elements. | 34 // Add permalinks to heading elements. |
| 32 function addPermalinkHeadings(container) { | 35 function addPermalinkHeadings(container) { |
| 33 if (container) { | 36 if (container) { |
| 34 ['h2','h3','h4'].forEach(function(h, i) { | 37 ['h2','h3','h4'].forEach(function(h, i) { |
| 35 [].forEach.call(container.querySelectorAll(h), addPermalink); | 38 [].forEach.call(container.querySelectorAll(h), addPermalink); |
| 36 }); | 39 }); |
| 37 } | 40 } |
| 38 } | 41 } |
| 39 | 42 |
| 43 function toggleStickySidenav(){ | |
| 44 window.scrollY >= tocOffsetTop ? toc.classList.add('sticky') : | |
| 45 toc.classList.remove('sticky'); | |
|
not at google - send to devlin
2014/06/03 17:44:00
you can write this as toc.classList.toggle('sticky
pearlchen
2014/06/05 19:38:47
Done.
| |
| 46 } | |
| 47 | |
| 40 function onScroll(e) { | 48 function onScroll(e) { |
| 41 window.scrollY >= sidebarOffsetTop ? sidebar.classList.add('sticky') : | 49 window.clearTimeout( debounceTimeout ); |
|
not at google - send to devlin
2014/06/03 17:44:00
no spaces
pearlchen
2014/06/05 19:38:47
Done.
| |
| 42 sidebar.classList.remove('sticky'); | 50 debounceTimeout = window.setTimeout(toggleStickySidenav); |
| 51 toggleStickySidenav(); | |
| 43 } | 52 } |
| 44 | 53 |
| 45 function onMediaQuery(e) { | 54 function onMediaQuery(e) { |
| 46 if (e.matches) { | 55 if (e.matches) { |
| 47 // On tablet & desktop, show permalinks, manage TOC position. | 56 // On tablet & desktop, show permalinks, manage TOC position. |
| 48 document.body.classList.remove('no-permalink'); | 57 document.body.classList.remove('no-permalink'); |
| 49 sidebarOffsetTop = sidebar.offsetParent.offsetTop | 58 sidebarOffsetTop = sidebar.offsetParent.offsetTop; |
| 59 tocOffsetTop = sidebarOffsetTop + toc.offsetTop; | |
| 50 document.addEventListener('scroll', onScroll); | 60 document.addEventListener('scroll', onScroll); |
| 51 } else { | 61 } else { |
| 52 // On mobile, hide permalinks. TOC is hidden, doesn't need to scroll. | 62 // On mobile, hide permalinks. TOC is hidden, doesn't need to scroll. |
| 53 document.body.classList.add('no-permalink'); | 63 document.body.classList.add('no-permalink'); |
| 54 document.removeEventListener('scroll', onScroll); | 64 document.removeEventListener('scroll', onScroll); |
| 55 } | 65 } |
| 56 } | 66 } |
| 57 | 67 |
| 58 // Toggle collapsible sections (mobile). | 68 // Toggle collapsible sections (mobile). |
| 59 articleBody.addEventListener('click', function(e) { | 69 articleBody.addEventListener('click', function(e) { |
| 60 if (e.target.localName == 'h2' && !isLargerThanMobileQuery.matches) { | 70 if (e.target.localName == 'h2' && !isLargerThanMobileQuery.matches) { |
| 61 e.target.parentElement.classList.toggle('active'); | 71 e.target.parentElement.classList.toggle('active'); |
| 62 } | 72 } |
| 63 }); | 73 }); |
| 64 | 74 |
| 65 sidebar.querySelector('#toc').addEventListener('click', function(e) { | 75 toc.addEventListener('click', function(e) { |
| 66 var parent = e.target.parentElement; | 76 if (e.target.localName === 'a') { |
|
pearlchen
2014/06/03 22:52:13
Simplified this click callback by changing if to:
| |
| 67 if (e.target.localName == 'a' && parent.classList.contains('toplevel')) { | 77 |
| 68 // Allow normal link click if h2 toplevel heading doesn't have h3s. | 78 // Clicking on subnav menu item where it's toplevel li is already open: |
| 69 if (parent.querySelector('.toc')) { | 79 // do nothing |
| 70 e.preventDefault(); | 80 if (e.target.parentElement.parentElement.parentElement. |
|
not at google - send to devlin
2014/06/03 17:44:00
this looks like it could easily regress. could you
pearlchen
2014/06/03 22:52:13
This always come out false for me. However, I thin
not at google - send to devlin
2014/06/05 17:09:40
i missed out the .length in my code sample, sorry
| |
| 71 parent.classList.toggle('active'); | 81 classList.contains('active')){ |
| 82 return; | |
| 72 } | 83 } |
| 84 | |
| 85 // Clicking on a toplevel menu item: | |
| 86 | |
| 87 // if already open, do nothing | |
| 88 if (e.target.parentElement.classList.contains('active')){ | |
|
not at google - send to devlin
2014/06/03 17:44:00
in fact maybe the selector only needs to be '.acti
| |
| 89 return; | |
| 90 } | |
| 91 | |
| 92 // otherwise, close any previously open subnavs | |
| 93 [].forEach.call(toc.querySelectorAll('.active'), function(li){ | |
|
not at google - send to devlin
2014/06/03 17:44:00
function(li) { not function(li){
pearlchen
2014/06/05 19:38:47
Done.
| |
| 94 li.classList.remove('active'); | |
| 95 }); | |
| 96 | |
| 97 // then open the clicked one | |
| 98 if (e.target.classList.contains('hastoc')) { | |
|
not at google - send to devlin
2014/06/03 17:44:00
do you need to step up the parent chain to find th
| |
| 99 e.target.parentElement.classList.add('active'); | |
| 100 } | |
| 101 | |
| 73 } | 102 } |
| 74 }); | 103 }); |
| 75 | 104 |
| 76 // Add +/- expander to headings with subheading children. | 105 // Add +/- expander to headings with subheading children. |
| 77 [].forEach.call(toc.querySelectorAll('.toplevel'), function(heading) { | 106 [].forEach.call(toc.querySelectorAll('.toplevel'), function(heading) { |
| 78 if (heading.querySelector('.toc')) { | 107 if (heading.querySelector('.toc')) { |
| 79 heading.firstChild.classList.add('hastoc'); | 108 heading.firstChild.classList.add('hastoc'); |
| 80 } | 109 } |
| 81 }); | 110 }); |
| 82 | 111 |
| 83 isLargerThanMobileQuery.addListener(onMediaQuery); | 112 isLargerThanMobileQuery.addListener(onMediaQuery); |
| 84 onMediaQuery(isLargerThanMobileQuery); | 113 onMediaQuery(isLargerThanMobileQuery); |
| 114 toggleStickySidenav(); | |
| 85 | 115 |
| 86 addPermalinkHeadings(articleBody); | 116 addPermalinkHeadings(articleBody); |
| 87 | 117 |
| 88 }()); | 118 }()); |
| OLD | NEW |