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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     5 "use strict";
     7 module.metadata = {
     8   "stability": "unstable"
     9 };
    11 const { emit } = require("../events");
    12 const { getCodeForKey, toJSON } = require("../../keyboard/utils");
    13 const { has } = require("../../util/array");
    14 const { isString } = require("../../lang/type");
    16 const INITIALIZER = "initKeyEvent";
    17 const CATEGORY = "KeyboardEvent";
    19 function Options(options) {
    20   if (!isString(options))
    21     return options;
    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 }
    33 var keyEvent = exports.keyEvent = function keyEvent(element, type, options) {
    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 }
    52 exports.keyDown = function keyDown(element, options) {
    53   keyEvent(element, "keydown", Options(options));
    54 };
    56 exports.keyUp = function keyUp(element, options) {
    57   keyEvent(element, "keyup", Options(options));
    58 };
    60 exports.keyPress = function keyPress(element, options) {
    61   keyEvent(element, "keypress", Options(options));
    62 };

mercurial