summaryrefslogtreecommitdiff
path: root/chromium/ui/webui/resources/cr_elements/cr_profile_avatar_selector/cr_profile_avatar_selector.js
blob: f0b92a1b3968289749a10ec49b65fee08ce4d1e5 (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2016 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 'cr-profile-avatar-selector' is an element that displays
 * profile avatar icons and allows an avatar to be selected.
 */

/**
 * @typedef {{url: string,
 *            label: string,
 *            index: (number),
 *            isGaiaAvatar: (boolean),
 *            selected: (boolean)}}
 */
export let AvatarIcon;

import {Polymer, html} from '//resources/polymer/v3_0/polymer/polymer_bundled.min.js';

import '../cr_button/cr_button.m.js';
import '../shared_vars_css.m.js';
import '../shared_style_css.m.js';
import {getImage} from '../../js/icon.m.js';
import '//resources/polymer/v3_0/paper-styles/color.js';
import '//resources/polymer/v3_0/paper-tooltip/paper-tooltip.js';
import './cr_profile_avatar_selector_grid.js';

Polymer({
  is: 'cr-profile-avatar-selector',

  _template: html`{__html_template__}`,

  properties: {
    /**
     * The list of profile avatar URLs and labels.
     * @type {!Array<!AvatarIcon>}
     */
    avatars: {
      type: Array,
      value() {
        return [];
      }
    },

    /**
     * The currently selected profile avatar icon, if any.
     * @type {?AvatarIcon}
     */
    selectedAvatar: {
      type: Object,
      notify: true,
    },

    ignoreModifiedKeyEvents: {
      type: Boolean,
      value: false,
    },

    /**
     * The currently selected profile avatar icon index, or '-1' if none is
     * selected.
     * @type {number}
     */
    tabFocusableAvatar_: {
      type: Number,
      computed: 'computeTabFocusableAvatar_(avatars, selectedAvatar)',
    },
  },

  /**
   * @param {number} index
   * @return {string}
   * @private
   */
  getAvatarId_(index) {
    return 'avatarId' + index;
  },

  /**
   * @param {number} index
   * @param {!AvatarIcon} item
   * @return {string}
   * @private
   */
  getTabIndex_(index, item) {
    if (item.index === this.tabFocusableAvatar_) {
      return '0';
    }

    // If no avatar is selected, focus the first element of the grid on 'tab'.
    if (this.tabFocusableAvatar_ === -1 && index === 0) {
      return '0';
    }
    return '-1';
  },

  /**
   * @return {number}
   * @private
   */
  computeTabFocusableAvatar_() {
    const selectedAvatar =
        this.avatars.find(avatar => this.isAvatarSelected(avatar));
    return selectedAvatar ? selectedAvatar.index : -1;
  },

  /** @private */
  getSelectedClass_(avatarItem) {
    // TODO(dpapad): Rename 'iron-selected' to 'selected' now that this CSS
    // class is not assigned by any iron-* behavior.
    return this.isAvatarSelected(avatarItem) ? 'iron-selected' : '';
  },

  /**
   * @param {AvatarIcon} avatarItem
   * @return {string}
   * @private
   */
  getCheckedAttribute_(avatarItem) {
    return this.isAvatarSelected(avatarItem) ? 'true' : 'false';
  },

  /**
   * @param {AvatarIcon} avatarItem
   * @return {boolean}
   * @private
   */
  isAvatarSelected(avatarItem) {
    return !!avatarItem &&
        (avatarItem.selected ||
         (!!this.selectedAvatar &&
          this.selectedAvatar.index === avatarItem.index));
  },

  /**
   * @param {string} iconUrl
   * @return {string} A CSS image-set for multiple scale factors.
   * @private
   */
  getIconImageSet_(iconUrl) {
    return getImage(iconUrl);
  },

  /**
   * @param {!Event} e
   * @private
   */
  onAvatarTap_(e) {
    // |selectedAvatar| is set to pass back selection to the owner of this
    // component.
    this.selectedAvatar =
        /** @type {!{model: {item: !AvatarIcon}}} */ (e).model.item;
  },
});