jiti-meet/modules/keyboardshortcut/keyboardshortcut.js

155 lines
4.6 KiB
JavaScript
Raw Normal View History

/* global APP, $ */
2015-01-22 16:26:05 +00:00
//maps keycode to character, id of popover for given function and function
var shortcuts = {};
function initShortcutHandlers() {
shortcuts = {
"ESCAPE": {
character: "Esc",
function: function() {
APP.UI.showKeyboardShortcutsPanel(false);
}
},
"C": {
character: "C",
id: "toggleChatPopover",
2016-02-05 18:53:31 +00:00
function: function() {
APP.UI.toggleChat();
}
},
"D": {
character: "D",
id: "toggleDesktopSharingPopover",
function: function () {
APP.conference.toggleScreenSharing();
2016-02-05 18:53:31 +00:00
}
},
"F": {
character: "F",
id: "filmstripPopover",
2016-02-05 18:53:31 +00:00
function: function() {
APP.UI.toggleFilmStrip();
}
},
"M": {
character: "M",
id: "mutePopover",
2016-02-05 18:53:31 +00:00
function: function() {
APP.conference.toggleAudioMuted();
}
},
"R": {
2016-06-20 21:13:17 +00:00
character: "R",
function: function() {
APP.conference.maybeToggleRaisedHand();
}
},
"T": {
character: "T",
function: function() {
2015-12-03 13:11:01 +00:00
APP.conference.muteAudio(true);
2015-01-22 16:26:05 +00:00
}
},
"V": {
character: "V",
id: "toggleVideoPopover",
2016-02-05 18:53:31 +00:00
function: function() {
APP.conference.toggleVideoMuted();
}
},
"?": {
character: "?",
function: function(e) {
APP.UI.toggleKeyboardShortcutsPanel();
}
2015-01-22 16:26:05 +00:00
}
};
}
2015-01-22 16:26:05 +00:00
var KeyboardShortcut = {
init: function () {
initShortcutHandlers();
var self = this;
2015-01-22 16:26:05 +00:00
window.onkeyup = function(e) {
var key = self.getKeyboardKey(e).toUpperCase();
var num = parseInt(key, 10);
2015-01-22 16:26:05 +00:00
if(!($(":focus").is("input[type=text]") ||
$(":focus").is("input[type=password]") ||
$(":focus").is("textarea"))) {
if (shortcuts.hasOwnProperty(key)) {
shortcuts[key].function(e);
2015-01-22 16:26:05 +00:00
}
else if (!isNaN(num) && num >= 0 && num <= 9) {
APP.UI.clickOnVideo(num + 1);
2015-01-22 16:26:05 +00:00
}
//esc while the smileys are visible hides them
} else if (key === "ESCAPE" &&
$('#smileysContainer').is(':visible')) {
APP.UI.toggleSmileys();
2015-01-22 16:26:05 +00:00
}
};
window.onkeydown = function(e) {
if(!($(":focus").is("input[type=text]") ||
$(":focus").is("input[type=password]") ||
$(":focus").is("textarea"))) {
var key = self.getKeyboardKey(e).toUpperCase();
if(key === "T") {
2016-02-09 16:29:50 +00:00
if(APP.conference.isLocalAudioMuted())
APP.conference.muteAudio(false);
2015-01-22 16:26:05 +00:00
}
}
};
$('body').popover({ selector: '[data-toggle=popover]',
trigger: 'click hover',
content: function() {
return this.getAttribute("content") +
self.getShortcut(this.getAttribute("shortcut"));
}
});
2015-01-22 16:26:05 +00:00
},
/**
*
* @param id indicates the popover associated with the shortcut
* @returns {string} the keyboard shortcut used for the id given
*/
getShortcut: function (id) {
for (var key in shortcuts) {
if (shortcuts.hasOwnProperty(key)) {
if (shortcuts[key].id === id) {
return " (" + shortcuts[key].character + ")";
2015-01-22 16:26:05 +00:00
}
}
}
return "";
},
/**
* @param e a KeyboardEvent
* @returns {string} e.key or something close if not supported
*/
getKeyboardKey: function (e) {
if (typeof e.key === "string") {
return e.key;
}
if (e.type === "keypress" && (
(e.which >= 32 && e.which <= 126) ||
(e.which >= 160 && e.which <= 255) )) {
return String.fromCharCode(e.which);
}
// try to fallback (0-9A-Za-z and QWERTY keyboard)
switch (e.which) {
case 27:
return "Escape";
case 191:
return e.shiftKey ? "?" : "/";
}
if (e.shiftKey || e.type === "keypress") {
return String.fromCharCode(e.which);
} else {
return String.fromCharCode(e.which).toLowerCase();
}
2015-01-22 16:26:05 +00:00
}
};
module.exports = KeyboardShortcut;