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 /** | 5 /** |
6 * Adds toggle controls to the fat navbar. | 6 * Adds toggle controls to the fat navbar. |
7 */ | 7 */ |
8 | 8 |
9 (function() { | 9 (function() { |
10 var isTouch = (('ontouchstart' in window) || (navigator.msMaxTouchPoints > 0)); | 10 var isTouch = (('ontouchstart' in window) || (navigator.msMaxTouchPoints > 0)); |
11 var isLargerThanPhone = window.matchMedia('screen and (min-width: 580px)').match
es; | 11 var isLargerThanPhoneQuery = window.matchMedia('screen and (min-width: 581px)'); |
12 | 12 |
13 var fatNav = document.querySelector('#fatnav'); | 13 var fatNav = document.querySelector('#fatnav'); |
14 var search = document.querySelector('#search'); | 14 var search = document.querySelector('#search'); |
| 15 var mobileNavCollasper = document.querySelector('#topnav .collase-icon'); |
15 | 16 |
16 function hideActive(parentNode) { | 17 function hideActive(parentNode) { |
17 //parentNode.classList.remove('active'); | 18 //parentNode.classList.remove('active'); |
18 | 19 |
19 [].forEach.call(parentNode.querySelectorAll('.active'), function(el, i) { | 20 [].forEach.call(parentNode.querySelectorAll('.active'), function(el, i) { |
20 el.classList.remove('active'); | 21 el.classList.remove('active'); |
21 }); | 22 }); |
22 } | 23 } |
23 | 24 |
24 // Clicking outside the fatnav. | 25 // Clicking outside the fatnav. |
25 document.body.addEventListener('click', function(e) { | 26 document.body.addEventListener('click', function(e) { |
26 hideActive(fatNav); | 27 hideActive(fatNav); |
27 }); | 28 }); |
28 | 29 |
29 | 30 |
30 // Fatnav activates onclick and closes on mouseleave. | 31 // Fatnav activates onclick and closes on mouseleave. |
31 var pillars = document.querySelectorAll('.pillar'); | 32 var pillars = document.querySelectorAll('.pillar'); |
32 [].forEach.call(pillars, function(pillar, i) { | 33 [].forEach.call(pillars, function(pillar, i) { |
33 pillar.addEventListener('click', function(e) { | 34 pillar.addEventListener('click', function(e) { |
34 if (e.target.classList.contains('toplevel')) { | 35 if (e.target.classList.contains('toplevel')) { |
35 e.stopPropagation(); // prevent body handler from being called. | 36 e.stopPropagation(); // prevent body handler from being called. |
36 var wasAlreadyOpen = this.classList.contains('active'); | 37 var wasAlreadyOpen = this.classList.contains('active'); |
37 hideActive(fatNav); // de-activate other fatnav items. | 38 hideActive(fatNav); // de-activate other fatnav items. |
38 wasAlreadyOpen ? this.classList.remove('active') : | 39 wasAlreadyOpen ? this.classList.remove('active') : |
39 this.classList.add('active'); | 40 this.classList.add('active'); |
40 } | 41 } |
41 }); | 42 }); |
42 }); | 43 }); |
43 | 44 |
44 if (isLargerThanPhone) { | 45 // Search button is used in tablet & desktop mode. |
45 search.addEventListener('click', function(e) { | 46 // In phone mode search is embedded in the menu. |
46 e.stopPropagation(); | 47 search.addEventListener('click', function(e) { |
| 48 if (!isLargerThanPhoneQuery.matches) |
| 49 return; |
| 50 e.stopPropagation(); |
47 | 51 |
48 // Only toggle if magnifying glass is clicked. | 52 // Only toggle if magnifying glass is clicked. |
49 if (e.target.localName == 'img') { | 53 if (e.target.localName == 'img') { |
50 var wasAlreadyOpen = this.classList.contains('active'); | 54 var wasAlreadyOpen = this.classList.contains('active'); |
51 hideActive(fatNav); // de-activate other fatnav items. | 55 hideActive(fatNav); // de-activate other fatnav items. |
52 wasAlreadyOpen ? this.classList.remove('active') : | 56 wasAlreadyOpen ? this.classList.remove('active') : |
53 this.classList.add('active'); | 57 this.classList.add('active'); |
54 if (!wasAlreadyOpen) { | 58 if (!wasAlreadyOpen) { |
55 var searchField = document.getElementById('chrome-docs-cse-input'); | 59 var searchField = document.getElementById('chrome-docs-cse-input'); |
56 var cse = google && google.search && google.search.cse && google.search.
cse.element.getElement('results') || null; | 60 var cse = google && google.search && google.search.cse && |
57 if (cse) | 61 google.search.cse.element.getElement('results') || null; |
58 cse.clearAllResults(); | 62 if (cse) |
59 searchField.select(); | 63 cse.clearAllResults(); |
60 searchField.focus(); | 64 searchField.select(); |
61 } | 65 searchField.focus(); |
62 } | 66 } |
63 }); | 67 } |
| 68 }); |
64 | 69 |
65 } else { | 70 // In phone mode, show the fatnav when the menu button is clicked. |
66 var mobileNavCollasper = document.querySelector('#topnav .collase-icon'); | 71 mobileNavCollasper.addEventListener('click', function(e) { |
67 mobileNavCollasper.addEventListener('click', function(e) { | 72 if (isLargerThanPhoneQuery.matches) |
68 e.stopPropagation(); | 73 return; |
69 fatNav.classList.toggle('active'); | 74 e.stopPropagation(); |
70 this.classList.toggle('active'); | 75 fatNav.classList.toggle('active'); |
71 }); | 76 this.classList.toggle('active'); |
72 } | 77 }); |
73 | 78 |
74 if (!isTouch) { | 79 if (!isTouch) { |
75 // Hitting ESC hides fatnav menus. | 80 // Hitting ESC hides fatnav menus. |
76 document.body.addEventListener('keydown', function(e) { | 81 document.body.addEventListener('keydown', function(e) { |
77 if (e.keyCode == 27) { // ESC | 82 if (e.keyCode == 27) { // ESC |
78 hideActive(fatNav); | 83 hideActive(fatNav); |
79 } | 84 } |
80 }); | 85 }); |
81 } | 86 } |
82 | 87 |
83 })(); | 88 })(); |
OLD | NEW |