cros: Remove default pinned apps trial.
[chromium-blink-merge.git] / chrome / browser / resources / chromeos / keyboard / keyboard_utils.js
blob27c862a8c378c24a5a234099f9f58633d2732bb7
1 // Copyright (c) 2013 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.
5 /**
6  * Namespace for keyboard utility functions.
7  */
8 var keyboard = {};
10 /**
11  * Swallows left and right keypress and keyup events.
12  * @param {KeyboardEvent} event Raised event.
13  * @private
14  */
15 keyboard.onKeyIgnore_ = function(event) {
16   if (event.ctrlKey || event.shiftKey || event.altKey || event.metaKey)
17     return;
19   if (event.keyIdentifier == 'Left' || event.keyIdentifier == 'Right') {
20     event.stopPropagation();
21     event.preventDefault();
22   }
25 /**
26  * Converts right/left into tab/shift-tab key events.
27  * @param {KeyboardEvent} event Raised event.
28  * @private
29  */
30 keyboard.onKeyDown_ = function(event) {
31    if (event.ctrlKey || event.shiftKey || event.altKey || event.metaKey)
32      return;
34    if (event.keyIdentifier == 'Left') {
35      keyboard.raiseKeyFocusPrevious(document.activeElement);
36      event.stopPropagation();
37      event.preventDefault();
38    } else if (event.keyIdentifier == 'Right') {
39      keyboard.raiseKeyFocusNext(document.activeElement);
40      event.stopPropagation();
41      event.preventDefault();
42    }
45 /**
46  * Raises tab/shift-tab keyboard events.
47  * @param {HTMLElement} element Element that should receive the event.
48  * @param {string} eventType Keyboard event type.
49  * @param {boolean} shift True if shift should be on.
50  * @private
51  */
52 keyboard.raiseTabKeyEvent_ = function(element, eventType, shift) {
53   var event = document.createEvent('KeyboardEvent');
54   event.initKeyboardEvent(
55       eventType,
56       true,  // canBubble
57       true,  // cancelable
58       window,
59       'U+0009',
60       0,  // keyLocation
61       false,  // ctrl
62       false,  // alt
63       shift,  // shift
64       false);  // meta
65   element.dispatchEvent(event);
68 /**
69  * Raises shift+tab keyboard events to focus previous element.
70  * @param {HTMLElement} element Element that should receive the event.
71  */
72 keyboard.raiseKeyFocusPrevious = function(element) {
73   keyboard.raiseTabKeyEvent_(element, 'keydown', true);
74   keyboard.raiseTabKeyEvent_(element, 'keypress', true);
75   keyboard.raiseTabKeyEvent_(element, 'keyup', true);
78 /**
79  * Raises tab keyboard events to focus next element.
80  * @param {HTMLElement} element Element that should receive the event.
81  */
82 keyboard.raiseKeyFocusNext = function(element) {
83   keyboard.raiseTabKeyEvent_(element, 'keydown', false);
84   keyboard.raiseTabKeyEvent_(element, 'keypress', false);
85   keyboard.raiseTabKeyEvent_(element, 'keyup', false);
88 /**
89  * Initializes event handling for arrow keys driven focus flow.
90  */
91 keyboard.initializeKeyboardFlow = function() {
92   document.addEventListener('keydown',
93       keyboard.onKeyDown_, true);
94   document.addEventListener('keypress',
95       keyboard.onKeyIgnore_, true);
96   document.addEventListener('keyup',
97       keyboard.onKeyIgnore_, true);