summaryrefslogtreecommitdiffstats
path: root/remoting/tools/javascript_key_tester/chord_tracker.js
blob: 29384a0b56b804f58bf9cc80fb9d8756848f6605 (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
/* Copyright (c) 2014 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.
 */

/**
 * @constructor
 * @param {HTMLElement} parentDiv
 */
var ChordTracker = function(parentDiv) {
  /** @type {HTMLElement} */
  this.parentDiv_ = parentDiv;
  /** @type {HTMLElement} */
  this.currentDiv_ = null;
  /** @type {Object<HTMLElement>} */
  this.pressedKeys_ = {};
};

/**
 * @param {string} code The PNaCl "code" string.
 * @param {string} title
 * @return {void}
 */
ChordTracker.prototype.addKeyUpEvent = function(code, title) {
  var span = this.addSpanElement_('key-up', code, title);
  delete this.pressedKeys_[code];
  if (!this.keysPressed_()) {
    this.end_();
  }
};

/**
 * @param {string} code The PNaCl "code" string.
 * @param {string} title
 * @return {void}
 */
ChordTracker.prototype.addKeyDownEvent = function(code, title) {
  var span = this.addSpanElement_('key-down', code, title);
  this.pressedKeys_[code] = span;
};

/**
 * @param {string} characterText
 * @param {string} title
 * @return {void}
 */
ChordTracker.prototype.addCharEvent = function(characterText, title) {
  this.addSpanElement_('char-event', characterText, title);
};

/**
 * @return {void}
 */
ChordTracker.prototype.releaseAllKeys = function() {
  this.end_();
  for (var i in this.pressedKeys_) {
    this.pressedKeys_[i].classList.add('unreleased');
  }
  this.pressedKeys_ = {};
}

/**
 * @private
 * @param {string} className
 * @param {string} text
 * @param {string} title
 * @return {HTMLElement}
 */
ChordTracker.prototype.addSpanElement_ = function(className, text, title) {
  this.begin_();
  var span = /** @type {HTMLElement} */ (document.createElement('span'));
  span.classList.add(className);
  span.classList.add('key-div');
  span.innerText = text;
  span.title = title;
  this.currentDiv_.appendChild(span);
  return span;
}

/**
 * @private
 */
ChordTracker.prototype.begin_ = function() {
  if (this.currentDiv_) {
    return;
  }
  this.currentDiv_ = /** @type {HTMLElement} */ (document.createElement('div'));
  this.currentDiv_.classList.add('chord-div');
  this.parentDiv_.appendChild(this.currentDiv_);
};

/**
 * @private
 */
ChordTracker.prototype.end_ = function() {
  if (!this.currentDiv_) {
    return;
  }
  if (this.keysPressed_()) {
    this.currentDiv_.classList.add('some-keys-still-pressed');
  } else {
    this.currentDiv_.classList.add('all-keys-released');
  }
  this.currentDiv_ = null;
};

/**
 * @return {boolean} True if there are any keys pressed down.
 * @private
 */
ChordTracker.prototype.keysPressed_ = function() {
  for (var property in this.pressedKeys_) {
    return true;
  }
  return false;
};