aboutsummaryrefslogtreecommitdiff
path: root/src/PrisbeamApp.ts
blob: a44e1e4ec65b4c867f0202f19a7164ccca0b9fbb (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import {PrisbeamDataStore} from "./PrisbeamDataStore";
import {PrisbeamListType} from "libprisbeam";
import {PrisbeamAppDisplay} from "./PrisbeamAppDisplay";
import {PrisbeamSettings} from "./PrisbeamSettings";
import {PrisbeamSearch} from "./PrisbeamSearch";
import {PrisbeamActions} from "./PrisbeamActions";
import {PrisbeamAI} from "./PrisbeamAI";
import {PrisbeamLoader} from "./PrisbeamLoader";
import {PrisbeamPropertyStore} from "libprisbeam/src/PrisbeamPropertyStore";
import {PrisbeamDerpibooru} from "./PrisbeamDerpibooru";

export class PrisbeamApp {
    dataStore: PrisbeamDataStore;
    bootstrap: any;
    display: PrisbeamAppDisplay;
    settings: PrisbeamSettings;
    search: PrisbeamSearch;
    actions: PrisbeamActions;
    ai: PrisbeamAI;
    loader: PrisbeamLoader;
    propertyStore: PrisbeamPropertyStore;
    derpibooru: PrisbeamDerpibooru;

    constructor(bootstrap: any) {
        this.bootstrap = bootstrap;
        this.dataStore = new PrisbeamDataStore(this);
        this.display = new PrisbeamAppDisplay(this);
        this.settings = new PrisbeamSettings(this);
        this.search = new PrisbeamSearch(this);
        this.actions = new PrisbeamActions(this);
        this.ai = new PrisbeamAI(this);
        this.loader = new PrisbeamLoader(this);
        this.derpibooru = new PrisbeamDerpibooru(this);

        this.search.loadSearchModule();
    }

    createPropertyStore() {
        this.propertyStore = this.dataStore.database.propertyStore;
    }

    async finishLoading() {
        document.getElementById("loading-btn").classList.add("disabled");

        document.getElementById("load").innerText = "Loading interface...";
        document.getElementById("progress").classList.add("progress-bar-striped");
        document.getElementById("progress").style.width = "100%";

        this.dataStore.loaded = true;
        this.dataStore.page = 1;
        this.dataStore.currentView = await this.dataStore.database.frontend.getAllImages(PrisbeamListType.Array) as any[];
        this.display.updateDisplay();

        this.dataStore.loader.hide();
        document.getElementById("app").classList.remove("disabled");
    }

    loadingError(msg: string) {
        let li = document.createElement("li");
        li.classList.add("list-group-item");
        li.classList.add("list-group-item-warning");
        li.innerHTML = msg;

        document.getElementById("loader-errors-list").append(li);
        document.getElementById("loader-errors").style.display = "";
        this.dataStore.hadErrorsLoading = true;
    }

    async loadApp() {
        if (this.dataStore.loaded) return;

        document.getElementById("load").innerText = "Waiting for application...";
        document.getElementById("progress").classList.remove("progress-bar-striped");
        document.getElementById("progress").style.width = "0%";

        await this.loader.findDatabase();
        this.loader.checkBusyUpdating();
        await this.loader.initializeDatabase();
        await this.loader.updateCache();
        await this.loader.completeLoading();
        await this.derpibooru.initialize();
    }

    safeUnload() {
        let modal = new this.bootstrap.Modal(document.getElementById("close-dialog"));
        modal.show();

        return new Promise<void>((res) => {
            this.ai.unload();

            if (!this.derpibooru.window?.isDestroyed()) {
                this.derpibooru.window.destroy();
            }

            if (this.dataStore.database && !this.dataStore.unloaded) {
                (async () => {
                    try {
                        await this.dataStore.database.close();
                    } catch (e) {
                        console.error(e);
                    }

                    this.dataStore.unloaded = true;

                    res();
                })();
            } else {
                res();
            }
        });
    }

    async safeReload() {
        await this.safeUnload();
        location.reload();
    }

    // noinspection JSUnusedGlobalSymbols
    async safeClose() {
        await this.safeUnload();
        window.close();
    }

    bootstrapTooltips() {
        const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
        //@ts-ignore
        [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl));
    }
}