| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 * @fileoverview | 6 * @fileoverview |
| 7 * 'settings-menu' shows a menu with a hardcoded set of pages and subpages. | 7 * 'settings-menu' shows a menu with a hardcoded set of pages and subpages. |
| 8 */ | 8 */ |
| 9 Polymer({ | 9 Polymer({ |
| 10 is: 'settings-menu', | 10 is: 'settings-menu', |
| 11 | 11 |
| 12 behaviors: [settings.RouteObserverBehavior], | |
| 13 | |
| 14 properties: { | 12 properties: { |
| 15 advancedOpened: { | 13 advancedOpened: { |
| 16 type: Boolean, | 14 type: Boolean, |
| 17 notify: true, | 15 notify: true, |
| 18 }, | 16 }, |
| 19 | 17 |
| 20 /** @private */ | |
| 21 aboutSelected_: Boolean, | |
| 22 | |
| 23 /** | 18 /** |
| 24 * Dictionary defining page visibility. | 19 * Dictionary defining page visibility. |
| 25 * @type {!GuestModePageVisibility} | 20 * @type {!GuestModePageVisibility} |
| 26 */ | 21 */ |
| 27 pageVisibility: { | 22 pageVisibility: { |
| 28 type: Object, | 23 type: Object, |
| 29 }, | 24 }, |
| 30 }, | 25 }, |
| 31 | 26 |
| 32 ready: function() { | 27 listeners: { |
| 33 // When a <paper-submenu> is created with the [opened] attribute as true, | 28 'topMenu.tap': 'onLinkTap_', |
| 34 // its _active member isn't correctly initialized. See this bug for more | 29 'subMenu.tap': 'onLinkTap_', |
| 35 // info: https://github.com/PolymerElements/paper-menu/issues/88. This means | 30 }, |
| 36 // the first tap to close an opened Advanced section does nothing (because | 31 |
| 37 // it calls .open() on an opened menu instead of .close(). This is a fix for | 32 /** @override */ |
| 38 // that bug without changing that code through its public API. | 33 attached: function() { |
| 39 // | 34 var currentPath = settings.getCurrentRoute().path; |
| 40 // TODO(dbeam): we're currently deciding whether <paper-{,sub}menu> are | 35 |
| 41 // right for our needs (there have been minor a11y problems). If we decide | 36 // Focus the initially selected path. |
| 42 // to keep <paper-{,sub}menu>, fix this bug with a local Chrome CL (ex: | 37 var anchors = this.root.querySelectorAll('a'); |
| 43 // https://codereview.chromium.org/2412343004) or a Polymer PR (ex: | 38 for (var i = 0; i < anchors.length; ++i) { |
| 44 // https://github.com/PolymerElements/paper-menu/pull/107). | 39 if (anchors[i].getAttribute('href') == currentPath) { |
| 45 if (this.advancedOpened) | 40 this.setSelectedUrl_(anchors[i].href); |
| 46 this.$.advancedSubmenu.open(); | 41 break; |
| 42 } |
| 43 } |
| 47 }, | 44 }, |
| 48 | 45 |
| 49 /** | 46 /** |
| 50 * @param {!settings.Route} newRoute | 47 * Prevent clicks on sidebar items from navigating. These are only links for |
| 48 * accessibility purposes, taps are handled separately by <iron-selector>. |
| 49 * @param {!Event} event |
| 50 * @private |
| 51 */ | 51 */ |
| 52 currentRouteChanged: function(newRoute) { | 52 onLinkTap_: function(event) { |
| 53 // Make the three menus mutually exclusive. | 53 if (event.target.hasAttribute('href')) |
| 54 if (settings.Route.ABOUT.contains(newRoute)) { | 54 event.preventDefault(); |
| 55 this.aboutSelected_ = true; | 55 }, |
| 56 this.$.advancedMenu.selected = null; | 56 |
| 57 this.$.basicMenu.selected = null; | 57 /** |
| 58 } else if (settings.Route.ADVANCED.contains(newRoute)) { | 58 * Keeps both menus in sync. |url| needs to come from |element.href| because |
| 59 this.aboutSelected_ = false; | 59 * |iron-list| uses the entire url. Using |getAttribute| will not work. |
| 60 // For routes from URL entry, we need to set selected. | 60 * @param {string} url |
| 61 this.$.advancedMenu.selected = newRoute.path; | 61 */ |
| 62 this.$.basicMenu.selected = null; | 62 setSelectedUrl_: function(url) { |
| 63 } else if (settings.Route.BASIC.contains(newRoute)) { | 63 this.$.topMenu.selected = this.$.subMenu.selected = url; |
| 64 this.aboutSelected_ = false; | |
| 65 this.$.advancedMenu.selected = null; | |
| 66 // For routes from URL entry, we need to set selected. | |
| 67 this.$.basicMenu.selected = newRoute.path; | |
| 68 } | |
| 69 }, | 64 }, |
| 70 | 65 |
| 71 /** | 66 /** |
| 72 * @param {!Event} event | 67 * @param {!Event} event |
| 73 * @private | 68 * @private |
| 74 */ | 69 */ |
| 75 openPage_: function(event) { | 70 onSelectorActivate_: function(event) { |
| 76 var route = settings.getRouteForPath(event.currentTarget.dataset.path); | 71 this.setSelectedUrl_(event.detail.selected); |
| 77 assert(route, 'settings-menu has an an entry with an invalid path'); | 72 |
| 73 var path = new URL(event.detail.selected).pathname; |
| 74 var route = settings.getRouteForPath(path); |
| 75 assert(route, 'settings-menu has an entry with an invalid route.'); |
| 78 settings.navigateTo( | 76 settings.navigateTo( |
| 79 route, /* dynamicParams */ null, /* removeSearch */ true); | 77 route, /* dynamicParams */ null, /* removeSearch */ true); |
| 80 }, | 78 }, |
| 81 | 79 |
| 82 /** | 80 /** |
| 83 * @param {boolean} opened Whether the menu is expanded. | 81 * @param {boolean} opened Whether the menu is expanded. |
| 84 * @return {string} Which icon to use. | 82 * @return {string} Which icon to use. |
| 85 * @private | 83 * @private |
| 86 * */ | 84 * */ |
| 87 arrowState_: function(opened) { | 85 arrowState_: function(opened) { |
| 88 return opened ? 'cr:arrow-drop-up' : 'cr:arrow-drop-down'; | 86 return opened ? 'cr:arrow-drop-up' : 'cr:arrow-drop-down'; |
| 89 }, | 87 }, |
| 90 }); | 88 }); |
| OLD | NEW |