All files / src/web page-actions.js

97.61% Statements 41/42
91.66% Branches 11/12
91.66% Functions 11/12
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79      1x                   12x 12x 12x 12x   12x 12x   12x     1x 1x       12x   12x     3x 1x 1x         12x 1x 1x   12x 1x 1x   12x   12x 12x   12x 12x         12x       1x 1x       1x 1x       2x 1x      
import { downloadDriveData } from "./dom-utils.js";
 
const UnloadWarning =
    "It seems like you're still using the emulator. If you're in Chrome, it's impossible for jsbeeb to prevent some shortcuts (like ctrl-W) from performing their default behaviour (e.g. closing the window).\n" +
    "As a workaround, create an 'Application Shortcut' from the Tools menu.  When jsbeeb runs as an application, it *can* prevent ctrl-W from closing the window.";
 
/**
 * The page around the emulator: the reset menu items and the filestore
 * download, what focus and unload do to the machine, the dialogs a URL
 * can ask for, and the version stamp.
 */
export class PageActions {
    constructor({ loop, processor, keyboard, audioHandler, rewindUI, modals, parsedQuery, version }) {
        this.processor = processor;
        this.rewindUI = rewindUI;
        this.pageListeners = new AbortController();
        const { signal } = this.pageListeners;
 
        for (const el of document.querySelectorAll(".initially-hidden")) el.classList.remove("initially-hidden");
        document.getElementById("paste-form").addEventListener("submit", (event) => event.preventDefault());
 
        window.addEventListener(
            "blur",
            () => {
                keyboard.clearKeys();
                loop.setEmulationLead(audioHandler.setWindowFocused(false));
            },
            { signal },
        );
        window.addEventListener("focus", () => loop.setEmulationLead(audioHandler.setWindowFocused(true)), { signal });
 
        window.addEventListener(
            "beforeunload",
            (event) => {
                if (!loop.isRunning() || !processor.sysvia.hasAnyKeyDown()) return;
                event.preventDefault();
                event.returnValue = UnloadWarning;
            },
            { signal },
        );
 
        document.getElementById("hard-reset").addEventListener("click", (event) => {
            event.preventDefault();
            this.hardReset();
        });
        document.getElementById("soft-reset").addEventListener("click", (event) => {
            event.preventDefault();
            this.softReset();
        });
        document.getElementById("download-filestore-link").addEventListener("click", () => this.downloadFilestore());
 
        if (Object.hasOwn(parsedQuery, "about")) modals.show("info");
        if (Object.hasOwn(parsedQuery, "pp-tos")) modals.show("pp-tos");
 
        const versionElement = document.getElementById("jsbeeb-version");
        Eif (versionElement) versionElement.textContent = `Version ${version}`;
    }
 
    /** Stops listening to the window and document; the elements go with the page. */
    dispose() {
        this.pageListeners.abort();
    }
 
    hardReset() {
        this.rewindUI.reset();
        this.processor.reset(true);
    }
 
    softReset() {
        this.processor.inputLog?.record({ kind: "reset" });
        this.processor.reset(false);
    }
 
    downloadFilestore() {
        if (!this.processor.filestore) return;
        downloadDriveData(this.processor.filestore.scsi, "scsi", ".dat");
    }
}