| // Copyright 2015 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| /** |
| * @fileoverview |
| * 'settings-menu' shows a menu with a hardcoded set of pages and subpages. |
| */ |
| import 'chrome://resources/cr_elements/cr_button/cr_button.m.js'; |
| import 'chrome://resources/cr_elements/cr_icons_css.m.js'; |
| import 'chrome://resources/cr_elements/cr_menu_selector/cr_menu_selector.js'; |
| import 'chrome://resources/cr_elements/icons.m.js'; |
| import 'chrome://resources/polymer/v3_0/iron-collapse/iron-collapse.js'; |
| import 'chrome://resources/polymer/v3_0/iron-icon/iron-icon.js'; |
| import 'chrome://resources/polymer/v3_0/iron-selector/iron-selector.js'; |
| import '../icons.js'; |
| import '../settings_shared_css.js'; |
| |
| import {assert} from 'chrome://resources/js/assert.m.js'; |
| import {html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js'; |
| import {loadTimeData} from '../i18n_setup.js'; |
| import {PageVisibility} from '../page_visibility.js'; |
| import {Route, RouteObserverBehavior, Router} from '../router.js'; |
| |
| Polymer({ |
| is: 'settings-menu', |
| |
| _template: html`{__html_template__}`, |
| |
| behaviors: [RouteObserverBehavior], |
| |
| properties: { |
| advancedOpened: { |
| type: Boolean, |
| value: false, |
| notify: true, |
| }, |
| |
| /** |
| * Dictionary defining page visibility. |
| * @type {!PageVisibility} |
| */ |
| pageVisibility: Object, |
| |
| /** @private */ |
| enableLandingPageRedesign_: { |
| type: Boolean, |
| value: () => loadTimeData.getBoolean('enableLandingPageRedesign'), |
| }, |
| }, |
| |
| /** @param {!Route} newRoute */ |
| currentRouteChanged(newRoute) { |
| // Focus the initially selected path. |
| const anchors = this.root.querySelectorAll('a'); |
| for (let i = 0; i < anchors.length; ++i) { |
| const anchorRoute = |
| Router.getInstance().getRouteForPath(anchors[i].getAttribute('href')); |
| if (anchorRoute && anchorRoute.contains(newRoute)) { |
| this.setSelectedUrl_(anchors[i].href); |
| return; |
| } |
| } |
| |
| this.setSelectedUrl_(''); // Nothing is selected. |
| }, |
| |
| focusFirstItem() { |
| const firstFocusableItem = |
| this.shadowRoot.querySelector('[role=menuitem]:not([hidden])'); |
| if (firstFocusableItem) { |
| firstFocusableItem.focus(); |
| } |
| }, |
| |
| /** @private */ |
| onAdvancedButtonToggle_() { |
| this.advancedOpened = !this.advancedOpened; |
| }, |
| |
| /** |
| * Prevent clicks on sidebar items from navigating. These are only links for |
| * accessibility purposes, taps are handled separately by <iron-selector>. |
| * @param {!Event} event |
| * @private |
| */ |
| onLinkClick_(event) { |
| if (event.target.matches('a:not(#extensionsLink)')) { |
| event.preventDefault(); |
| } |
| }, |
| |
| /** |
| * Keeps both menus in sync. |url| needs to come from |element.href| because |
| * |iron-list| uses the entire url. Using |getAttribute| will not work. |
| * @param {string} url |
| */ |
| setSelectedUrl_(url) { |
| this.$.topMenu.selected = this.$.subMenu.selected = url; |
| }, |
| |
| /** |
| * @param {!Event} event |
| * @private |
| */ |
| onSelectorActivate_(event) { |
| this.setSelectedUrl_(event.detail.selected); |
| |
| const path = new URL(event.detail.selected).pathname; |
| const route = Router.getInstance().getRouteForPath(path); |
| assert(route, 'settings-menu has an entry with an invalid route.'); |
| Router.getInstance().navigateTo( |
| route, /* dynamicParams */ null, /* removeSearch */ true); |
| }, |
| |
| /** |
| * @param {boolean} opened Whether the menu is expanded. |
| * @return {string} Which icon to use. |
| * @private |
| * */ |
| arrowState_(opened) { |
| return opened ? 'cr:arrow-drop-up' : 'cr:arrow-drop-down'; |
| }, |
| |
| /** @private */ |
| onExtensionsLinkClick_() { |
| chrome.metricsPrivate.recordUserAction( |
| 'SettingsMenu_ExtensionsLinkClicked'); |
| }, |
| |
| /** |
| * @param {boolean} bool |
| * @return {string} |
| * @private |
| */ |
| boolToString_(bool) { |
| return bool.toString(); |
| }, |
| }); |