Add namespacing to hotkeys.js.

This commit is contained in:
voussoir 2018-07-22 19:55:29 -07:00
parent 63a3ecfa88
commit 1bff642ca7
2 changed files with 22 additions and 15 deletions

View file

@ -1,11 +1,15 @@
HOTKEYS = {};
var hotkeys = {};
hotkeys.HOTKEYS = {};
hotkeys.hotkey_identifier =
function hotkey_identifier(key, ctrlKey, shiftKey, altKey)
{
// Return the string that will represent this hotkey in the dictionary.
return key.toLowerCase() + "." + (ctrlKey & 1) + "." + (shiftKey & 1) + "." + (altKey & 1);
}
hotkeys.hotkey_human =
function hotkey_human(key, ctrlKey, shiftKey, altKey)
{
// Return the string that will be displayed to the user to represent this hotkey.
@ -18,13 +22,15 @@ function hotkey_human(key, ctrlKey, shiftKey, altKey)
return mods + key.toUpperCase();
}
hotkeys.register_hotkey =
function register_hotkey(key, ctrlKey, shiftKey, altKey, action, description)
{
identifier = hotkey_identifier(key, ctrlKey, shiftKey, altKey);
human = hotkey_human(key, ctrlKey, shiftKey, altKey);
HOTKEYS[identifier] = {"action": action, "human": human, "description": description}
identifier = hotkeys.hotkey_identifier(key, ctrlKey, shiftKey, altKey);
human = hotkeys.hotkey_human(key, ctrlKey, shiftKey, altKey);
hotkeys.HOTKEYS[identifier] = {"action": action, "human": human, "description": description}
}
hotkeys.should_prevent_hotkey =
function should_prevent_hotkey(event)
{
if (event.target.tagName == "INPUT" && event.target.type == "checkbox")
@ -37,13 +43,14 @@ function should_prevent_hotkey(event)
}
}
hotkeys.show_all_keybinds =
function show_all_keybinds()
{
// Display an Alert with a list of all the keybinds.
var lines = [];
for (var identifier in HOTKEYS)
for (var identifier in hotkeys.HOTKEYS)
{
var line = HOTKEYS[identifier]["human"] + " : " + HOTKEYS[identifier]["description"];
var line = hotkeys.HOTKEYS[identifier]["human"] + " : " + hotkeys.HOTKEYS[identifier]["description"];
lines.push(line);
}
lines = lines.join("\n");
@ -55,15 +62,15 @@ window.addEventListener(
"keydown",
function(event)
{
if (should_prevent_hotkey(event)) { return; }
identifier = hotkey_identifier(event.key, event.ctrlKey, event.shiftKey, event.altKey);
if (hotkeys.should_prevent_hotkey(event)) { return; }
identifier = hotkeys.hotkey_identifier(event.key, event.ctrlKey, event.shiftKey, event.altKey);
console.log(identifier);
if (identifier in HOTKEYS)
if (identifier in hotkeys.HOTKEYS)
{
HOTKEYS[identifier]["action"]();
hotkeys.HOTKEYS[identifier]["action"]();
event.preventDefault();
}
}
);
register_hotkey("/", 0, 0, 0, show_all_keybinds, "Show keybinds.");
hotkeys.register_hotkey("/", 0, 0, 0, hotkeys.show_all_keybinds, "Show keybinds.");

View file

@ -317,10 +317,10 @@ photo_clipboard.on_pageload =
function on_pageload()
{
window.addEventListener("storage", photo_clipboard.on_storage_event, false);
register_hotkey("a", 1, 0, 0, photo_clipboard.select_all_photos, "Select all photos.");
register_hotkey("d", 1, 0, 0, photo_clipboard.unselect_all_photos, "Deselect all photos.");
register_hotkey("c", 0, 0, 0, photo_clipboard.clipboard_tray_collapse_toggle, "Toggle clipboard tray.");
register_hotkey("c", 0, 1, 0, photo_clipboard.open_full_clipboard_tab, "Open full clipboard page.");
hotkeys.register_hotkey("a", 1, 0, 0, photo_clipboard.select_all_photos, "Select all photos.");
hotkeys.register_hotkey("d", 1, 0, 0, photo_clipboard.unselect_all_photos, "Deselect all photos.");
hotkeys.register_hotkey("c", 0, 0, 0, photo_clipboard.clipboard_tray_collapse_toggle, "Toggle clipboard tray.");
hotkeys.register_hotkey("c", 0, 1, 0, photo_clipboard.open_full_clipboard_tab, "Open full clipboard page.");
photo_clipboard.load_clipboard();
photo_clipboard.update_pagestate();
}