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
3 // found in the LICENSE file.
6 * Adds toggle controls to the fat navbar.
10 var isTouch = (('ontouchstart' in window) || (navigator.msMaxTouchPoints > 0));
11 var isLargerThanPhoneQuery = window.matchMedia('screen and (min-width: 581px)');
13 var fatNav = document.querySelector('#fatnav');
14 var search = document.querySelector('#search');
15 var mobileNavCollasper = document.querySelector('#topnav .collase-icon');
17 var catLinks = fatNav.querySelectorAll('.category > a');
18 var catPath = findCategoryPath();
20 function hideActive(parentNode) {
21 //parentNode.classList.remove('active');
23 [].forEach.call(parentNode.querySelectorAll('.active'), function(el, i) {
24 el.classList.remove('active');
28 function findCategoryPath() {
29 var sel = document.querySelector('.inline-site-toc .selected');
30 if (!sel || !sel.parentElement.previousElementSibling)
32 return sel.parentElement.previousElementSibling.getAttribute('href');
35 function findPillar(el) {
37 while (p && !p.classList.contains('pillar')) {
43 // Clicking outside the fatnav.
44 document.body.addEventListener('click', function(e) {
48 // Fatnav activates onclick and closes on mouseleave.
49 var pillars = document.querySelectorAll('.pillar');
50 [].forEach.call(pillars, function(pillar, i) {
51 pillar.addEventListener('click', function(e) {
52 if (e.target.classList.contains('toplevel')) {
53 e.stopPropagation(); // prevent body handler from being called.
54 var wasAlreadyOpen = this.classList.contains('active');
55 hideActive(fatNav); // de-activate other fatnav items.
56 wasAlreadyOpen ? this.classList.remove('active') :
57 this.classList.add('active');
62 // Search button is used in tablet & desktop mode.
63 // In phone mode search is embedded in the menu.
64 search.addEventListener('click', function(e) {
65 if (!isLargerThanPhoneQuery.matches)
69 // Only toggle if magnifying glass is clicked.
70 if (e.target.localName == 'img') {
71 var wasAlreadyOpen = this.classList.contains('active');
72 hideActive(fatNav); // de-activate other fatnav items.
73 wasAlreadyOpen ? this.classList.remove('active') :
74 this.classList.add('active');
75 if (!wasAlreadyOpen) {
76 var searchField = document.getElementById('chrome-docs-cse-input');
77 var cse = google && google.search && google.search.cse &&
78 google.search.cse.element.getElement('results') || null;
80 cse.clearAllResults();
87 // In phone mode, show the fatnav when the menu button is clicked.
88 mobileNavCollasper.addEventListener('click', function(e) {
89 if (isLargerThanPhoneQuery.matches)
92 fatNav.classList.toggle('active');
93 this.classList.toggle('active');
97 // Hitting ESC hides fatnav menus.
98 document.body.addEventListener('keydown', function(e) {
99 if (e.keyCode == 27) { // ESC
105 // Highlight selected menu item based on the current URL
106 for (var i = 0, a; a = catLinks[i]; i++) {
107 var href = a.getAttribute('href');
108 if (href === window.location.pathname || catPath && href === catPath) {
109 a.classList.add('highlight');
112 p.classList.add('highlight');