Skip to content

Commit

Permalink
Use block to prevent global variables, add use strict (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver3 authored Jun 9, 2024
1 parent e9a49e8 commit 2a6479b
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 214 deletions.
182 changes: 91 additions & 91 deletions django_admin_keyboard_shortcuts/static/admin/js/shortcuts.js
Original file line number Diff line number Diff line change
@@ -1,99 +1,98 @@
let previousKey = undefined;
const shortcutFunctions = new Map([
["g i", () => { document.location.href = "/admin/"; }],
["g l", () => showDialog("model-list-dialog")]
]);

function registerDeclarativeShortcuts() {
const elements = document.querySelectorAll('[data-keyboard-shortcut]');
for (const element of elements) {
shortcutFunctions.set(element.getAttribute('data-keyboard-shortcut'), () => {
element.click();
});
'use strict';
{
let previousKey = undefined;
const shortcutFunctions = new Map([
["g i", () => { document.location.href = "/admin/"; }],
["g l", () => showDialog("model-list-dialog")]
]);

function registerDeclarativeShortcuts() {
const elements = document.querySelectorAll('[data-keyboard-shortcut]');
for (const element of elements) {
shortcutFunctions.set(element.getAttribute('data-keyboard-shortcut'), () => {
element.click();
});
}
}
}

function isApple() {
return (
navigator.platform.indexOf("Mac") === 0 ||
navigator.platform === "iPhone"
)
}
function isApple() {
return (navigator.platform.indexOf("Mac") === 0 || navigator.platform === "iPhone")
}

function removePreviousKey(key) {
if (previousKey === key) {
previousKey = undefined;
function removePreviousKey(key) {
if (previousKey === key) {
previousKey = undefined;
}
}
}

function storePreviousKey(key) {
previousKey = key;
setTimeout(function() { removePreviousKey(key); }, 5000);
}
function storePreviousKey(key) {
previousKey = key;
setTimeout(function () {
removePreviousKey(key);
}, 5000);
}

function showDialog(id) {
dialog = document.getElementById(id);
dialog.showModal();
}
function showDialog(id) {
const dialog = document.getElementById(id);
dialog.showModal();
}

function showShortcutsDialog() {
showDialog("shortcuts-dialog");
}
function showShortcutsDialog() {
showDialog("shortcuts-dialog");
}

function showDialogOnClick() {
dialogButton = document.getElementById("open-shortcuts");
dialogButton.addEventListener("click", showShortcutsDialog);
}
function showDialogOnClick() {
const dialogButton = document.getElementById("open-shortcuts");
dialogButton.addEventListener("click", showShortcutsDialog);
}

function handleKeyUp(event) {
const shortcut = previousKey ? `${previousKey} ${event.key}` : event.key;
if (shortcutFunctions.has(shortcut)) {
shortcutFunctions.get(shortcut)();
} else {
storePreviousKey(event.key);
function handleKeyUp(event) {
const shortcut = previousKey ? `${previousKey} ${event.key}` : event.key;
if (shortcutFunctions.has(shortcut)) {
shortcutFunctions.get(shortcut)();
} else {
storePreviousKey(event.key);
}
}
}

function replaceModifiers() {
if (isApple()) {
document.querySelectorAll(".shortcut-keys .alt").forEach(function(modifier) {
modifier.innerHTML = "⌥";
});
function replaceModifiers() {
if (isApple()) {
document.querySelectorAll(".shortcut-keys .alt").forEach(function (modifier) {
modifier.innerHTML = "⌥";
});
}
}
}

// This overlaps a lot with the initSidebarQuickFilter function
// defined in django/contrib/admin/static/admin/js/nav_sidebar.js
// If/when merged into core, we could try to reuse some parts
function filterModelList() {
const modelListDialog = document.getElementById('model-list-dialog');
if (!modelListDialog) {
function filterModelList() {
const modelListDialog = document.getElementById('model-list-dialog');
if (!modelListDialog) {
return;
}
}

const appSections = [];

const appSections = [];

modelListDialog.querySelectorAll('section').forEach(section => {
const options = [];
section.querySelectorAll('li a').forEach(container => options.push({
title: container.innerHTML,
node: container
}));

appSections.push({
node: section,
options,
modelListDialog.querySelectorAll('section').forEach(section => {
const options = [];
section.querySelectorAll('li a').forEach(container => options.push({
title: container.innerHTML, node: container
}));

appSections.push({
node: section, options,
});
});
});

function checkValue(event) {
function checkValue(event) {
let filterValue = event.target.value;
if (filterValue) {
filterValue = filterValue.toLowerCase();
filterValue = filterValue.toLowerCase();
}
if (event.key === 'Escape') {
filterValue = '';
event.target.value = ''; // clear input
filterValue = '';
event.target.value = ''; // clear input
}

appSections.forEach(section => {
Expand All @@ -119,27 +118,28 @@ function filterModelList() {
// Set display for the app section
section.node.style.display = sectionDisplay;
});
}
}

const nav = document.getElementById('model-list-dialog-search');
nav.addEventListener('change', checkValue, false);
nav.addEventListener('input', checkValue, false);
nav.addEventListener('keyup', checkValue, false);
const nav = document.getElementById('model-list-dialog-search');
nav.addEventListener('change', checkValue, false);
nav.addEventListener('input', checkValue, false);
nav.addEventListener('keyup', checkValue, false);

// We don't want to show anything on the list until the user starts typing
checkValue({ target: { value: ''} , key: ''})
}
// We don't want to show anything on the list until the user starts typing
checkValue({target: {value: ''}, key: ''})
}


if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", showDialogOnClick);
document.addEventListener("DOMContentLoaded", replaceModifiers);
document.addEventListener("DOMContentLoaded", filterModelList);
document.addEventListener("DOMContentLoaded", registerDeclarativeShortcuts);
} else {
showDialogOnClick();
replaceModifiers();
filterModelList();
registerDeclarativeShortcuts();
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", showDialogOnClick);
document.addEventListener("DOMContentLoaded", replaceModifiers);
document.addEventListener("DOMContentLoaded", filterModelList);
document.addEventListener("DOMContentLoaded", registerDeclarativeShortcuts);
} else {
showDialogOnClick();
replaceModifiers();
filterModelList();
registerDeclarativeShortcuts();
}
document.addEventListener("keyup", handleKeyUp);
}
document.addEventListener("keyup", handleKeyUp);
Original file line number Diff line number Diff line change
@@ -1,42 +1,46 @@
let saveButton = null;
let saveAndAddButton = null;
let saveAndContinueButton = null;
'use strict';
{
let saveButton = null;
let saveAndAddButton = null;
let saveAndContinueButton = null;
let deleteButton = null;

function setUpShortcuts() {
saveButton = document.querySelector("input[name=_save]");
saveAndAddButton = document.querySelector("input[name=_addanother]");
saveAndContinueButton = document.querySelector("input[name=_continue]");
deleteButton = document.querySelector(".deletelink");
}
function setUpShortcuts() {
saveButton = document.querySelector("input[name=_save]");
saveAndAddButton = document.querySelector("input[name=_addanother]");
saveAndContinueButton = document.querySelector("input[name=_continue]");
deleteButton = document.querySelector(".deletelink");
}

function handleKeyUp(event) {
switch (event.code) {
case "KeyS":
if (event.altKey) {
saveButton.click();
}
break;
case "KeyA":
if (event.altKey) {
saveAndAddButton.click();
}
break;
case "KeyC":
if (event.altKey) {
saveAndContinueButton.click();
}
break;
case "KeyD":
if (event.altKey) {
deleteButton.click();
}
break;
function handleKeyUp(event) {
switch (event.code) {
case "KeyS":
if (event.altKey) {
saveButton.click();
}
break;
case "KeyA":
if (event.altKey) {
saveAndAddButton.click();
}
break;
case "KeyC":
if (event.altKey) {
saveAndContinueButton.click();
}
break;
case "KeyD":
if (event.altKey) {
deleteButton.click();
}
break;
}
}
}

if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", setUpShortcuts);
} else {
setUpShortcuts();
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", setUpShortcuts);
} else {
setUpShortcuts();
}
document.addEventListener("keyup", handleKeyUp);
}
document.addEventListener("keyup", handleKeyUp);
Loading

0 comments on commit 2a6479b

Please sign in to comment.