import { t } from "../services/i18n.js"; import BasicWidget from "./basic_widget.js"; import ws from "../services/ws.js"; import options from "../services/options.js"; import syncService from "../services/sync.js"; import { escapeQuotes } from "../services/utils.js"; const TPL = `
`; export default class SyncStatusWidget extends BasicWidget { constructor() { super(); this.syncState = "unknown"; this.allChangesPushed = false; this.settings = { titlePlacement: "right" }; } doRender() { this.$widget = $(TPL); this.$widget.hide(); this.$widget.find(".sync-status-icon:not(.sync-status-in-progress)").on("click", () => syncService.syncNow()); ws.subscribeToMessages((message) => this.processMessage(message)); } showIcon(className) { if (!options.get("syncServerHost")) { this.toggleInt(false); return; } bootstrap.Tooltip.getOrCreateInstance(this.$widget.find(`.sync-status-${className}`), { html: true, placement: this.settings.titlePlacement, fallbackPlacements: [this.settings.titlePlacement] }); this.$widget.show(); this.$widget.find(".sync-status-icon").hide(); this.$widget.find(`.sync-status-${className}`).show(); } processMessage(message) { if (message.type === "sync-pull-in-progress") { this.syncState = "in-progress"; this.lastSyncedPush = message.lastSyncedPush; } else if (message.type === "sync-push-in-progress") { this.syncState = "in-progress"; this.lastSyncedPush = message.lastSyncedPush; } else if (message.type === "sync-finished") { this.syncState = "connected"; this.lastSyncedPush = message.lastSyncedPush; } else if (message.type === "sync-failed") { this.syncState = "disconnected"; this.lastSyncedPush = message.lastSyncedPush; } else if (message.type === "frontend-update") { this.lastSyncedPush = message.data.lastSyncedPush; } this.allChangesPushed = this.lastSyncedPush === ws.getMaxKnownEntityChangeSyncId(); if (["unknown", "in-progress"].includes(this.syncState)) { this.showIcon(this.syncState); } else { this.showIcon(`${this.syncState}-${this.allChangesPushed ? "no-changes" : "with-changes"}`); } } }