Update mojo sdk to rev c02a28868825edfa57ab77947b8cb15e741c5598
[chromium-blink-merge.git] / remoting / tools / javascript_key_tester / chord_tracker.js
blob29384a0b56b804f58bf9cc80fb9d8756848f6605
1 /* Copyright (c) 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 */
6 /**
7 * @constructor
8 * @param {HTMLElement} parentDiv
9 */
10 var ChordTracker = function(parentDiv) {
11 /** @type {HTMLElement} */
12 this.parentDiv_ = parentDiv;
13 /** @type {HTMLElement} */
14 this.currentDiv_ = null;
15 /** @type {Object<HTMLElement>} */
16 this.pressedKeys_ = {};
19 /**
20 * @param {string} code The PNaCl "code" string.
21 * @param {string} title
22 * @return {void}
24 ChordTracker.prototype.addKeyUpEvent = function(code, title) {
25 var span = this.addSpanElement_('key-up', code, title);
26 delete this.pressedKeys_[code];
27 if (!this.keysPressed_()) {
28 this.end_();
32 /**
33 * @param {string} code The PNaCl "code" string.
34 * @param {string} title
35 * @return {void}
37 ChordTracker.prototype.addKeyDownEvent = function(code, title) {
38 var span = this.addSpanElement_('key-down', code, title);
39 this.pressedKeys_[code] = span;
42 /**
43 * @param {string} characterText
44 * @param {string} title
45 * @return {void}
47 ChordTracker.prototype.addCharEvent = function(characterText, title) {
48 this.addSpanElement_('char-event', characterText, title);
51 /**
52 * @return {void}
54 ChordTracker.prototype.releaseAllKeys = function() {
55 this.end_();
56 for (var i in this.pressedKeys_) {
57 this.pressedKeys_[i].classList.add('unreleased');
59 this.pressedKeys_ = {};
62 /**
63 * @private
64 * @param {string} className
65 * @param {string} text
66 * @param {string} title
67 * @return {HTMLElement}
69 ChordTracker.prototype.addSpanElement_ = function(className, text, title) {
70 this.begin_();
71 var span = /** @type {HTMLElement} */ (document.createElement('span'));
72 span.classList.add(className);
73 span.classList.add('key-div');
74 span.innerText = text;
75 span.title = title;
76 this.currentDiv_.appendChild(span);
77 return span;
80 /**
81 * @private
83 ChordTracker.prototype.begin_ = function() {
84 if (this.currentDiv_) {
85 return;
87 this.currentDiv_ = /** @type {HTMLElement} */ (document.createElement('div'));
88 this.currentDiv_.classList.add('chord-div');
89 this.parentDiv_.appendChild(this.currentDiv_);
92 /**
93 * @private
95 ChordTracker.prototype.end_ = function() {
96 if (!this.currentDiv_) {
97 return;
99 if (this.keysPressed_()) {
100 this.currentDiv_.classList.add('some-keys-still-pressed');
101 } else {
102 this.currentDiv_.classList.add('all-keys-released');
104 this.currentDiv_ = null;
108 * @return {boolean} True if there are any keys pressed down.
109 * @private
111 ChordTracker.prototype.keysPressed_ = function() {
112 for (var property in this.pressedKeys_) {
113 return true;
115 return false;