1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123

ash / webui / common / resources / navigation_selector.js [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import './navigation_icons.html.js';
import 'chrome://resources/ash/common/cr_elements/cr_expand_button/cr_expand_button.js';
import 'chrome://resources/ash/common/cr_elements/cr_shared_style.css.js';
import 'chrome://resources/ash/common/cr_elements/cr_shared_vars.css.js';
import 'chrome://resources/polymer/v3_0/iron-collapse/iron-collapse.js';
import 'chrome://resources/polymer/v3_0/iron-icon/iron-icon.js';

import {PolymerElement} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';

import {getTemplate} from './navigation_selector.html.js';

/**
 * @typedef {{
 *   name: string,
 *   pageIs: string,
 *   icon: string,
 *   initialData: ?Object,
 *   id: string,
 * }}
 */
export let SelectorItem;

/**
 * @fileoverview
 * 'navigation-selector' is a side bar navigation element. To populate the
 * navigation-selector you must provide the element with an array of
 * SelectorItem's.
 */
export class NavigationSelectorElement extends PolymerElement {
  static get is() {
    return 'navigation-selector';
  }

  static get template() {
    return getTemplate();
  }

  static get properties() {
    return {
      /**
       * @type {?SelectorItem}
       * Notifies parent elements of the the selected item.
       */
      selectedItem: {
        type: Object,
        value: null,
        observer: 'selectedItemChanged_',
        notify: true,
      },

      /**
       * @type {!Array<!SelectorItem>}
       */
      selectorItems: {
        type: Array,
        value: () => [],
      },
    };
  }

  /**
   * @param {Event} e
   * @private
   */
  onSelected_(e) {
    this.dispatchEvent(new CustomEvent(
        'navigation-selected', {bubbles: true, composed: true}));
    this.selectedItem = e.model.item;
  }

  /** @protected */
  selectedItemChanged_() {
    // Update any top-level entries.
    const items = /** @type {!NodeList<!HTMLDivElement>} */(
        this.shadowRoot.querySelectorAll('.navigation-item'));
    this.updateSelected_(items);
  }

  /**
   * @param {!NodeList<!HTMLDivElement>} items
   * @private
   */
  updateSelected_(items) {
    for (const item of items) {
      if (item.textContent.trim() === this.selectedItem.name) {
        item.classList.add('selected');
        item.setAttribute('aria-current', 'true');
      } else {
        item.classList.remove('selected');
        item.removeAttribute('aria-current');
      }
    }
  }

  /**
   * @param {!SelectorItem} item
   * @return {string}
   * @protected
   */
  getIcon_(item) {
    return item.icon;
  }

  /**
   * @param {!SelectorItem} item
   * @return {string}
   * @protected
   */
  computeInitialClass_(item) {
    let classList = 'navigation-item';
    if (!!this.selectedItem && item.name == this.selectedItem.name) {
      // Add the initial .selected class to the currently selected entry.
      classList += ' selected';
    }
    return classList;
  }
}

customElements.define(NavigationSelectorElement.is, NavigationSelectorElement);