1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/**
6 * @fileoverview Widget presenting all keyboard commands.
7 */
8
9goog.provide('cvox.KeyboardHelpWidget');
10
11goog.require('cvox.ChromeVox');
12goog.require('cvox.CommandStore');
13goog.require('cvox.KeyUtil');
14goog.require('cvox.OverlayWidget');
15
16/**
17 * @constructor
18 * @extends {cvox.OverlayWidget}
19 */
20cvox.KeyboardHelpWidget = function() {
21  goog.base(this, '');
22  this.container_ = document.createElement('div');
23  var list = [];
24  var callbacks = [];
25  var keymap = cvox.ChromeVoxKbHandler.handlerKeyMap;
26
27  keymap.bindings().forEach(goog.bind(function(pair) {
28    var command = pair.command;
29    var keySeq = pair.sequence;
30    var message = command;
31    try {
32      var id = cvox.CommandStore.messageForCommand(command);
33      if (!id) {
34        return;
35      }
36      message = cvox.ChromeVox.msgs.getMsg(id);
37    } catch (e) {
38      // TODO(dtseng): We have some commands that don't have valid message id's.
39    }
40
41    var commandElement = document.createElement('p');
42    commandElement.id = command;
43    commandElement.setAttribute('role', 'menuitem');
44    commandElement.textContent =
45        message + ' - ' + cvox.KeyUtil.keySequenceToString(keySeq, true);
46    this.container_.appendChild(commandElement);
47  }, this));
48};
49goog.inherits(cvox.KeyboardHelpWidget, cvox.OverlayWidget);
50goog.addSingletonGetter(cvox.KeyboardHelpWidget);
51
52
53/**
54 * @override
55 */
56cvox.KeyboardHelpWidget.prototype.show = function() {
57  goog.base(this, 'show');
58  this.host_.appendChild(this.container_);
59};
60
61
62/**
63 * @override
64 */
65cvox.KeyboardHelpWidget.prototype.getNameMsg = function() {
66  return ['keyboard_help_intro'];
67};
68
69/**
70 * @override
71 */
72cvox.KeyboardHelpWidget.prototype.onKeyDown = function(evt) {
73  if (evt.keyCode == 13) { // Enter
74    var currentCommand =
75        cvox.ChromeVox.navigationManager.getCurrentNode().parentNode.id;
76    this.hide();
77    cvox.ChromeVoxEventSuspender.withSuspendedEvents(
78        cvox.ChromeVoxUserCommands.commands[currentCommand])();
79    evt.preventDefault();
80    evt.stopPropagation();
81    return true;
82  } else {
83    return goog.base(this, 'onKeyDown', evt);
84  }
85};
86