Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(633)

Side by Side Diff: chrome/common/extensions/docs/static/js/article.js

Issue 160593003: Docserver: Fixes for expand/collapse issue and fatnav closing on mouseleave. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/common/extensions/docs/static/js/fatnav.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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: 581px)');
13 14
14 var sidebar = document.querySelector('.inline-toc'); 15 var sidebar = document.querySelector('.inline-toc');
16 var sidebarOffsetTop = null;
15 var articleBody = document.querySelector('[itemprop="articleBody"]'); 17 var articleBody = document.querySelector('[itemprop="articleBody"]');
16 18
19 // Bomb out unless we're on an article page and have a TOC.
20 if (!(sidebar && articleBody)) {
21 return;
22 }
17 23
18 function addPermalink(el) { 24 function addPermalink(el) {
19 el.classList.add('has-permalink'); 25 el.classList.add('has-permalink');
20 var id = el.id || el.textContent.toLowerCase().replace(' ', '-'); 26 var id = el.id || el.textContent.toLowerCase().replace(' ', '-');
21 el.insertAdjacentHTML('beforeend', 27 el.insertAdjacentHTML('beforeend',
22 '<a class="permalink" title="Permalink" href="#' + id + '">#</a>'); 28 '<a class="permalink" title="Permalink" href="#' + id + '">#</a>');
23 } 29 }
24 30
25 // Add permalinks to heading elements. 31 // Add permalinks to heading elements.
26 function addPermalinkHeadings(container) { 32 function addPermalinkHeadings(container) {
27 if (container) { 33 if (container) {
28 ['h2','h3','h4'].forEach(function(h, i) { 34 ['h2','h3','h4'].forEach(function(h, i) {
29 [].forEach.call(container.querySelectorAll(h), addPermalink); 35 [].forEach.call(container.querySelectorAll(h), addPermalink);
30 }); 36 });
31 } 37 }
32 } 38 }
33 39
34 // Bomb out if we're not on an article page and have a TOC. 40 function onScroll(e) {
35 if (!(sidebar && articleBody)) { 41 window.scrollY >= sidebarOffsetTop ? sidebar.classList.add('sticky') :
36 return; 42 sidebar.classList.remove('sticky');
Renato Mangini (chromium) 2014/02/13 01:35:15 nit: fix indentation
37 } 43 }
38 44
39 if (isLargerThanMobile) { 45 function onMediaQuery(e) {
40 var toc = sidebar.querySelector('#toc'); 46 if (e.matches) {
41 var offsetTop = sidebar.offsetParent.offsetTop 47 // Prevent permalinks from appearing on mobile.
Renato Mangini (chromium) 2014/02/13 01:35:15 I think this comment is wrong, since removing a "n
48 document.body.classList.remove('no-permalink');
42 49
Renato Mangini (chromium) 2014/02/13 01:35:15 nit: remove white lines in these if/else blocks, s
43 document.addEventListener('scroll', function(e) { 50 sidebarOffsetTop = sidebar.offsetParent.offsetTop
44 window.scrollY >= offsetTop ? sidebar.classList.add('sticky') :
45 sidebar.classList.remove('sticky');
46 });
47 51
48 // Add +/- expander to headings with subheading children. 52 document.addEventListener('scroll', onScroll);
49 [].forEach.call(toc.querySelectorAll('.toplevel'), function(heading) { 53 } else {
50 if (heading.querySelector('.toc')) { 54 // Prevent permalinks from appearing on mobile.
51 heading.firstChild.classList.add('hastoc'); 55 document.body.classList.add('no-permalink');
52 }
53 });
54 56
55 toc.addEventListener('click', function(e) { 57 document.removeEventListener('scroll', onScroll);
56 var parent = e.target.parentElement; 58 }
57 if (e.target.localName == 'a' && e.target.classList.contains('hastoc') && 59 }
58 parent.classList.contains('toplevel')) { 60
59 // Allow normal link click if h2 toplevel heading doesn't have h3s. 61 // Toggle collapsible sections (mobile).
62 articleBody.addEventListener('click', function(e) {
63 if (e.target.localName == 'h2' && !isLargerThanMobileQuery.matches) {
64 e.target.parentElement.classList.toggle('active');
65 }
66 });
67
68 sidebar.querySelector('#toc').addEventListener('click', function(e) {
69 var parent = e.target.parentElement;
70 if (e.target.localName == 'a' && parent.classList.contains('toplevel')) {
71 // Allow normal link click if h2 toplevel heading doesn't have h3s.
72 if (parent.querySelector('.toc')) {
60 e.preventDefault(); 73 e.preventDefault();
61 parent.classList.toggle('active'); 74 parent.classList.toggle('active');
62 } 75 }
63 }); 76 }
77 });
64 78
65 } else { 79 // Add +/- expander to headings with subheading children.
66 // Prevent permalinks from appearong on mobile. 80 [].forEach.call(toc.querySelectorAll('.toplevel'), function(heading) {
67 document.body.classList.add('no-permalink'); 81 if (heading.querySelector('.toc')) {
82 heading.firstChild.classList.add('hastoc');
83 }
84 });
68 85
69 articleBody.addEventListener('click', function(e) { 86 isLargerThanMobileQuery.addListener(onMediaQuery);
70 if (e.target.localName == 'h2') { 87 onMediaQuery(isLargerThanMobileQuery);
71 e.target.parentElement.classList.toggle('active');
72 }
73 });
74 }
75 88
76 addPermalinkHeadings(articleBody); 89 addPermalinkHeadings(articleBody);
77 90
78 }()); 91 }());
OLDNEW
« no previous file with comments | « no previous file | chrome/common/extensions/docs/static/js/fatnav.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698