Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / remoting / tools / javascript_key_tester / chord_tracker.js
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  */
5
6 /**
7  * @constructor
8  * @param {HTMLElement} parentDiv
9  */
10 var ChordTracker = function(parentDiv) {
11   this.parentDiv_ = parentDiv;
12   this.currentDiv_ = null;
13   this.pressedKeys_ = {};
14 };
15
16 /**
17  * @param {Event} event The keyup or keydown event.
18  */
19 ChordTracker.prototype.addKeyEvent = function(event) {
20   this.begin_();
21   var span = document.createElement('span');
22   span.title = this.makeTitle_(event);
23   if (event.type == 'keydown') {
24     span.classList.add('key-down');
25     this.pressedKeys_[event.keyCode] = span;
26   } else {
27     span.classList.add('key-up');
28     delete this.pressedKeys_[event.keyCode];
29   }
30   span.innerText = this.keyName_(event.keyCode);
31   this.currentDiv_.appendChild(span);
32   if (!this.keysPressed_()) {
33     this.end_();
34   }
35 };
36
37 ChordTracker.prototype.releaseAllKeys = function() {
38   this.end_();
39   for (var i in this.pressedKeys_) {
40     this.pressedKeys_[i].classList.add('unreleased');
41   }
42   this.pressedKeys_ = {};
43 }
44
45 /**
46  * @private
47  */
48 ChordTracker.prototype.begin_ = function() {
49   if (this.currentDiv_) {
50     return;
51   }
52   this.currentDiv_ = document.createElement('div');
53   this.currentDiv_.classList.add('chord-div');
54   this.parentDiv_.appendChild(this.currentDiv_);
55 };
56
57 /**
58  * @private
59  */
60 ChordTracker.prototype.end_ = function() {
61   if (!this.currentDiv_) {
62     return;
63   }
64   if (this.keysPressed_()) {
65     this.currentDiv_.classList.add('some-keys-still-pressed');
66   } else {
67     this.currentDiv_.classList.add('all-keys-released');
68   }
69   this.currentDiv_ = null;
70 };
71
72 /**
73  * @return {boolean} True if there are any keys pressed down.
74  * @private
75  */
76 ChordTracker.prototype.keysPressed_ = function() {
77   for (var property in this.pressedKeys_) {
78     return true;
79   }
80   return false;
81 };
82
83 /**
84  * @param {number} keyCode The keyCode field of the keyup or keydown event.
85  * @return {string} A human-readable representation of the key.
86  * @private
87  */
88 ChordTracker.prototype.keyName_ = function(keyCode) {
89   var result = keyboardMap[keyCode];
90   if (!result) {
91     result = '<' + keyCode + '>';
92   }
93   return result;
94 };
95
96 /**
97  * @param {Event} event The keyup or keydown event.
98  * @private
99  */
100 ChordTracker.prototype.makeTitle_ = function(event) {
101   return 'type: ' + event.type + '\n' +
102          'alt: ' + event.altKey + '\n' +
103          'shift: ' + event.shiftKey + '\n' +
104          'control: ' + event.controlKey + '\n' +
105          'meta: ' + event.metaKey + '\n' +
106          'charCode: ' + event.charCode + '\n' +
107          'keyCode: ' + event.keyCode + '\n' +
108          'keyIdentifier: ' + event.keyIdentifier + '\n' +
109          'repeat: ' + event.repeat + '\n';
110 };