mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-03 20:06:08 +01:00 
			
		
		
		
	chore(client/ts): port setup.js entrypoint to TS
This commit is contained in:
		@@ -1,107 +0,0 @@
 | 
				
			|||||||
import utils from "./services/utils.js";
 | 
					 | 
				
			||||||
import macInit from "./services/mac_init.js";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
macInit.init();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function SetupModel() {
 | 
					 | 
				
			||||||
    if (syncInProgress) {
 | 
					 | 
				
			||||||
        setInterval(checkOutstandingSyncs, 1000);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const serverAddress = `${location.protocol}//${location.host}`;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    $("#current-host").html(serverAddress);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    this.step = ko.observable(syncInProgress ? "sync-in-progress" : "setup-type");
 | 
					 | 
				
			||||||
    this.setupType = ko.observable();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    this.setupNewDocument = ko.observable(false);
 | 
					 | 
				
			||||||
    this.setupSyncFromDesktop = ko.observable(false);
 | 
					 | 
				
			||||||
    this.setupSyncFromServer = ko.observable(false);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    this.syncServerHost = ko.observable();
 | 
					 | 
				
			||||||
    this.syncProxy = ko.observable();
 | 
					 | 
				
			||||||
    this.password = ko.observable();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    this.setupTypeSelected = () => !!this.setupType();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    this.selectSetupType = () => {
 | 
					 | 
				
			||||||
        if (this.setupType() === "new-document") {
 | 
					 | 
				
			||||||
            this.step("new-document-in-progress");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            $.post("api/setup/new-document").then(() => {
 | 
					 | 
				
			||||||
                window.location.replace("./setup");
 | 
					 | 
				
			||||||
            });
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            this.step(this.setupType());
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    this.back = () => {
 | 
					 | 
				
			||||||
        this.step("setup-type");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        this.setupType("");
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    this.finish = async () => {
 | 
					 | 
				
			||||||
        const syncServerHost = this.syncServerHost();
 | 
					 | 
				
			||||||
        const syncProxy = this.syncProxy();
 | 
					 | 
				
			||||||
        const password = this.password();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (!syncServerHost) {
 | 
					 | 
				
			||||||
            showAlert("Trilium server address can't be empty");
 | 
					 | 
				
			||||||
            return;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (!password) {
 | 
					 | 
				
			||||||
            showAlert("Password can't be empty");
 | 
					 | 
				
			||||||
            return;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // not using server.js because it loads too many dependencies
 | 
					 | 
				
			||||||
        const resp = await $.post("api/setup/sync-from-server", {
 | 
					 | 
				
			||||||
            syncServerHost: syncServerHost,
 | 
					 | 
				
			||||||
            syncProxy: syncProxy,
 | 
					 | 
				
			||||||
            password: password
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (resp.result === "success") {
 | 
					 | 
				
			||||||
            this.step("sync-in-progress");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            setInterval(checkOutstandingSyncs, 1000);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            hideAlert();
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            showAlert(`Sync setup failed: ${resp.error}`);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
async function checkOutstandingSyncs() {
 | 
					 | 
				
			||||||
    const { outstandingPullCount, initialized } = await $.get("api/sync/stats");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (initialized) {
 | 
					 | 
				
			||||||
        if (utils.isElectron()) {
 | 
					 | 
				
			||||||
            const remote = utils.dynamicRequire("@electron/remote");
 | 
					 | 
				
			||||||
            remote.app.relaunch();
 | 
					 | 
				
			||||||
            remote.app.exit(0);
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            utils.reloadFrontendApp();
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
        $("#outstanding-syncs").html(outstandingPullCount);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function showAlert(message) {
 | 
					 | 
				
			||||||
    $("#alert").html(message);
 | 
					 | 
				
			||||||
    $("#alert").show();
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function hideAlert() {
 | 
					 | 
				
			||||||
    $("#alert").hide();
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
ko.applyBindings(new SetupModel(), document.getElementById("setup-dialog"));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
$("#setup-dialog").show();
 | 
					 | 
				
			||||||
							
								
								
									
										134
									
								
								src/public/app/setup.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										134
									
								
								src/public/app/setup.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,134 @@
 | 
				
			|||||||
 | 
					import utils from "./services/utils.js";
 | 
				
			||||||
 | 
					import macInit from "./services/mac_init.js";
 | 
				
			||||||
 | 
					import ko from "knockout";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					macInit.init();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// TriliumNextTODO: properly make use of below types
 | 
				
			||||||
 | 
					// type SetupModelSetupType = "new-document" | "sync-from-desktop" | "sync-from-server" | "";
 | 
				
			||||||
 | 
					// type SetupModelStep = "sync-in-progress" | "setup-type" | "new-document-in-progress" | "sync-from-desktop";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class SetupModel {
 | 
				
			||||||
 | 
					    syncInProgress: boolean;
 | 
				
			||||||
 | 
					    step: ko.Observable<string>;
 | 
				
			||||||
 | 
					    setupType: ko.Observable<string>;
 | 
				
			||||||
 | 
					    setupNewDocument: ko.Observable<boolean>;
 | 
				
			||||||
 | 
					    setupSyncFromDesktop: ko.Observable<boolean>;
 | 
				
			||||||
 | 
					    setupSyncFromServer: ko.Observable<boolean>;
 | 
				
			||||||
 | 
					    syncServerHost: ko.Observable<string|undefined>;
 | 
				
			||||||
 | 
					    syncProxy: ko.Observable<string|undefined>;
 | 
				
			||||||
 | 
					    password: ko.Observable<string|undefined>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    constructor(syncInProgress: boolean) {
 | 
				
			||||||
 | 
					        this.syncInProgress = syncInProgress;
 | 
				
			||||||
 | 
					        this.step = ko.observable(syncInProgress ? "sync-in-progress" : "setup-type");
 | 
				
			||||||
 | 
					        this.setupType = ko.observable("");
 | 
				
			||||||
 | 
					        this.setupNewDocument = ko.observable(false);
 | 
				
			||||||
 | 
					        this.setupSyncFromDesktop = ko.observable(false);
 | 
				
			||||||
 | 
					        this.setupSyncFromServer = ko.observable(false);
 | 
				
			||||||
 | 
					        this.syncServerHost = ko.observable();
 | 
				
			||||||
 | 
					        this.syncProxy = ko.observable();
 | 
				
			||||||
 | 
					        this.password = ko.observable();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (this.syncInProgress) {
 | 
				
			||||||
 | 
					            setInterval(checkOutstandingSyncs, 1000);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        const serverAddress = `${location.protocol}//${location.host}`;
 | 
				
			||||||
 | 
					        $("#current-host").html(serverAddress);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // this is called in setup.ejs
 | 
				
			||||||
 | 
					    setupTypeSelected() {
 | 
				
			||||||
 | 
					        return !!this.setupType();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    selectSetupType() {
 | 
				
			||||||
 | 
					        if (this.setupType() === "new-document") {
 | 
				
			||||||
 | 
					            this.step("new-document-in-progress");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            $.post("api/setup/new-document").then(() => {
 | 
				
			||||||
 | 
					                window.location.replace("./setup");
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            this.step(this.setupType());
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    back() {
 | 
				
			||||||
 | 
					        this.step("setup-type");
 | 
				
			||||||
 | 
					        this.setupType("");
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    async finish () {
 | 
				
			||||||
 | 
					        const syncServerHost = this.syncServerHost();
 | 
				
			||||||
 | 
					        const syncProxy = this.syncProxy();
 | 
				
			||||||
 | 
					        const password = this.password();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!syncServerHost) {
 | 
				
			||||||
 | 
					            showAlert("Trilium server address can't be empty");
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!password) {
 | 
				
			||||||
 | 
					            showAlert("Password can't be empty");
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // not using server.js because it loads too many dependencies
 | 
				
			||||||
 | 
					        const resp = await $.post("api/setup/sync-from-server", {
 | 
				
			||||||
 | 
					            syncServerHost: syncServerHost,
 | 
				
			||||||
 | 
					            syncProxy: syncProxy,
 | 
				
			||||||
 | 
					            password: password
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (resp.result === "success") {
 | 
				
			||||||
 | 
					            this.step("sync-in-progress");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            setInterval(checkOutstandingSyncs, 1000);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            hideAlert();
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            showAlert(`Sync setup failed: ${resp.error}`);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async function checkOutstandingSyncs() {
 | 
				
			||||||
 | 
					    const { outstandingPullCount, initialized } = await $.get("api/sync/stats");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (initialized) {
 | 
				
			||||||
 | 
					        if (utils.isElectron()) {
 | 
				
			||||||
 | 
					            const remote = utils.dynamicRequire("@electron/remote");
 | 
				
			||||||
 | 
					            remote.app.relaunch();
 | 
				
			||||||
 | 
					            remote.app.exit(0);
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            utils.reloadFrontendApp();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        $("#outstanding-syncs").html(outstandingPullCount);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function showAlert(message: string) {
 | 
				
			||||||
 | 
					    $("#alert").html(message);
 | 
				
			||||||
 | 
					    $("#alert").show();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function hideAlert() {
 | 
				
			||||||
 | 
					    $("#alert").hide();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function getSyncInProgress() {
 | 
				
			||||||
 | 
					    const el = document.getElementById('syncInProgress');
 | 
				
			||||||
 | 
					    if (!el || !(el instanceof HTMLMetaElement)) return false;
 | 
				
			||||||
 | 
					    return !!parseInt(el.content);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					addEventListener("DOMContentLoaded", (event) => {
 | 
				
			||||||
 | 
					    ko.applyBindings(new SetupModel(getSyncInProgress()), document.getElementById("setup-dialog"));
 | 
				
			||||||
 | 
					    $("#setup-dialog").show();
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		Reference in New Issue
	
	Block a user