All files / src/web page-actions.js

98% Statements 49/50
87.5% Branches 14/16
93.33% Functions 14/15
100% Lines 38/38

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 80 81 82 83 84 85 86 87 88 89 90 91      1x                   14x 14x 14x 14x   14x 14x   14x     1x 1x       14x     14x 14x     2x 1x 1x       14x   14x     3x 1x 1x         14x 1x 1x   14x 1x 1x   14x   14x 14x   14x 14x         14x       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 drop guard, 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 });
 
        // Files are the media window's to take; anything else dropped on the page would replace it.
        const carriesFiles = (event) => [...(event.dataTransfer?.types ?? [])].includes("Files");
        document.addEventListener(
            "dragover",
            (event) => {
                if (carriesFiles(event)) return;
                event.preventDefault();
                event.dataTransfer.dropEffect = "none";
            },
            { signal },
        );
        document.addEventListener("drop", (event) => event.preventDefault(), { 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.reset(false);
    }
 
    downloadFilestore() {
        if (!this.processor.filestore) return;
        downloadDriveData(this.processor.filestore.scsi, "scsi", ".dat");
    }
}