Debian Codegraph @

plugins/record/mousetrap-record.js

raw symbols SCIP
1 /**
2 * This extension allows you to record a sequence using Mousetrap.
3 *
4 * @author Dan Tao <[email protected]>
5 */
6 (function(Mousetrap) {
7 /**
8 * the sequence currently being recorded
9 *
10 * @type {Array}
11 */
12 var _recordedSequence = [],
13
14 /**
15 * a callback to invoke after recording a sequence
16 *
17 * @type {Function|null}
18 */
19 _recordedSequenceCallback = null,
20
21 /**
22 * a list of all of the keys currently held down
23 *
24 * @type {Array}
25 */
26 _currentRecordedKeys = [],
27
28 /**
29 * temporary state where we remember if we've already captured a
30 * character key in the current combo
31 *
32 * @type {boolean}
33 */
34 _recordedCharacterKey = false,
35
36 /**
37 * a handle for the timer of the current recording
38 *
39 * @type {null|number}
40 */
41 _recordTimer = null,
42
43 /**
44 * the original handleKey method to override when Mousetrap.record() is
45 * called
46 *
47 * @type {Function}
48 */
49 _origHandleKey = Mousetrap.prototype.handleKey;
50
51 /**
52 * handles a character key event
53 *
54 * @param {string} character
55 * @param {Array} modifiers
56 * @param {Event} e
57 * @returns void
58 */
59 function _handleKey(character, modifiers, e) {
60 var self = this;
61
62 if (!self.recording) {
63 _origHandleKey.apply(self, arguments);
64 return;
65 }
66
67 // remember this character if we're currently recording a sequence
68 if (e.type == 'keydown') {
69 if (character.length === 1 && _recordedCharacterKey) {
70 _recordCurrentCombo();
71 }
72
73 for (i = 0; i < modifiers.length; ++i) {
74 _recordKey(modifiers[i]);
75 }
76 _recordKey(character);
77
78 // once a key is released, all keys that were held down at the time
79 // count as a keypress
80 } else if (e.type == 'keyup' && _currentRecordedKeys.length > 0) {
81 _recordCurrentCombo();
82 }
83 }
84
85 /**
86 * marks a character key as held down while recording a sequence
87 *
88 * @param {string} key
89 * @returns void
90 */
91 function _recordKey(key) {
92 var i;
93
94 // one-off implementation of Array.indexOf, since IE6-9 don't support it
95 for (i = 0; i < _currentRecordedKeys.length; ++i) {
96 if (_currentRecordedKeys[i] === key) {
97 return;
98 }
99 }
100
101 _currentRecordedKeys.push(key);
102
103 if (key.length === 1) {
104 _recordedCharacterKey = true;
105 }
106 }
107
108 /**
109 * marks whatever key combination that's been recorded so far as finished
110 * and gets ready for the next combo
111 *
112 * @returns void
113 */
114 function _recordCurrentCombo() {
115 _recordedSequence.push(_currentRecordedKeys);
116 _currentRecordedKeys = [];
117 _recordedCharacterKey = false;
118 _restartRecordTimer();
119 }
120
121 /**
122 * ensures each combo in a sequence is in a predictable order and formats
123 * key combos to be '+'-delimited
124 *
125 * modifies the sequence in-place
126 *
127 * @param {Array} sequence
128 * @returns void
129 */
130 function _normalizeSequence(sequence) {
131 var i;
132
133 for (i = 0; i < sequence.length; ++i) {
134 sequence[i].sort(function(x, y) {
135 // modifier keys always come first, in alphabetical order
136 if (x.length > 1 && y.length === 1) {
137 return -1;
138 } else if (x.length === 1 && y.length > 1) {
139 return 1;
140 }
141
142 // character keys come next (list should contain no duplicates,
143 // so no need for equality check)
144 return x > y ? 1 : -1;
145 });
146
147 sequence[i] = sequence[i].join('+');
148 }
149 }
150
151 /**
152 * finishes the current recording, passes the recorded sequence to the stored
153 * callback, and sets Mousetrap.handleKey back to its original function
154 *
155 * @returns void
156 */
157 function _finishRecording() {
158 if (_recordedSequenceCallback) {
159 _normalizeSequence(_recordedSequence);
160 _recordedSequenceCallback(_recordedSequence);
161 }
162
163 // reset all recorded state
164 _recordedSequence = [];
165 _recordedSequenceCallback = null;
166 _currentRecordedKeys = [];
167 }
168
169 /**
170 * called to set a 1 second timeout on the current recording
171 *
172 * this is so after each key press in the sequence the recording will wait for
173 * 1 more second before executing the callback
174 *
175 * @returns void
176 */
177 function _restartRecordTimer() {
178 clearTimeout(_recordTimer);
179 _recordTimer = setTimeout(_finishRecording, 1000);
180 }
181
182 /**
183 * records the next sequence and passes it to a callback once it's
184 * completed
185 *
186 * @param {Function} callback
187 * @returns void
188 */
189 Mousetrap.prototype.record = function(callback) {
190 var self = this;
191 self.recording = true;
192 _recordedSequenceCallback = function() {
193 self.recording = false;
194 callback.apply(self, arguments);
195 };
196 };
197
198 Mousetrap.prototype.handleKey = function() {
199 var self = this;
200 _handleKey.apply(self, arguments);
201 };
202
203 Mousetrap.init();
204
205 })(Mousetrap);