mirror of
https://github.com/webodf/WebODF.git
synced 2026-12-01 16:56:05 +01:00
Move control of event bindings from SessionController to HyperlinkClickHandler so bindings will be updated whenever modifier is changed. This allows some simplification as HyperlinkClickHandler can simply use the KeyboardHandler.Modifier.
234 lines
6.8 KiB
JavaScript
234 lines
6.8 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright (C) 2012-2013 KO GmbH <copyright@kogmbh.com>
|
|
*
|
|
* @licstart
|
|
* The JavaScript code in this page is free software: you can redistribute it
|
|
* and/or modify it under the terms of the GNU Affero General Public License
|
|
* (GNU AGPL) as published by the Free Software Foundation, either version 3 of
|
|
* the License, or (at your option) any later version. The code is distributed
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU AGPL for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this code. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
* As additional permission under GNU AGPL version 3 section 7, you
|
|
* may distribute non-source (e.g., minimized or compacted) forms of
|
|
* that code without the copy of the GNU GPL normally required by
|
|
* section 4, provided you include this license notice and a URL
|
|
* through which recipients can access the Corresponding Source.
|
|
*
|
|
* As a special exception to the AGPL, any HTML file which merely makes function
|
|
* calls to this code, and for that purpose includes it by reference shall be
|
|
* deemed a separate work for copyright law purposes. In addition, the copyright
|
|
* holders of this code give you permission to combine this code with free
|
|
* software libraries that are released under the GNU LGPL. You may copy and
|
|
* distribute such a system following the terms of the GNU AGPL for this code
|
|
* and the LGPL for the libraries. If you modify this code, you may extend this
|
|
* exception to your version of the code, but you are not obligated to do so.
|
|
* If you do not wish to do so, delete this exception statement from your
|
|
* version.
|
|
*
|
|
* This license applies to this entire compilation.
|
|
* @licend
|
|
* @source: http://www.webodf.org/
|
|
* @source: https://github.com/kogmbh/WebODF/
|
|
*/
|
|
|
|
/*global gui, runtime */
|
|
/*jslint bitwise: true*/
|
|
|
|
/**
|
|
* @constructor
|
|
* @return {?}
|
|
*/
|
|
gui.KeyboardHandler = function KeyboardHandler() {
|
|
"use strict";
|
|
var modifier = gui.KeyboardHandler.Modifier,
|
|
/**@type{?function(!KeyboardEvent):boolean}*/
|
|
defaultBinding = null,
|
|
/**@type{!Object.<string,function():boolean>}*/
|
|
bindings = {};
|
|
|
|
/**
|
|
* @param {!KeyboardEvent} e
|
|
* @return {!number}
|
|
*/
|
|
function getModifiers(e) {
|
|
var modifiers = modifier.None;
|
|
if (e.metaKey) { modifiers |= modifier.Meta; }
|
|
if (e.ctrlKey) { modifiers |= modifier.Ctrl; }
|
|
if (e.altKey) { modifiers |= modifier.Alt; }
|
|
if (e.shiftKey) { modifiers |= modifier.Shift; }
|
|
return modifiers;
|
|
}
|
|
|
|
/**
|
|
* @param {!number} keyCode
|
|
* @param {!number} modifiers
|
|
* @return {!string}
|
|
*/
|
|
function getKeyCombo(keyCode, modifiers) {
|
|
if (!modifiers) {
|
|
modifiers = modifier.None;
|
|
}
|
|
// When a modifier key is pressed on it's own, different browsers + platforms
|
|
// may report the state of the corresponding modifier flag as trailing the event.
|
|
// For example, pressing the ctrl key in FF on Linux will report (ctrlKey:false)
|
|
// in the keydown event, while Chrome on Linux will report (ctrlKey:true) in the
|
|
// keydown event.
|
|
//
|
|
// Easiest way to cope with this is to manually normalize these events.
|
|
switch (keyCode) {
|
|
case gui.KeyboardHandler.KeyCode.LeftMeta:
|
|
case gui.KeyboardHandler.KeyCode.RightMeta:
|
|
case gui.KeyboardHandler.KeyCode.MetaInMozilla:
|
|
modifiers |= modifier.Meta;
|
|
break;
|
|
case gui.KeyboardHandler.KeyCode.Ctrl:
|
|
modifiers |= modifier.Ctrl;
|
|
break;
|
|
case gui.KeyboardHandler.KeyCode.Alt:
|
|
modifiers |= modifier.Alt;
|
|
break;
|
|
case gui.KeyboardHandler.KeyCode.Shift:
|
|
modifiers |= modifier.Shift;
|
|
break;
|
|
}
|
|
|
|
return keyCode + ':' + modifiers;
|
|
}
|
|
|
|
/**
|
|
* @param {?Function} callback
|
|
*/
|
|
this.setDefault = function (callback) {
|
|
defaultBinding = callback;
|
|
};
|
|
|
|
/**
|
|
* Bind to the specified keycode + modifiers. To bind directly to one of the modifiers, simply
|
|
* pass in the modifier as the keyCode (e.g., Keycode.Ctrl), and set the modifiers to Modifier.None.
|
|
* This class will take care of binding to the appropriate modifiers to ensure the keybinding works as
|
|
* expected.
|
|
*
|
|
* @param {!number} keyCode
|
|
* @param {!number} modifiers
|
|
* @param {!Function} callback
|
|
* @param {boolean=} overwrite Set to true to force a binding to be overwritten
|
|
*/
|
|
this.bind = function (keyCode, modifiers, callback, overwrite) {
|
|
var keyCombo = getKeyCombo(keyCode, modifiers);
|
|
runtime.assert(overwrite || bindings.hasOwnProperty(keyCombo) === false,
|
|
"tried to overwrite the callback handler of key combo: " + keyCombo);
|
|
bindings[keyCombo] = callback;
|
|
};
|
|
|
|
/**
|
|
* @param {!number} keyCode
|
|
* @param {!number} modifiers
|
|
*/
|
|
this.unbind = function (keyCode, modifiers) {
|
|
var keyCombo = getKeyCombo(keyCode, modifiers);
|
|
delete bindings[keyCombo];
|
|
};
|
|
|
|
/*
|
|
* removes all the bindings includes the default binding
|
|
*/
|
|
this.reset = function () {
|
|
defaultBinding = null;
|
|
bindings = {};
|
|
};
|
|
|
|
/**
|
|
* @param {!KeyboardEvent} e
|
|
*/
|
|
this.handleEvent = function (e) {
|
|
var keyCombo = getKeyCombo(e.keyCode, getModifiers(e)),
|
|
/**@type{function():boolean|undefined}*/
|
|
callback = bindings[keyCombo],
|
|
handled = false;
|
|
|
|
if (callback) {
|
|
handled = callback();
|
|
} else if (defaultBinding !== null) {
|
|
handled = defaultBinding(e);
|
|
}
|
|
|
|
if (handled) {
|
|
if (e.preventDefault) {
|
|
e.preventDefault();
|
|
} else {
|
|
e.returnValue = false;
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
/**@const*/
|
|
gui.KeyboardHandler.Modifier = {
|
|
None: 0,
|
|
Meta: 1,
|
|
Ctrl: 2,
|
|
Alt: 4,
|
|
CtrlAlt: 6,
|
|
Shift: 8,
|
|
MetaShift: 9,
|
|
CtrlShift: 10,
|
|
AltShift: 12
|
|
};
|
|
|
|
/**@const*/
|
|
gui.KeyboardHandler.KeyCode = {
|
|
Backspace: 8,
|
|
Tab: 9,
|
|
Clear: 12,
|
|
Enter: 13,
|
|
Shift: 16,
|
|
Ctrl: 17,
|
|
Alt: 18,
|
|
End: 35,
|
|
Home: 36,
|
|
Left: 37,
|
|
Up: 38,
|
|
Right: 39,
|
|
Down: 40,
|
|
Delete: 46,
|
|
A: 65,
|
|
B: 66,
|
|
C: 67,
|
|
D: 68,
|
|
E: 69,
|
|
F: 70,
|
|
G: 71,
|
|
H: 72,
|
|
I: 73,
|
|
J: 74,
|
|
K: 75,
|
|
L: 76,
|
|
M: 77,
|
|
N: 78,
|
|
O: 79,
|
|
P: 80,
|
|
Q: 81,
|
|
R: 82,
|
|
S: 83,
|
|
T: 84,
|
|
U: 85,
|
|
V: 86,
|
|
W: 87,
|
|
X: 88,
|
|
Y: 89,
|
|
Z: 90,
|
|
LeftMeta: 91,
|
|
RightMeta: 93,
|
|
MetaInMozilla: 224
|
|
};
|
|
|
|
(function () {
|
|
"use strict";
|
|
return gui.KeyboardHandler;
|
|
}());
|