2020-10-25 21:13:48 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
*
|
2020-10-24 01:04:25 +00:00
|
|
|
* This file Copyright (C) 2020 Mnemosyne LLC
|
|
|
|
*
|
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { createDialogContainer } from './utils.js';
|
|
|
|
|
|
|
|
export class RemoveDialog extends EventTarget {
|
|
|
|
constructor(options) {
|
|
|
|
super();
|
|
|
|
|
|
|
|
// options: remote, torrents, trash
|
|
|
|
this.options = options;
|
|
|
|
this.elements = RemoveDialog._create(options);
|
|
|
|
this.elements.dismiss.addEventListener('click', () => this._onDismiss());
|
|
|
|
this.elements.confirm.addEventListener('click', () => this._onConfirm());
|
|
|
|
document.body.append(this.elements.root);
|
|
|
|
this.elements.dismiss.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
if (!this.closed) {
|
|
|
|
this.elements.root.remove();
|
|
|
|
this.dispatchEvent(new Event('close'));
|
|
|
|
for (const key of Object.keys(this)) {
|
|
|
|
delete this[key];
|
|
|
|
}
|
|
|
|
this.closed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onDismiss() {
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onConfirm() {
|
2021-05-19 13:43:46 +00:00
|
|
|
const { remote, torrents, trash } = this.options;
|
2020-10-24 01:04:25 +00:00
|
|
|
if (torrents.length > 0) {
|
2021-10-25 22:24:20 +00:00
|
|
|
remote.removeTorrents(torrents, trash);
|
2020-10-24 01:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
static _create(options) {
|
|
|
|
const { trash } = options;
|
|
|
|
const { heading, message } = RemoveDialog._createMessage(options);
|
|
|
|
|
|
|
|
const elements = createDialogContainer('remove-dialog');
|
|
|
|
elements.heading.textContent = heading;
|
|
|
|
elements.message.textContent = message;
|
|
|
|
elements.confirm.textContent = trash ? 'Trash' : 'Remove';
|
|
|
|
return elements;
|
|
|
|
}
|
|
|
|
|
|
|
|
static _createMessage(options) {
|
|
|
|
let heading = null;
|
|
|
|
let message = null;
|
2021-05-19 13:43:46 +00:00
|
|
|
const { torrents, trash } = options;
|
2020-10-24 01:04:25 +00:00
|
|
|
const [torrent] = torrents;
|
2021-05-19 13:43:46 +00:00
|
|
|
if (trash && torrents.length === 1) {
|
2020-10-24 01:04:25 +00:00
|
|
|
heading = `Remove ${torrent.getName()} and delete data?`;
|
|
|
|
message =
|
|
|
|
'All data downloaded for this torrent will be deleted. Are you sure you want to remove it?';
|
2021-05-19 13:43:46 +00:00
|
|
|
} else if (trash) {
|
2020-10-24 01:04:25 +00:00
|
|
|
heading = `Remove ${torrents.length} transfers and delete data?`;
|
|
|
|
message =
|
|
|
|
'All data downloaded for these torrents will be deleted. Are you sure you want to remove them?';
|
|
|
|
} else if (torrents.length === 1) {
|
|
|
|
heading = `Remove ${torrent.getName()}?`;
|
|
|
|
message =
|
|
|
|
'Once removed, continuing the transfer will require the torrent file. Are you sure you want to remove it?';
|
|
|
|
} else {
|
|
|
|
heading = `Remove ${torrents.length} transfers?`;
|
|
|
|
message =
|
|
|
|
'Once removed, continuing the transfers will require the torrent files. Are you sure you want to remove them?';
|
|
|
|
}
|
|
|
|
return { heading, message };
|
|
|
|
}
|
|
|
|
}
|