summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/commander/app.ts
blob: 2436ad5d3e85a4a2fa637f6f873e8cf6f2e7ef18 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import './icons.html.js';
import './option.js';
import 'chrome://resources/cr_elements/icons.html.js';
import 'chrome://resources/polymer/v3_0/iron-icon/iron-icon.js';
import 'chrome://resources/cr_elements/cr_shared_vars.css.js';

import {addWebUIListener} from 'chrome://resources/js/cr.m.js';
import {Debouncer, DomRepeatEvent, enqueueDebouncer, microTask, PolymerElement} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';

import {getTemplate} from './app.html.js';
import {BrowserProxy, BrowserProxyImpl} from './browser_proxy.js';
import {Action, Option, ViewModel} from './types.js';

export interface CommanderAppElement {
  $: {
    input: HTMLInputElement,
    inputRow: HTMLElement,
  };
}


export class CommanderAppElement extends PolymerElement {
  static get is() {
    return 'commander-app';
  }

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

  static get properties() {
    return {
      options_: Array,
      focusedIndex_: {
        type: Number,
        notify: true,
        observer: 'onFocusedIndexChanged_',
      },
      promptText_: String,
      resultSetId_: Number,
    };
  }

  private options_: Option[];
  private focusedIndex_: number;
  private promptText_: string|null;
  private browserProxy_: BrowserProxy;
  private resultSetId_: number|null;
  private savedInput_: string;
  private showNoResults_: boolean;
  private debouncer_: Debouncer|null;

  constructor() {
    super();
    this.browserProxy_ = BrowserProxyImpl.getInstance();
  }

  override ready() {
    super.ready();
    addWebUIListener('view-model-updated', this.onViewModelUpdated_.bind(this));
    addWebUIListener('initialize', this.initialize_.bind(this));
    this.initialize_();
  }

  /**
   * Resets the UI for a new session.
   */
  private initialize_() {
    this.options_ = [];
    this.$.input.value = '';
    this.$.input.focus();
    this.focusedIndex_ = -1;
    this.resultSetId_ = null;
    this.promptText_ = null;
    this.savedInput_ = '';
    this.showNoResults_ = false;
  }

  private onKeydown_(e: KeyboardEvent) {
    if (e.key === 'Escape') {
      this.browserProxy_.dismiss();
      return;
    }
    if (e.key === 'ArrowUp') {
      e.preventDefault();
      this.focusedIndex_ = (this.focusedIndex_ + this.options_.length - 1) %
          this.options_.length;
    } else if (e.key === 'ArrowDown') {
      e.preventDefault();
      this.focusedIndex_ = (this.focusedIndex_ + 1) % this.options_.length;
    } else if (e.key === 'Enter') {
      if (this.focusedIndex_ >= 0 &&
          this.focusedIndex_ < this.options_.length) {
        this.notifySelectedAtIndex_(this.focusedIndex_);
      }
    } else if (
        this.promptText_ && e.key === 'Backspace' &&
        this.$.input.value === '') {
      this.browserProxy_.promptCancelled();
      this.promptText_ = null;
      this.$.input.value = this.savedInput_;
      e.preventDefault();
      this.onInput_();
    }
  }

  private onInput_() {
    this.browserProxy_.textChanged(this.$.input.value);
  }

  private onViewModelUpdated_(viewModel: ViewModel) {
    if (viewModel.action === Action.DISPLAY_RESULTS) {
      this.options_ = viewModel.options || [];
      this.resultSetId_ = viewModel.resultSetId;
      this.showNoResults_ = this.resultSetId_ != null &&
          this.$.input.value !== '' && this.options_.length === 0;
      if (this.options_.length > 0) {
        this.focusedIndex_ = 0;
      }
    } else if (viewModel.action === Action.PROMPT) {
      this.showNoResults_ = false;
      this.options_ = [];
      this.resultSetId_ = viewModel.resultSetId;
      this.promptText_ = viewModel.promptText || null;
      this.savedInput_ = this.$.input.value;
      this.$.input.value = '';
      this.onInput_();
    }
  }

  private onDomChange_() {
    this.debouncer_ = Debouncer.debounce(this.debouncer_, microTask, () => {
      this.browserProxy_.heightChanged(document.body.offsetHeight);
    });
    enqueueDebouncer(this.debouncer_);
  }

  /**
   * Called when a result option is clicked via mouse.
   */
  private onOptionClick_(e: DomRepeatEvent<Option>) {
    this.notifySelectedAtIndex_(e.model.index);
  }

  /**
   *  Used to set `aria-activedescendant` when the focused option changes.
   */
  private onFocusedIndexChanged_() {
    if (this.focusedIndex_ === -1) {
      this.$.inputRow.removeAttribute('aria-activedescendant');
    } else {
      this.$.inputRow.setAttribute(
          'aria-activedescendant', this.getOptionId_(this.focusedIndex_));
    }
  }

  /**
   * Informs the browser that the option at |index| was selected.
   */
  private notifySelectedAtIndex_(index: number) {
    if (this.resultSetId_ !== null) {
      this.browserProxy_.optionSelected(index, this.resultSetId_);
    }
  }

  private getOptionClass_(index: number): string {
    return index === this.focusedIndex_ ? 'focused' : '';
  }

  /**
   * An id is required for aria-activedescendant
   */
  private getOptionId_(index: number): string {
    return 'option-' + index;
  }

  private computeShowChip_(): boolean {
    return this.promptText_ !== null;
  }

  private computeExpanded_(): string {
    return this.options_.length > 0 ? 'true' : 'false';
  }

  private computeAriaSelected_(index: number): string {
    return index === this.focusedIndex_ ? 'true' : 'false';
  }
}

declare global {
  interface HTMLElementTagNameMap {
    'commander-app': CommanderAppElement;
  }
}

customElements.define(CommanderAppElement.is, CommanderAppElement);