import BasicWidget from "../basic_widget.js"; import utils from "../../services/utils.js"; import UpdateAvailableWidget from "./update_available.js"; import options from "../../services/options.js"; const TPL = ` `; export default class GlobalMenuWidget extends BasicWidget { constructor() { super(); this.updateAvailableWidget = new UpdateAvailableWidget(); } doRender() { this.$widget = $(TPL); const $button = this.$widget.find(".global-menu-button"); $button.tooltip({ trigger: "hover" }); $button.on("click", () => $button.tooltip("hide")); this.$widget.find(".show-about-dialog-button").on('click', () => this.triggerCommand("openAboutDialog")); const isElectron = utils.isElectron(); this.$widget.find(".logout-button").toggle(!isElectron); this.$widget.find(".open-dev-tools-button").toggle(isElectron); this.$widget.find(".switch-to-mobile-version-button").toggle(!isElectron); this.$widget.on('click', '.dropdown-item', e => { if ($(e.target).parent(".zoom-buttons")) { return; } this.$widget.find("[data-toggle='dropdown']").dropdown('toggle'); }); this.$widget.find(".global-menu-button-update-available").append( this.updateAvailableWidget.render() ); this.$updateToLatestVersionButton = this.$widget.find(".update-to-latest-version-button"); if (!utils.isElectron()) { this.$widget.find(".zoom-container").hide(); } this.$zoomState = this.$widget.find(".zoom-state"); this.$widget.on('show.bs.dropdown', () => this.updateZoomState()); this.$widget.find(".zoom-buttons").on("click", // delay to wait for the actual zoom change () => setTimeout(() => this.updateZoomState(), 300) ); this.updateVersionStatus(); setInterval(() => this.updateVersionStatus(), 8 * 60 * 60 * 1000); } updateZoomState() { if (!utils.isElectron()) { return; } const zoomFactor = utils.dynamicRequire('electron').webFrame.getZoomFactor(); const zoomPercent = Math.round(zoomFactor * 100); this.$zoomState.text(zoomPercent + "%"); } async updateVersionStatus() { await options.initializedPromise; if (options.get("checkForUpdates") !== 'true') { return; } const latestVersion = await this.fetchLatestVersion(); this.updateAvailableWidget.updateVersionStatus(latestVersion); this.$updateToLatestVersionButton.toggle(latestVersion > glob.triliumVersion); this.$updateToLatestVersionButton.find(".version-text").text(`Version ${latestVersion} is available, click to download.`); } async fetchLatestVersion() { const RELEASES_API_URL = "https://api.github.com/repos/zadam/trilium/releases/latest"; const resp = await fetch(RELEASES_API_URL); const data = await resp.json(); return data.tag_name.substring(1); } downloadLatestVersionCommand() { window.open("https://github.com/zadam/trilium/releases/latest"); } }