forked from jummbus/jummbox
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathSongRecoveryPrompt.ts
60 lines (47 loc) · 2.82 KB
/
SongRecoveryPrompt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) 2012-2022 John Nesky and contributing authors, distributed under the MIT license, see accompanying the LICENSE.md file.
import { SongDocument } from "./SongDocument";
import { RecoveredSong, RecoveredVersion, SongRecovery, versionToKey } from "./SongRecovery";
import { Prompt } from "./Prompt";
import { HTML } from "imperative-html/dist/esm/elements-strict";
const {button, div, h2, p, select, option, iframe} = HTML;
export class SongRecoveryPrompt implements Prompt {
private readonly _songContainer: HTMLDivElement = div();
private readonly _cancelButton: HTMLButtonElement = button({class: "cancelButton"});
public readonly container: HTMLDivElement = div({class: "prompt", style: "width: 300px;"},
h2("Song Recovery"),
div({style: "max-height: 385px; overflow-y: auto;"},
p("This is a TEMPORARY list of songs you have recently modified. Please keep your own backups of songs you care about! SONGS THAT USE SAMPLES WILL TAKE A WHILE TO LOAD, so be patient!"),
this._songContainer,
p("(If \"Display Song Data in URL\" is enabled in your preferences, then you may also be able to find song versions in your browser history. However, song recovery won't work if you were browsing in private/incognito mode.)"),
),
this._cancelButton,
);
constructor(private _doc: SongDocument) {
this._cancelButton.addEventListener("click", this._close);
const songs: RecoveredSong[] = SongRecovery.getAllRecoveredSongs();
if (songs.length == 0) {
this._songContainer.appendChild(p("There are no recovered songs available yet. Try making a song!"));
}
for (const song of songs) {
const versionMenu: HTMLSelectElement = select({style: "width: 100%;"});
for (const version of song.versions) {
versionMenu.appendChild(option({ value: version.time }, version.name + ": " + new Date(version.time).toLocaleString()));
}
const player: HTMLIFrameElement = iframe({style: "width: 100%; height: 60px; border: none; display: block;"});
player.src = "player/" + (OFFLINE ? "index.html" : "") + "#song=" + window.localStorage.getItem(versionToKey(song.versions[0]));
const container: HTMLDivElement = div({style: "margin: 4px 0;"}, div({class: "selectContainer", style: "width: 100%; margin: 2px 0;"}, versionMenu), player);
this._songContainer.appendChild(container);
versionMenu.addEventListener("change", () => {
const version: RecoveredVersion = song.versions[versionMenu.selectedIndex];
player.contentWindow!.location.replace("player/" + (OFFLINE ? "index.html" : "") + "#song=" + window.localStorage.getItem(versionToKey(version)));
player.contentWindow!.dispatchEvent(new Event("hashchange"));
});
}
}
private _close = (): void => {
this._doc.undo();
}
public cleanUp = (): void => {
this._cancelButton.removeEventListener("click", this._close);
}
}