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 isLargerThanMobile = window.matchMedia('screen and (min-width: 580px)').matc hes; | 12 var isLargerThanMobileQuery = |
| 13 window.matchMedia('screen and (min-width: 580px)'); | |
| 14 isLargerThanMobileQuery.addListener(onMediaQuery); | |
|
Jeffrey Yasskin
2014/02/04 22:22:02
I'm surprised this works before onMediaQuery is de
Arthur Evans
2014/02/07 22:50:47
Moved query definition and listener to the end.
| |
| 13 | 15 |
| 14 var sidebar = document.querySelector('.inline-toc'); | 16 var sidebar = document.querySelector('.inline-toc'); |
| 17 var sidebaroOffsetTop = null; | |
|
Jeffrey Yasskin
2014/02/04 22:22:02
spelling: "sidebaro"?
Arthur Evans
2014/02/07 22:50:47
Done.
| |
| 15 var articleBody = document.querySelector('[itemprop="articleBody"]'); | 18 var articleBody = document.querySelector('[itemprop="articleBody"]'); |
| 19 var setupComplete = false; | |
| 16 | 20 |
| 21 // Bomb out if we're not on an article page and have a TOC. | |
|
Jeffrey Yasskin
2014/02/04 22:22:02
s/if we're not/unless we're/ to avoid precedence p
Arthur Evans
2014/02/07 22:50:47
Done.
| |
| 22 if (!(sidebar && articleBody)) { | |
| 23 return; | |
| 24 } | |
| 17 | 25 |
| 18 function addPermalink(el) { | 26 function addPermalink(el) { |
| 19 el.classList.add('has-permalink'); | 27 el.classList.add('has-permalink'); |
| 20 var id = el.id || el.textContent.toLowerCase().replace(' ', '-'); | 28 var id = el.id || el.textContent.toLowerCase().replace(' ', '-'); |
| 21 el.insertAdjacentHTML('beforeend', | 29 el.insertAdjacentHTML('beforeend', |
| 22 '<a class="permalink" title="Permalink" href="#' + id + '">#</a>'); | 30 '<a class="permalink" title="Permalink" href="#' + id + '">#</a>'); |
| 23 } | 31 } |
| 24 | 32 |
| 25 // Add permalinks to heading elements. | 33 // Add permalinks to heading elements. |
| 26 function addPermalinkHeadings(container) { | 34 function addPermalinkHeadings(container) { |
| 27 if (container) { | 35 if (container) { |
| 28 ['h2','h3','h4'].forEach(function(h, i) { | 36 ['h2','h3','h4'].forEach(function(h, i) { |
| 29 [].forEach.call(container.querySelectorAll(h), addPermalink); | 37 [].forEach.call(container.querySelectorAll(h), addPermalink); |
| 30 }); | 38 }); |
| 31 } | 39 } |
| 32 } | 40 } |
| 33 | 41 |
| 34 // Bomb out if we're not on an article page and have a TOC. | 42 function onScroll(e) { |
|
Jeffrey Yasskin
2014/02/04 22:22:02
We'll be able to replace this with position:sticky
| |
| 35 if (!(sidebar && articleBody)) { | 43 window.scrollY >= sidebaroOffsetTop ? sidebar.classList.add('sticky') : |
| 36 return; | 44 sidebar.classList.remove('sticky'); |
| 37 } | 45 } |
| 38 | 46 |
| 39 if (isLargerThanMobile) { | 47 function onMediaQuery(e) { |
| 40 var toc = sidebar.querySelector('#toc'); | 48 if (e.matches) { |
| 41 var offsetTop = sidebar.offsetParent.offsetTop | 49 // Prevent permalinks from appearing on mobile. |
| 50 document.body.classList.remove('no-permalink'); | |
| 42 | 51 |
| 43 document.addEventListener('scroll', function(e) { | 52 sidebaroOffsetTop = sidebar.offsetParent.offsetTop |
| 44 window.scrollY >= offsetTop ? sidebar.classList.add('sticky') : | |
| 45 sidebar.classList.remove('sticky'); | |
| 46 }); | |
| 47 | 53 |
| 48 toc.addEventListener('click', function(e) { | 54 document.addEventListener('scroll', onScroll); |
| 49 var parent = e.target.parentElement; | 55 |
| 50 if (e.target.localName == 'a' && parent.classList.contains('toplevel')) { | 56 } else { |
| 51 // Allow normal link click if h2 toplevel heading doesn't have h3s. | 57 // Prevent permalinks from appearing on mobile. |
| 52 if (parent.querySelector('.toc')) { | 58 document.body.classList.add('no-permalink'); |
|
Jeffrey Yasskin
2014/02/04 22:22:02
Surely this can be done in the CSS, inside a media
ericbidelman
2014/02/04 22:53:42
It can, but .no-permalink is a generic class we're
Jeffrey Yasskin
2014/02/04 22:58:42
Can you show me where? https://code.google.com/p/c
ericbidelman
2014/02/05 23:58:09
Yep, https://code.google.com/p/chromium/codesearch
Jeffrey Yasskin
2014/02/07 23:37:33
I would still prefer that this SCSS said:
@media
| |
| 53 e.preventDefault(); | 59 |
| 54 parent.classList.toggle('active'); | 60 document.removeEventListener('scroll', onScroll); |
| 55 } | 61 } |
| 62 } | |
| 63 | |
| 64 // Toggle collapsible sections (mobile) | |
|
Jeffrey Yasskin
2014/02/04 22:22:02
This is getting registered in more places than jus
Arthur Evans
2014/02/07 22:50:47
Since the "mobile" logic is based purely on width,
Jeffrey Yasskin
2014/02/07 23:37:33
My thought was that this code is running unconditi
| |
| 65 articleBody.addEventListener('click', function(e) { | |
| 66 if (e.target.localName == 'h2') { | |
| 67 e.target.parentElement.classList.toggle('active'); | |
| 68 } | |
| 69 }); | |
| 70 | |
| 71 sidebar.querySelector('#toc').addEventListener('click', function(e) { | |
| 72 var parent = e.target.parentElement; | |
| 73 if (e.target.localName == 'a' && parent.classList.contains('toplevel')) { | |
| 74 // Allow normal link click if h2 toplevel heading doesn't have h3s. | |
| 75 if (parent.querySelector('.toc')) { | |
| 76 e.preventDefault(); | |
| 77 parent.classList.toggle('active'); | |
| 56 } | 78 } |
| 57 }); | 79 } |
| 80 }); | |
| 58 | 81 |
| 59 } else { | 82 onMediaQuery(isLargerThanMobileQuery); |
| 60 // Prevent permalinks from appearong on mobile. | |
| 61 document.body.classList.add('no-permalink'); | |
| 62 | |
| 63 articleBody.addEventListener('click', function(e) { | |
| 64 if (e.target.localName == 'h2') { | |
| 65 e.target.parentElement.classList.toggle('active'); | |
| 66 } | |
| 67 }); | |
| 68 } | |
| 69 | 83 |
| 70 addPermalinkHeadings(articleBody); | 84 addPermalinkHeadings(articleBody); |
| 71 | 85 |
| 72 }()); | 86 }()); |
| 87 | |
|
Jeffrey Yasskin
2014/02/04 22:22:02
Try not to add extra empty lines at the end of the
Arthur Evans
2014/02/07 22:50:47
Done.
| |
| OLD | NEW |