141 lines
5.0 KiB
JavaScript
Raw Normal View History

import server from "../../../services/server.js";
import utils from "../../../services/utils.js";
import dialogService from "../../dialog.js";
2019-11-20 21:35:18 +01:00
const TPL = `
<h4>Keyboard shortcuts</h4>
<p>Multiple shortcuts for the same action can be separated by comma.</p>
<div class="form-group">
<input type="text" class="form-control" id="keyboard-shortcut-filter" placeholder="Type text to filter shortcuts...">
</div>
2019-11-20 21:35:18 +01:00
<div style="overflow: auto; height: 500px;">
<table id="keyboard-shortcut-table" cellpadding="10">
<thead>
<tr>
<th>Action name</th>
<th>Shortcuts</th>
<th>Default shortcuts</th>
<th>Description</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<div style="display: flex; justify-content: space-between">
2019-11-20 22:48:32 +01:00
<button class="btn btn-primary" id="options-keyboard-shortcuts-reload-app">Reload app to apply changes</button>
2019-11-20 21:35:18 +01:00
2019-11-20 22:48:32 +01:00
<button class="btn" id="options-keyboard-shortcuts-set-all-to-default">Set all shortcuts to the default</button>
2019-11-20 21:35:18 +01:00
</div>
`;
let globActions;
2019-11-20 21:35:18 +01:00
export default class KeyboardShortcutsOptions {
constructor() {
2021-10-11 22:30:23 +02:00
$("#options-shortcuts").html(TPL);
2019-11-20 21:35:18 +01:00
2021-08-24 22:59:51 +02:00
$("#options-keyboard-shortcuts-reload-app").on("click", () => utils.reloadFrontendApp());
2019-11-20 22:48:32 +01:00
2019-11-20 21:35:18 +01:00
const $table = $("#keyboard-shortcut-table tbody");
server.get('keyboard-actions').then(actions => {
globActions = actions;
2019-11-20 21:35:18 +01:00
for (const action of actions) {
2019-11-20 23:10:41 +01:00
const $tr = $("<tr>");
if (action.separator) {
$tr.append(
$('<td colspan="4">')
.attr("style","background-color: var(--accented-background-color); font-weight: bold;")
.text(action.separator)
2019-11-20 21:35:18 +01:00
)
2019-11-20 23:10:41 +01:00
}
else {
$tr.append($("<td>").text(action.actionName))
.append($("<td>").append(
$(`<input type="text" class="form-control">`)
.val(action.effectiveShortcuts.join(", "))
.attr('data-keyboard-action-name', action.actionName)
.attr('data-default-keyboard-shortcuts', action.defaultShortcuts.join(", "))
)
)
.append($("<td>").text(action.defaultShortcuts.join(", ")))
.append($("<td>").text(action.description));
}
2019-11-20 21:35:18 +01:00
$table.append($tr);
}
});
2019-11-20 22:48:32 +01:00
$table.on('change', 'input.form-control', e => {
const $input = $(e.target);
const actionName = $input.attr('data-keyboard-action-name');
const shortcuts = $input.val()
.replace('+,', "+Comma")
.split(",")
2019-11-20 23:10:41 +01:00
.map(shortcut => shortcut.replace("+Comma", "+,"))
.filter(shortcut => !!shortcut);
2019-11-20 21:35:18 +01:00
2019-11-20 22:48:32 +01:00
const opts = {};
opts['keyboardShortcuts' + actionName.substr(0, 1).toUpperCase() + actionName.substr(1)] = JSON.stringify(shortcuts);
2019-11-20 21:35:18 +01:00
2019-11-20 22:48:32 +01:00
server.put('options', opts);
});
2019-11-20 21:35:18 +01:00
2019-11-20 22:48:32 +01:00
$("#options-keyboard-shortcuts-set-all-to-default").on('click', async () => {
if (!await dialogService.confirm("Do you really want to reset all keyboard shortcuts to the default?")) {
2019-11-20 22:48:32 +01:00
return;
}
$table.find('input.form-control').each(function() {
const defaultShortcuts = $(this).attr('data-default-keyboard-shortcuts');
if ($(this).val() !== defaultShortcuts) {
$(this)
.val(defaultShortcuts)
.trigger('change');
}
});
});
const $filter = $("#keyboard-shortcut-filter");
$filter.on('keyup', () => {
const filter = $filter.val().trim().toLowerCase();
$table.find("tr").each((i, el) => {
if (!filter) {
$(el).show();
return;
}
const actionName = $(el).find('input').attr('data-keyboard-action-name');
if (!actionName) {
$(el).hide();
return;
}
const action = globActions.find(act => act.actionName === actionName);
if (!action) {
$(el).hide();
return;
}
$(el).toggle(!!( // !! to avoid toggle overloads with different behavior
action.actionName.toLowerCase().includes(filter)
|| action.defaultShortcuts.some(shortcut => shortcut.toLowerCase().includes(filter))
|| action.effectiveShortcuts.some(shortcut => shortcut.toLowerCase().includes(filter))
|| (action.description && action.description.toLowerCase().includes(filter))
));
});
});
2019-11-20 21:35:18 +01:00
}
}