OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 * Javascript for Sidebar, served from chrome://bluetooth-internals/. | 6 * Javascript for Sidebar, served from chrome://bluetooth-internals/. |
7 */ | 7 */ |
8 | 8 |
9 cr.define('sidebar', function() { | 9 cr.define('sidebar', function() { |
| 10 /** @typedef {{pageName: string, text: string}} */ |
| 11 var SidebarItem; |
| 12 |
10 /** @const {!cr.ui.pageManager.PageManager}*/ | 13 /** @const {!cr.ui.pageManager.PageManager}*/ |
11 var PageManager = cr.ui.pageManager.PageManager; | 14 var PageManager = cr.ui.pageManager.PageManager; |
12 | 15 |
13 /** | 16 /** |
14 * A side menu that lists the currently navigable pages. | 17 * A side menu that lists the currently navigable pages. |
15 * @constructor | 18 * @constructor |
16 * @param {!Element} sidebarDiv The div corresponding to the sidebar. | 19 * @param {!Element} sidebarDiv The div corresponding to the sidebar. |
17 * @extends {PageManager.Observer} | 20 * @extends {PageManager.Observer} |
18 */ | 21 */ |
19 function Sidebar(sidebarDiv) { | 22 function Sidebar(sidebarDiv) { |
(...skipping 13 matching lines...) Expand all Loading... |
33 this.overlayDiv_.addEventListener('click', this.close.bind(this)); | 36 this.overlayDiv_.addEventListener('click', this.close.bind(this)); |
34 | 37 |
35 window.matchMedia('screen and (max-width: 600px)').addListener( | 38 window.matchMedia('screen and (max-width: 600px)').addListener( |
36 function(query) { if (!query.matches) this.close(); }.bind(this)); | 39 function(query) { if (!query.matches) this.close(); }.bind(this)); |
37 } | 40 } |
38 | 41 |
39 Sidebar.prototype = { | 42 Sidebar.prototype = { |
40 __proto__: PageManager.Observer.prototype, | 43 __proto__: PageManager.Observer.prototype, |
41 | 44 |
42 /** | 45 /** |
| 46 * Adds a new list item to the sidebar using the given |item|. |
| 47 * @param {!SidebarItem} item |
| 48 */ |
| 49 addItem: function(item) { |
| 50 var sidebarItem = document.createElement('li'); |
| 51 sidebarItem.dataset.pageName = item.pageName.toLowerCase(); |
| 52 |
| 53 var button = document.createElement('button'); |
| 54 button.classList.add('custom-appearance'); |
| 55 button.textContent = item.text; |
| 56 button.addEventListener('click', this.onItemClick_.bind(this)); |
| 57 sidebarItem.appendChild(button); |
| 58 |
| 59 this.sidebarList_.appendChild(sidebarItem); |
| 60 }, |
| 61 |
| 62 /** |
43 * Closes the sidebar. Only applies to layouts with window width <= 600px. | 63 * Closes the sidebar. Only applies to layouts with window width <= 600px. |
44 */ | 64 */ |
45 close: function() { | 65 close: function() { |
46 this.sidebarDiv_.classList.remove('open'); | 66 this.sidebarDiv_.classList.remove('open'); |
47 document.body.style.overflow = ''; | 67 document.body.style.overflow = ''; |
48 document.dispatchEvent(new CustomEvent('contentfocus')); | 68 document.dispatchEvent(new CustomEvent('contentfocus')); |
49 }, | 69 }, |
50 | 70 |
51 /** | 71 /** |
52 * Opens the sidebar. Only applies to layouts with window width <= 600px. | 72 * Opens the sidebar. Only applies to layouts with window width <= 600px. |
53 */ | 73 */ |
54 open: function() { | 74 open: function() { |
55 document.body.style.overflow = 'hidden'; | 75 document.body.style.overflow = 'hidden'; |
56 this.sidebarDiv_.classList.add('open'); | 76 this.sidebarDiv_.classList.add('open'); |
57 document.dispatchEvent(new CustomEvent('contentblur')); | 77 document.dispatchEvent(new CustomEvent('contentblur')); |
58 }, | 78 }, |
59 | 79 |
60 /** | 80 /** |
| 81 * Removes a sidebar item where |pageName| matches the item's pageName. |
| 82 * @param {string} pageName |
| 83 */ |
| 84 removeItem: function(pageName) { |
| 85 pageName = pageName.toLowerCase(); |
| 86 var query = 'li[data-page-name="' + pageName + '"]'; |
| 87 this.sidebarList_.removeChild(this.sidebarList_.querySelector(query)); |
| 88 }, |
| 89 |
| 90 /** |
61 * Called when a page is navigated to. | 91 * Called when a page is navigated to. |
62 * @override | 92 * @override |
63 * @param {string} path The path of the page being visited. | 93 * @param {string} path The path of the page being visited. |
64 */ | 94 */ |
65 updateHistory: function(path) { | 95 updateHistory: function(path) { |
66 this.sidebarContent_.querySelectorAll('li').forEach(function(item) { | 96 this.sidebarContent_.querySelectorAll('li').forEach(function(item) { |
67 item.classList.toggle('selected', item.dataset.pageName === path); | 97 item.classList.toggle('selected', item.dataset.pageName === path); |
68 }); | 98 }); |
69 }, | 99 }, |
70 | 100 |
71 /** | 101 /** |
72 * Switches the page based on which sidebar list button was clicked. | 102 * Switches the page based on which sidebar list button was clicked. |
73 * @param {!Event} event | 103 * @param {!Event} event |
74 * @private | 104 * @private |
75 */ | 105 */ |
76 onItemClick_: function(event) { | 106 onItemClick_: function(event) { |
77 this.close(); | 107 this.close(); |
78 PageManager.showPageByName(event.target.parentNode.dataset.pageName); | 108 PageManager.showPageByName(event.target.parentNode.dataset.pageName); |
79 }, | 109 }, |
80 }; | 110 }; |
81 | 111 |
82 return { | 112 return { |
83 Sidebar: Sidebar | 113 Sidebar: Sidebar |
84 }; | 114 }; |
85 }); | 115 }); |
OLD | NEW |