2023-11-01 21:11:11 +00:00
|
|
|
/* @license This file Copyright © Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
or any future license endorsed by Mnemosyne LLC.
|
|
|
|
License text can be found in the licenses/ folder. */
|
2020-10-24 01:04:25 +00:00
|
|
|
|
|
|
|
import { setEnabled } from './utils.js';
|
|
|
|
|
|
|
|
export class ContextMenu extends EventTarget {
|
|
|
|
constructor(action_manager) {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.action_listener = this._update.bind(this);
|
|
|
|
this.action_manager = action_manager;
|
|
|
|
this.action_manager.addEventListener('change', this.action_listener);
|
|
|
|
|
|
|
|
Object.assign(this, this._create());
|
|
|
|
this.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
show() {
|
|
|
|
for (const [action, item] of Object.entries(this.actions)) {
|
|
|
|
setEnabled(item, this.action_manager.isEnabled(action));
|
|
|
|
}
|
|
|
|
document.body.append(this.root);
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
if (!this.closed) {
|
|
|
|
this.action_manager.removeEventListener('change', this.action_listener);
|
|
|
|
this.root.remove();
|
|
|
|
this.dispatchEvent(new Event('close'));
|
|
|
|
for (const key of Object.keys(this)) {
|
|
|
|
delete this[key];
|
|
|
|
}
|
|
|
|
this.closed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_update(event_) {
|
|
|
|
const e = this.actions[event_.action];
|
|
|
|
if (e) {
|
|
|
|
setEnabled(e, event_.enabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_create() {
|
|
|
|
const root = document.createElement('div');
|
|
|
|
root.role = 'menu';
|
|
|
|
root.classList.add('context-menu', 'popup');
|
2023-09-27 15:48:16 +00:00
|
|
|
root.addEventListener('contextmenu', (e_) => {
|
|
|
|
e_.preventDefault();
|
|
|
|
});
|
|
|
|
root.style.pointerEvents = 'none';
|
2020-10-24 01:04:25 +00:00
|
|
|
|
|
|
|
const actions = {};
|
2022-10-21 17:22:59 +00:00
|
|
|
const add_item = (action, warn = false) => {
|
2020-10-24 01:04:25 +00:00
|
|
|
const item = document.createElement('div');
|
|
|
|
const text = this.action_manager.text(action);
|
|
|
|
item.role = 'menuitem';
|
2022-10-21 17:22:59 +00:00
|
|
|
if (warn) {
|
|
|
|
item.classList.add('context-menuitem', 'warning');
|
|
|
|
} else {
|
|
|
|
item.classList.add('context-menuitem');
|
|
|
|
}
|
2020-10-24 01:04:25 +00:00
|
|
|
item.dataset.action = action;
|
|
|
|
item.textContent = text;
|
|
|
|
const keyshortcuts = this.action_manager.keyshortcuts(action);
|
|
|
|
if (keyshortcuts) {
|
|
|
|
item.setAttribute('aria-keyshortcuts', keyshortcuts);
|
|
|
|
}
|
|
|
|
item.addEventListener('click', () => {
|
|
|
|
this.action_manager.click(action);
|
|
|
|
this.close();
|
|
|
|
});
|
|
|
|
actions[action] = item;
|
|
|
|
root.append(item);
|
|
|
|
};
|
|
|
|
|
|
|
|
const add_separator = () => {
|
|
|
|
const item = document.createElement('div');
|
|
|
|
item.classList.add('context-menu-separator');
|
|
|
|
root.append(item);
|
|
|
|
};
|
|
|
|
|
|
|
|
add_item('resume-selected-torrents');
|
|
|
|
add_item('resume-selected-torrents-now');
|
|
|
|
add_item('pause-selected-torrents');
|
|
|
|
add_separator();
|
|
|
|
add_item('move-top');
|
|
|
|
add_item('move-up');
|
|
|
|
add_item('move-down');
|
|
|
|
add_item('move-bottom');
|
|
|
|
add_separator();
|
2022-10-21 17:22:59 +00:00
|
|
|
add_item('remove-selected-torrents', true);
|
|
|
|
add_item('trash-selected-torrents', true);
|
2020-10-24 01:04:25 +00:00
|
|
|
add_separator();
|
|
|
|
add_item('verify-selected-torrents');
|
|
|
|
add_item('show-move-dialog');
|
|
|
|
add_item('show-rename-dialog');
|
2022-02-10 15:25:11 +00:00
|
|
|
add_item('show-labels-dialog');
|
2020-10-24 01:04:25 +00:00
|
|
|
add_separator();
|
|
|
|
add_item('reannounce-selected-torrents');
|
|
|
|
add_separator();
|
|
|
|
add_item('select-all');
|
|
|
|
add_item('deselect-all');
|
|
|
|
|
|
|
|
return { actions, root };
|
|
|
|
}
|
|
|
|
}
|