tagwiki/resources/script.js

96 lines
2.7 KiB
JavaScript

/* https://raw.githubusercontent.com/ccampbell/mousetrap/master/plugins/global-bind/mousetrap-global-bind.min.js */
(function(a){var c={},d=a.prototype.stopCallback;a.prototype.stopCallback=function(e,b,a,f){return this.paused?!0:c[a]||c[f]?!1:d.call(this,e,b,a)};a.prototype.bindGlobal=function(a,b,d){this.bind(a,b,d);if(a instanceof Array)for(b=0;b<a.length;b++)c[a[b]]=!0;else c[a]=!0};a.init()})(Mousetrap);
// OWN, CUSTOM CODE BELOW
// for some odd reason, without using a timeout, `.click()` does not always work
function reliableClick(domElement) {
window.setTimeout(function () {
domElement.click();
}, 1);
}
function reliableClickById(id) {
const elem = document.getElementById(id);
if (elem) {
reliableClick(elem);
}
}
// Modified: https://stackoverflow.com/a/35173443/12271202
// dir: 1 for down, -1 for up
function indexFocusSwitch(dir) {
let indexList = document.getElementById('index');
if (indexList) {
var focussableElements = 'a:not([disabled])';
var focussable = Array.prototype.filter.call(indexList.querySelectorAll(focussableElements),
function (element) {
return true;
}
);
var index = focussable.indexOf(document.activeElement);
if(focussable.length > 0) {
var nextElement = focussable[(index + dir + focussable.length) % focussable.length] || focussable[0];
nextElement.focus();
}
}
}
Mousetrap.bind('n', function() {
window.location.href = '/?edit=true';
});
Mousetrap.bind('e', function() {
reliableClickById('edit-button');
});
Mousetrap.bind('d', function() {
reliableClickById('delete-button');
});
Mousetrap.bindGlobal(['alt+enter', 'ctrl+enter'], function() {
reliableClickById('save-button');
});
Mousetrap.bind('s', function() {
reliableClickById('save-button');
});
Mousetrap.bindGlobal('esc', function() {
let textarea = document.getElementById('source-editor');
if (textarea && textarea === document.activeElement) {
textarea.blur();
let button = document.getElementById('cancel-button');
if (button) {
button.focus();
}
} else {
reliableClickById('cancel-button');
reliableClickById('up-button');
}
});
Mousetrap.bind(['ctrl+left', 'h'], function() {
reliableClickById('cancel-button');
reliableClickById('up-button');
});
Mousetrap.bind(['ctrl+right', 'l'], function() {
const elem = document.activeElement;
if (elem) {
reliableClick(elem);
}
});
Mousetrap.bind(['ctrl+down', 'j'], function() {
indexFocusSwitch(1);
});
Mousetrap.bind(['ctrl+up', 'k'], function() {
indexFocusSwitch(-1);
});
// auto-select first element on index pages
indexFocusSwitch(1);