addon-sdk/source/lib/sdk/dom/events/keys.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:bb91d97f103a
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 "use strict";
6
7 module.metadata = {
8 "stability": "unstable"
9 };
10
11 const { emit } = require("../events");
12 const { getCodeForKey, toJSON } = require("../../keyboard/utils");
13 const { has } = require("../../util/array");
14 const { isString } = require("../../lang/type");
15
16 const INITIALIZER = "initKeyEvent";
17 const CATEGORY = "KeyboardEvent";
18
19 function Options(options) {
20 if (!isString(options))
21 return options;
22
23 var { key, modifiers } = toJSON(options);
24 return {
25 key: key,
26 control: has(modifiers, "control"),
27 alt: has(modifiers, "alt"),
28 shift: has(modifiers, "shift"),
29 meta: has(modifiers, "meta")
30 };
31 }
32
33 var keyEvent = exports.keyEvent = function keyEvent(element, type, options) {
34
35 emit(element, type, {
36 initializer: INITIALIZER,
37 category: CATEGORY,
38 settings: [
39 !("bubbles" in options) || options.bubbles !== false,
40 !("cancelable" in options) || options.cancelable !== false,
41 "window" in options && options.window ? options.window : null,
42 "control" in options && !!options.control,
43 "alt" in options && !!options.alt,
44 "shift" in options && !!options.shift,
45 "meta" in options && !!options.meta,
46 getCodeForKey(options.key) || 0,
47 options.key.length === 1 ? options.key.charCodeAt(0) : 0
48 ]
49 });
50 }
51
52 exports.keyDown = function keyDown(element, options) {
53 keyEvent(element, "keydown", Options(options));
54 };
55
56 exports.keyUp = function keyUp(element, options) {
57 keyEvent(element, "keyup", Options(options));
58 };
59
60 exports.keyPress = function keyPress(element, options) {
61 keyEvent(element, "keypress", Options(options));
62 };
63

mercurial