summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/ntp4/command.js
blob: 198f8e6eb6e7a5bd1a6919e4c6ab7994316cb473 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// NOTE: This file depends on ui.js (or the autogenerated ui.m.js module
// version). These files and all files that depend on them are deprecated, and
// should only be used by legacy UIs that have not yet been updated to new
// patterns. Use Web Components in any new code.

/**
 * @fileoverview A command is an abstraction of an action a user can do in the
 * UI.
 *
 * When the focus changes in the document for each command a canExecute event
 * is dispatched on the active element. By listening to this event you can
 * enable and disable the command by setting the event.canExecute property.
 *
 * When a command is executed a command event is dispatched on the active
 * element. Note that you should stop the propagation after you have handled the
 * command if there might be other command listeners higher up in the DOM tree.
 */

// clang-format off
import {assert} from 'chrome://resources/js/assert.js';
import {define as crUiDefine} from 'chrome://resources/js/cr/ui.js';
import {KeyboardShortcutList} from 'chrome://resources/js/keyboard_shortcut_list.js';
import {dispatchPropertyChange, getPropertyDescriptor, PropertyKind} from 'chrome://resources/js/cr.m.js';

import {MenuItem} from './menu_item.js';
// clang-format on

/**
 * Creates a new command element.
 * @constructor
 * @extends {HTMLElement}
 */
export const Command = crUiDefine('command');

Command.prototype = {
  __proto__: HTMLElement.prototype,

  /**
   * Initializes the command.
   */
  decorate() {
    CommandManager.init(assert(this.ownerDocument));

    if (this.hasAttribute('shortcut')) {
      this.shortcut = this.getAttribute('shortcut');
    }
  },

  /**
   * Executes the command by dispatching a command event on the given element.
   * If |element| isn't given, the active element is used instead.
   * If the command is {@code disabled} this does nothing.
   * @param {HTMLElement=} opt_element Optional element to dispatch event on.
   */
  execute(opt_element) {
    if (this.disabled) {
      return;
    }
    const doc = this.ownerDocument;
    if (doc.activeElement) {
      const e = new Event('command', {bubbles: true});
      e.command = this;

      (opt_element || doc.activeElement).dispatchEvent(e);
    }
  },

  /**
   * Call this when there have been changes that might change whether the
   * command can be executed or not.
   * @param {Node=} opt_node Node for which to actuate command state.
   */
  canExecuteChange(opt_node) {
    dispatchCanExecuteEvent(this, opt_node || this.ownerDocument.activeElement);
  },

  /**
   * The keyboard shortcut that triggers the command. This is a string
   * consisting of a key (as reported by WebKit in keydown) as
   * well as optional key modifiers joinded with a '|'.
   *
   * Multiple keyboard shortcuts can be provided by separating them by
   * whitespace.
   *
   * For example:
   *   "F1"
   *   "Backspace|Meta" for Apple command backspace.
   *   "a|Ctrl" for Control A
   *   "Delete Backspace|Meta" for Delete and Command Backspace
   *
   * @type {string}
   */
  shortcut_: '',
  get shortcut() {
    return this.shortcut_;
  },
  set shortcut(shortcut) {
    const oldShortcut = this.shortcut_;
    if (shortcut !== oldShortcut) {
      this.keyboardShortcuts_ = new KeyboardShortcutList(shortcut);

      // Set this after the keyboardShortcuts_ since that might throw.
      this.shortcut_ = shortcut;
      dispatchPropertyChange(this, 'shortcut', this.shortcut_, oldShortcut);
    }
  },

  /**
   * Whether the event object matches the shortcut for this command.
   * @param {!Event} e The key event object.
   * @return {boolean} Whether it matched or not.
   */
  matchesEvent(e) {
    if (!this.keyboardShortcuts_) {
      return false;
    }
    return this.keyboardShortcuts_.matchesEvent(e);
  },
};

/**
 * The label of the command.
 * @type {string}
 */
Command.prototype.label;
Object.defineProperty(
    Command.prototype, 'label',
    getPropertyDescriptor('label', PropertyKind.ATTR));

/**
 * Whether the command is disabled or not.
 * @type {boolean}
 */
Command.prototype.disabled;
Object.defineProperty(
    Command.prototype, 'disabled',
    getPropertyDescriptor('disabled', PropertyKind.BOOL_ATTR));

/**
 * Whether the command is hidden or not.
 */
Object.defineProperty(
    Command.prototype, 'hidden',
    getPropertyDescriptor('hidden', PropertyKind.BOOL_ATTR));

/**
 * Whether the command is checked or not.
 * @type {boolean}
 */
Command.prototype.checked;
Object.defineProperty(
    Command.prototype, 'checked',
    getPropertyDescriptor('checked', PropertyKind.BOOL_ATTR));

/**
 * The flag that prevents the shortcut text from being displayed on menu.
 *
 * If false, the keyboard shortcut text (eg. "Ctrl+X" for the cut command)
 * is displayed in menu when the command is associated with a menu item.
 * Otherwise, no text is displayed.
 * @type {boolean}
 */
Command.prototype.hideShortcutText;
Object.defineProperty(
    Command.prototype, 'hideShortcutText',
    getPropertyDescriptor('hideShortcutText', PropertyKind.BOOL_ATTR));

/**
 * Dispatches a canExecute event on the target.
 * @param {!Command} command The command that we are testing for.
 * @param {EventTarget} target The target element to dispatch the event on.
 */
function dispatchCanExecuteEvent(command, target) {
  const e = new CanExecuteEvent(command);
  target.dispatchEvent(e);
  command.disabled = !e.canExecute;
}

/**
 * The command managers for different documents.
 * @type {!Map<!Document, !CommandManager>}
 */
const commandManagers = new Map();

/**
 * Keeps track of the focused element and updates the commands when the focus
 * changes.
 * @param {!Document} doc The document that we are managing the commands for.
 * @constructor
 */
function CommandManager(doc) {
  doc.addEventListener('focus', this.handleFocus_.bind(this), true);
  // Make sure we add the listener to the bubbling phase so that elements can
  // prevent the command.
  doc.addEventListener('keydown', this.handleKeyDown_.bind(this), false);
}

/**
 * Initializes a command manager for the document as needed.
 * @param {!Document} doc The document to manage the commands for.
 */
CommandManager.init = function(doc) {
  if (!commandManagers.has(doc)) {
    commandManagers.set(doc, new CommandManager(doc));
  }
};

CommandManager.prototype = {

  /**
   * Handles focus changes on the document.
   * @param {Event} e The focus event object.
   * @private
   * @suppress {checkTypes}
   * TODO(vitalyp): remove the suppression.
   */
  handleFocus_(e) {
    const target = e.target;

    // Ignore focus on a menu button or command item.
    if (target.menu || target.command || (target instanceof MenuItem)) {
      return;
    }

    const commands = Array.prototype.slice.call(
        target.ownerDocument.querySelectorAll('command'));

    commands.forEach(function(command) {
      dispatchCanExecuteEvent(command, target);
    });
  },

  /**
   * Handles the keydown event and routes it to the right command.
   * @param {!Event} e The keydown event.
   */
  handleKeyDown_(e) {
    const target = e.target;
    const commands = Array.prototype.slice.call(
        target.ownerDocument.querySelectorAll('command'));

    for (let i = 0, command; command = commands[i]; i++) {
      if (command.matchesEvent(e)) {
        // When invoking a command via a shortcut, we have to manually check
        // if it can be executed, since focus might not have been changed
        // what would have updated the command's state.
        command.canExecuteChange();

        if (!command.disabled) {
          e.preventDefault();
          // We do not want any other element to handle this.
          e.stopPropagation();
          command.execute();
          return;
        }
      }
    }
  },
};

/**
 * The event type used for canExecute events.
 * @param {!Command} command The command that we are evaluating.
 * @extends {Event}
 * @constructor
 * @class
 */
export function CanExecuteEvent(command) {
  const e = new Event('canExecute', {bubbles: true, cancelable: true});
  e.__proto__ = CanExecuteEvent.prototype;
  e.command = command;
  return e;
}

CanExecuteEvent.prototype = {
  __proto__: Event.prototype,

  /**
   * The current command
   * @type {Command}
   */
  command: null,

  /**
   * Whether the target can execute the command. Setting this also stops the
   * propagation and prevents the default. Callers can tell if an event has
   * been handled via |this.defaultPrevented|.
   * @type {boolean}
   */
  canExecute_: false,
  get canExecute() {
    return this.canExecute_;
  },
  set canExecute(canExecute) {
    this.canExecute_ = !!canExecute;
    this.stopPropagation();
    this.preventDefault();
  },
};